// JavaScript Document
jQuery(document).ready(function(){
	// initilize drop down navigation using superfish plugin
	//$(".nav").superfish();
	
	//initialize login form toggle link
	$("#link_login").click(function () {
		//console.log("show login form");
		$(".login_form").toggle();
		//$(this).toggleClass("active");
		return false;
	});
		
	// animate nav mouseovers
	$('.nav a').mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 -174px)"}, {duration:500})
		});
	
	// fix IE CSS hover flicker
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}

	var related_category_ID = $("#related_category_ID");
	if (related_category_ID) {
		$("#related_category_ID").change(updateProductSelect).change();
	}
	
	//use shipping address checkbox in checkout
	var use_shipping_address = $("#use_shipping_address");
	if (use_shipping_address) {
		$("#use_shipping_address").change(updateBillingAddress).change();
		
		// assign change handlers for shipping address - if use_shipping_address is checked, copy value to billing address
		$("#shipping_address").change(updateBillingAddress);
		$("#shipping_address2").change(updateBillingAddress);
		$("#shipping_city").change(updateBillingAddress);
		$("#shipping_state").change(updateBillingAddress);
		$("#shipping_province").change(updateBillingAddress);
		$("#shipping_country").change(updateBillingAddress);
		$("#shipping_zip").change(updateBillingAddress);
	}	
	
	//if country is USA (country_ID = '226') then show state otherwise show province
	$("#shipping_country").change(toggleShippingStateProvince);
	$("#billing_country").change(toggleBillingStateProvince);
	//set initial show / hide
	toggleShippingStateProvince();
	toggleBillingStateProvince();
	
	// if in checkout assign next buttons inside the accordion
	$('#button_checkout_step_1').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 1); // switch to second section
		return false;
	});
	$('#button_checkout_step_2').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 2); // switch to third section
		return false;
	});
	$('#button_checkout_step_3').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 0); // switch to first section
		return false;
	});
	$('#button_checkout_step_4').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 3); // switch to fourth section
		return false;
	});
	$('#button_checkout_step_5').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 1); // switch to second section
		return false;
	});
	$('#button_checkout_step_6').click(function() { // bind click event to button
		$(".accordion").accordion('activate', 2); // switch to third section
		return false;
	});
	
	//add and remove rows to table in wheel recoating form
	$('#link_wheel_add').click(function() { // bind click event to link
		$('#table_recoating_wheels tbody>tr:last').clone(true).insertAfter('#table_recoating_wheels tbody>tr:last');
		$('#table_recoating_wheels tbody>tr:last #link_wheel_delete').show();
		$('#table_recoating_wheels tbody>tr:first #link_wheel_delete').hide();
		return false;
	});
	$('#table_recoating_wheels tbody>tr:first #link_wheel_delete').hide();
	$('#link_wheel_delete').click(function() { // bind click event to link
		$(this).parent().parent().remove();
		return false;
	});
	
	formHelpersInit();
	
	//update shopping cart when quantity is changed by user
	$('.cart_quantity').change(updateCart);
	$('.cart_country').change(updateCart);
	$('.cart_coupon').change(updateCart);
	$('.cart_coupon_button').hide();
		
	// load the JQZoom feature for the wheel recoats section
	try {
		$('.zoom').jqzoom();
	} catch(e) { }
});

//****** FUNCTIONS *********************************//
function formHelpersInit() {
	// check for change in admin form status
	$("#status").change(updateAdminForm);
	// check for change in admin form status
	$("#product_type").change(updateAdminForm);
	// check for change in admin form status
	$("#category_ID").change(updateAdminForm);
	// check for change in admin form status
	$("#checkout_user_ID").change(updateCheckoutForm);
	
	//add datepickers for start and end dates for news and calendars
	$("#date_start").datepicker({ dateFormat: $.datepicker.ATOM, showOn: "both", buttonImage: "images/icons/calendar.png", buttonImageOnly: true });
	$("#date_end").datepicker({ dateFormat: $.datepicker.ATOM, showOn: "both", buttonImage: "images/icons/calendar.png", buttonImageOnly: true });
	
	//accordian for checkout form
	$(".accordion").accordion({ header: "h3", autoHeight: false });
}

// register onclick events for search_tips div show hide
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// update select based on chosen category
function updateProductSelect() {
	$("#related_ID").load("admin_products_related.cfc?method=get_products", 
		{	category_ID: $("#related_category_ID").val(),
			product_ID: $("#add_related_product_ID").val()
		}
	);
	return false;
}

// update shopping Cart
function updateCart() {
	$("#shopping_cart_wrapper").load("shopping_cart_update.cfm", 
		$("#shopping_cart").serializeArray(),
		function() {
			$('.cart_quantity').change(updateCart);
			$('.cart_country').change(updateCart);
			$('.cart_coupon').change(updateCart);
			$('.cart_coupon_button').hide();
		}
	);
	return false;
}

