/**
 * Lightbox for n-n.ch.
 *
 * @package jQuery
 * @subpackage nn
 * @author Remo Häusler <remo.haeusler@hotmail.com>
 */
 
var nn = nn || {};
nn.LightBox = {
	
	EVENT_CLOSE: 'close',
	
    jHelper:null,
    jInstance:null,
    index:-1,
    interval:null,
    _play: false,
    _preloads: [],
    
    build: function (settings)
    {
        var settings = jQuery.extend({
            //items: 'a[rel=LightBox]',
            speed: (1 * 1000),
            duration: (3 * 1000),
            play: false,
            menu: false
        }, settings);
        
        this.settings = settings;
        var jLightBox = this;
        
        return this.each(function(i){
            //this.$LightBox = jSelf;
            jQuery(this).bind('click', function(){
                nn.LightBox.init(jLightBox, i);
                return false;
            });
        });
    },
    
    init: function (jLightBox, index)
    {
        nn.LightBox.jInstance = jLightBox;
        nn.LightBox.jHelper = jQuery('<div id="jSlideShowHelper" style="display:none">'
        + '<div id="jSlideShowBackground">&nbsp;</div>'
        + '<div id="jSlideShowContainer"></div>'
        + '<div id="jLightBoxBar">'
        	+ '<div id="jLightBoxInfo"></div>'
        	+ '<div id="jLightBoxControll"></div>'
		+ '</div>'
        + '</div>')
        .appendTo('body');
        
		// IE6: Simulate position fixed...
        if (jQuery.browser.msie && jQuery.browser.version < 7){
           nn.LightBox.jHelper.css('position', 'absolute');
           jQuery(window).bind('scroll', function(event){
                nn.LightBox.jHelper.css('top', jQuery(window).scrollTop() + 'px');
           });
           jQuery(window).trigger('scroll');
        }
        
        if (jLightBox.settings.menu){
            // Prev Button...
            jQuery('<img id="jLightBoxPrevButton" src="pix/icons/prev.gif" alt="[&lt;]" title="Previous" />').bind('click', function(event){
                nn.LightBox.prev();
                return false;
            })
			.css('cursor', 'pointer')
			.appendTo('#jLightBoxControll');
            // Play Button...
            jQuery('<img id="jLightBoxPlayButton" src="pix/icons/play.gif" alt="[Play]" title="Play" />').bind('click', function(event){
                nn.LightBox.play();
                return false;
            })
			.css('cursor', 'pointer')
			.appendTo('#jLightBoxControll');
            // Stop Button...
            jQuery('<img id="jLightBoxStopButton" src="pix/icons/stop.gif" alt="[Stop]" title="Stop" />').bind('click', function(event){
                nn.LightBox.stop();
                return false;
            })
			.css('cursor', 'pointer')
			.appendTo('#jLightBoxControll');
            // Next Button...
            jQuery('<img id="jLightBoxNextButton" src="pix/icons/next.gif" alt="[&gt;]" title="Next" />').bind('click', function(event){
                nn.LightBox.next();
                return false;
            })
			.css('cursor', 'pointer')
			.appendTo('#jLightBoxControll');
            // Close Button...
            jQuery('<img id="jLightBoxCloseButton" src="pix/icons/close.gif" alt="[x]" title="Close" />').bind('click', function(event){
                nn.LightBox.close();
                return false;
            })
			.css('cursor', 'pointer')
			.appendTo('#jLightBoxControll');
        }
        
        nn.LightBox._play = jLightBox.settings.play;
        if (jLightBox.settings.play){
            nn.LightBox.play(); // Do not start playing, just hide the button...
        } else {
            nn.LightBox.stop();
        }
        nn.LightBox.jHelper.css('display', 'block');
        jQuery('#jSlideShowBackground')
        .click(function(event){
            nn.LightBox.close();
        })
        .css('opacity', 0)
        .animate({'opacity': 0.85}, 'fast', function(event){
            nn.LightBox.show(index);
            //alert(event)
        });
        //jQuery.event.trigger('init');
		// */
		//nn.LightBox.show(index);
    },
    
    show: function (index)
    {
        var node = nn.LightBox.jInstance.get(index);
        if (node == null){
            return false;
        }
        nn.LightBox.index = index;

        var speed = nn.LightBox.jInstance.settings.speed;
        jQuery('#jSlideShowContainer .jSlideShowImage div').fadeOut(speed, function(event){
            jQuery(this).parent().remove();
        });

        var source = jQuery(node).attr('href');
        var jImage = jQuery('<div class="jSlideShowImage">'
        + '<div>'
        + '<img src="' + source + '" alt="" />'
        + '</div>'
        + '</div>')
        .appendTo('#jSlideShowContainer');

        var current = index + 1;
        var total = nn.LightBox.jInstance.length;
        jQuery('#jLightBoxInfo').empty().html(current + ' / ' + total);

        jQuery('#jSlideShowContainer .jSlideShowImage:last img')
        .bind('load', function(){
            var jSelf = jQuery(this);
            var width = jSelf.width();
            var height = jSelf.height();
            jSelf
            .parent()
            .hide()
            .fadeIn(speed, nn.LightBox.onShow)
            .css('left', -(width / 2) + 'px')
            .css('top', -(height / 2) + 'px')
            .css('visibility', 'visible');
        });
        
        nn.LightBox.preload(index + 1);
    },
    
    preload: function (index)
    {
        var node = nn.LightBox.jInstance.get(index);
        if (node == null){
            return false;
        }
        if (nn.LightBox._preloads[index] == null){
            var source = jQuery(node).attr('href');
            var img = new Image();
            img.src = source;
            nn.LightBox._preloads[index] = img;
        }
        return true;
    },
    
    next: function ()
    {
        var index = nn.LightBox.index;
        if (++index >= nn.LightBox.jInstance.length){
            index = 0;
        }
        nn.LightBox.show(index);
    },

    prev: function ()
    {
        var index = nn.LightBox.index;
        if (--index < 0){
            index = nn.LightBox.jInstance.length - 1;
        }
        nn.LightBox.show(index);
    },
    
    play: function ()
    {
        //if (!nn.LightBox._play){
            
            jQuery('#jLightBoxPlayButton').hide();
            jQuery('#jLightBoxStopButton').show();
        //}
        if (!nn.LightBox._play){
            nn.LightBox._play = true;
            nn.LightBox.next();
        }
    },

    stop: function ()
    {
        nn.LightBox._play = false;
        clearTimeout(nn.LightBox.timeout);
        jQuery('#jLightBoxPlayButton').show();
        jQuery('#jLightBoxStopButton').hide();
    },
    
    close: function ()
    {
        clearTimeout(nn.LightBox.timeout);
        //mediaPlayerCommand('stop');
        jQuery.event.trigger(nn.LightBox.EVENT_CLOSE, [], nn.LightBox);
		
		jQuery('#jSlideShowHelper').remove();
        jQuery('#player').remove();
    },
    
    onShow: function ()
    {
        clearTimeout(nn.LightBox.timeout);
        if (nn.LightBox._play){
            var duration = nn.LightBox.jInstance.settings.duration;
            nn.LightBox.timeout = setTimeout('nn.LightBox.next()', duration);
        }
    }
}
jQuery.fn.lightBox = nn.LightBox.build;
