//var $j = jQuery.noConflict();
function change_zoom(img, regpath, detailpath) {
    //zoom_hide();
    var mir = getElementByID_Master("RealZoomH");
    if (mir != null) {
        change_zoom_realzoom(img, regpath, detailpath);
        return;
    }

    var mz = getElementByID_Master("MagicZoomHidden");
    if (mz != null) {
        change_zoom_magiczoom(img, regpath, detailpath);
        return;
    }

    var ppht = getElementByID_Master("PrettyPopupHeight");
    var pp = getElementByID_Master("PrettyPhotoPopup");
    var height = "457";

    if (ppht != null)
        height = ppht.value;

    if (pp != null) {
        if (regpath == null)
            pp.href = "/store/zoom_popup.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=760&height=" + height;
        else
            pp.href = "/store/zoom_popup.aspx?img=" + detailpath + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=760&height=" + height;
    }

    var ppext = getElementByID_Master("PrettyPhotoPopupExtended");
    if (ppext != null) {
        if (regpath == null)
            ppext.href = "/store/zoom_popup_ext.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=795&height=" + height;
        else
            ppext.href = "/store/zoom_popup_ext.aspx?img=" + detailpath + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=795&height=" + height;
    }

    var pphl = getElementByID_Master("PrettyPopupHl");
    if (pphl != null) {
        var img = '';

        if (getElementByID_Master("ExtraMediaButton1") != null) {
            img = aExtraGroupColors[g_color][1];
            getElementByID_Master("ExtraMediaButton1").href = "/store/zoom_popup.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=868&height=" + height;
        }
        if (getElementByID_Master("ExtraMediaButton2") != null) {
            img = aExtraGroupColors[g_color][2];
            getElementByID_Master("ExtraMediaButton2").href = "/store/zoom_popup.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=868&height=" + height;
        }
        if (getElementByID_Master("ExtraMediaButton3") != null) {
            img = aExtraGroupColors[g_color][3];
            getElementByID_Master("ExtraMediaButton3").href = "/store/zoom_popup.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=868&height=" + height;
        }
        if (getElementByID_Master("ExtraMediaButton4") != null) {
            img = aExtraGroupColors[g_color][4];
            getElementByID_Master("ExtraMediaButton4").href = "/store/zoom_popup.aspx?img=" + img.replace(".jpg", "_z.jpg") + "&iframe=true&width=868&height=" + height;
        }
    }


    var zoomImage = getElementByID_Master("ZoomImage");

    if (zoomImage != null) {
        try {
            var displayiImg = img;
            if (displayiImg.toUpperCase().indexOf("STORE/PRODUCTIMAGES/DETAILS") > 0) {
                displayiImg = displayiImg.replace(".jpg", "_z.jpg");
            }
            else {
                if (getElementByID_Master("ContentPlaceHolder1_s7ProductImage")) {
                    if (getElementByID_Master("ContentPlaceHolder1_S7ZoomHiddenPath") != null) {
                        displayiImg = document.getElementById("ContentPlaceHolder1_S7ZoomHiddenPath").value;
                        getElementByID_Master("ContentPlaceHolder1_ZoomImage").href = displayiImg;
                    }
                } else {
                    displayiImg = "/store/productimages/details/" + displayiImg.replace(".jpg", "_z.jpg");
                }
            }
            zoomImage.href = displayiImg;
            try {
                $(".zoomWrapperImage").find("img").attr("src", displayiImg);
            }
            catch (err) { }
        }
        catch (err) { }
    }

    var modalzoom = document.getElementById("ContentPlaceHolder1_DetailModal1_ProductZoomImage");
    if (modalzoom != null) {
        try {
            modalzoom.src = "/store/productimages/details/" + img.replace(".jpg", "_z.jpg");
        }
        catch (err) { }
    }

    //change_fullZoom();
}
$(document).ready(function () {


    configPersistentCart();
    if ($('.bag').length)
        loadMiniCart();

    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

        if ($.fn.bgiframe) {
            $('.sub-menu').bgiframe();
        }

        if ($.fn.bgiframe) {
            $('.sub-menu-single').bgiframe();
        }
    }
    $('UL#photo-list LI.current').each(function (i) {
        if (!i) {
            var photoUrl = $(this).children('a').attr('href');
            var photoZoom = $(this).children('a').attr('rel');
            loadPhoto(photoUrl, photoZoom);
        }
    });

    var pc = getElementByID_Master("ProductColors");

    if (pc != null) {

        var length = pc.options.length;

        if (length == 2)
            getElementByID_Master("ProductColors").selectedIndex = 1;

        if (aSKUPreviousCost.length > 0) {
            if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                $('#sale-price').removeClass('sale-price');
                $('#sale-text').hide();
            }
            else { $('#sale-text').show(); }

            $(".unselected").click(function () {
                if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                    $('#sale-price').removeClass('sale-price');
                    $('#sale-text').hide();
                } else {
                    $('#sale-price').addClass('sale-price');
                    $('#sale-text').show();
                }
            });

            $(".selected").click(function () {
                if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                    $('#sale-price').removeClass('sale-price');
                    $('#sale-text').hide();
                } else {
                    $('#sale-price').addClass('sale-price');
                    $('#sale-text').show();
                }
            });
        }
    }

    $('.stay-connected').hover(function () {
        $('.stay-connected-box').show();
    });

    $('.stay-connected-box .close').click(function () {
        $('.stay-connected-box').hide();
    });

    $('.thumbs A').click(function () {
        $('UL#photo-list LI').removeClass('current');
        $(this).parent('LI').addClass('current');
        var photoUrl = $(this).attr('href');
        var photoZoom = $(this).attr('rel');
        $('#photo-preview').fadeOut(300, function () {
            loadPhoto(photoUrl, photoZoom);
        });
        $('#photo-preview').fadeIn(300);
        return false;
    });

    if ($('div.trigger-accordian').length > 0) {
        $('div.trigger-accordian').click(function () {
            if ($(this).hasClass('open')) {
                $(this).removeClass('open');
                $(this).addClass('close');
                $(this).next().slideDown(200);
                return false;
            } else {
                $(this).removeClass('close');
                $(this).addClass('open');
                $(this).next().slideUp(200);
                return false;
            }
        });
    }

});