// update select based on chosen category
function updateBillingAddress() {
	if ($("#use_shipping_address").is(":checked")) {
		$("#billing_address").val($("#shipping_address").val());
		$("#billing_address2").val($("#shipping_address2").val());
		$("#billing_city").val($("#shipping_city").val());
		$("#billing_state").val($("#shipping_state").val());
		$("#billing_province").val($("#shipping_province").val());
		$("#billing_country").val($("#shipping_country").val());
		$("#billing_zip").val($("#shipping_zip").val());
		
		$("#billing_address").attr("disabled","disabled");
		$("#billing_address2").attr("disabled","disabled");
		$("#billing_city").attr("disabled","disabled");
		$("#billing_state").attr("disabled","disabled");
		$("#billing_province").attr("disabled","disabled");
		$("#billing_country").attr("disabled","disabled");
		$("#billing_zip").attr("disabled","disabled");
		
	} else {
		$("#billing_address").removeAttr("disabled");
		$("#billing_address2").removeAttr("disabled");
		$("#billing_city").removeAttr("disabled");
		$("#billing_state").removeAttr("disabled");
		$("#billing_province").removeAttr("disabled");
		$("#billing_country").removeAttr("disabled");
		$("#billing_zip").removeAttr("disabled");
	}
	return false;
}

// show or hide billing address based on USA (country_ID = '226') selected as country
function toggleShippingStateProvince(){
	var shipping_country = $("#shipping_country").val();
	if (shipping_country == "226") {
		$("#shipping_state").show();
		$("#label_shipping_state").show();
		$("#shipping_province").hide();
		$("#label_shipping_province").hide();
	} else {
		$("#shipping_state").hide();
		$("#label_shipping_state").hide();
		$("#shipping_province").show();
		$("#label_shipping_province").show();
	}
}

// show or hide billing address based on USA (country_ID = '226') selected as country
function toggleBillingStateProvince(){
	var billing_country = $("#billing_country").val();
	if (billing_country == "226") {
		$("#billing_state").show();
		$("#label_billing_state").show();
		$("#billing_province").hide();
		$("#label_billing_province").hide();
	} else {
		$("#billing_state").hide();
		$("#label_billing_state").hide();
		$("#billing_province").show();
		$("#label_billing_province").show();
	}
}

// update admin form
function updateAdminForm() {
	//console.log("called update admin form");
	$("#admin_report_form").load("admin_reports.cfc?method=admin_report_form", 
		{	status: $("#status").val(),
			product_type: $("#product_type").val(), 
			category_ID: $("#category_ID").val() 
		},
		function() {
			//console.log("callback after update admin form");
			formHelpersInit();	
		}
	);
	return false;
}

// update checkout form
function updateCheckoutForm() {
	//console.log("called update checkout form");
	$("#form_checkout").load("checkout.cfc?method=checkout_form", 
		{	checkout_user_ID: $("#checkout_user_ID").val()
		},
		function() {
			//console.log("callback after update checkout form");
			formHelpersInit();	
		}
	);
	return false;
}

// get a random number between min and max
function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
 
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}

addLoadEvent(fix_IE_flicker);
addLoadEvent(flash_init);

function fix_IE_flicker() {
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}	
}

function flash_init() {
	//console.log("called flash_init");
	try {
		// place swf on home page
		var flash_replace = document.getElementById("flash_home");
		if (flash_replace) {
			var swf_file = "home.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_home", "820", "330", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on about page
		var flash_replace = document.getElementById("flash_about");
		if (flash_replace) {
			var swf_file = "about.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_about", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on rubberhog page
		var flash_replace = document.getElementById("flash_rubberhog");
		if (flash_replace) {
			var swf_file = "rubberhog.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_rubberhog", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on rubberhog design center page
		var flash_replace = document.getElementById("flash_rubberhog_design");
		if (flash_replace) {
			var swf_file = "rubberhog_design.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_rubberhog_design", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on rubberhog grinding page
		var flash_replace = document.getElementById("flash_rubberhog_rgw");
		if (flash_replace) {
			var swf_file = "rubberhog_grinding.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_rubberhog_rgw", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on rubberhog surgery page
		var flash_replace = document.getElementById("flash_rubberhog_rst");
		if (flash_replace) {
			var swf_file = "rubberhog_surgery.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_rubberhog_rst", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on rubberhog recoating page
		var flash_replace = document.getElementById("flash_rubberhog_recoating");
		if (flash_replace) {
			var swf_file = "rubberhog_recoating.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_rubberhog_recoating", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on shieldzall page
		var flash_replace = document.getElementById("flash_shieldzall");
		if (flash_replace) {
			var swf_file = "shieldzall.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_shieldzall", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on custom services page
		var flash_replace = document.getElementById("flash_custom");
		if (flash_replace) {
			var swf_file = "custom_services.swf";
			swfobject.embedSWF("flash/" + swf_file, "flash_custom", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
		
		// place swf on kutzall pages
		var flash_replace = document.getElementById("flash_kutzall");
		if (flash_replace) {
			// pick a random swf file to rotate through
			var swf_file_index = rand(1,3);
			var swf_file = "";
			if(swf_file_index == 1) {
				swf_file = "kutzall_ice.swf";
			} else if(swf_file_index == 2) {
				swf_file = "kutzall_shop.swf";
			} else if(swf_file_index == 3) {
				swf_file = "kutzall_wood.swf";
			} 
			swfobject.embedSWF("flash/" + swf_file, "flash_kutzall", "820", "307", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
			//console.log("called swfobject.embedSWF");
		}
	} catch(e) {
		//console.log("CAUGHT ERROR: " + e);	
	}
}