
$(document).ready(function(){

    setEnterEvent();
    
    if ($("#TabbedPanels1").length != 0) {
        var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
    }
    var Redirurl = "/";
    if (location.pathname == "/current_cart") {
        location.href = Redirurl;
    }
    setAdvancedSearchPage();
    setOpenCartClick();
    $("#btnsearch").click(function(){
        $("#searchForm").submit();
    });
    
    //ImageSetting
    var imgUl = $("#imgList ul");
    var imgList = $("#hiddenimg li img");
    for (var i = 0; i < imgList.length; i++) {
        $(imgUl).append("<li><a href='" + $(imgList[i]).attr("src") + "'><img src='" + $(imgList[i]).attr("src") + "' width='60' height='57' border='0' alt='' /></a></li>");
        
    }
    $("#imgUl").find('li:first').addClass('active');
    $("#imgUl").galleria({
    
        history: false,
        clickNext: false,
        insert: '#mainImg',
        onImage: function(image, caption, thumb){
            // fade in the image 
            var imgsrc=image.attr("src").replace(/THUMBNAIL/,"FORMATTED");
			image.attr("src",imgsrc)
            image.attr("height", 180);
            image.attr("width", 280);
            image.hide().fadeIn(500);
            
            // animate active thumbnail's opacity to 1, other list elements to 0.6
            //thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.3);
            //thumb.fadeTo('fast', 1).addClass('selected');
        },
        
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb){
        
            var $li = thumb.parent();
            thumb.addClass("img-list-1eave");
            opacity = $li.is('.active') ? 1 : 0.3;
            
            // hover effects for list elements
            $li.hover(function(){
                $li.fadeTo(200, 1);
                
                thumb.removeClass("img-list-1eave");
                thumb.addClass("img-list-hover");
            }, function(){
                thumb.removeClass("img-list-hover");
                thumb.addClass("img-list-1eave");
                
                $li.not('.active').fadeTo(200, opacity);
            })
        }
    });// display first image when Galleria is loaded
    if ($(".galleria_wrapper").length > 0) {
        $(".galleria_wrapper").addClass("jqzoom")
        $(".galleria_wrapper").jqueryzoom({
            xzoom: 300, //default 200
            yzoom: 270, //default 200
            offset: 10, //default 10
            position: "right", //default "right"
            preload: 1,
            lens: 1
        });
    }
    set_product_variations();
    
    //if($(".content").height()<550)
    //{
    //	$(".content").height(550)
    //}
    $("#more").hover(function(){
        $("#menuMore").show("fast")
    }, function(){
    
    });
    
    $("#submm").hover(function(){
        $("#menuMore").show("fast")
        
    }, function(){
        $("#menuMore").hide("fast")
    });
    
    if ($("#leftside").height() < 400) {
    
        $("#leftside").height(400);
    }
    
    if ($("#leftside-2").height() < 400) {
    
        $("#leftside-2").height(400);
    }
    
    if ($("#leftside").height() > $("#rightside").height()) {
        $("#rightside").height($("#leftside").height());
    }
    
    if ($("#leftside-2").height() > $("#rightside-2").height()) {
        $("#rightside-2").height($("#leftside-2").height());
        
    }
    
    if ($("#leftside").height() < $("#rightside").height()) {
    
        $("#leftside").height($("#rightside").height());
    }
    
    if ($("#leftside-2").height() < $("#rightside-2").height()) {
    
        $("#leftside-2").height($("#rightside-2").height());
    }
    
    $("span.toggle_custom_upload_file").click(function(){
        $(this).next("div.custom_upload_file_form_div").toggle('fast');
        $("div.custom_upload_file").toggle("fast")
    })
    $("form.custom_upload_file_form").submit(ajaxFileUpload);
})

function setOpenCartClick(){
    $("#openCart").toggle(function(){
        $("#top-2").show("1000")
        $(this).html("close cart");
    }, function(){
        $("#top-2").hide("1000")
        $(this).html("open cart");
    });
    
    $("#closecart").click(function(){
        $("#openCart").click();
        
    });
}

function setEnterEvent(){
    if ($("#txtemail").length > 0) {
        $("#txtemail").bind('keydown', 'return', function(evt){
            checkEmail();
        });
    }
    
}

