$(document).ready(function () {
	if($("select[name='inffinet_render_photo_gallery_state']").length > 0) {
		$("select[name='inffinet_render_photo_gallery_state']").change(function () {
			var stateSel = $(this).find("option:selected").val();
			$.ajax({
				type: 'post',
				url: '/ajax_php/get_county_info.php',
				data: "id="+stateSel,
				success: function(msg) {
					var msgSplit = msg.split("|");
					var i = 0;
					var countyHTML = '<option value="">Select State First</option>';
					
					if(msgSplit.length > 0) {
						countyHTML = '<option value="">-Select County-</option>';
						for(i = 0; i < msgSplit.length; i++) {
							var county = msgSplit[i];
							var countyData = county.split("~");
							var selected = "";
							if(parseInt($("input[name='submitted_county']").val()) === parseInt(countyData[0])) {
								selected = ' selected="selected"';
							}
							countyHTML += '<option value="'+countyData[0]+'"'+selected+'>'+countyData[1]+'</option>';
						}
					}
					
					$("select[name='inffinet_render_photo_gallery_county']").html(countyHTML);
				}
			});
		});
	}
	if($(".product_main_image").length > 0) {
		$(".product_main_image").jqzoom();
	}
	$(".product_main_image").click(function () {
		var img = $(this).attr("href");
		var rel = $(this).attr("rel");
		if(rel !== "") {
			var newImg = new Image();
			newImg.src = img;
			var curHeight = newImg.height;
			var curWidth = newImg.width;
			var newImageWindow = window.open(img,"ProductLargeImage","status=1,toolbar=0,menubar=0,width="+curWidth+",height="+curHeight);
			newImageWindow.moveTo(0,0);
		}
		return false;
	});
	$("input[name='inffinet_add_to_cart']").click(function () {
		var qty = 0;
		if($("input[name='qty']").length > 0) {
			qty = parseInt($("input[name='qty']").val(),10);
		} else if($("input[name='inffinet_product_qty']").length > 0) {
			qty = parseInt($("input[name='inffinet_product_qty']").val(),10);
		}
		if(qty > 0) {
			if($(".inffinet_option_list").length > 0) {
				var goForward = "";
				$("select.inffinet_option_list").each(function () {
					if(goForward !== false) {
						if(trim($(this).find("option:selected").val()) !== "") {
							goForward = true;
						} else {
							goForward = false;
						}
					}
				});
				
				if(goForward === "" || goForward === true) {
					return true;
				} else {
					alert("Please select the options for this product to continue.");
					return goForward;
				}
			} else {
				return true;
			}
		} else {
			alert("Please enter quantity");
			return false;
		}
	});
	$(".inffinet_product_upsell img").css("cursor","pointer");
	$(".inffinet_product_upsell img").click(function () {
		window.location = $(this).parent().find("a").attr("href");
	});
	$(".expand_subcat_nav").click(function () {
		var catId = $(this).attr("rel");
		
		$("#sub_cat_nav_"+catId).slideToggle('normal',function () {
			if($(this).css("display") != "none") {
				$(".expand_subcat_nav[rel='"+catId+"'] img").attr("src","/images/base/icon_contract.gif");
			} else {
				$(".expand_subcat_nav[rel='"+catId+"'] img").attr("src","/images/base/icon_expand.gif");
			}
		});
		
		return false;
	});
	/*Modules Render*/
	if($(".img_module").length > 0) {
		$(".img_module").each(function () {
			$(this).find('img').css("width",$(this).css("width"));
			$(this).find('img').css("height",$(this).css("height"));
		});
	}
});
function updatePhotGalleryForm(code) {
	$("option[id='state_"+code+"']").attr("selected","selected");
	
	var stateSel = $("select[name='inffinet_render_photo_gallery_state'] option:selected").val();
	$.ajax({
		type: 'post',
		url: '/ajax_php/get_county_info.php',
		data: "id="+stateSel,
		success: function(msg) {
			var msgSplit = msg.split("|");
			var i = 0;
			var countyHTML = '<option value="">Select State First</option>';
			
			if(msgSplit.length > 0) {
				countyHTML = '<option value="">-Select County-</option>';
				for(i = 0; i < msgSplit.length; i++) {
					var county = msgSplit[i];
					var countyData = county.split("~");
					var selected = "";
					if(parseInt($("input[name='submitted_county']").val()) === parseInt(countyData[0])) {
						selected = ' selected="selected"';
					}
					countyHTML += '<option value="'+countyData[0]+'"'+selected+'>'+countyData[1]+'</option>';
				}
			}
			
			$("select[name='inffinet_render_photo_gallery_county']").html(countyHTML);
		}
	});
}
