///<reference path="/layout/js/jquery.js" />
var GlobalFieldID = { searchBox: '#searchcatalog', dialogBox: '#shoppingbag', linkShopCart: '#link-shoppbag', mailingInput: '#mailinginput', mailingJoin: '#joinmailing', joinuslink: '#joinuslink', carttotal: '#carttotallabel', LinkCantFindColor: '#linkcantfindcolor', cantfindcolormodal: '#modal-requestcolor', linkcloseprodemailmodal: '#linkcloseprodemailmodal', detailpagecheckoutbutton: '#ContentPlaceHolder1_GoToCheckout' };
var cartopen = false;
var shoppingToteOpen = false;
var navigatingShoppingTote = false;
var _page = "";
var _sideNavSpeedRatio = 2;
var brow = parseInt(jQuery.browser.version, 10);
var diableSelectBoxList = { "disable": ["addressbook-add-edit.aspx"] };

function HOBOUtilities() { }

SetCufon = function () {
    Cufon.replace('.querty', { fontFamily: 'Futura MdCn BT' })('#pagination a', { fontFamily: 'Futura MdCn BT' })('#pagination', { fontFamily: 'Futura MdCn BT' })
			('.nav-line', { fontFamily: 'Qwerty' })('.head h1', { fontFamily: 'Futura MdCn BT' })('.headright h1', { fontFamily: 'Futura MdCn BT' })('.giftcertbottomnav', { fontFamily: 'Futura MdCn BT' });

    Cufon('#sidebar UL LI UL LI A', {
        fontFamily: 'Qwerty',
        hover: {
            color: '-linear-gradient(#ab5c30, #ab5c30)'
        }
    });


    LoadCufon();
};
LoadCufon = function () {
    if (jQuery('.querty').length)
        jQuery('.querty').css('visibility', 'visible');
    if (jQuery('#pagination').length)
        jQuery('#pagination').css('visibility', 'visible');
    if (jQuery('.head h1').length)
        jQuery('.head h1').css('visibility', 'visible');
    if (jQuery('.headright h1').length)
        jQuery('.headright h1').css('visibility', 'visible');
    if (jQuery('#proddetails').length)
        Cufon.replace('#UnitCostSpan', { fontFamily: 'Futura MdCn BT' });
};
HOBOUtilities.prototype.SetDefaults = function () {
    if (jQuery('#proddetails').length) {
        ut.SetCantFindColor();
    }
    if (jQuery('#ContentPlaceHolder1_MyList') != null) {
        jQuery('#ContentPlaceHolder1_MyList').removeAttr('style');
    }
    if (jQuery("select") != null) {
        var diableSelectBox = false;
        jQuery(diableSelectBoxList.disable).each(function (i) {
            if (window.location.href.toLowerCase().indexOf(diableSelectBoxList.disable[i]) > 0) {
                diableSelectBox = true;
            }
        });
//        if (!diableSelectBox) {
//            jQuery("select").selectbox();
//        }
    }
    ut.MailingList();
};
HOBOUtilities.prototype.SetCantFindColor = function () {
    if (jQuery('#lnkCantFindColor').length) {
        jQuery('#lnkCantFindColor').live('click', function () {

            if (jQuery('#ql-container').length) {
                if (typeof hidePopUp != "undefined") {
                    hidePopUp(true);
                }
            }

            jQuery('.overlay').show();

            var pidURL = "/store/product_email_signup.aspx?productid=" + jQuery(this).attr('title');
            jQuery('#signupemailiframe').removeAttr('src');
            jQuery('#signupemailiframe').attr('src', pidURL);

            (jQuery.browser.msie) ? jQuery('#modalCantFind').show() : jQuery('#modalCantFind').fadeIn('slow');

            jQuery('#modalCantFind').center();
            jQuery(window).resize(function () {
                jQuery('#modalCantFind').center();
            });
        });

        jQuery('#lnkCloseCantFind').click(function () {
            jQuery('.modalbg').hide();
            jQuery('#modalCantFind').hide();
            if (jQuery('.overlay').length) {
                jQuery('.overlay').fadeOut(500);
            }
            jQuery('#popup-overlay').hide();
        });
    }
};
HOBOUtilities.prototype.formatCurrency = function (strValue) {

    strValue = strValue.toString().replace(/\$|\,/g, '');

    var dblValue = parseFloat(strValue);
    var blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    var dblValue = Math.floor(dblValue * 100 + 0.50000000001);
    var intCents = dblValue % 100;
    var strCents = intCents.toString();

    dblValue = Math.floor(dblValue / 100).toString();

    if (intCents < 10) {
        strCents = "0" + strCents;
    }

    for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++) {
        dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' + dblValue.substring(dblValue.length - (4 * i + 3));
    }

    return (((blnSign) ? '' : '-') + '$' + dblValue + '.' + strCents);
};