function setMenu(){
    var hightlight = false;
    var hightlightNum = -1;
    var firstMenu;
    var subMenu;
    var hightlightMainMenu;
    var isProduct = false;
    var isThirdMenu = false;
    var isForthMenu = false;
    var forthMenu;
    var thirdMenu;
    $.each(menu, function(i, n){
        if (i == 0) {
            firstMenu = n;
        }
        
        $.each(n.taxonomy.children, function(ci, cn){
            if (location.pathname.split(',')[0] == cn.taxonomy.link) {
                hightlight = true;
                subMenu = cn;
                hightlightNum = ci;
                return false;
                
            }
            if ($("#collUrl").val() != undefined && $("#collUrl").val() != "" && cn.taxonomy.link == $("#collUrl").val()) {
                isProduct = true;
                subMenu = cn;
                hightlightNum = ci;
                return false;
            }
            if (cn.taxonomy.children != undefined) {
                $.each(cn.taxonomy.children, function(cci, ccn){
                    if (location.pathname.split(',')[0] == ccn.taxonomy.link) {
                        hightlight = true;
                        subMenu = cn;
                        hightlightNum = ci;
                        isThirdMenu = true;
                        thirdMenu = ccn;
                        
                        return false;
                    }
                    if ($("#collUrl").val() != undefined && $("#collUrl").val() != "" && ccn.taxonomy.link == $("#collUrl").val()) {
                        isProduct = true;
                        subMenu = cn;
                        hightlightNum = ci;
                        isThirdMenu = true;
                        thirdMenu = ccn;
                        return false;
                    }
                    if (ccn.taxonomy.children != undefined) {
                        $.each(ccn.taxonomy.children, function(cci, cccn){
                            if (location.pathname.split(',')[0] == cccn.taxonomy.link) {
                                hightlight = true;
                                subMenu = cn;
                                hightlightNum = ci;
                                isForthMenu = true;
                                forthMenu = cccn;
                                thirdMenu = ccn;
                                
                                return false;
                            }
                            if ($("#collUrl").val() != undefined && $("#collUrl").val() != "" && cccn.taxonomy.link == $("#collUrl").val()) {
                                isProduct = true;
                                subMenu = cn;
                                hightlightNum = ci;
                                isForthMenu = true;
                                forthMenu = cccn;
                                thirdMenu = ccn;
                                return false;
                            }
                            
                        });
                    }
                    
                });
            }
            if (hightlight) {
                return false;
            }
        });
        
        if (hightlight || location.pathname.split(',')[0] == n.taxonomy.link || isProduct) {
            hightlight = false;
            
            $.each(n.taxonomy.children, function(ci, cn){
                if (hightlightNum == ci) {
                    $("#catoSub").html(cn.taxonomy.name + " > ")
                    if (cn.taxonomy.children != undefined) {
                        $.each(cn.taxonomy.children, function(cci, ccn){
                            if (ccn.taxonomy.children != undefined) {
                                $("#catoSub").after('<li><a class="a1" href="' + ccn.taxonomy.link + '">' + ccn.taxonomy.name + ' > </a><ul id=forth_' + ccn.taxonomy.id + '></ul></li>');
                                $.each(ccn.taxonomy.children, function(cci, cccn){
                                    $("#catoSub").nextAll("li").find("#forth_" + ccn.taxonomy.id).append('<li><a class="a4" href="' + cccn.taxonomy.link + '">' + cccn.taxonomy.name + '</a></li>')
                                })
                            }
                            else {
                                $("#catoSub").after('<li><a class="a1" href="' + ccn.taxonomy.link + '">' + ccn.taxonomy.name + '</a></li>');
                            }
                            
                        });
                    }
                    if (ci < 6) {
                    
                        $("#submenu > ul").append('<li><a style="color:#fff" href="' + cn.taxonomy.link + '">' + cn.taxonomy.name + '</a></li>')
                    }
                    else {
                        $("#menuMore > ul").append('<li><a style="color:#fff" href="' + cn.taxonomy.link + '">' + cn.taxonomy.name + '</a></li>')
                    }
                }
                else {
                    if (ci < 6) {
                    
                        $("#submenu > ul").append('<li><a href="' + cn.taxonomy.link + '">' + cn.taxonomy.name + '</a></li>')
                    }
                    else {
                        $("#menuMore > ul").append('<li><a href="' + cn.taxonomy.link + '">' + cn.taxonomy.name + '</a></li>')
                    }
                }
                
            });
            hightlightMainMenu = n;
            $("#mainnav > ul").append('<li class="out"><a href="' + n.taxonomy.link + '"><span>' + n.taxonomy.name + '</span></a></li>');
            $("#catoMain > a").attr("href", n.taxonomy.link);
            $("#catoMain > a").html(n.taxonomy.name + " > ")
            var subm = "";
            subm = subMenu != undefined ? subMenu.taxonomy.name : "";
            if (isProduct) {
                isProduct = false;
                if (thirdMenu == undefined) {
                    $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                    '</a><a class="a1" href="' +
                    subMenu.taxonomy.link +
                    '">' +
                    subMenu.taxonomy.name +
                    '</a><span class="fb c1">' +
                    $("#procName").html() +
                    '</span>');
                }
                else 
                    if (thirdMenu != undefined && forthMenu == undefined) {
                    
                        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        subMenu.taxonomy.link +
                        '">' +
                        subMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        thirdMenu.taxonomy.link +
                        '">' +
                        thirdMenu.taxonomy.name +
                        '</a><span class="fb c1">' +
                        $("#procName").html() +
                        '</span>');
                    }
                    else 
                        if (thirdMenu != undefined && forthMenu != undefined) {
                            $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                            '</a><a class="a1" href="' +
                            subMenu.taxonomy.link +
                            '">' +
                            subMenu.taxonomy.name +
                            '</a><a class="a1" href="' +
                            thirdMenu.taxonomy.link +
                            '">' +
                            thirdMenu.taxonomy.name +
                            '</a><a class="a1" href="' +
                            forthMenu.taxonomy.link +
                            '">' +
                            forthMenu.taxonomy.name +
                            '</a><span class="fb c1">' +
                            $("#procName").html() +
                            '</span>');
                        }
            }
            else 
                if (isThirdMenu) {
                    if (thirdMenu.taxonomy.name.length > 45) {
                        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        subMenu.taxonomy.link +
                        '">' +
                        subMenu.taxonomy.name +
                        '</a><span class="fb c1">' +
                        thirdMenu.taxonomy.name.substr(0, 45) +
                        "..." +
                        '</span>');
                    }
                    else {
                        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        subMenu.taxonomy.link +
                        '">' +
                        subMenu.taxonomy.name +
                        '</a><span class="fb c1">' +
                        thirdMenu.taxonomy.name +
                        '</span>');
                    }
                }
                else 
                    if (isForthMenu) {
                        var thirdmenutext = thirdMenu.taxonomy.name;
                        var forthmenutext = forthMenu.taxonomy.name;
                        if (thirdmenutext.length > 30) {
                            thirdmenutext = thirdmenutext.substr(0, 30) + "...";
                        }
                        if (forthmenutext.length > 30) {
                            forthmenutext = forthmenutext.substr(0, 30) + "...";
                        }
                        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        subMenu.taxonomy.link +
                        '">' +
                        subMenu.taxonomy.name +
                        '</a><a class="a1" href="' +
                        thirdMenu.taxonomy.link +
                        '">' +
                        thirdmenutext +
                        '</a><span class="fb c1">' +
                        forthmenutext +
                        '</span>');
                    }
                    else {
                        if (subm != "") {
                            if (subm.length > 45) {
                                $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name + '</a><span class="fb c1">' + subm.substr(0, 45) + "..." + '</span>');
                            }
                            else {
                                $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><a class="a1" href="' + hightlightMainMenu.taxonomy.link + '">' + hightlightMainMenu.taxonomy.name + '</a><span class="fb c1">' + subm + '</span>');
                            }
                        }
                        else {
                        
                            if (location.pathname != "/") {
                                $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><span class="fb c1">' + hightlightMainMenu.taxonomy.name + '</span>');
                            }
                            
                        }
                    }
            
        }
        else {
        
            $("#mainnav > ul").append('<li><a href="' + n.taxonomy.link + '"><span>' + n.taxonomy.name + '</span></a></li>')
        }
        
    });
    if (location.pathname == "/account" || location.pathname == "/account/") {
        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><span class="fb c1">My Account</span>');
    }
    if (location.pathname == "/login" || location.pathname == "/login/") {
        $("#sitemap").html('<a class="a1" href="' + firstMenu.taxonomy.link + '">' + firstMenu.taxonomy.name + '</a><span class="fb c1">Login</span>');
    }
    if ($("#menuMore > ul").find("li").length == 0) {
        $("#more").parent("div").hide();
    }
    if ($("#mainnav > ul").find("li:first").attr("class") == "out") {
        $("#mainnav > ul").find("li:first").removeClass("out");
        $("#submenu").removeClass();
        $("#submenu").addClass("nav-3d");
        $("#submenu > ul").find("li").each(function(){
            $("a", this).attr("style") != undefined && $("a", this).attr("style") != "" && $("a", this).css("color", "#fcac0a");
        })
        $("#sitemap").find("a:first").hide();
        
    }
    $("#mainnav > ul").find("li:first").addClass("hm");
    
    if ($("#submenu > ul").find("li").length == 0) {
        //$("#submenu").css("background","#F1EEEB url(../images/bgimg-8.png) repeat-x scroll center top")
        $("#mainnavwrap").removeClass("nav-height");
        $("#mainnavwrap").addClass("nav2d-height");
        $("#submenu").hide();
        
    }
}

