function doPopup(URL, WIDTH, HEIGHT) {
	url = URL;
	width = WIDTH;  // width of window in pixels
	height = HEIGHT; // height of window in pixels
	windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT + "toolbar=no," + "location=no," + "directories=no," + "status=no," + "menubar=no," + "scrollbars=no," + "resizable=no,";
	preview = document.open(URL, "_blank", windowprops);
}

function ShowOtherInfo(ItemID) {
    document.getElementById(ItemID).style.display = 'block';
}
function HideOtherInfo(ItemID) {
    document.getElementById(ItemID).style.display = 'none';
}

function HideShowOtherInfo(ItemID) {

    if (document.getElementById(ItemID).style.display == 'none')
        document.getElementById(ItemID).style.display = 'block';
    else
        document.getElementById(ItemID).style.display = 'none';
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;


var replaceClass = function(e, oldClass, newClass) {
	if (e.hasClass(oldClass)) {
		e.removeClass(oldClass);
		e.addClass(newClass);
	}
}

var switchTab = function(tabId) {

	var contentTabId = tabId.split("-")[tabId.split("-").length - 1];

	var tabContents = $("#tab-contents > div");
	/* show/hide content */
	var offTabContents = tabContents.not("#" + contentTabId);
	var onTabContent = tabContents.filter("#" + contentTabId);

	/* changing color of selected/deselected tabs */
	var tabs = $(".area-switch li").not(".submenu-delimeter");
	var offTabs = tabs.not("#" + tabId);
	var onTab = tabs.filter("#" + tabId);

	var offStyledElements = offTabs.find(".inactive, .active");
	var onStyledElements = onTab.find(".inactive, .active");

	replaceClass(offTabs, "active", "inactive");
	replaceClass(onTab, "inactive", "active");

	offStyledElements.each(function() {
		replaceClass($(this), "active", "inactive");
	});

	onStyledElements.each(function() {
		replaceClass($(this), "inactive", "active");
	});

	offTabContents.hide();
	onTabContent.show();
	//onTabContent.fadeIn("fast");

}

var toggleCheckbox = function(checkbox, forceStatus) {
	var oldStatus = checkbox.hasClass("off") ? "off" : "on";
	var status = checkbox.hasClass("off") ? "on" : "off";
	if (forceStatus != null) status = forceStatus;
	checkbox.removeClass(oldStatus).addClass(status).attr("src", "img/icons/checkbox_" + status + ".gif");
	return status;
}



$(document).ready(function(){

	var filterTimeout = null;

	var applyOrdering = function(obj) {
		var o = $(obj.target).val();
		var o = o.split("|");

		$("#product-filter input[name=OrderBy]").attr("value", o[0]);
		$("#product-filter input[name=OrderType]").attr("value", o[1]);

		//var base = $("form#OrderByFilter_id #BaseUrl_id").val();
		//location.replace(base + "&OrderBy=" + o[0] + "&OrderType=" + o[1]);

		var reqParams = {};
		$("#product-filter input").each(function(){
			reqParams[this.name] = this.value;
		});


		$("#product-list-content").html("");
		$.get("com/ProductList.cfc?method=show", reqParams,	function(data) {
			$("#product-list-content").html(jQuery.trim(data));
			$("form#OrderByFilter_id #OrderBy_id").bind("change", function(e) {
				applyOrdering(e);
			});
		});

	}


	var showProductList = function() {
		// genre categories
		if ($("#product-genre-filter .checkbox-image").filter(".off").length == 0) {
			toggleCheckbox($("#product-genre-filter .checkbox-all-image"), "on");
		}
		else {
			toggleCheckbox($("#product-genre-filter .checkbox-all-image"), "off");
		}
		var genreCategoryID = [];
		$("#product-genre-filter .checkbox-image").filter(".on").each(function(){
			var c = $(this).attr("id").split("-");
			genreCategoryID.push(c[1]);
		});
		$("#product-genre-filter input[name=GenreCategoryID]").attr("value", genreCategoryID.length > 0 ? genreCategoryID.join("|") : 0);

		// flags
		if ($("#product-flag-filter .checkbox-image").filter(".off").length == 0) {
			toggleCheckbox($("#product-flag-filter .checkbox-all-image"), "on");
		}
		else {
			toggleCheckbox($("#product-flag-filter .checkbox-all-image"), "off");
		}
		$("#product-flag-filter .checkbox-image").each(function(){
			var flag = $(this).attr("id").split("-");
			var isOn = $(this).hasClass("on");
			$("#product-filter input[name=" + flag[1] + "]").attr("value", isOn ? "1" : "");

		});

		// prices

		if ($("#product-price-filter .checkbox-image").filter(".off").length == 0) {
			toggleCheckbox($("#product-price-filter .checkbox-all-image"), "on");
		}
		else {
			toggleCheckbox($("#product-price-filter .checkbox-all-image"), "off");
		}
		var priceFrom = [];
		var priceTo = [];
		$("#product-price-filter .checkbox-image").filter(".on").each(function(){
			var r = $(this).attr("id").split("-");
			priceFrom.push(r[1]);
			priceTo.push(r[2]);
		});
		$("#product-filter input[name=PriceFrom]").attr("value", priceFrom.length > 0 ? priceFrom.join("|") : 0);
		$("#product-filter input[name=PriceTo]").attr("value", priceTo.length > 0 ? priceTo.join("|") : 0);

		var reqParams = {};
		$("#product-filter input").each(function(){
			reqParams[this.name] = this.value;
		});

		function showProducts() {
			$("#product-list-content").html("");
			$.get("com/ProductList.cfc?method=show", reqParams,	function(data) {
				$("#product-list-content").html(jQuery.trim(data));
				$("form#OrderByFilter_id #OrderBy_id").bind("change", function(e) {
					applyOrdering(e);
				});
			});

		}
		filterTimeout = setTimeout(showProducts, 1200);
	}

	$("#product-filter .checkbox-image").click(function(){
		clearTimeout(filterTimeout);
		toggleCheckbox($(this));
		showProductList();
	});


	$("#product-price-filter .checkbox-all-image").click(function(){
		clearTimeout(filterTimeout);
		var s = toggleCheckbox($(this));
		toggleCheckbox($("#product-price-filter .checkbox-image"), s);
		showProductList();
	});


	$("#product-flag-filter .checkbox-all-image").click(function(){
		clearTimeout(filterTimeout);
		var s = toggleCheckbox($(this));
		toggleCheckbox($("#product-flag-filter .checkbox-image"), s);
		showProductList();
	});


	$("#product-genre-filter .checkbox-all-image").click(function(){
		clearTimeout(filterTimeout);
		var s = toggleCheckbox($(this));
		toggleCheckbox($("#product-genre-filter .checkbox-image"), s);
		showProductList();
	});

	/* manage costomerInfoForm radio buttons (shipping addresses)  START */
	$("#CustomerInfoForm #PreviousOrderShipping").change(function(){
		var v = $(this).val();
		if (v != '') {
			var address = eval(v);
			$("#ShippingFirstName").attr("value", address[0]);
			$("#ShippingLastName").attr("value", address[1]);
			$("#ShippingAddress").attr("value", address[2]);
			$("#ShippingZIP").attr("value", address[3]);
			$("#ShippingCity").attr("value", address[4]);
			$("#ShippingCountryID").val(address[5]);
		}
		else {
			$("#ShippingFirstName").attr("value", "");
			$("#ShippingLastName").attr("value", "");
			$("#ShippingAddress").attr("value", "");
			$("#ShippingZIP").attr("value", "");
			$("#ShippingCity").attr("value", "");
			$("#ShippingCountryID").val("");
		}
	});

	$("#CustomerInfoForm").submit(function(){
		if (!$("#ShippingAddressDifferent", this).attr("checked") && $("#ShippingAddressFromOrder", this).val() == 0) {
			$("#ShippingFirstName").attr("value", $("#FirstName").val());
			$("#ShippingLastName").attr("value", $("#LastName").val());
			$("#ShippingAddress").attr("value", $("#Address").val());
			$("#ShippingZIP").attr("value", $("#ZIP").val());
			$("#ShippingCity").attr("value", $("#City").val());
			$("#ShippingCountryID").val($("#CountryID").val());
		}
		//return false;
	});

	$("#CustomerInfoForm #ShippingAddressInvoice").click(function(){
		if ($(this).attr("select")){
			$("#ShippingAddressForm").hide();
			$("#SelectShippingAddress").hide();
			$("#ShippingAddressForm input").attr("disabled",true);
			$("#ShippingAddressForm select").attr("disabled",true);
		}

	});

	$("#CustomerInfoForm #ShippingAddressOther").click(function(){
		if ($(this).attr("select")){
			$("#ShippingAddressForm input").attr("disabled",false);
			$("#ShippingAddressForm select").attr("disabled",false);
			$("#ShippingAddressForm").show();
			$("#SelectShippingAddress").show();
			$("option:first", "#CustomerInfoForm #SelectShippingAddress #PreviousOrderShipping").attr("selected","selected");
			var v = $("option:first", "#CustomerInfoForm #PreviousOrderShipping").val();
			if (v != undefined && v != null && v != '') {
				var address = eval(v);
				$("#ShippingFirstName").attr("value", address[0]);
				$("#ShippingLastName").attr("value", address[1]);
				$("#ShippingAddress").attr("value", address[2]);
				$("#ShippingZIP").attr("value", address[3]);
				$("#ShippingCity").attr("value", address[4]);
				$("#ShippingCountryID").val(address[5]);
			}
			else {
				$("#ShippingFirstName").attr("value", "");
				$("#ShippingLastName").attr("value", "");
				$("#ShippingAddress").attr("value", "");
				$("#ShippingZIP").attr("value", "");
				$("#ShippingCity").attr("value", "");
				$("#ShippingCountryID").val("");
			}
		}
	});

	/*
	$("#CustomerInfoForm #ShippingAddressOther").click(function(){
		if ($(this).attr("select")){
			$("#ShippingAddressForm").show();
			$("#SelectShippingAddress").show();
			$("option:first", "#CustomerInfoForm #PreviousOrderShipping").attr("selected","selected");
			$("#ShippingAddress").attr("value", $("#Address").val());
			$("#ShippingZIP").attr("value", $("#ZIP").val());
			$("#ShippingCity").attr("value", $("#City").val());
			$("#ShippingCountryID").val($("#CountryID").val());
		}
	});
	*/

	$("#CustomerInfoForm #ShippingAddressNew").click(function(){
		if ($(this).attr("select")){
			$("#ShippingAddressForm input").attr("disabled",false);
			$("#ShippingAddressForm select").attr("disabled",false);
			$("#ShippingAddressForm").show();
			$("#SelectShippingAddress").hide();
			$("#ShippingFirstName").attr("value", "");
			$("#ShippingLastName").attr("value", "");
			$("#ShippingAddress").attr("value", "");
			$("#ShippingZIP").attr("value", "");
			$("#ShippingCity").attr("value", "");
			$("#ShippingCountryID").val("");
		}
	});

	$("#shipping-form select[name=ShippingPriceID]").change(function(){
		if (this.value != '') $("#shipping-form").submit();

	});


	$(".registration-suggest-toggle").click(function(){
		$("#registration-suggest form[name=UserRegisterForm]").show("fast");
	});



    $("form#OrderByFilter_id #OrderBy_id").bind("change", function(e){
		applyOrdering(e);
    });

	$("#product-detail .description-short a.switch").click(function(){
		$("#product-detail .description-short").hide();
		$("#product-detail .description-full").show();
	});

	$("#product-detail .description-full a.switch").click(function(){
        $("#product-detail .description-short").show();
		$("#product-detail .description-full").hide();
	});

	$("#product-detail .rating .link").click(function() {
		switchTab($(this).attr("id"));
	});

	/* EnableList is defined in script (Default/objects/CenterObject.cfm) */
	$("ul.area-switch li").click(function() {
		switchTab($(this).attr("id"));
		if(EnableList.match($(this).attr("title")) == null){
			$("##co_DisplayAllProduct").hide();
		}else{
			$("##co_DisplayAllProduct").show();
			$("##co_DisplayAllProduct a").attr('href','index.cfm?module=Catalog&page=productFlagList&Flag='+$(this).attr('id').split("-")[2]);
		}
	});

});
	/*
	$("form#OrderByFilter_id #OrderBy_id").change(function(){
		var o = $(this).val();
		var o = o.split("|");
		var base = $("form#OrderByFilter_id #BaseUrl_id").val();
		location.replace(base + "&OrderBy=" + o[0] + "&OrderType=" + o[1]);
	});
	*/
/*
	$("#ajax-loading").bind("ajaxSend", function(){
		$(this).show();
		$(this).html('<img src=\"img/ajax-loader.gif\" width=\"128\" height=\"15\" alt=\"\">');
	}).bind("ajaxComplete", function(){
		$(this).hide();
		$(this).html('');
	});


});
*/