function Panel() {
    this.Close = function () {
        alert("close");
    };
}

function Test() {
    var test = "";


}

String.format = function (frmt, args) {
    for (var x = 0; x < arguments.length; x++) {
        frmt = frmt.replace("{" + x + "}", arguments[x + 1]);
    }
    return frmt;
};
HOBOUtilities.prototype.BindEvents = function () {


    jQuery(GlobalFieldID.linkShopCart).mouseover(function () {
        mc.SlideCartDown();
    });
	jQuery(GlobalFieldID.dialogBox).mouseleave(function () {
        jQuery(this).fadeOut(500);
    });
};
HOBOUtilities.prototype.MailingList = function () {
    var emailval = jQuery(GlobalFieldID.mailingInput).val();
};

HOBOUtilities.prototype.SetPRODDetSalePrice = function () {
    var salepriceactive = jQuery("span").hasClass('crossedout');
    if (salepriceactive) {
        jQuery('#UnitCostSpan').css("color", "#CC0000");
        jQuery('#UnitCostSpan').css("margin-left", "17px");
    }
};

function MiniCart() { }

MiniCart.prototype.GetCartCount = function () {

    var xmlhttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }

    try {
        if (topdomain) {
            var prot = "http://";
            if (isSecure())
                prot = "https://";
            xmlhttp.open("GET", prot + topdomain + "/store/os/shoppingcartcost.aspx", false);
            xmlhttp.send(null);
            var str = xmlhttp.responseText;
            xmlhttp.close;
            xmlhttp = false;
            var ary = str.split("|");
            if (ary.length > 1) {
                mc.BindCartTotal(ary[1]);
            }
        }
    }
    catch (e) { }

};
MiniCart.prototype.SlideCartDown = function () {
    jQuery(GlobalFieldID.dialogBox).slideDown({
        duration: 350,
        complete: mc.GetShoppingCart()
    });
};
MiniCart.prototype.BindCartTotal = function (carttotal) {
    jQuery(GlobalFieldID.carttotal).html(carttotal);

};
MiniCart.prototype.GetShoppingCart = function () {

    var url = '/minicart.aspx';
    jQuery.ajax({
        url: url,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        success: function (html) {
            jQuery('.myBag').empty();
            jQuery('.myBag').append(html);
        },
        error: function (e) {
            alert(e.responseText);
        }
    });


};
MiniCart.prototype.CloseShoppingCart = function (cart) {
    if (jQuery(cart).length > 0) {
        jQuery(cart).close();
    }
};
MiniCart.prototype.SideDownCart = function () {
    jQuery(GlobalFieldID.dialogBox).show();
};
MiniCart.prototype.DetailAddCArtHandler = function (event, args) {
//    var targetCtrl = args.EventTarget;
//    if (targetCtrl == "ctl00$ContentPlaceHolder1$addToCart") {
//        mc.GetCartCount();
//        mc.SlideCartDown();
//        setTimeout('jQuery(GlobalFieldID.dialogBox).fadeOut(500);', 5000);
    //    }
    if ($('.shopBag').length)
        loadMiniCart();

    $('.shopBag').mouseover();
    $('.myBag').fadeOut(5000, function () { });
};
var ut = new HOBOUtilities();
var mc = new MiniCart();