function isNum(str){
    return (str.search(/^\d+(\.\d+)?$/) != -1);
}

function getOption(){
    if (location.pathname.search("/commodities/show/product/") != -1) {
        var id = $("#firstOption").val();
        var url = "/product_option/option_values/" + id + "/" + $("#commodity_id").val() + "/0-0";
        $.post(url, function(data){
            var op = eval("(" + data + ")")
            $.each(op, function(i, n){
                $("#" + id).append("<option value='" + n.product_option_value.id + "'>" + n.product_option_value.value + "</option>")
            })
            if (op.length == 1) {
                var urlSec = "/product_option/option_values/" + id + "/" + $("#commodity_id").val() + "/0-0";
            }
            
        });
    }
}

var product_variations;

function set_product_variations(){
    if (location.pathname.search("/commodities/show/product/") != -1) {
        var url = "/commodities/variations/" + $("#commodity_id").val() + "/product";
        $.post(url, function(data){
            product_variations = eval("(" + data + ")");
            
            getOptionValue();
            bindAddtoCatrClick();
        })
    }
}

function bindAddtoCatrClick(){
    $("#btnAddtocart").click(function(){
        $("#err").html("");
        if (!isNum($("#quantity").val())) {
            $("#err").html("Please enter a valid quantity.");
            return false;
        }
        else 
            if (Number($("#quantity").val()) > Number($("#stock").val())) {
                $("#err").html("Currently " + $("#stock").val() + " item(s) are available.");
            }
            else {
            
                var commodity_id = $("#commodity_id").val();
                var commodity_type = $("#commodity_type").val();
                var quantity = $("#quantity").val();
                var product_variation_ids = $("#product_variation_ids").val();
                
                //$("#addtocartForm").submit();
                
                if ($("#top-2").css("display") == "none") {
                    $("#openCart").click();
                    
                }
                //$("#top-2").find("tr[id^='tr_cart_item']:last").after('<tr><td colspan="6" align="center"><img src="/themes/Packaging_Specialties/assets/images/shoppingcart_load.gif"/></td></tr>');
                $("#top-2").append('<div width="100%" style="text-align:center"><img src="/themes/Packaging_Specialties/assets/images/shoppingcart_load.gif"/></div>')
                $.ajax({
                
                    type: "POST",
                    url: "/cart_items.js",
                    data: {
                        "commodity_id": commodity_id,
                        "commodity_type": commodity_type,
                        "quantity": quantity,
                        "product_variation_ids[]": product_variation_ids
                    },
                    success: function(msg){
                    
                        $.post("/current_cart", function(data){
                        
                            var cartHtml = $("#shoppingcart", data).html();
                            if (cartHtml == null) {
                                cartHtml = $(data).html();
                            }
                            $("#shoppingcart").html(cartHtml);
                            
                            setOpenCartClick();
                            if ($("#top-2").css("display") == "none") {
                                $("#openCart").click();
                            }
                        })
                    }
                });
            }
    });
}

