var fader_gallery = function(id_name, objName, level2){
    this._level2 = level2;
    this._id = id_name;
    this._obj_name = objName;
    this._animating = false;
    this._top_lvl_div = "topDiv";
    this._img_arr_num = 1;
    this._num_imgs = $('#' + this._id + ' #thumbList').children().size();
    this._timer;
    this._add_event('thumbList');
    this._timer_start();
};
fader_gallery.prototype._add_event = function(location){
    var obj_name = this._obj_name, div_location = location;
    $('#' + this._id + ' #' + div_location).children().each(function(){
        $(this).attr('href', "javascript:" + obj_name + '.change_img( "' + $(this).children('img').attr('src') + '", ' + $(this).children('img').attr('id') + ', "' + div_location + '" )');
    });
};
fader_gallery.prototype._timer_start = function(){
    var obj = this;
    this._timer = setTimeout(function(){
        obj.change_img();
    }, 5000);
};
fader_gallery.prototype._load_level2_imgs = function(id){
    var obj = this;
    $('#level2Images').load('script/level2images.php?id=' + id, function(){
        $('#level2Images').children().each(function(){
            $(this).fadeIn('default');
        });
        obj._add_event('level2Images');
    });
};
fader_gallery.prototype.change_img = function(src, arr_num, location){
    clearTimeout(this._timer);
    if (arr_num) {
        this._img_arr_num = arr_num;
    }
    var next_div = (this._top_lvl_div == "topDiv") ? next_div = "bottomDiv" : next_div = "topDiv", imgId = $('#' + this._id + ' #thumbList > :nth-child(' + this._img_arr_num + ')').children('img').attr('class'), obj = this;
    if (!src) {
        src = $('#' + this._id + ' #thumbList > :nth-child(' + this._img_arr_num + ')').children('img').attr('src')
    }
    
    $('#' + next_div + ' img').attr('src', src);
    (next_div == "topDiv") ? $('#topDiv').fadeIn(function(){
        if (obj._level2 && location == "thumbList" || !location) {
            obj._load_level2_imgs(imgId);
        }
    }) : $('#topDiv').fadeOut(function(){
        if (obj._level2 && location == "thumbList" || !location) {
            obj._load_level2_imgs(imgId);
        }
    });
    
    (this._top_lvl_div == "topDiv") ? this._top_lvl_div = "bottomDiv" : this._top_lvl_div = "topDiv";
    if (this._img_arr_num < this._num_imgs && !arr_num) {
        this._img_arr_num = this._img_arr_num + 1;
    }
    if (this._img_arr_num == this._num_imgs && !arr_num) {
        this._img_arr_num = 1;
    }
    this._timer_start();
};

