// JavaScript Document

function createObjectReply() {
	var request_type;
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http2 = createObjectReply();
var http3 = createObjectReply();

function getCategory(cat) {
	 
	 document.getElementById('browse').innerHTML = '<div class="loading">Loading '+cat+'...</div>';
	 
	  // Set the random number to add to URL request
	 nocache = Math.random();
	 // Pass the login variables like URL variable
	 http2.open('get', '/scripts/GetCategory.php?cat='+cat+'&nocache='+nocache);
	
	 http2.onreadystatechange = setCategory;
	 http2.send(null);
	 
	 http3.open('get', '/scripts/SetCatNav.php?cat='+cat+'&nocache='+nocache);
	 http3.onreadystatechange = SetCatNav;
	 http3.send(null);
}
function setCategory() {
	if(http2.readyState == 4){ 
		var response = http2.responseText;
		
		document.getElementById('browse').innerHTML = response;
	
		initTooltips();
		initButtons();
		$(".scrollable").each(function () {
			$(this).scrollable();
		});
	}
}

function SetCatNav() {
	if(http3.readyState == 4){ 
		var response = http3.responseText;
		
		document.getElementById('snapNav').innerHTML = response;
	
		initTooltips();
		initButtons();
		$(".scrollable").each(function () {
			$(this).scrollable();
		});
	}
}

function getSnapCat(cat) {
	 
	 document.getElementById('scrollerBox').innerHTML = '<div class="loading">Loading '+cat+'...</div>';
	 
	  // Set the random number to add to URL request
	 nocache = Math.random();
	 // Pass the login variables like URL variable
	 http2.open('get', '/scripts/GetSnapCat.php?cat='+cat+'&nocache='+nocache);
	
	 http2.onreadystatechange = setSnapCat;
	 http2.send(null);

	 http3.open('get', '/scripts/SetSnapCatNav.php?cat='+cat+'&nocache='+nocache);
	 http3.onreadystatechange = SetCatNav;
	 http3.send(null);
}
function setSnapCat() {
	if(http2.readyState == 4){ 
		var response = http2.responseText;
		
		document.getElementById('scrollerBox').innerHTML = response;
	
		initTooltips();
		initButtons();
		$(".scrollable").each(function () {
			$(this).scrollable();
		});
	}
}

function getThumbs(cat,id) {
	 
	 document.getElementById('folioThumbs').innerHTML = '<div class="loading">Loading projects...</div>';
	 
	  // Set the random number to add to URL request
	 nocache = Math.random();
	 // Pass the login variables like URL variable
	 http2.open('get', '/scripts/GetThumbs.php?cat='+cat+'&id='+id+'&nocache='+nocache);
	
	 http2.onreadystatechange = setThumbs;
	 http2.send(null);
}

function setThumbs() {
	if(http2.readyState == 4){ 
		var response = http2.responseText;
		
		document.getElementById('folioThumbs').innerHTML = response;
		initTooltips();
		initButtons();
		$(".scrollable").each(function () {
			$(this).scrollable();
		});
	}
}

function getSnapshot(cat,id) {
	 
	 //document.getElementById('folioThumbs').innerHTML = '<div class="loading">Loading projects...</div>';
	 
	  // Set the random number to add to URL request
	 nocache = Math.random();
	 // Pass the login variables like URL variable
	 http2.open('get', '/scripts/GetSnapshot.php?cat='+cat+'&id='+id+'&nocache='+nocache);
	
	 http2.onreadystatechange = setSnapshot;
	 http2.send(null);
}

function setSnapshot() {
	if(http2.readyState == 4){ 
		var response = http2.responseText;
		
		document.getElementById('homeSnap').innerHTML = response;
		initTooltips();
		initButtons();
		$(".scrollable").each(function () {
			$(this).scrollable();
		});
	}
}

function getRandom() {
	 
	 //document.getElementById('folioThumbs').innerHTML = '<div class="loading">Loading projects...</div>';
	 
	  // Set the random number to add to URL request
	 nocache = Math.random();
	 // Pass the login variables like URL variable
	 http2.open('get', '/scripts/GetRandom.php?&nocache='+nocache);
	
	 http2.onreadystatechange = setSnapshot;
	 http2.send(null);
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
 
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
 
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
