
itemsPerPage = 4;
curPos = 0;

$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "http://atlanticbcs.com/plans/_gallery.xml",
		dataType: "xml",
		success: function(xmlData)
		{
			xmlDataSet = xmlData;
			browseXML_custom();
		}
	});
	$.ajax({
		type: "GET",
		url: "http://atlanticbcs.com/plans/_plans.xml",
		dataType: "xml",
		success: function(xmlData2)
		{
			xmlDataSet2 = xmlData2;
			browseXML_stock();
		}
	});
});



function browseXML_custom()
{

	resultSetLength = $("customplan",xmlDataSet).length;
	
	strToAppend = "<h3>Gallery of Custom Homes</h3><div class='planNav'><p><span>";

	if (curPos + itemsPerPage > resultSetLength)
	{
		showingThrough = resultSetLength;
	}
	else
	{
		showingThrough = parseInt(curPos + itemsPerPage);
	}
	strToAppend += "showing <b>" + parseInt(curPos + 1) + "</b> through <b>" + showingThrough + "</b> of <b>" + resultSetLength + "</b>";
	strToAppend += " plans";
	strToAppend += "</span>";
	strToAppend += "show me:&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = 4;curPos = 0;browseXML_custom();return false;'>4</a> &nbsp;|&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = 8;curPos = 0;browseXML_custom();return false;'>8</a> &nbsp;|&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = " + resultSetLength + ";curPos = 0;browseXML_custom();return false;'>view all</a></p></div>";
	
	
	
	strToAppend += "<div class='planPagination'><p>";

	if (itemsPerPage != resultSetLength)
	{
		if (curPos == 0) // First page. Go forward only.
		{
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML_custom();return false;'>view more plans &raquo;</a>";
		}
		if (curPos > 0 && parseInt(curPos + itemsPerPage) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML_custom();return false;'>&laquo; previous plans</a>";
			strToAppend += " &nbsp;|&nbsp; ";
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML_custom();return false;'>view more plans &raquo;</a>";
		}
		if (parseInt(curPos + itemsPerPage) >= resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML_custom();return false;'>&laquo; previous plans</a>";
		}
	}

	strToAppend += "</p></div>";
	
	
	strToAppend += "<div class='planList'><ul>";

	$("title:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
			
			strToAppend += "<li><a class='zoom' href='http://atlanticbcs.com/plans/gallery/" + $("imagefilename:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "'>";
			strToAppend += "<img src='http://atlanticbcs.com/plans/gallery/thumb/" + $("imagefilename:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "' alt='" + $(this).text() + "' />";
			strToAppend += "</a>";
			strToAppend += "<p><strong>" + $(this).text() + "</strong>";
			strToAppend += "</p></li>";
	});

	strToAppend += "</ul><div style='clear:left'></div></div>";

	$("#widget2").html(strToAppend);
	$('a.zoom').fancyzoom();
	
	
	// NEED TO GET THIS SORTED
	
	//$('#homePlans li:nth-child(3)').css("background","red");

	/*
	var obj = $(".planNav a");
	var index = 0;
	obj.eq(index).addClass("selected");
	
	obj.click(function(e) {
	  e.preventDefault();
	  var current = obj.index($(this));
	  if(index != current) {
		obj.eq(index).removeClass("selected");
		obj.eq(current).addClass("selected");
		index = current
	  }
	});
	*/
		
}



function browseXML_stock()
{

	resultSetLength = $("stockplan",xmlDataSet2).length;
	
	strToAppend = "<h3>Stock Residential Plans</h3><div class='planNav'><p><span>";

	if (curPos + itemsPerPage > resultSetLength)
	{
		showingThrough = resultSetLength;
	}
	else
	{
		showingThrough = parseInt(curPos + itemsPerPage);
	}
	strToAppend += "showing <b>" + parseInt(curPos + 1) + "</b> through <b>" + showingThrough + "</b> of <b>" + resultSetLength + "</b>";
	strToAppend += " plans";
	strToAppend += "</span>";
	strToAppend += "show me:&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = 4;curPos = 0;browseXML_stock();return false;'>4</a> &nbsp;|&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = 8;curPos = 0;browseXML_stock();return false;'>8</a> &nbsp;|&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = " + resultSetLength + ";curPos = 0;browseXML_stock();return false;'>view all</a></p></div>";
	
	
	
	strToAppend += "<div class='planPagination'><p>";

	if (itemsPerPage != resultSetLength)
	{
		if (curPos == 0) // First page. Go forward only.
		{
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML_stock();return false;'>view more plans &raquo;</a>";
		}
		if (curPos > 0 && parseInt(curPos + itemsPerPage) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML_stock();return false;'>&laquo; previous plans</a>";
			strToAppend += " &nbsp;|&nbsp; ";
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML_stock();return false;'>view more plans &raquo;</a>";
		}
		if (parseInt(curPos + itemsPerPage) >= resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML_stock();return false;'>&laquo; previous plans</a>";
		}
	}

	strToAppend += "</p></div>";
	
	
	strToAppend += "<div class='planList'><ul>";

	$("title:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet2).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
			
			strToAppend += "<li><a class='zoom' href='http://atlanticbcs.com/plans/plan/" + $("imagefilename:eq(" + parseInt(curPos + i) + ")",xmlDataSet2).text() + "'>";
			strToAppend += "<img src='http://atlanticbcs.com/plans/thumb/" + $("imagefilename:eq(" + parseInt(curPos + i) + ")",xmlDataSet2).text() + "' alt='" + $(this).text() + "' />";
			strToAppend += "</a>";
			strToAppend += "<p><strong>" + $(this).text() + "</strong>, ";
			strToAppend += $("sqft:eq(" + parseInt(curPos + i) + ")",xmlDataSet2).text() + "&nbsp;sqft";
			strToAppend += "</p></li>";
	});

	strToAppend += "</ul><div style='clear:left'></div></div>";

	$("#widget").html(strToAppend);
	$('a.zoom').fancyzoom();
	
	
	// NEED TO GET THIS SORTED
	
	//$('#homePlans li:nth-child(3)').css("background","red");

	/*
	var obj = $(".planNav a");
	var index = 0;
	obj.eq(index).addClass("selected");
	
	obj.click(function(e) {
	  e.preventDefault();
	  var current = obj.index($(this));
	  if(index != current) {
		obj.eq(index).removeClass("selected");
		obj.eq(current).addClass("selected");
		index = current
	  }
	});
	*/
		
}







