/******************************************/
/* Author: Werner de Bruin
/* Date: Aug 2009
/* Libraries used:JQuery
/******************************************/

/* <![CDATA[ */

var pl = $('<p id="preloader">&nbsp;Loading ...</p>');
//var cHeight = "698px";
var itemsPerPage = 20;
var curPos = 1;
var pos = 0;
var act = 0;
var tag = "";

$(function() {	
	$('#wrapper').html(pl);
	$.ajax({
		type: "GET",
		url: "home.php/?feed=rss2&cat=3",
		dataType: "xml",
		success: function(xmlData)
		{					
			xmlDataSet = xmlData;
			browseXML();
		},
		error: function()
		{
			$("#wrapper").html("There's been an error loading the page. Please <a href='http://www.levis.com.au'>try again</a>");
		}	
	});
});

function filter(tag,act) {//function for filter drop down
	var filter = "&tag=" + tag;	
	$('#wrapper').empty();
	$.ajax({
		type: "GET",
		url: "home.php/?feed=rss2&cat=3"+filter,
		dataType: "xml",
		success: function(xmlData)
		{					
			xmlDataSet = xmlData;
			browseXML();			
		},
		error: function()
		{
			$("#wrapper").html("There's been an error loading the page. Please <a href='http://www.levis.com.au'>try again</a>");
		}	
	});
}

function browseXML()
{
	//$('#wrapper').empty();
	resultSetLength = $("item",xmlDataSet).length;
	strToAppend = "<div id='pageLnks'>";

/*
	Determine what set of matched results the user is
	currently browsing. Because we started "curPos" at 0,
	we must add 1 to get the actual current position.
*/
	if (curPos + itemsPerPage > resultSetLength)
	{
		showingThrough = resultSetLength;
	}
	else
	{
		showingThrough = parseInt(curPos + itemsPerPage);
	}

	if (itemsPerPage < resultSetLength)
	{
		if (curPos == 1) // First page. Go forward only.
		{
			strToAppend += "<a href='#'>PREV</a>";
			strToAppend += "<span class='floatLft'>&nbsp;/&nbsp;</span>";
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";paging();browseXML();return false;'>NEXT</a>";
		}
		if (curPos > 1 && parseInt(curPos + itemsPerPage) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";paging();browseXML();return false;'>PREV</a>";
			strToAppend += "<span class='floatLft'>&nbsp;/&nbsp;</span>";
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";paging();browseXML();return false;'>NEXT</a>";
		}
		if (parseInt(curPos + itemsPerPage) > resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";paging();browseXML();return false;'>PREV</a>";
			strToAppend += "<span class='floatLft'>&nbsp;/&nbsp;</span>";
			strToAppend += "<a href='#'>NEXT</a>";
		}
		
		strToAppend += "<div id='pageNumbers'>";
		var curPage = Math.round(curPos/itemsPerPage);	
		for(y=0; y<Math.ceil(resultSetLength/itemsPerPage); y++) {
			if (parseInt(y) == curPage) strToAppend += "<span class='curPage'>" + parseInt(y + 1) + "</span>";			
			else strToAppend += "<span>" + parseInt(y + 1) + "</span>";	
		}
		strToAppend += "</div>";

	}
	strToAppend += "</div>";
	$('#pages').html(strToAppend);	
		
	$("title:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
		x = i + 1;	   
		if(i%4 == 0)$('#wrapper').append('<div id="'+x+'000" class="expanded"></div>');		
		var cID = $("description:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text();
		var imageStr = $(this).parent().find('[nodeName="content:encoded"]').text();		
		var image = imageStr.substring(imageStr.indexOf("src")+5, imageStr.indexOf(".jpg")+4);
		var html = '';
		html += '<div id='+(i+1)+' class="module"><a href="javascript://"'
		+ '" class="aExpand" id="'+cID+'" onmouseover="thumbOver('+(i+1)+')" onmouseout="thumbOut('+(i+1)+')">'
		+ '<img src="'+image+'" border="0" width="231" height="349" />'
		+ '</a></div>';	
		//add module
		$('#wrapper').append(html);
	});
	//remove loader
	$(pl).remove();
	$('#wrapper').fadeIn(1500);
}

