/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
//Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.content = ctrl.rel;
		lbContent = $(this.content);
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		//ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
        this.getScroll();

        if (!browser)
	    {	        
	        getBrowserInfo();
	    }

	    if(browser == 'Safari' || browser == 'Netscape Navigator'){
	        window.scroll(0,0);
	    }
	    
		if (browser == 'Internet Explorer'){
			if ($("aspnetForm"))
			{
    			$("aspnetForm").style["overflow"] = "hidden";
    		}
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
			this.hideObjects('hidden');
		}

		this.displayLightbox("block");
	},
	
	// Turn everything on - mainly the IE fixes
	activatePersistentLightBox: function(){
	    if (!browser)
	    {	        
	        getBrowserInfo();
	    }
		if (browser == 'Internet Explorer'){
			if ($("aspnetForm"))
			{
    			$("aspnetForm").style["overflow"] = "hidden";
    		}

    		if (!pscStandAlone)
    		{
			    this.hideSelects('hidden');
			}
    
            this.hideObjects('hidden');
		}

		this.displayPersistentLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// In IE, object elements hover on top of the lightbox
	hideObjects: function(visibility){
	    var arrAbsoluteRoots = getPageAbsoluteRoot(window, self)
	    var rootWindow = arrAbsoluteRoots[0];

		objects = rootWindow.document.getElementsByTagName('object');
		for(i = 0; i < objects.length; i++) {
			objects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	// shows the newly selected lightbox
	displayLightbox: function(display){   
	    
	    if (!lbOverlay)
	    {
	        addLightboxMarkup();
	    }
	    
	    // display overlay and current lightbox
		lbOverlay.style.display = display; 
		lbContent.style.display = display;
		 var arrayPageSize = getPageSize();
	    lbOverlay.style["height"] = arrayPageSize[1] + 'px';
	    lbOverlay.style["width"] = arrayPageSize[0] + 'px';
		if(display != 'none') this.actions();

        // Hide select boxes as they will 'peek' through the image in IE
        selects = lbContent.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }
        
        if (display ==  "block")
        {
            if (browser != 'Safari')
            {
                document.body.style.overflow = "hidden";
            }
              
            var lightboxDiv = lbContent;
            lightboxDiv.style.zIndex   = 1001;
        }
        else
        {
            if (browser != 'Safari')
            {
                document.body.style.overflow = "auto";
            }
            
            
        }
	},
	
	// shows the newly selected lightbox
	displayPersistentLightbox: function(display){   
	    
	    if (!lbOverlay)
	    {
	        addLightboxMarkup();
	    }	    
	    
	    // display overlay and current lightbox
		lbOverlay.style.display = display; 
		//lbContent.style.display = display;
		if(display != 'none') this.actions();

        var arrayPageSize = getPageSize();
	    lbOverlay.style["height"] = arrayPageSize[1] + 'px';
	    lbOverlay.style["width"] = arrayPageSize[0] + 'px';

        // Hide select boxes as they will 'peek' through the image in IE
        selects = lbContent.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

        // Hide select boxes as they will 'peek' through the image in IE
        selects = lbContent.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "";
        }
        
        if (display ==  "block")
        {
            var lightboxDiv = lbContent;
            lightboxDiv.style.zIndex   = 1001;
        }
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			//lbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
        this.setScroll(0,this.yPos);

        if (!browser)
	    {	        
	        getBrowserInfo();
	    }
		if (browser == "Internet Explorer"){
		
			if ($("aspnetForm"))
			{
			    $("aspnetForm").style["overflow"] = "hidden";
            }
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
			this.hideObjects('visible');
		}

		this.displayLightbox("none");
		
        if (typeof(placePsc) == "function")
        {
            placePsc();
        }
        g_clicked = false;
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivatePersistentLightBox: function(){
	    if (g_pscStandAloneDatePickerField)
	    {
	        updateDateField(g_pscStandAloneDatePickerField, null); 
	    }
	    if (!browser)
	    {	        
	        getBrowserInfo();
	    }
		if (browser == "Internet Explorer"){
			if ($("aspnetForm"))
			{
			    $("aspnetForm").style["overflow"] = "hidden";
            }

    		if (!pscStandAlone)
    		{
			    this.hideSelects("visible");
			}

            this.hideObjects('visible');
		}
		this.displayPersistentLightbox("none");
		lbContent.style.zIndex = "";
	},
	setLightBoxName: function(rel)
	{
	    this.content = rel;
	    lbContent = $(this.content);
	},
	
	setLightBoxObject: function(obj)
	{
	    lbContent = obj;
	},
	
	setLightBoxOverlayObject: function(obj)
	{
	    if (obj)
	    {
    	    lbOverlay = obj;
	    }
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	// initialize function for lightbox class will enable listeners for the lightboxes
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {
    if (!document.getElementById("overlay"))
    {
    	bod 				= document.getElementsByTagName('body')[0];

	    overlay 			= document.createElement('div');
	    overlay.id			= 'overlay';

	    bod.appendChild(overlay);
	    lbOverlay = overlay;
    }
    else if (lbOverlay == null)
    {
        lbOverlay = document.getElementById("overlay");
    }
}