// JavaScript Document

var oHttp;

function initOverlayAjax(){
	oHttp = getOHttpRequest();
}

function getOHttpRequest(){
	try{
		req = new XMLHttpRequest();
	} catch(err1){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHttp");
			} catch(err3){
				req = false;
			}
		}
	}
	return req;
}

/**** portfolio overlay links generator ****/
function sendCategoryRequest(categories){
	var categoryIds = categories.split("|");
	for(var i=0; i<categoryIds.length; i++){
		document.getElementById(categoryIds[i] + "_links").innerHTML = "<div><img src='assets/images/preloader.gif' alt='Loading...'/></div>";
	}
	
	var phpIds = categories;
	var randomNum = Math.round(Math.random()*1000000000);
	var myurl = 'service/getCategory.php?categoryIds=' + phpIds + '&rand=' + randomNum;
	oHttp.open("GET", myurl, true);
	oHttp.onreadystatechange = receiveCategoryReply;
	oHttp.send(null);
}

function receiveCategoryReply(){
	if(oHttp.readyState == 4){
		if(oHttp.status == 200){
			fillCategory(oHttp.responseText);
		}
		else{
			alert("Error:" + oHttp.statusText);
		}
	}
	else{
		//loading handler goes here...
	}
}

function fillCategory(e){
	//console.log(e);
	var reply = e.split("|");
	var divId;
	var content;
	
	for(var i=0; i<reply.length; i++){
		if(i%2 != 0){
			divId = reply[parseInt(i-1)];
			content = reply[i];
			if(divId != "" && content != ""){
				document.getElementById(divId + '_links').innerHTML = content;
			}
		}
	}
	
	$(".portfolio_category a").unbind("mouseover").unbind("mouseout")
	.mouseover(function(e){
		var id = $(this).attr("href").replace("#/work/", "");
		showPreview(e, id);
	}).mouseout(function(){
		hidePreview();
	}).mousemove(function(e){
		movePreview(e);
	});
}
