var xmlhttp2;
var xmlhttp3;
var currentCategory;

var projectList;

function showCategory(str){
	xmlhttp2=GetXmlHttpObject();
	if (xmlhttp2==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	currentCategory = str;
	var url="projectsxml.php";
	url=url+"?category="+str;
	url=url+"&sid="+Math.random();
	xmlhttp2.onreadystatechange=categoryStateChanged;
	xmlhttp2.open("GET",url,true);
	xmlhttp2.send(null);
	
}

function showFeatured(){
	xmlhttp3=GetXmlHttpObject();
	if (xmlhttp3==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	currentCategory = "featured"
	var url="projectsxml.php";
	url=url+"?category=featured";
	url=url+"&sid="+Math.random();
	xmlhttp3.onreadystatechange=featuredStateChanged;
	xmlhttp3.open("GET",url,true);
	xmlhttp3.send(null);
}

function categoryStateChanged(){
	if (xmlhttp2.readyState==4){
		var pastItems = $(".jcarousel-item").size();
		
		xmlDoc=xmlhttp2.responseXML;
		var thumbs = "";
		
		var halfway = Math.ceil($(xmlDoc).find("clients").children().size()/2);
		var totalItems = $(xmlDoc).find("clients").children().size();
		var firstItem;
		
		currentCarousel.reset();
		projectList = new Array();
		
		clients = $(xmlDoc).find("client").each(function(i){
			if(i == 0){
				firstItem = $(this).find("id").text();
			}
			if(i % 2 == 0){
				thumbs = "<li id=\"Car" + $(this).find("id").text() + "\"\><ul>";
				client2 = "";
			}
			else
				client2 = "client2";
			projectList[i] = $(this).find("id").text();
			thumbs += "<li id=\"client_" + $(this).find("id").text() + "\" class='client " + client2 + "' ><a href=\"portfolio.php\" onclick=\"showproject("+ $(this).find("id").text() + ", '" + currentCategory + "'); return false;\" style=\"background: url(portfolio/" + $(this).find("thumbnail").text() + ")\" title=\"" + $(this).find("clientname").text() + "\"></a><span>" + $(this).find("clientname").text() + "</span></li>";
			if(i % 2 == 1){
				thumbs += "</ul></li>";
				currentCarousel.add(Math.ceil(i/2), thumbs);
				//alert(Math.ceil(i/2) + thumbs);
			}
		});
		if(totalItems % 2 == 1){
			thumbs += "</ul></li>";
			currentCarousel.add(halfway, thumbs);
			//alert(halfway +  thumbs);
		}
		
		currentCarousel.size(halfway);
		
		for(i = halfway+1; i < pastItems; i++){
			//currentCarousel.remove(i);
		}
		currentCarousel.reload();
		
		$(".arrow_right").unbind("click", arrow_next_project)
		var arrow_right = getCurrentArrowRight();
		arrow_right.click(arrow_next_project);
		$(".arrow_left").unbind("click", arrow_previous_project);
		var arrow_left = getCurrentArrowLeft();
		arrow_left.click(arrow_previous_project);
		
		if(currentCategory == "themes"){
			$(".ImageGroup").hide();
			$(".MyImage_theme").show();
		}
		else if(currentCategory == "homepages"){
			$(".ImageGroup").hide();
			$(".MyImage_homepages").show();
		}
		else if(currentCategory == "logos"){
			$(".ImageGroup").hide();
			$(".MyImage_logopicks").show();
		}
		else
			showproject(firstItem, currentCategory);
	}
}

function featuredStateChanged(){
	if (xmlhttp3.readyState==4){
		var pastItems = $(".jcarousel-item").size();
		
		xmlDoc=xmlhttp3.responseXML;
		var thumbs = "";
		
		var halfway = Math.ceil($(xmlDoc).find("clients").children().size()/2);
		var totalItems = $(xmlDoc).find("clients").children().size();
		var firstItem;
		
		currentCarousel.reset();
		projectList = new Array();
		
		clients = $(xmlDoc).find("client").each(function(i){
			if(i == 0){
				firstItem = $(this).find("id").text();
			}
			if(i % 2 == 0){
				thumbs = "<li id=\"Car" + $(this).find("id").text() + "\"\><ul>";
				client2 = "";
			}
			else
				client2 = "client2";
			projectList[i] = $(this).find("id").text();
			thumbs += "<li id=\"client_" + $(this).find("id").text() + "\" class='client " + client2 + "' ><a href=\"portfolio.php\" onclick=\"showproject("+ $(this).find("id").text() + ", '" + currentCategory + "'); return false;\" style=\"background: url(portfolio/" + $(this).find("thumbnail").text() + ")\" title=\"" + $(this).find("clientname").text() + "\"></a><span>" + $(this).find("clientname").text() + "</span></li>";
			if(i % 2 == 1){
				thumbs += "</ul></li>";
				currentCarousel.add(Math.ceil(i/2), thumbs);
			}
		});
		if(totalItems % 2 == 1){
			thumbs += "</ul></li>";
			currentCarousel.add(halfway, thumbs);
		}
		
		currentCarousel.size(halfway);
		
		for(i = halfway; i < pastItems; i++){
			currentCarousel.remove(i);
		}
		
		$(".arrow_right").unbind("click", arrow_next_project)
		var arrow_right = getCurrentArrowRight();
		arrow_right.click(arrow_next_project);
		$(".arrow_left").unbind("click", arrow_previous_project);
		var arrow_left = getCurrentArrowLeft();
		arrow_left.click(arrow_previous_project);
	
		//showproject(firstItem, currentCategory);
	
		
	}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}