jQuery(document).ready(function() {
	LoadHomePageRowTemplates(0, 4, 'homepage-featured-properties', 'homepage-featured-properties');
});



/**
* Rowtemplate containers loader
*/
function LoadHomePageRowTemplates(start, amount, searchtype, templatename) {
	// Load in special offers window
	jQuery("#" + searchtype + "-tab .hp-rowtemplate-container-inner").html("").hide();
	jQuery("#" + searchtype + "-tab .hp-rowtemplate-container").css({background: 'url("/images/loading.gif") no-repeat scroll 50% 50% transparent'});
	jQuery.get("/includes/propsearch/getRowTemplateData.php", { start: start, amount: amount, data: searchtype, rowtemplate: templatename, response: "json"}, function(templatedata) {
		if(countJson(templatedata) > 0) {
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container").css({background: 'none'});
			if(jQuery("#" + searchtype + "-tab .hp-rowtemplate-container .next-row-template").size() == 0) {
				jQuery("#" + searchtype + "-tab .hp-rowtemplate-container").append('<a href="#" class="next-row-template">Next</a><a href="#" class="prev-row-template">Prev</a>').fadeIn(500);
			}
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container-inner").html(templatedata.RESULTDATA).fadeIn(500);
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container .next-row-template").unbind("click");
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container .next-row-template").click(function() {
				var newstart = parseInt(templatedata.VISIBLE) + parseInt(templatedata.START);
				var end = parseInt(templatedata.AMOUNT);
				newstart = ((newstart >= end) ? 0 : newstart);
				LoadHomePageRowTemplates(newstart, amount, searchtype, templatename);
				return false;
			});
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container .prev-row-template").unbind("click");
			jQuery("#" + searchtype + "-tab .hp-rowtemplate-container .prev-row-template").click(function() {
				var newstart = parseInt(templatedata.START) - parseInt(templatedata.VISIBLE);
				var end = parseInt(templatedata.AMOUNT);
				newstart = ((newstart < 0) ? (end-amount) : newstart);
				LoadHomePageRowTemplates(newstart, amount, searchtype, templatename);
				return false;
			});
		}
	}, "json");
}

/**
* Json Count function
*/
function countJson(obj) {
	var prop;
	var count = 0;
	for (prop in obj) {
		count++;
	}
	return count;
}
