/// <reference path="~/js/libraries/jquery-1.3.2-vsdoc.js" />
/// <reference path="default.js" />
var selectedCategory = "";
var defaultNumItems = "12";

function resetProductFilter() {   
	dds = document.getElementsByTagName("SELECT");
	for (i=0; i<dds.length; i++) {
		found = false;
		dd = dds[i];
		if (dd.id == null) {
		    continue;
		} 
		if (dd.id != "numitems" && dd.id != "orderby") {
		    for (x=0; x<dd.options.length; x++) {
			    try {
				    if (dd.options[x].className == "selected") {
					    dd.selectedIndex = x;
					    found = true;
					    break;
				    }
			    } catch (ex) {}
		    }
		    if (!found) {
			    dd.selectedIndex = 0;
		    }
		}
	}
}
/*
cid
scid
brand
styletype
*/
var filterElements = new Array("back" , "cup" , "colour" , "brand", "price", "dress", "styletype", "numitems", "sort", "include", "page");
function getSelectValueByName(elName) {
    el = document.getElementsByName(elName)[0];
    return getSelectValue(el);
}

function stripSelectedValue(text) {
	if (text.indexOf(">") > -1 && text.indexOf("<") > -1) {
	    text = text.substring(text.indexOf(">") + 2, text.indexOf("<"));
    }
    if (text.indexOf("(") > -1) {
        text = text.substring(0, text.indexOf("("));
    }
    return text.strip();
}

function getSeoSelectValue(name) {
	var _el = document.getElementsByName(name)[0];
	if (_el == null) {
	    alert("Null");
	    return "";
	}
	return stripSelectedValue(getSeoString(getSelectText(_el)));
}

function getSeoString(text) {
    return stripSelectedValue(text.toLowerCase()).replace(/ /g, "-").replace("&","and");
}

function reactivateElements() {
    return;
	dds = document.getElementsByTagName("SELECT");
	for (i=0; i<dds.length; i++) {
		dd = dds[i];
		dd.disabled = false;
	}
}

