var THgalleryWorker = new Array();
function THgallery(images, duration, target, label, path) {
    this.images       = images;
    this.target       = target;
    this.label        = label;
    this.labelImg     = typeof(THgallery.arguments[5]) != "undefined" ? THgallery.arguments[5] : false;
    this.labelImgs    = typeof(THgallery.arguments[6]) != "undefined" ? THgallery.arguments[6] : false;
    this.thumbsbox    = false;
    this.duration     = duration;
    this.path         = path;
    this.currentSlide = 0;
    this.isStarted    = false;
    this.ready        = true;
    this.isRunning    = false;
    this.watchdogEl   = null;
    this.watchdogIdx  = null;
    this.stop();

    if (this.labelImgs && typeof(this.images) != "undefined") {
        jQuery("#" + this.labelImgs).html(this.images.length);
    }
    var thisObj = this;
    if (typeof(THgallery.arguments[7]) != "undefined") {
        jQuery('#' + THgallery.arguments[7]).click(function(){
           thisObj.prev();
           return false;
        });
    }
    if (typeof(THgallery.arguments[8]) != "undefined") {
        jQuery('#' + THgallery.arguments[8]).click(function(){
           thisObj.next();
           return false;
        });
    }
    if (typeof(THgallery.arguments[9]) != "undefined") {
        jQuery('#' + THgallery.arguments[9]).click(function(){
           thisObj.toggle();
           return false;
        });
    }
    if (typeof(THgallery.arguments[10] != "undefined")) {
        this.thumbsbox = THgallery.arguments[10];
        jQuery('#' + this.thumbsbox).html('');
        for (i in this.images) {
            jQuery('#' + this.thumbsbox).append('<div id="THgalleryThumb' + i + '"><img src="' + this.path.replace(/[0-9]{3,4}x[0-9]{3,4}/g, 'thumbs') + this.images[i]['image'] + '" alt="" /></div>');
            jQuery('#THgalleryThumb' + i).click(function(){ jQuery('#loading').show(); var z = jQuery(this).attr('id').replace(/\D/g, ''); var running = thisObj.isRunning; thisObj.stop(); thisObj.show(z); if (running) { thisObj.run(); } });
            jQuery('#THgalleryThumb' + i).mouseover(function(){ jQuery(this).addClass('hover') });
            jQuery('#THgalleryThumb' + i).mouseout(function(){ jQuery(this).removeClass('hover') });
        }
    }
    if (typeof(this.images) != "undefined" && this.images.length > 1) {
        //$('#galleryMenuWrapper,#galleryMenuContent,#galleryWrapper,#galleryContent').wipeUp(400);
    }
}

THgallery.prototype.start = function() {
    if (!this.isStarted) {
        jQuery('#loading').show();
        thisObj = this;
        this.isStarted = true;
        this.run();
        //this.setWorker(window.setTimeout( function() { thisObj.run(); }, this.duration ));
    }
}

THgallery.prototype.run = function() {
    this.isRunning = true;
    jQuery('#galleryPause>img').attr('src',  jQuery('#galleryPause>img').attr('src').replace(/play/, 'pause'));
    var thisObj = this;
    if (!this.ready || !document.getElementById('bgimg').complete) {
        this.setWorker(window.setTimeout( function() { thisObj.run(); }, this.duration ));
        //this.setWorker(window.setTimeout(function(thisObj) { thisObj.run(); }, this.duration, this));
        return;
    }

    //window.setTimeout(function(thisObj) { thisObj.show( thisObj.currentSlide ); }, this.duration, this);
    //this.setWorker(window.setTimeout(function(thisObj) { thisObj.run(); }, this.duration, this));
    //this.setWorker(window.setTimeout(function() { thisObj.show( thisObj.currentSlide ); }, this.duration));
    this.show( this.currentSlide );
    if (typeof(this.images) != "undefined" && this.images.length > 1) {
        this.setWorker(window.setTimeout(function() { thisObj.run(); }, this.duration));
    }
}

THgallery.prototype.stop = function() {
    this.clearWorker('');
    this.isRunning = false;
    jQuery('#galleryPause>img').attr('src',  jQuery('#galleryPause>img').attr('src').replace(/pause/, 'play'));
}

THgallery.prototype.toggle = function() {
    if (this.isRunning) {
        this.stop();
    }
    else {
        this.ready = true;
        this.run();
    }
}


THgallery.prototype.next = function() {
    var running = this.isRunning;
    this.stop();
    jQuery('#loading').show();
    this.show( this.currentSlide );
    if (running) {
        this.run();
    }
}

THgallery.prototype.prev = function() {
    var running = this.isRunning;
    this.stop();
    jQuery('#loading').show();
    this.currentSlide -= 2;
    if (this.currentSlide < 0) {
        this.currentSlide = this.images.length - 1;
    }
    this.show( this.currentSlide );
    if (running) {
        this.run();
    }
}

