/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

var google_map = false;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
//Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox2 = Class.create();

lightbox2.prototype = {
	yPos : 0,
	xPos : 0,

	width: 790,
	height: 440,

	on_top: false,
	
	auto_height: false,

	initialize: function(ctrl) {
		if (ctrl){
			this.content = ctrl.href;
			Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
			ctrl.onclick = function(){return false;};
		}
	},
	
	set_top: function (f){
		this.on_top = f;
	},
	
	set_size: function(w,h,ah){
		this.width = w;
		$('lightbox2').style.width = ''+this.width+'px';
		
		this.height = h;
		$('lightbox2').style.height = ''+this.height+'px';
		$('lightbox2').style.marginTop = '-'+(this.height/2)+'px';
		
		if(ah) this.auto_height = true;
		
		return this;
	},
	
	show1: function(content, params){
		this.set_size(this.width, this.height);
		this.content = content;
		this.activate(params);	
	},
	
	show2: function(text){
		this.set_size(this.width, this.height);
		this.activate2();
		$('overlay2').style.display = 'block';
		$('lightbox2').style.display = 'block';
		$('lightbox2').className = "done";	
		sethtml('lightbox2', '<div id="lbContent2 b2" class="hasLayout">' + text + '</div><div id="lbLoadMessage2" class="hasLayout"></div>');
		this.actions();
	},
	
	
	show_map: function(loc, region){
		var arrayPageSize = getPageSize();
		$('overlay2').style.height = arrayPageSize[1]+'px';
		
		this.hideSelects('hidden');
		if (browser == 'Internet Explorer'){
			//this.getScroll();
			//this.prepareIE('100%', 'hidden');
			//this.setScroll(0,0);
			
			
			
			var arrayPageScroll = getPageScroll();
			
			if (version != 7) {
				$('lightbox2').style.top = ''+parseInt(arrayPageScroll[1] + ((arrayPageSize[3]-this.height)/2) + (this.height/2))+'px';
			}
			
			$('lightbox2').style.left = ''+parseInt((arrayPageSize[0]-this.width)/2+10)+'px';
		} else {
			$('lightbox2').style.left = ''+parseInt((arrayPageSize[0]-this.width)/2)+'px';
			
		}
		
		$('overlay2').style.display = "block";
		$('lightbox2').style.display = "block";
		
		
		info = "<div id='lbContent2'><div id=\"g_m_loc\" style=\"width: 780px; height: 430px; text-align: center;\"></div></div>";
		new Insertion.Before($('lbLoadMessage2'), info);
		
		$('lightbox2').className = "done";
		
		google_map = true;
		this.actions();	
		
		start_loading('g_m_loc', '<br/>Proszę czekać... <br/>Trwa ustalanie położenia...', '<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>');
		setTimeout("g_start_map(\"" + loc + "\", " + region + ")", 1000);
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(params){
		this.activate2();
		this.displayLightbox("block", params);
	},
	
	activate2: function(){
		var arrayPageSize = getPageSize();
		$('overlay2').style.height = arrayPageSize[1]+'px';
		
		this.hideSelects('hidden');
		if (browser == 'Internet Explorer'){
			
			
			var arrayPageScroll = getPageScroll();
			
			if (version < 7) {
				if (this.on_top) {
					$('lightbox2').style.top = ''+parseInt(arrayPageScroll[1] + ((arrayPageSize[3]-this.height)/6) + (this.height/2))+'px';
				} else {
					$('lightbox2').style.top = ''+parseInt(arrayPageScroll[1] + ((arrayPageSize[3]-this.height)/2) + (this.height/2))+'px';
				}
			} else {
				if (this.on_top) $('lightbox2').style.top = '20%';
			}
			
			$('lightbox2').style.left = ''+parseInt((arrayPageSize[0]-this.width)/2+10)+'px';
		} else {
			$('lightbox2').style.left = ''+parseInt((arrayPageSize[0]-this.width)/2)+'px';
			
			if (this.on_top) $('lightbox2').style.top = '20%';
		}
	},
	
	// 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;
		}
		
		selects = document.getElementsByTagName("object");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	
		selects = document.getElementsByTagName("embeds");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
		
		
		selects = document.getElementsByClassName("youtube");
		for (i = 0; i != selects.length; i++) {
			selects[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); 
	},
	
	displayLightbox: function(display, params){
		$('overlay2').style.display = display;
		$('lightbox2').style.display = display;
		if(display != 'none') this.loadInfo(params);
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function(params) {
		var myAjax = new Ajax.Request(
        this.content,
        {method: params ? 'post' : 'get', parameters: (params ? params : ""), onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
		$('lightbox2').className = "loading";	
		start_loading('lbLoadMessage2', '<br/>'+lngx_ladowanie + '', '<table style="height: '+(this.height/2-30)+'px;"><tr><td>&nbsp;</td></tr></table><center>');
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		$('lightbox2').className = "done";	
		sethtml('lightbox2', '<div id="lbContent2" class="hasLayout b2">' + response.responseText + '</div><div id="lbLoadMessage2" class="hasLayout"></div>');
		//new Insertion.Before($('lbLoadMessage2'), info)
		
		if(this.auto_height)$('lightbox2').style.height = 'auto';
		this.actions();
	},
	
	// 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
	insert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('lbContent2'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'get', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if ($('lbContent2')){
			Element.remove($('lbContent2'));
			//if (browser == "Internet Explorer"){
				//this.setScroll(0,this.yPos);
				//this.prepareIE("auto", "auto");
				this.hideSelects("visible");
			//}
			
			if (google_map){
				try{GUnload();}catch(e){};
				google_map = false;
			}
			
			this.displayLightbox("none");
		}
		
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox2(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() {
	var bod 				= document.getElementsByTagName('body')[0];
	var overlay 			= document.createElement('div');
	overlay.id		= 'overlay2';
	overlay.className="lbAction";
	overlay.rel="deactivate";
	var lb					= document.createElement('div');
	lb.id				= 'lightbox2';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage2" class="hasLayout b2">' +
						  '<p style="text-align: center;"><br/><br/><br/>'+lngx_ladowanie+'</p>' +
						  '</div>';
	bod.appendChild(overlay);
	bod.appendChild(lb);
}

//address book

function abook_remove(url, ret){
	var us = document.getElementById('abook_users').getElementsByTagName('input');
	var ids='';
	var names='';
	for(i=0;i<us.length;++i){
		if (us[i].checked){
			ids+=us[i].name.substring(2)+';';
			names+=us[i].value+', ';
		}
	}
	if (ids==''){
		alert('Wybierz osoby do których chcesz usunąć z kontaktów.');
		return false;
	}
	start_loading('lbContent2','Proszę czekać... Trwa pobieranie danych...<\/center><br/><br/><br/>', '<br/><br/><br/><br/><br/><br/><br/><br/><center>');
	new Ajax.Request(url,
		  {
		    method:'POST',
		    parameters: {ids: ids},
		    onSuccess: function(transport){
		    	if (!ret) $('lbContent2').innerHTML=transport.responseText;
		    },
		    
		    onFailure: function(){ alert('Something went wrong...'); }
		  });
	return true;	  
}