//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------//

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------


function showListDetails()
{
	$(this).blur();
	tmpButton = $(this);
	tmpTr = $(this).parent("td").parent("tr");
	
	if($("#list-table").attr("class")=='user')
	{
		ajaxData = '&act=detailUser';
	} else ajaxData = '&act=detailContract';
	
	tmpCloseTr = $("#list-table tr.active_toggle_box");
	
	if(tmpCloseTr.attr("class")=='active_toggle_box')
	{
		//delete details
		$("#list-table tr.active_toggle_box .toggle").slideToggle("normal", function(){
			tmpCloseTr.remove();
			$("#list-table tr.active").removeClass("active");
			
			//ajax
			tmpTr.addClass("active");
			$.ajax({
				data: ajaxData,
				url: "ajax_detail.php",
				type: "GET",
				cache: false,
				beforeSend: function(){
					$("#loading div").attr("style","background-image:url(fileadmin/templates/images/ajax-loader.gif);");
			    $("#loading").attr("style","display:block;");
			   },
				success: function(html){
					tmpTr.after(html);
					
					$("#loading").attr("style","display:none;");
					
					$("#list-table tr.active_toggle_box .toggle").slideToggle("normal");
					tmpButton.unbind('click', showListDetails);
					tmpButton.click(hideListDetails);
				}
			});
			
		});
		
		$("#list-table tr.active a.bu_toggle").unbind('click', hideListDetails);
		$("#list-table tr.active a.bu_toggle").click(showListDetails);
		
	}
	else
	{
		//ajax
		tmpTr.addClass("active");
		$.ajax({
			data: ajaxData,
			url: "ajax_detail.php",
			type: "GET",
			cache: false,
			beforeSend: function(){
				$("#loading div").attr("style","background-image:url(fileadmin/templates/images/ajax-loader.gif);");
		    $("#loading").attr("style","display:block;");
		   },
			success: function(html){
				tmpTr.after(html);
				
				$("#loading").attr("style","display:none;");
				
				$("#list-table tr.active_toggle_box .toggle").slideToggle("normal");
				tmpButton.unbind('click', showListDetails);
				tmpButton.click(hideListDetails);
			}
		});
	}
	
}

function hideListDetails()
{
	$(this).blur();
	$("#list-table tr.active_toggle_box .toggle").slideToggle("normal", function(){
		$("#list-table tr.active_toggle_box").remove();
		$("#list-table tr.active").removeClass("active");
	});
	
	$("#list-table tr.active a.bu_toggle").unbind('click', hideListDetails);
	$("#list-table tr.active a.bu_toggle").click(showListDetails);
}


function popup( url, pWidth, pHeight , param )
{
		var opac=0.7;
	 
		//overlay
		var arrayPageSize = getPageSize();
		
		// calculate top and left offset for the lightbox 
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = (pWidth > arrayPageSize[0]) ? 0 : ((arrayPageSize[0]-pWidth)/2)+arrayPageScroll[0];
		
		var overlayWidth = ((pWidth+20+lightboxLeft) > arrayPageSize[0]) ? pWidth+20+lightboxLeft  : arrayPageSize[0] ;
		var overlayHeight = ((pHeight+20+lightboxTop) > arrayPageSize[1]) ? pHeight+20+lightboxTop  : arrayPageSize[1]+lightboxTop ;
		
		//overlay values
		var styleOverlayValue = 'width:'+overlayWidth+'px; height:'+overlayHeight+'px;';
		
		//lightbox values
		var styleLightboxValue = 'width:'+pWidth+'px; top:'+lightboxTop+'px; left:'+lightboxLeft+'px;';
    
    var styleLoading = 'background-image:url(fileadmin/templates/images/ajax-loader-popup.gif); top:'+lightboxTop+'px;';
		
		//start overlay
		
		//for ie6 bug 
		$("select").attr("style","visibility:hidden;");
		
		$("#overlay").attr("style",styleOverlayValue);
		$("#overlay").fadeTo("fast", opac);

			 
		//ajax
		$.ajax({
			data: param,
			url: url,
			type: "POST",
			cache: false,
			beforeSend: function(){
				$("#loading div").attr("style",styleLoading);
		        $("#loading").attr("style","display:block;");
		   },
			success: function(html){
				$("#lightbox #lb_content").html(html);
				
				$("#loading").attr("style","display:none;");
				
				$("#lightbox").attr("style",styleLightboxValue);
				$("#lightbox").slideDown("normal");
				
				$("#lightbox #lb_close").click(popupClose);
			}
		});

}

function popupClose()
{
	//for ie6 bug
	$("select").attr("style","visibility:show;");
	$("#lightbox").fadeOut("fast");
	$("#overlay").fadeOut("fast");
}