//////////////////////////////////////////////////////////////////////
// Lightbox				                                            //
//                                                                  //
// DOM - select object                                              //
// 								                                    //
// Copyright (C) 2006 - 2008  design project, www.dsnproject.hr     //
//								                                    //
// License: Commercial						                        //
//////////////////////////////////////////////////////////////////////
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function removeMylightbox_external(){
	removeMylightbox();
}
function removeMylightbox(settings){

	$('#mylightbox2_overlay').fadeOut("fast",function(){
		$('#mylightbox2_overlay').remove();									   
	});
	
	$('#mylightbox2_cont').fadeOut("fast",function(){
	    $('#mylightbox2_cont').remove();			   
	});	
}
function createMylightbox(settings){
	
//	settings.width=>640,
//	settings.height=>480,
//	settings.url=>'http://www.google.com',
//  settings.html=>'', // text to go in lightbox
//	settings.lightboxeffect=>'',effect to use when displaying lightbox: fade // default animated,....
//  settings.hide_title=>true,
//	settings.disable_overlay_close=>true,
//  settings.disable_overlay=>false, // it will hide overlay
//	settings.disable_drag

	
	size=Array(settings.width,settings.height);
	
	
	$("#mylightbox2_overlay").remove();			
	$("#mylightbox2_cont").remove();	
	
	$('#mylightbox2_overlay').unbind("click");	
	$(".mylightbox2_close").unbind("click");
	
	arrayPageSize=getPageSize();
																		
	if (!settings.disable_overlay){
		
		$("body").append('<div id="mylightbox2_overlay"></div>');		
				
		if (!settings.disable_overlay_close){
			$('#mylightbox2_overlay').livequery("click",function(){
				removeMylightbox(settings);
			});			
		}
		
		$("body").livequery("resize",function(){
			$('#mylightbox2_overlay').css({width:arrayPageSize[0],height:arrayPageSize[1]});											 
		});
	}	
	
								
	$("body").append('<div id="mylightbox2_cont"></div>');			
	
	if ($('#error_overlay').css('opacity')!=.5 && !settings.disable_overlay){	
		$('#mylightbox2_overlay').css({display:'block',height:arrayPageSize[1],opacity:0});			
		$('#mylightbox2_overlay').fadeTo("fast", .5);
	}

	switch (settings.lightboxeffect){
	default:						
		$('#mylightbox2_cont').css({display:'block',width:size[0],marginLeft:-size[0]/2,opacity:0});									
		
		if (size[1]){
			$('#mylightbox2_cont').css({height:size[1],marginTop:(-size[1]/2)-30});
			marginTopFrom=(-size[1]/2);
		}else {
			marginTopFrom=arrayPageSize[3]/9;
			
			$('#mylightbox2_cont').css({height:size[1],marginTop:marginTopFrom-35,top:0});	
//			marginTopFrom='10%';
		}
		$('#mylightbox2_cont').animate({marginTop:marginTopFrom,opacity:1},"slow","easeOutExpo",function(){
			myLightBox_load(settings);
		});
		break;
	case 'fade':
		$('#mylightbox2_cont').css({display:'block',width:size[0],marginLeft:-size[0]/2,opacity:0});						
		
		if (size[1])			
			$('#mylightbox2_cont').css({height:size[1],marginTop:(-size[1]/2)});
		else
			$('#mylightbox2_cont').css({height:size[1],marginTop:(arrayPageSize[3]/9),top:0});							
		
		$('#mylightbox2_cont').fadeTo("slow",1,function(){
			myLightBox_load(settings);
		});
		break;
	case 'classic':
		$('#mylightbox2_cont').css({display:'block',width:50,marginLeft:-10});						
		
		if (size[1]){
			$('#mylightbox2_cont').css({height:20,marginTop:(-size[1]/2)});
			
			$("#mylightbox2_cont").animate({ 					
				height: size[1],						   
   			    opacity: 1
		        }, 500 , "",function(){
					$("#mylightbox2_cont").animate({ 
    		    	width:size[0],
   	    			marginLeft: -size[0]/2							
				},500,"easeInOutExpo",function(){										
					myLightBox_load(settings);
		
				});
			});				
		} else {
			$('#mylightbox2_cont').css({marginTop:(arrayPageSize[3]/9),top:0});										
			$("#mylightbox2_cont").animate({ 								   
   			    opacity: 1
		        }, 500 , "",function(){
					$("#mylightbox2_cont").animate({ 
    		    	width:size[0],
   	    			marginLeft: -size[0]/2							
				},500,"easeInOutExpo",function(){										
					myLightBox_load(settings);
		
				});
			});		
		}
			
		break;
	}

}
function myLightBox_load(settings){
	
	var output='';
	var padding=0;
	
	if (!settings.hide_title){
		output=output+'<div id="mylightbox2_title"><a href="javascript:void(0);" id="mylightbox2_close" class="mylightbox2_close">Close [x]</a></div>';
		padding=26;
	}

	if (!settings.html)
		output=output+'<iframe style="margin: 0px;" name="mylightbox2_iframe" id="mylightbox2_iframe" width="'+size[0]+'" height="'+(size[1]-padding)+'" frameborder="0" src="'+settings.url+'" scrolling="auto"></iframe>';
	else {
		output=output+'<div  id="mylightbox2_html_cont" style="width: '+size[0]+'px; ';
		if (size[1])
			output=output+'height: '+(size[1]-padding)+'px';
		
		output=output+'">'+settings.html+'</div>';
	}
	
	//Container.innerHTML=output;
	$("#mylightbox2_cont").html(output);	
	
	//enable lightbox window drag and drop
	if (!settings.disable_drag)
		$("#mylightbox2_cont").draggable();
				
	$('.mylightbox2_close').livequery("click",function(){
		removeMylightbox();
		return false;
	});
	
}
function myLightbox(){
	$("a").livequery('click',function(){
		var rel_att=$(this).attr("rel");
		
		if (rel_att){
			if (rel_att.toLowerCase().substring(0,10)=='mylightbox'){						
			
				eval("var settings="+rel_att.substring(10,rel_att.length));
				
				if (!settings.url)
					settings.url=$(this).attr("href");	
				
				createMylightbox(settings);
				return false;				
			}
		}
	})		
}

$(document).livequery(myLightbox);