function loadPhoto(url, zoom){
	if(url == ''){ return; }
	var photo = new Image();
	$(photo).load(function(){
		$("#photo-preview").removeClass("loading").html("").append("<a rel='magnify' href=" + zoom + "></a>")
		$("#photo-preview").children("A").append(this);
		$("a[rel*=magnify]").magnify();
	}).attr('src', url); 
}

//PERS CART

var ie = (/MSIE/.test(navigator.userAgent));

$.fn.showContent = function (s, cb) {
	ie ? $(this).stop(true, true).show(0, cb) : $(this).stop(true, true).fadeIn(s, cb);
}

$.fn.hideContent = function (s) {
	ie ? $(this).stop(true, true).hide() : $(this).stop(true, true).fadeOut(s);
}

configPersistentCart = function (cc) {
    $('.bag').hover(function () {
        cc = $(this).children('.cartContents');
        cc.showContent('slow');
        clearTimeout(timeOut);
    }, function () {
        cc = $(this).children('.cartContents');
        cc.hideContent('fast');
    });
}

var timeOut;
openCloseMiniCart = function () {
	$.ajax({
	    url: '/store/MiniCart.aspx',
	    cache: false,
        async:false,
		success: function (html) {
			$('.cartContents').html(html).showContent('slow', function () {
				timeOut = setTimeout(function () { $('.cartContents').hideContent('fast'); }, 6000);
			});
		}
	});
	render_cart_header("CartItemsLBGet", "CartTotalLBGet");
}

loadMiniCart = function () {
	$.ajax({
	    url: '/store/MiniCart.aspx',
	    cache: false,
		success: function (html) {
			//alert(html);
			$('.cartContents').html(html);
		}
	});
	render_cart_header("CartItemsLBGet", "CartTotalLBGet");
}

removeItem = function (rid) {
	$.ajax({
		type: 'POST',
		contentType: 'application/json; charset=utf-8',
		url: '/store/MiniCart.aspx/RemoveItem',
		data: '{rid:"' + rid + '"}',
		dataType: 'json',
		success: function (msg) {
			loadMiniCart();
		},
		error: function (e) { alert('error: ' + e.responseText); }
	});
}