function getOptionValue(){
    var arr = new Array();
    $("select[id^='op_']").each(function(){
        var id = $(this).attr("id").split("_")[1];
        var val = $(this).val();
        var op = id + "_" + val;
        arr.push(op);
    });
    var num = 0;
    var ishav = false;
    var opval;
    $.each(product_variations, function(i, n){
        num = 0;
        $.each(arr, function(k, j){
            if ($.inArray(j, n) > -1) {
                num++;
            }
            if (num == arr.length) {
                ishav = true;
                return false;
            }
        })
        if (ishav) {
            opval = n;
            return false;
        }
    });
    
    $("#product_variation_ids").val(opval[opval.length - 1].product_variation.id);
    if ((opval[opval.length - 1].product_variation.selling_price <= opval[opval.length - 1].product_variation.sale_price)||opval[opval.length - 1].product_variation.sale_price == null) {
        $("#vari_price").html("$" + FormatPrice(opval[opval.length - 1].product_variation.selling_price));
    }
    else {
        $("#vari_price").html('<span class="f11 c9 line-through">$' + FormatPrice(opval[opval.length - 1].product_variation.selling_price) + '</span><br/><span class="fb">$' + FormatPrice(opval[opval.length - 1].product_variation.sale_price) + '</span>');
    }
    $("#stock").val(opval[opval.length - 1].product_variation.stock);
}

