/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//functions of pageload

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
parent.document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var menuStatus = 0;
var imageselectorStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}	

function imageselector(obj){


	//loads popup only if it is disabled
	if(imageselectorStatus==0){
		$("#imageselectorbg").css({
			"opacity": "0.7"
		});
		$("#imageselectorbg").fadeIn("slow");
		$("#imageselector").fadeIn("slow");
		imageselectorStatus = 1;
	}

//centering popup

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#imageselector").height();
	var popupWidth = $("#imageselector").width();
	//centering
	$("#imageselector").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#imageselectorbg").css({
		"height": windowHeight
	});
	
//placing imagepage into imageselectorbox

window.open('upload.php?upload=plaatje&obj=' + obj,'upload');
//window.open('imageselector.php?file=image&obj=' + obj,'imageselectorArea');

//ajaxpage('imageselector.php?file=image&obj=' + obj, 'imageselectorArea');	

}

function downloadselector(obj){


	//loads popup only if it is disabled
	if(imageselectorStatus==0){
		$("#imageselectorbg").css({
			"opacity": "0.7"
		});
		$("#imageselectorbg").fadeIn("slow");
		$("#imageselector").fadeIn("slow");
		imageselectorStatus = 1;
	}

//centering popup

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#imageselector").height();
	var popupWidth = $("#imageselector").width();
	//centering
	$("#imageselector").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#imageselectorbg").css({
		"height": windowHeight
	});
	
//placing imagepage into imageselectorbox
ajaxpage('imageselector.php?file=download&obj=' + obj, 'imageselectorArea');	

}

function ShowQuickviewer(){
	
		//loads popup only if it is disabled
	if(popupStatus==0){
		//document.getElementById('quickviewer').style.visibility='visible';
		//$("#quickviewer").fadeIn("slow");
		$("#quickviewer").fadeTo(400,1) ;
		popupStatus = 1;
	}
	else if(popupStatus==1){
	$("#quickviewer").fadeTo(400,0) ;
		popupStatus = 0;
	}
	
}

function HideQuickviewer(){
	
		//loads popup only if it is disabled
	if(popupStatus==1){
		$("#quickviewer").fadeTo(400,0) ;
		popupStatus = 0;
	}
	
}

//disabling popup with jQuery magic!
function ximageselector(){
	//disables popup only if it is enabled
	if(imageselectorStatus==1){
		$("#imageselectorbg").fadeOut("slow");
		$("#imageselector").fadeOut("slow");
		imageselectorStatus = 0;
	}
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("kjhkjh").click(function(){
		//centering with css
		//centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#imageselectorClose").click(function(){
		ximageselector();
	});
	//Click out event!
	$("#imageselectorbg").click(function(){
		ximageselector();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && imageselectorStatus==1){
			ximageselector();
		}
	});

});