$(document).ready(function(){
	//custom filter select box
	//$("select").custSelectBox();
	
	
	$("a.aExpand").live("click", function(){
	
		if ($(this).attr("id") != "") {			
			//Google Analytics tracking of article viewing
			pageTracker._trackPageview('/home.php/?tracking=article&p=' + $(this).attr("id"));
		
			//open previous thumb
			$(".current").show(1500).removeClass("current");
			//close current thumb
			var e = $(this).parent();
			e.hide("slow").addClass("current");		
			
			//which row to expand
			var id = e.attr("id");
			var ee = "";		
			if (id<5) 				 	{ ee="1000";  $(".expanded").not("#1000").html("").animate({width:"1px", height:"1px"}).hide(1000); }
			else if ((id>4)&&(id<9)) 	{ ee="5000";  $(".expanded").not("#5000").html("").animate({width:"1px", height:"1px"}).hide(1000); }
			else if ((id>8)&&(id<13)) 	{ ee="9000";  $(".expanded").not("#9000").html("").animate({width:"1px", height:"1px"}).hide(1000); }
			else if ((id>12)&&(id<17)) 	{ ee="13000"; $(".expanded").not("#13000").html("").animate({width:"1px", height:"1px"}).hide(1000); }
			else if ((id>16)&&(id<21)) 	{ ee="17000"; $(".expanded").not("#17000").html("").animate({width:"1px", height:"1px"}).hide(1000); }
			else ee="5000";		
			$("#"+ee).show().stop().animate({width:"470px", height:"348px"});
			$("#"+ee).html(pl);

			$.ajax({
				type: "GET",
				//url: "home.php/?feed=rss2&p="+$(this).attr("id")+"&cachebuster=" + 1*new Date(),
				url: "callPost.php?cID="+$(this).attr("id")+"&cachebuster=" + 1*new Date(),
				success: function(content){
		
					//remove loader
					$(pl).remove();			

					$("#"+ee).fadeIn().html('<a href="javascript://" id="closeBtn"><img src="/wp-content/themes/levis/images/x.gif" width="15" height="15" border="0" alt="close" /></a><div>'+content+'</div>');
					$("#"+ee).show().stop().animate({width:"464px", height:"100%", minHeight:"348px"}, 1500);
					var cHeight = $("#"+ee).height(); 

					if (cHeight < 350) cHeight = "349px";
					else if ((cHeight > 349) && (cHeight < 699)) cHeight = "700px";
					else if ((cHeight > 698) && (cHeight < 1048)) cHeight = "1049px";
					else if ((cHeight > 1047) && (cHeight < 1397)) cHeight = "1400px";
					else if ((cHeight > 1396) && (cHeight < 1748)) cHeight = "1753px";
					else if ((cHeight > 1745) && (cHeight < 2097)) cHeight = "2104px";
					else if ((cHeight > 2096) && (cHeight < 2448)) cHeight = "2455px";
					else if ((cHeight > 2447) && (cHeight < 2799)) cHeight = "2806px";
					else cHeight = "100%";
					
					if(ee<13000) $("#"+ee).show().stop().animate({width:"464px", height:cHeight, minHeight:"349px"}, 1500);
					/*if(ee>1000) {
						pos = $("#"+ee).position();
						setTimeout("$.scrollTo(pos.top)",5000);
					} else setTimeout("$.scrollTo(0)",5000);*/				
					
				}
			});	
				
		} else {		
			return false;
		}
		
		
	});
	
	//expanded panel's close button function
	$("#closeBtn").live("click", function(){
		$(this).fadeOut();
		$("object").hide();
		$(this).parent(1000).stop().animate({width:"1px", height:"1px"},1500).hide();				
		$(".current").show(1500).removeClass("current");
		if( $.browser.safari ) $(this).parent(1000).empty();
	});

	

});

//Paging animation
function paging() {
	$("#wrapper").fadeOut(1500).show().html(pl);
}
//Mouseover fading
function thumbOver(thumbID){
	$("#"+thumbID).fadeTo("normal", 0.5); // This sets the opacity to 100% on hover
}
function thumbOut(thumbID){
	 $("#"+thumbID).fadeTo("fast", 1.0); // This sets the opacity back to 60% on mouseout
}

/* ]]> */