function showMiniCart(objLink) {
    
    if ($(objLink).html() == "View")
    {
    	refreshMiniCart();
    	//$("#minicartiframe").height(120);
    }
    	
    /*$(".mini-cart").css("left", $("#shopping-cart-area").offset().left);
    $(".mini-cart").css("top", $("#shopping-cart-area").offset().top + $("#shopping-cart-area").innerHeight());*/
    $(".mini-cart").slideToggle("slow");

    $(objLink).html($(objLink).html() == "View" ? "Hide" : "View");
}

function resizeMiniCart(resizeHeight) {
    $("#minicartiframe").animate({
    	height: resizeHeight + 40
  	}, 500);
}

function refreshMiniCart()
{
  populateMiniCart();
	//$("#minicartiframe").attr("src",$("#minicartiframe").attr("src")); 
}

function resizeBox(resizeHeight) {
    $('#fancybox-content').height(resizeHeight);
    $('#fancybox-wrap').height(resizeHeight + 20);
}

function slideSwitch() {
    var active = $('#QuoteSlider li.active');

    if (active.length == 0 )
    	active = $('#QuoteSlider li:last');

    var next = active.next().length ? active.next() : $('#QuoteSlider li:first');

    active.addClass('last-active');

    next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            active.removeClass('active last-active');
        });
}

hasItemsInCart = false;

function populateMiniCart(){
  var cartTotal = 0;

  $.ajax({
    url : '/ajax/?query=cartitems',
    dataType : 'json',
    success : function(data){
      $('.mini-cart #MiniCartContents').html('');
      
      $.each(data, function(i, v){
        $('.mini-cart #MiniCartContents').append('<li>'+ v[0] +' x <a href="'+ v[3] +'">'+ v[1]+'</a></li>');
        cartTotal += parseFloat(v[0] * v[2]);
      });
      
      $('#CartTotalLabel').html(cartTotal.toFixed(2));
      hasItemsInCart = (cartTotal > 0) ? true : false;
      
      if(hasItemsInCart)
        $('#ctl00_ShoppingCartArea').show();
    }
  });
}

function adjustStyle(width) {
    width = parseInt(width);
    
    if ((width <= 1024))    
    {
        $("body").addClass("medium");
    }
    else
    {
        $("body").removeClass("medium");
    }
}

$(document).ready(function () {
  	
    populateMiniCart();
    
    resizeMiniCart(100);

    $('#upper-content-area').corner("top");
    $('#category-meta').corner();
    $('#product-meta-details').corner();
    
    if ($.browser.msie) {
    	$('.rounded').not("img,input").corner();
    	$('.smallrounded').not("img,input").corner("3px");
    }
    else
    {
    	$('.rounded').corner();
    	$('.smallrounded').corner("3px");
    }
    
    $('#coming-soon-text').corner("right");

    $(".defaultText").focus(function (srcc) {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaultText").blur(function () {
        if ($(this).val() == "") {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });

    $(".defaultText").blur();
    
    $(".search-box").autocomplete({
		source: "/search/autocomplete.aspx",
		minLength: 2,
		select: function( event, ui ) {
			location.href = ui.item.value; 
		}
	});
  

    $(".addToCart").fancybox({
        'padding': 20,
        'width': 500,
        'height': 300,
        'overlayOpacity': 0.4,
        'scrolling': 'no',
        'titleShow': false,
        'type': 'iframe',
        onClosed: function () {
            refreshMiniCart();

            $.ajax({
                url: "/ajax/",
                data: "query=cartcount",
                context: document.body,
                success: function (msg) {
                    $('#ctl00_ShoppingCartArea').show();

                    var resultDiv = $("#result", msg);
                    $('#ctl00_CartItemCount').html(resultDiv.html());
                }
            });
        }
    });

    $(".modal").fancybox({
        'padding': 20,
        'autoDimensions': false,
        'width': 500,
        'height': 'auto'
    });

	function megaHoverOver(){
		// Don't show for IE7 due to z-index issues
		if (parseInt($.browser.version, 10) == 7)
			return;
		
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul.top-nav li .sub").css({'opacity':'0'});
	$("ul.top-nav li").hoverIntent(config);
	
	adjustStyle($(window).width());
	
    $(window).resize(function() {
        adjustStyle($(this).width());
    });
});