function FormatPrice(price){
    price = price.toString();
    if (price.split(".").length >= 2) {
        if (price.split(".")[1].length < 2) {
            return price + "0"
        }
        return price;
    }
    return price + ".00";
}

function setAdvancedSearchPage(){
    var i = 0;
    var isfirst = true;
    $("#abovePage > li").each(function(){
        i++;
        if ((location.pathname + location.search) == $("a", this).attr("alt")) {
            isfirst = false;
            $("a", this).css("color", "#413636")
        }
        $("a", this).html(i.toString());
    })
    if (isfirst) {
        $("#abovePage").find("li:first > a").css("color", "#413636")
    }
    var k = 0;
    isfirst = true;
    $("#belowPage > li").each(function(){
        k++;
        if ((location.pathname + location.search) == $("a", this).attr("alt")) {
            isfirst = false;
            $("a", this).css("color", "#413636")
        }
        $("a", this).html(k.toString());
    })
    if (isfirst) {
        $("#belowPage").find("li:first > a").css("color", "#413636")
    }
    if (location.search != "") {
        var per_page = location.search.match(/per_page=[^&]*/g);
        
        $("#per_page option").each(function(){
        
            if ($(this).val() == per_page[0].split("=")[1]) {
                $(this).attr("selected", "selected")
            }
        })
        
        var sort_option = location.search.match(/sort_option=[^&]+/g);
        var sort_sign = location.search.match(/sort_sign=[^&]+/g);
        $("#sort_option option").each(function(){
            if ($(this).val() == sort_option[0].split("=")[1] + "_" + sort_sign[0].split("=")[1]) {
                $(this).attr("selected", "selected")
            }
        })
    }
}

function setAdvancedSearchOption(OPtype, OPvalue){
    var per_page = $("#per_page").val();
    var sortPrice = $("#sort_option").val().split("_");
    var sort_option = sortPrice[0];
    var sort_sign = sortPrice[1];
    var url = location.pathname;
    url += "?per_page=" + per_page + "&page_number=1&sort_option=" +
    sort_option +
    "&sort_sign=" +
    sort_sign +
    "&filte_options=&" +
    OPtype +
    "=" +
    OPvalue;
    //?per_page=20&sort_option=price&sort_sign=asc&price_space=&filte_options=&page_number=1
    location.href = url;
}

function AdvancedSearchPage(searchUrl){
    location.href = searchUrl;
}

function updateShoppingCart(id){
    $("#mess_" + id).html('<img src="/themes/Packaging_Specialties/assets/images/update_load.gif"/>')
    $.ajax({
        type: "PUT",
        url: "/cart_items/" + id + ".js",
        data: {
            "cart_item[quantity]": $("#quantity_" + id).val()
        },
        success: function(){
            $.get("/current_cart", function(data){
            
                $("#cart_total").html($("#cart_total", data).html())
                $("#item_" + id + "_total").html($("#item_" + id + "_total", data).html())
                $("#items_total").html($("#cart_total", data).html())
                $("#mess_" + id).html('Successful!');
            })
        }
    });
}