THgallery.prototype.show = function(index) {
    if (typeof(this.images) != "undefined" && typeof(this.images[ index ]) == "undefined") {
       index = 0;
    }
    if (typeof(formFocus) != "undefined" && formFocus) {
        return;
    }
    this.currentSlide = index;
    this.ready = false;
    var src = this.path + this.images[ index ]['image'];
    var target = this.target;
    var label  = this.label;
    var text   = this.images[ index ]['text'];
    var logo   = typeof(this.images[ index ]['logo']) != "undefined" ? this.images[ index ]['logo'] : "";
    var myObj  = this;
    
    var newImg = jQuery('<img />')
            .attr('src', src)
            //.appendTo('body')
            .load(function(){
                myObj.imageLoaded(this, index);
            })


    this.setWorker(newImg);
    this.watchdogEl = newImg;
    this.watchdogIdx = index;
    if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Opera") != -1) {
        this.watchdog();
    }
}

var c = 0;
THgallery.prototype.watchdog = function() {
    var el = this.watchdogEl;
    var index = this.watchdogIdx;
    var thisObj = this;
    if ((typeof(el.complete) == 'undefined')) {
        this.setWorker(window.setTimeout( function() { thisObj.imageLoaded(thisObj.watchdogEl, thisObj.watchdogIdx); }, 1000 ));
    }
    else {
        this.setWorker(window.setTimeout( function() { thisObj.watchdog(); }, this.duration ));
    }
}

THgallery.prototype.imageLoaded = function(el, index) {
    var src = this.path + this.images[ index ]['image'];
    var target = this.target;
    var label  = this.label;
    var text   = this.images[ index ]['text'];
    var logo   = typeof(this.images[ index ]['logo']) != "undefined" ? this.images[ index ]['logo'] : "";
    var myObj  = this;
    jQuery(target).fadeTo('fast',1);

            jQuery("#" + target).clone().attr('id', 'THgallerySlide').css('z-index', '0').insertAfter("#" + target);
            var id = jQuery('#THgallerySlide>img').attr('id');
            jQuery('#THgallerySlide>img').attr('id', 'THgallerySlideImg');
            jQuery('#THgallerySlideImg').attr('src', jQuery(el).attr('src') );
            jQuery('#THgallerySlide').css('height', jQuery('#' + target).height() + "px");
            jQuery('#THgallerySlide').css('z-index', 0);
            jQuery('#THgallerySlideImg').css("display", "block");
//            jQuery("#" + target).fadeOut('fast', function() {
                jQuery("#" + target).remove();
                jQuery('#THgallerySlide').css('z-index', '1');
                jQuery('#THgallerySlide').attr('id', target);
                jQuery('#THgallerySlideImg').attr('id', id);
                if (logo != "") {
                    $('#logoNoMenu>img').attr('src', '/images/' + logo);
                }
                else {
                    $('#logoNoMenu>img').attr('src', '/images/sp_weiss.png');
                }
                jQuery(label).html(text);
                if (myObj.labelImg) {
                    var x = parseInt(myObj.currentSlide) + 1;
                    jQuery("#" + myObj.labelImg).html(x);
                }
                jQuery('#' + myObj.thumbsbox + " div").removeClass('active');
                jQuery('#THgalleryThumb' + myObj.currentSlide).addClass('active');
                var w1 = jQuery('#' + myObj.thumbsbox).parent().width();
                var w2 = totalWidth('#' + myObj.thumbsbox + ' div');
                var m  = 0;
                if (w2 * myObj.images.length > w1) {
                    var c = Math.round((w1 - w2) / 2);
                        m = c - myObj.currentSlide * w2;
                        m = m > 0 ? 0 : m;
                        m = m < -1 * (w2 * myObj.images.length - w1) ? -1 * (w2 * myObj.images.length - w1) : m;
                }
                jQuery('#' + myObj.thumbsbox).animate({ marginLeft: m + "px"}, 'normal');
                backgroundImage();
                myObj.currentSlide++;
                myObj.ready = true;
                jQuery('#loading').hide();
//            });
            jQuery(el).remove();

}

THgallery.prototype.setWorker = function(id) {
    THgalleryWorker[ THgalleryWorker.length ] = id;
    return id;
}

THgallery.prototype.clearWorker = function(id) {
    if (id == '') {
        for (var i in THgalleryWorker) {
            if (typeof(THgalleryWorker[i]) == "object") {
                jQuery(THgalleryWorker[i]).remove();
            }
            else {
                window.clearTimeout(THgalleryWorker[i]);
            }
        }
    }
    else if (typeof(id) == "object") {
        jQuery(id).remove();
    }
    else if (typeof(THgalleryWorker[id]) != "undefined") {
        window.clearTimeout(THgalleryWorker[id]);
    }
}



/*
var images = new Array();
for (var i = 0; i < 22; i++) {
    var j = i + 1;
    images[i] = new Object();
    images[i]['image'] = j + ".jpg";
    images[i]['text']  = "Text " + j;
    if (i == 1 || i == 5) {
        images[i]['logo'] = 'sp_braun.png';
    }
}
var duration = 5000;
var target   = "bgimage";
var label    = "#labelName";
var path     = "/hotel/raeumlichkeiten/gallery/2560x1600/";

$(document).ready(function() {
        $('#galleryMenuWrapper,#galleryMenuContent,#galleryWrapper,#galleryContent').wipeUp(400);
        window.setTimeout("var gal = new THgallery(images, duration, target, label, path, 'labelImg', 'labelImgs', 'galleryPrv', 'galleryNxt', 'galleryPause', 'galleryThumbs'); gal.start()", 5000);
});
*/
