//var $j = jQuery.noConflict();
$(document).ready(function () {

	$('.secondaryNav li:last-child').css({ marginBottom: 0 });

	var container = $('.homeContainer');
	var sideBar = $('.sideNav');

	var containerHeight = container.height() - 145;
	var sideBarHeight = sideBar.height() + 145;

	(container.height() > sideBar.height()) ? sideBar.css({ height: containerHeight + 'px' }) : container.css({ height: sideBarHeight + 'px' });

	configPersistentCart();
	if ($('.bagWrap').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;
			}
		});
	}

	//=========FREE SHIPPING INFORMATION POPUP CODE=======
	$('#freeShippingPop').css('cursor', 'pointer').click(function () {
		$('.shippingInfoPop').css({ 'display': 'block' });
	});
	$('.shippingInfoPop').click(function () {
		$('.shippingInfoPop').css({ 'display': 'none' });
	});
	//=========END=======

});      //document ready

//Reconfigure HTML pages to have left nav and right content matching heights
HTMLHeightMatch = function () {
	var content = $('#contentbg');
	var sideBar = $('.sideNav');

	var contentHeight = content.height() - 145;
	var sideBarHeight = sideBar.height() + 145;

	(content.height() > sideBar.height()) ? sideBar.css({ height: contentHeight + 'px' }) : content.css({ height: sideBarHeight + 'px' });
}


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) {
    $('.bagWrap').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,
		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); }
	});
}
