
//init global variables
var active_special_id = '';

//jQuery
$(document).ready(function() { 

	//init hide elements
	$("#fullDetailsWrapper").hide();
	
	// init table sorter/pager
	$("#specialsTable")
	.tablesorter()
	.tablesorterPager	({container: $("#pagination"), size:7});
	
	//init specials carousel
	$("div.scrollable").scrollable( {
			size: 3,
			clickable: false,
			interval: 10000,
			loop: true,
			keyboard: false
		} );

	// Validate Shop Local Form
	
	// Handle Category Link Click
	$(".category_link").click(function(){
		cat_id = $(this).attr("category_id");
		$.get("/Home/ajax.cfm", { action: "load_table", category_id: cat_id },
		   function(data){
			 $("#mainTableContent tbody").html(data)
			 $("#specialsTable")
				.tablesorter()
				.tablesorterPager	({container: $("#pagination"), size:7});
		});
		$.getJSON("/Home/ajax.cfm", { action: "category_details", category_id: cat_id },
			function(data){
				$("#specTableHeading").html(data.intro_text);
		});
	});
	
	//List mouseover effect on table
	$('#mainTableContent').mouseover(function(event) {
		var $tgt = $(event.target);
		if ($tgt.is('h4') | $tgt.is('div') | $tgt.is('p') | $tgt.is('a') | $tgt.is('td')) {
			$tgt.parents('td').addClass("offerItemSelected");
		}
	}).mouseout(function(event) {
		var $tgt = $(event.target);
		if ($tgt.is('h4') | $tgt.is('div') | $tgt.is('p') | $tgt.is('a')) {
			$tgt.parents('td').removeClass("offerItemSelected");
		}
	});
	
	// Handle Special Row Click - get full details of selected special
	$("#mainTableContent,#showCase span").click(function(event) {
		var $tgt = $(event.target);
		if ($tgt.is('h4') | $tgt.is('div') | $tgt.is('p') | $tgt.is('a') | $tgt.is('td') | $tgt.is('img')) {
			active_special_id = $tgt.parents('span').attr("id");
			$("#fullDetails").fadeTo("fast",0.01);
			$.getJSON("/Home/ajax.cfm", { action: "special_details", special_id: active_special_id },
				function(data){
					$("#fullDetails #title").html(data.title);
					$("#fullDetails #details").html(data.details);
					$("#fullDetails #conditions").html(data.conditions);
					$("#fullDetails #valid_from").html(data.valid_from);
					$("#fullDetails #valid_till").html(data.valid_till);
					$("#fullDetails #business_name").html(data.business_name);
					$("#fullDetails #loc_address").html(data.loc_address);
					$("#fullDetails #loc_town").html(data.loc_town);
					$("#fullDetails #loc_pcode").html(data.loc_pcode);
					$("#fullDetails #phone").html(data.phone);
					$("#fullDetails #email").html(data.email);
					$("#fullDetails #tstamp").html(data.tstamp);
					
					if (data.subscribed == 1){
						$("#actionSubscribe").hide();
						$("#actionSubscribed").show();
					}
					else {
						$("#actionSubscribe").show();
						$("#actionSubscribed").hide();
					}					
					
					$("#fullDetailsWrapper").slideDown("normal")
					$("#fullDetails").fadeTo("slow",1);
					
			});
			$("div.bgArrowSelected").removeClass("bgArrowSelected arrowSelected");
			$("#" + active_special_id + " div.bgArrowNormal").addClass("bgArrowSelected arrowSelected");
		}
	});

	//Handle Specials Full Details Actions
	//PRINT
	$("#fullDetailsAction #printDetails").click(function(){
		$("#fullDetailsWrapper").printElement({overrideElementCSS:['/_css/print-specialsDetails.css']});
	});
	
	//EMAIL
	$("#fullDetailsAction #emailDetails").click(function(){
		$("#overlay #content").load('/_includes/ovrEmailSpecialDetails.cfm', {'special_id': active_special_id});
		openOverlay();
	});

});