function deleteShoppingCart(id){
    $("#delete_cart_" + id).html('<img src="/themes/Packaging_Specialties/assets/images/update_load.gif"/>');
    $.ajax({
        type: "DELETE",
        url: "/cart_items/" + id + ".js",
        success: function(msg){
            $.get("/current_cart", function(data){
                $("#cart_total").html($("#cart_total", data).html())
                $("#item_q").html($("#item_q", data).html())
                $("#items_total").html($("#cart_total", data).html())
                $("#tr_cart_item_" + id).remove();
                
                if ($("tr[id^='tr_cart_item_']").length == 0) {
                    $("#shopping_cart_view").remove();
                }
            })
        }
    });
}

function set_Taxo(arr, divID){
    var price = "";
    var k = 0;
    $.each(arr, function(i, n){
        if ((i + 1) % 3 == 1) {
            k++;
            $("#" + divID).append("<div id='" + divID + "_child_" + k + "'></div>");
        }
        price = n.taxonomy.selling_price == "0" ? "" : n.taxonomy.selling_price
        if ((i + 1) % 3 != 0) {
            $("#" + divID + "_child_" + k).append('<div class="pic-group-L left"><div align="center" class="border-bottom-s p-img"><a href="' + n.taxonomy.link +
            '"><img height="118" border="0" width="190" src="'+
            n.taxonomy.thumbnail +
            '" alt="" /></a></div><div class="left text-1 lh18 c4"><a href="' +
            n.taxonomy.link +
            '" class="a1">' +
            n.taxonomy.name +
            '</a></div><div class="right lh18"><span class="fb c3">' +
            price +
            '</span></div><div class="clear"></div></div>')
        }
        else {
            $("#" + divID + "_child_" + k).append('<div class="pic-group-R right"><div align="center" class="border-bottom-s p-img"><a href="' + n.taxonomy.link +
            '"><img height="118" border="0" width="190" src="'+
            n.taxonomy.thumbnail +
            '" alt="" /></a></div><div class="left text-1 lh18 c4"><a href="' +
            n.taxonomy.link +
            '" class="a1">' +
            n.taxonomy.name +
            '</a></div><div class="right lh18"><span class="fb c3">' +
            price +
            '</span></div><div class="clear"></div></div><div class="clear"></div>')
        }
        
    })
    var totalDiv = $("div[id^='" + divID + "_child_" + "']").length;
    if (totalDiv > 2) {
        $("div[id^='" + divID + "_child_" + "']").each(function(){
            if (Number($(this).attr("id").split("_")[2]) >= 3) {
                $(this).hide();
            }
        });
        var page = Math.ceil(totalDiv / 2);
        $("#" + divID).append('<div class="clear"></div><div class="right pagelink fb f11"><ul id="' + divID + "_bel" + '"></ul></div>');
        for (var j = 1; j <= page; j++) {
            if (j == 1) {
                $("#" + divID + "_bel").append("<li><a id='" + divID + "_a_" + j + "' style='color:#583F37' onclick='SetCatePage(\"" + divID + "\"," + j + ")'>" + j + "</a></li>");
            }
            else {
                $("#" + divID + "_bel").append("<li><a id='" + divID + "_a_" + j + "' onclick='SetCatePage(\"" + divID + "\"," + j + ")'>" + j + "</a></li>");
            }
            
        }
        
    }
    
}


function SetCatePage(divID, pagenumber){
    var cate1 = pagenumber * 2 - 1;
    var cate2 = pagenumber * 2;
    $("div[id^='" + divID + "_child_" + "']").each(function(){
        $(this).hide();
    });
    
    $("#" + divID + "_child_" + cate1).show();
    $("#" + divID + "_child_" + cate2).show();
    $("#" + divID + "_bel > li").each(function(){
        $(this).find("a").css("color", "#E98400");
    });
    
    $("#" + divID + "_a_" + pagenumber).css("color", "#583F37");
}



