function CreateXMLObject() {

	var xmlHttp = null; // Variable to hold XML HTTP Object
	try {	// Try creating the object for
			// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest(); // Create object with XMLHttpRequest
	}
	catch (e) {	// If it didn't get created, try creating the object for
					// Internet Explorer
		try {
	    	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Create object for IE 6.0+
    	}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // If that failed, create the object for IE 5.5+
    	}
	}
	return xmlHttp;
}
function ShowImage(id) {
	
	xmlHttp = CreateXMLObject();
	if (xmlHttp == null) {	 // If that failed, the browser is very old
		alert("Your browser does not support AJAX!");
		return;
	}
	
	xmlHttp.onreadystatechange=function() { // Deal with readyState changes
		// readyState Values:
		// 0 => The request is not initialized
		// 1 => The request has been set up
		// 2 => The request has been sent
		// 3 => The request is in process
		// 4 => The request is complete
		
		if(xmlHttp.readyState==4) {
			document.getElementById("large_image").innerHTML = xmlHttp.responseText; // Set a value with the response from the server
		}
	}
	var url = "../galleries/image.php?id="+id;
	xmlHttp.open("GET",url,true); // ([POST/GET], <url of serverside script>, [TRUE/FALSE]-asynchronous)
	xmlHttp.send(null); // Send the request
}
function ShowGallery(id, image_id) {
	
	xmlHttp = CreateXMLObject();
	if (xmlHttp == null) {	 // If that failed, the browser is very old
		alert("Your browser does not support AJAX!");
		return;
	}
	
	xmlHttp.onreadystatechange=function() { // Deal with readyState changes
		// readyState Values:
		// 0 => The request is not initialized
		// 1 => The request has been set up
		// 2 => The request has been sent
		// 3 => The request is in process
		// 4 => The request is complete
		
		if(xmlHttp.readyState==4) {
			document.getElementById("sc_menu").innerHTML = xmlHttp.responseText; // Set a value with the response from the server
			ShowImage(image_id);
		}
	}
	var url = "../galleries/thumbnail.php?id="+id;
	xmlHttp.open("GET",url,true); // ([POST/GET], <url of serverside script>, [TRUE/FALSE]-asynchronous)
	xmlHttp.send(null); // Send the request
}
//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;

//loading popup with jQuery magic!  
function loadPopup() {  
	//loads popup only if it is disabled
	if(popupStatus==0) {
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		
		// Set popup status to enabled
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup() {
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		
		// Set popup status to disabled
		popupStatus = 0;
	}
}

//centering popup
function centerPopup() {
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	var windowWidth = 500;
	var windowHeight = 475;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight*3
	});

}

$(document).ready(function() {
	$(function(){
	  //Get our elements for faster access and set overlay width
	  var div = $('div.sc_menu'),
				   ul = $('ul.sc_menu'),
				   // unordered list's left margin
				   ulPadding = 15;
	
	
	  //Remove scrollbars
	  div.css({overflow: 'hidden'});
	
	
	  //When user move mouse over menu
	  div.mousemove(function(e){
		  //Get menu width
		  var divWidth = div.width();
		  //Find last image container
		  var lastLi = ul.find('li:last-child');
	
		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
		var left = ((e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth) - 30;
		div.scrollLeft(left);
	  });
	});
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function() {
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function() {
		disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup").click(function() {
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
});
