$(document).ready(function(){

	// product1 and product2 need to be set in the homepage.php file
	if (typeof(product1) == "undefined") product1 = 0;
	if (typeof(product2) == "undefined") product2 = 0;
	
	displayGallery(product1,product2);

});

function displayGallery(product1, product2) {

    // perform Ajax call
	$.ajax({
		type:"GET",
		url:"/controllers/__HomepageGalleryData.php",
		data: "product1=" + product1 + "&product2=" + product2,
		dataType:"xml",
		success:function(xml){

    		var productLogo = "";
    		var productName = "";
    		var productLink = "";
    		var productTitle = "";
    		var productBlurb = "";
    		var productReadMore = "";
	
			// hide the gallery placeholder
			$('#homepage_gallery_placeholder').hide();

			// display the buttons and gallery container
			if($(xml).find('productinfo').length < 8) {
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_right_spacer\"></div>");
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_left_spacer\"></div>");
			} else {
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_right\"><img src=\"/images/cycle_right.gif\" alt=\"Right\" width=\"30\" height=\"30\" /></div>");
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_right_disabled\"><img src=\"/images/cycle_right_disabled.gif\" width=\"30\" height=\"30\" /></div>");
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_left\"><img src=\"/images/cycle_left.gif\" alt=\"Left\" width=\"30\" height=\"30\" /></div>");
				$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_left_disabled\"><img src=\"/images/cycle_left_disabled.gif\" width=\"30\" height=\"30\" /></div>");
			}
			$('#homepage_gallery_placeholder').parent().append("<div id=\"cycle_top_franchises\"></div>");

			// variable declaration
			var i=0;
			var sContainer	= "<div class=\"cycle_page\">";
			var sHtml		= "";

    		// loop through xml
    		$(xml).find('productinfo').each(function(){

				// get data from xml
    			productLogo = $(this).find('productlogo').text();
    			productName = $(this).find('productname').text();
    			productLink = $(this).find('productlink').text();
    			productTitle = $(this).find('producttitle').text();
	    		productBlurb = $(this).find('productblurb').text();
	    		productReadMore = $(this).find('productreadmore').text();

				// hack for long German franchise name
				productName=productName.replace(/www./ig,"");
				productBlurb=productBlurb.replace(/www./ig,"");

				// construct business card html
				sHtml += "<div class=\"dom_homepage_businesscard\">";
				sHtml += "<p align=\"center\">";
				sHtml += "<a title=\""+ productTitle +"\" href=\"" + productLink + "\">";
				sHtml += "<img width=\"120\" height=\"51\" alt=\""+ productTitle +"\" src=\"" + productLogo + "\" />";
				sHtml += "</a>";
				sHtml += "</p>";
				sHtml += "<p align=\"center\">";
				sHtml += "<a title=\""+ productTitle +"\" href=\"" + productLink + "\">" + productName + "</a>";
				sHtml += "</p>";
				sHtml += "<p align=\"left\">";
				sHtml += productBlurb + " <a title=\""+ productTitle +"\" href=\"" + productLink + "\">" + productReadMore + "</a>";
				sHtml += "</p>";
				sHtml += "</div>";

				i++;
				if(i%4 == 0) {
	    			$('#cycle_top_franchises').append(sContainer + sHtml + "</div>");
	    			sHtml = "";
				}

    		});

			/*if(sHtml != "") {
	    		$('#cycle_top_franchises').append(sContainer + sHtml + "</div>");
	    		sHtml = "";
			}*/
    	},
    	complete:  function() {
        	onLoadGallery();
    	}
    });

}

function onLoadGallery() {
	// top franchises scroller
	$('#cycle_top_franchises').cycle({ 
		fx:     'scrollHorz', 
    	prev:   '#cycle_left', 
    	next:   '#cycle_right',
    	slideExpr: 'div.cycle_page', 
    	timeout: 10000,
    	pause: 1,
    	speed: 1800,
    	autostop: 1,
    	after:   onAfter
	});

	// highlight prev on hover
	$("#cycle_left img").hover(
		function () {
			$(this).attr({src: "/images/cycle_left_active.gif"});
		}, 
		function () {
			$(this).attr({src: "/images/cycle_left.gif"});
		}
	);

	// highlight next on hover
	$("#cycle_right img").hover(
		function () {
			$(this).attr({src: "/images/cycle_right_active.gif"});
		}, 
		function () {
			$(this).attr({src: "/images/cycle_right.gif"});
		}
	);
}

function onAfter(curr, next, opts, fwd) {
	if(opts.currSlide <= 0) {
		$('#cycle_left').hide();
		$('#cycle_left_disabled').show();
	} else {
		$('#cycle_left').show();
		$('#cycle_left_disabled').hide();
	}
	if(opts.currSlide == opts.slideCount-1) {
		$('#cycle_right').hide();
		$('#cycle_right_disabled').show();
	} else {
		$('#cycle_right').show();
		$('#cycle_right_disabled').hide();
	}
};