function ajaxFileUpload(){

    $.ajaxFileUpload({
        url: $(this).attr('action') + ".js",
        secureuri: false,
        fileElementId: $(this).find("input:file").attr('id'),
        dataType: 'text',
        success: function(data, status){
            attachment = eval('(' + data.replace(/<\/?pre>/g, '') + ')');
            $("div.custom_upload_file_form_div").hide("fast");
            $("div.custom_upload_file").html("<b>" + attachment.private_asset.filename + "</b>&nbsp;&nbsp;&nbsp;&nbsp;")
            $("div.custom_upload_file").append('<a href=\'javascript:delete_custom_upload_file("' + attachment.private_asset.attachable_id + '")\'>Del</a>&nbsp;&nbsp;&nbsp;&nbsp;')
            $("div.custom_upload_file").show("fast")
        },
        error: function(data, status, e){
            alert(status);
        }
    })
    return false;
}

function delete_custom_upload_file(cart_item_id){
    if (!confirm("Are you sure?")) 
        return;
    $.ajax({
        url: '/cart_items/' + cart_item_id + '/custom_file.js',
        data: '',
        type: 'delete',
        dataType: 'text',
        success: function(data){
            $("div.custom_upload_file").html('')
            //$("div.custom_upload_file_form_div").show("fast");
        },
        error: function(){
            alert("Error :(")
        }
    })
}

function toggle_custom_file_upload(){
    $("div.custom_upload_file_form_div").toggle('fast');
    $("div.custom_upload_file").toggle("fast")
}

function setbrowserjs(){
    var browser = navigator.appName
    var b_version = navigator.appVersion
    var version = parseFloat(b_version)
    
    if (browser == "Microsoft Internet Explorer" && (version == 4)) {
        $.getScript("/themes/Packaging_Specialties/assets/js/iepng.js");
    }
}

function selectCollPageChange(){
    var per_page = $("#per_page").val();
    var paraUrl = location.search;
    var sortPrice = $("#sort_option").val().split("_");
    var sort_option = sortPrice[0];
    var sort_sign = sortPrice[1];
    var pn = location.pathname;
    if (paraUrl == "") {
        paraUrl = "?per_page=" + per_page + "&page_number=1&sort_option=" + sort_option + "&sort_sign=" + sort_sign + "&filte_options=&price_space=0-";
    }
    else {
        paraUrl = paraUrl.replace(/per_page=[^&]*/, "per_page=" + per_page);
        paraUrl = paraUrl.replace(/sort_option=[^&]+/, "sort_option=" + sort_option);
        paraUrl = paraUrl.replace(/sort_sign=[^&]+/, "sort_sign=" + sort_sign);
		paraUrl = paraUrl.replace(/page_number=[^&]*/, "page_number=1");
        
    }
    location.href = pn + paraUrl;
}


function setFoot(){
    $.each(foot, function(i, n){
    
        $("#foot").append("<a href=" + n.taxonomy.link + ">" + n.taxonomy.name + "</a> | ")
    })
    $("#foot").append("@ 2009 Packaging Specialties")
}

function getOrder(orderType){
    $.get("/orders?order_by=" + orderType, function(data){
        $("#" + orderType).html(data);
    })
}

function checkEmail(){
    var email = $("#txtemail").val();
    var emailReg = new RegExp(/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/);
    if (email == "") {
        $("#emailMess").show();
        $("#emailMess").html("Please enter the e-mail address.")
        return false;
    }
    if (emailReg.test(email)) {
        $("#emailMess").show();
        $("#emailMess").html('<img src="/themes/Packaging_Specialties/assets/images/update_load.gif"/>');
        $.post("/newsletters", {
            "newsletter[email]": email
        }, function(){
            $("#emailMess").html("Successful.")
            $("#emailMess").fadeOut(5000);
        })
    }
    else {
        $("#emailMess").show();
        $("#emailMess").html("The e-mail address you entered is invalid.");
    }
}



function FormatRelatedProcPrice(strPrice)
{
	if(strPrice.indexOf("-")>=0)
	{
		var price=strPrice.split("-");
		if(price.length==2)
		{
			return FormatPrice(price[0])+"-"+FormatPrice(price[1]);
		}
	}
	else
	{
		return FormatPrice(strPrice);
	}
	
}
