/**
 *	Live Nation JavaScript Library - LightBox Class
 *
 *	@date		2008-05-09
 *	@author		Michael Giuliano
 *	@copyright	Live Nation (Music) UK
 */

var LBox = Class.create();

LBox.prototype = {

    detect: null,
    OS: null,
    browser: null,
    version: null,
    total: null,
    thestring: null,
    place: null,
    yPos : 0,
	xPos : 0,
	lbObject: null,
	overlayBox: null,
    lbHolder: null,
    lbContainer: null,

    initialize: function(args) {
        
        if(args !== null) {
            this.overlayBox = args[0];
            this.lbHolder = args[1];
            
            this.detect = navigator.userAgent.toLowerCase();
            this.getBrowserInfo();
            this.addLightboxMarkup();
    	    
	        this.lbObject = document.getElementsByClassName('lbOn')[0];
	        this.content = this.lbObject.rel;
	    }
	    
    },
    
    getBrowserInfo: function() {
	    if (this.checkIt('konqueror')) {
		    this.browser = "Konqueror";
		    this.OS = "Linux";
	    }
	    else if (this.checkIt('safari')) LBox.browser 	= "Safari"
	    else if (this.checkIt('omniweb')) LBox.browser 	= "OmniWeb"
	    else if (this.checkIt('opera')) LBox.browser 	= "Opera"
	    else if (this.checkIt('webtv')) LBox.browser 	= "WebTV";
	    else if (this.checkIt('icab')) LBox.browser 	= "iCab"
	    else if (this.checkIt('msie')) LBox.browser 	= "Internet Explorer"
	    else if (!this.checkIt('compatible')) {
		    this.browser = "Netscape Navigator"
		    this.version = this.detect.charAt(8);
	    }
	    else this.browser = "An unknown browser";

	    if (!this.version) this.version = this.detect.charAt(this.place + this.thestring.length);

	    if (!this.OS) {
		    if (this.checkIt('linux')) this.OS 		= "Linux";
		    else if (this.checkIt('x11')) this.OS 	= "Unix";
		    else if (this.checkIt('mac')) this.OS 	= "Mac"
		    else if (this.checkIt('win')) this.OS 	= "Windows"
		    else this.OS 							= "an unknown operating system";
	    }
    },
    
    checkIt: function(string) {
	    this.place = this.detect.indexOf(string) + 1;
	    this.thestring = string;
	    return this.place;
    },
    
    // 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; 
	},
	
	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); 
	},
	
	setScrolling: function(status) {
	    var htm = document.getElementsByTagName('body')[0];
	    switch(status) {
	        case 'hide':
	            htm.style.height = '100%';
	            htm.style.overflow = 'hidden';
	            break;
	        case 'show':
	            htm.style.height = 'auto';
	            htm.style.overflow = 'auto';
	            break;	           
	    }
	},
	
	addLightboxMarkup:function() {
        
        // Overlay Box
	    var bod = document.getElementsByTagName('body')[0];
	    var overlayBox = document.createElement('div');
	    overlayBox.id = this.overlayBox;
	    bod.appendChild(overlayBox);
    	
    	// Lighbox Box
	    var leightboxHolder = document.createElement('div');
	    leightboxHolder.id = this.lbHolder;
	    bod.appendChild(leightboxHolder);
	    var lb_container = document.createElement('div');
	    lb_container.className = 'lb_container';
	    leightboxHolder.appendChild(lb_container);
	    
    },
    
    // 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;
		}
	},
	
	// hide Flash elements
	hideFlashs: function(visibility){
		flashs = document.getElementsByTagName('div');
		for(i = 0; i < flashs.length; i++) {
		    if(flashs[i].className == 'flash') {
			    flashs[i].style.visibility = visibility;
			}
		}
	},
	
	displayLightbox: function(display){
		$(this.overlayBox).style.display = display;
		$(this.lbHolder).style.display = display;
		$(this.lbHolder).innerHTML = $(this.content).innerHTML;
	},
	
	lbOpen: function() {
        if (LBox.browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'auto');
			this.setScroll(0,0);
			this.hideSelects("hidden");			
		}
		this.setScrolling("show");
		this.hideFlashs("hidden");
		this.displayLightbox("block");
    },
    
    lbClose: function() {
        if (LBox.browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		this.setScrolling("hide");
		this.hideFlashs("visible");
		this.displayLightbox("none");
    },
    
    openMap: function(url) {
        var wRef = window.open(
            url, 
            "Map", 
            "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1"
        );
    }
    
};