$(function () { //DOCUMENT READY FUNCTION
	//SetCufon();
	ut.SetDefaults();
	//    ut.BindEvents();
	//    mc.GetCartCount();
	if (jQuery('a.subnav')) {
		jQuery('a.subnav').each(function () {
			jQuery(this).click(function () {
				toggleul(this);
			});
		});
	}
	ut.SetPRODDetSalePrice();

	///////LOAD HTML PAGE LEVEL BLOCK SCRIPTS HERE\\\\\\
	if ($('#colorFinderPage').length) {
		initColorFinderPage();
	}
	if ($('#lookbookPage').length) {
		initLookbookPage();
	}
	if ($('#handbagsPage').length) {
		initHandbagsPage();
	}
	if ($('#walletsPage').length) {
		initWalletsPage();
	}
});  //DOCUMENT READY FUNCTION

function toggleul(a) {
    ul = jQuery(a).next();
    ms = Math.ceil(ul.height() * _sideNavSpeedRatio);
    jQuery('.sidenav ul ul ul ul').each(function () {
        if (jQuery(this).offset().top != jQuery(ul).offset().top)
            jQuery(this).slideUp(ms);
    });
    jQuery(ul).slideToggle(ms);
}

function render_cart_header_mod() {

    try {
        getElementByID_Master("CartLiteralSgl").style.display = "none";
        getElementByID_Master("CartLiteralPlural").style.display = "none";
        (getElementByID_Master("CartItemsLBGet").innerHTML == "1") ? getElementByID_Master("CartLiteralSgl").style.display = "inline" : getElementByID_Master("CartLiteralPlural").style.display = "inline";
    }
    catch (e) { }
}

function removeItem(recid) {
    var params = "{ recid : '" + recid + "' }";
    jQuery.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: "/minicart.aspx/RemoveItem",
        data: params,
        success: function (str) {
            jQuery('#minicart-message').html("<span class='alert'>Item removed from your shopping bag.</span>");
            setTimeout(function () { mc.GetShoppingCart(); mc.GetCartCount(); }, 2000);
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}

jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ((jQuery(window).height() - this.outerHeight()) / 2) + jQuery(window).scrollTop() + "px");
    this.css("left", ((jQuery(window).width() - this.width()) / 2) + "px");
    return this;
};
$(document).ready(function () {

    //Search input dissapearing & re-appearing script
    $('.searchBox input[value="SEARCH"]').focus(function () {
        if ($(this).attr("value") == "SEARCH")
        { $(this).attr("value", ""); }
    }); $('.searchBox input[value="SEARCH"]').click(function () {
        if ($(this).attr("value") == "SEARCH")
        { $(this).attr("value", ""); }
    }); $('.searchBox input[value="SEARCH"]').blur(function () {
        if ($(this).attr("value") == "")
        { $(this).attr("value", "SEARCH"); }
    });

    //Search input dissapearing & re-appearing script
    $('.searchBox input[value="EMAIL"]').focus(function () {
        if ($(this).attr("value") == "EMAIL")
        { $(this).attr("value", ""); }
    }); $('.searchBox input[value="EMAIL"]').blur(function () {
        if ($(this).attr("value") == "")
        { $(this).attr("value", "EMAIL"); }
    });

    if ($('.shopBag').length)
        loadMiniCart();

    $('.shopBag').hover(
		function () {
		    $('.myBag').stop(true, true).fadeIn(400);
		},
		function () {
		    $('.myBag').stop(true, true).fadeOut(400);
		}
	);
}); //Document ready ends here


loadMiniCart = function () {
    $.ajax({
        url: '/MiniCart.aspx',
        cache: false,
        async: true,
        success: function (html) {
            //alert(html);
            $('.myBag').html(html);
        }
    });
    render_cart_header("CartItemsLBGet", "CartTotalLBGet");
};
removeItem = function (rid) {
    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: '/MiniCart.aspx/RemoveItem',
        data: "{recid:'" + rid + "'}",
        dataType: 'json',
        success: function (msg) {
            loadMiniCart();
        },
        error: function (e) { alert('error: ' + e.responseText); }
    });
};