function gotoSeoUrl() {
    v = ROOT_URL + "products/";
    category = selectedCategory;
    var scidEl = $.$("scid");

    if (scidEl != null && scidEl.tagName == "INPUT") {
        subCategory = $(scidEl).val();
    } else {
        subCategory = scidEl == null || scidEl.selectedIndex == 0 ? "" : getSeoSelectValue("scid");
    }
    if ($.$("brand").tagName == "INPUT") {
        brand = $.$("brand").value;
    } else {
        brand = $.$("brand").selectedIndex == 0 ? "" : getSeoString(getSelectValueByName("brand"));
    }
    if ($.$("styletype") != null && $.$("styletype").tagName == "INPUT") {
        styletype = $.$("styletype").value;
    } else {
        styletype = ($.$("styletype") == null || $.$("styletype").selectedIndex == 0) ? "" : getSelectValueByName("styletype"); 
    }
    var useSeoStyleType = true;

    if (scidEl != null) {
        for (var i = 0; i < scidEl.options.length; i++) {
            if (scidEl.options[i].text.toLowerCase().indexOf(styletype.toLowerCase() + " (") > -1) {
                useSeoStyleType = false;
                break;
            }
        }
    }
    if ($.$("include") != null && $.$("include").tagName == "INPUT") {
        inc = $.$("include").value;
    } else {
        inc = getSelectValueByName("include");
    }
    skipStyle = false;
    skipBrand = false;
    isSale = inc == "1";
    /*if (category == "sale" && subCategory != "all") {
    if (parseInt(getSelectValue($("scid"))) < 5) {
    v += subCategory.split("-")[0] + "/sale";	  
    } else {
    strippedSubCat = stripSelectedValue(getSelectText($("scid"))).toLowerCase();
    if (strippedSubCat.endsWith("sale")) {
    v += strippedSubCat.split(" ")[0] + "/sale";
    } else {
    v += "sale?scid=" + getSelectValue($("scid"));              
    }
    }
    } else {*/
    if (subCategory == "all" && brand != "*") {
        v += "brands/" + brand;
        if (category != "sale") {
            v += "/" + category;
        } else {
            category += "?cid=sl";
        }
        skipBrand = true;
    } else {
        v += category;

        if (inc == "1") {
            /* comment out if sale sub categories not enabled */
            if (subCategory.length > 0) {
                v += v.substring(v.length - 1) == "/" ? "" : "/";
                v += subCategory;
            }        
            v += v.substring(v.length - 1) == "/" ? "" : "/";
            v += "sale";
        }
        if (subCategory.length > 0 && subCategory != "all" && !isSale) {
            v += "/" + subCategory;
        } else if (styletype.length > 0 && styletype != "*" && useSeoStyleType && !isSale) {
            v += "/" + getSeoString(styletype);
            skipStyle = true;
        }
    }
    
    //}


    v += v.indexOf("?") == -1 ? "?" : "&";

    for (i = 0; i < filterElements.length; i++) {
        _elName = filterElements[i];
        el = document.getElementsByName(_elName)[0]; //.forms[0].elements[filterElements[i]];
        if (el == null) {
            continue;
        }

        if ((_elName == "styletype" && skipStyle) || (_elName == "brand" && skipBrand)) {
            continue;
        }
        if (_elName == "include" && getSelectValue($.$(_elName)) == "*") {
            continue;
        }
        if (el.name != null) {
            if (el.name == "page" && el.value == 1) {
                continue;
            }
            _v = "";
            switch (el.tagName.toUpperCase()) {
                case "INPUT":
                    switch (el.type.toUpperCase()) {
                        case "CHECKBOX":
                            if (el.checked) {
                                _v = "1";
                            }
                            break;

                        default:
                            _v = el.value;
                            break;
                    }
                    break;

                case "SELECT":
                    oValue = el.options[el.selectedIndex].value;
                    if (filterElements[i] == "numitems" && (oValue == defaultNumItems)) {
                        //skip
                    } else if (filterElements[i] == "include" && inc == "1") {
                    } else if (!el.disabled) {
                        _v = oValue;
                    }
                    break;

                default:
                    break;
            }

            if (_v.length > 0 && _v != "*") {
                v += el.name + "=" + _v + "&";
            }
        }
    }
    v = v.toLowerCase();
    if (v.endsWith("&")) {
        v = v.substring(0, v.length - 1);
    }
    if (v.endsWith("?")) {
        v = v.substring(0, v.length - 1);
    }
    location.href = v.replace("//", "/");
    return false;
}

function submitSearchForm() {
    //reactivateElements();
    document.forms[0].submit();
}

function submitForm() {
    //reactivateElements();
    gotoSeoUrl()
}

window.onpageshow = function (evt) {
	if (evt.persisted) resetProductFilter();
}

function swapProductImage(src) {
	swapImage("productImage", src);		
}
/*
function swapProductFlag(imageId, src) {
	swapImage(imageId, src);
	makePngTransparent($(imageId));	
}
*/
function swapProductFlag(divId, css) {
	$.$(divId).className = css;
}


function makePngTransparent(img) {
}

var rviVisible = true;
function showRecentlyViewedItems(link) {
    try {
        if (!rviVisible) {
            //new Effect.SlideDown("recentlyvieweditems", { duration: 1 });
            $("#recentlyvieweditems").slideDown("slow");
            link.innerHTML = "Recently viewed items >";
        } else {
            //new Effect.SlideUp("recentlyvieweditems", { duration: 1 });
            $("#recentlyvieweditems").slideUp("slow");
            link.innerHTML = "Recently viewed items <";
        }    
        rviVisible = !rviVisible;
    } catch (ex) {
    }
}

function setProductPrice(id, price) {
    $("#" + id).html(price);
}
