var Agent = jQuery.browser;


var ProductImagePreview = {};
ProductImagePreview.Version = 1;

ProductImagePreview.Init = function () {
  var li = $('#LargeImage');
  var fp = $('#FullPreview');
  var ip = $('#imagePopup');
	var pi = $('#productImage');
	var ht = $('#ProductInformation');
	var th = $('.Thumbnails');
	var ci = $('.Container.ItemDetail');
	var h  = ci.height()+20;
	fp.attr('src',ip.attr('href'));
  jQuery.each($('.thumbImage'), function () {
    $(this).click(function (event) {
      pi.attr('src', '/_AppData/Images/Detail/'	+ $(this).attr('id') + '.jpg');
			ip.attr('href','/_AppData/Images/Full/' 	+ $(this).attr('id') + '.jpg');
			fp.attr('src', ip.attr('href'));
    });
  });
  li.click(function (event) {
    li.hide();
		ci.height(h);
		th.show();
		ht.show();
  });
  ip.click(function (event) {
		ci.height(500);
		th.hide();
		ht.hide();
    li.show();
    if (Agent.msie) {
      event.cancelBubble = true;
    } else {
      event.stopPropagation();
    }
    return false;
  });
}


var MailingList = {};
MailingList.Version = 1;

MailingList.Init = function () {
  var ML = $('#MailingListEmail');
  ML.focus(function () {
    ML.val('');
    ML.css('color', '#000');
  });
}


var MenuRenderer = function(mData){
	$("#TopMenu").append(mData.menu);
	
	$("#TopMenu li.Depth0 > a").click(function(e) {
		if($(this).closest("li").hasClass("Active")) {
			// Activate link
		} else {
			e.preventDefault();
			$("#TopMenu li.Depth0, #TopMenu ul.Depth1").removeClass("Active");
			$(this).closest("li").addClass("Active");
			$(this).closest("ul").addClass("Active");
		}
	});
	$("#TopMenu li.Depth1 > a").click(function(e) {
		if($(this).closest("li").hasClass("Active")) {
			// Activate link
		} else {
			e.preventDefault();
			$("#TopMenu li.Depth1").removeClass("Active");
			$(this).closest("li").addClass("Active");
			$(this).closest("ul").addClass("Active");
		}
	});
	$("#TopMenu li.Depth1").append('<div class="Close">&times;</div>');
	$("#TopMenu li.Depth1 > div.Close").click(function(e) {
		$("#TopMenu li.Depth1, #TopMenu ul.Depth1").removeClass("Active");
	});
}


function InitHoverController() {
	// General hover replacement - For menus and other interface elements
	$('.HoverElement').hoverIntent( {
					sensitivity: 	10 		// number = sensitivity threshold (must be 1 or higher)
				,	interval: 		50 		// number = milliseconds for onMouseOver polling interval
				, timeout: 			250 	// number = milliseconds delay before onMouseOut
				, over: 				function() {
													$(this).addClass('Hover');
													$('.HoverContent')
															.stop(true, true)
															.hide();
													$(this).find('.HoverContent').fadeIn(100);
												} // function = onMouseOver callback (REQUIRED)
				, out: 					function() {
													$(this).removeClass('Hover');
													$(this).find('.HoverContent').fadeOut(100);
												} // function = onMouseOut callback (REQUIRED)
				
				}
		);


	// Multi element hover replacement - For menus and other interface elements
	$('.MultiHoverContainer').hoverIntent( {
					sensitivity: 	10 		// number = sensitivity threshold (must be 1 or higher)
				,	interval: 		50 		// number = milliseconds for onMouseOver polling interval
				, timeout: 			250 	// number = milliseconds delay before onMouseOut
				, over: 				function() {
													$(this).addClass('Hover');
													$('.HoverContent')
															.stop(true, true)
															.hide();
													$(this).find('.MultiHoverElement').addClass('Active');
													$(this).find('.MultiHoverElement.Active:hover').addClass('Hover');
													$(this).find('.MultiHoverElement.Active:hover .HoverContent')
															.stop(true, true)
															.addClass('Active')
															.fadeIn(100);
												} // function = onMouseOver callback (REQUIRED)
				, out: 					function() {
													$(this).removeClass('Hover');
													$(this).find('.MultiHoverElement').removeClass('Active Hover');
													$(this).find('.HoverContent')
															.stop(true, true)
															.removeClass('Active')
															.fadeOut(100);
												} // function = onMouseOut callback (REQUIRED)
				
				}
		);
	$('.MultiHoverContainer .MultiHoverElement').hover(
					function() {
						if($(this).hasClass('Active')) {
							$('.MultiHoverContainer .MultiHoverElement').removeClass('Hover');
							$(this).addClass('Hover');
							$('.MultiHoverContainer .MultiHoverElement .HoverContent').hide();
							$(this).find('.HoverContent').show();
						}
					}
				, function() {
						
					}
		);

}



$(document).ready(function () {
	var MenuScript = document.createElement('script');
	MenuScript.charset = "utf-8";
	MenuScript.src = MenuSettings.loggedin ? "/_Lib/Js/Generated/Menu/"+MenuSettings.shop+"-"+MenuSettings.language+"_LoggedIn_Menu.js?ts="+MenuSettings.timestamp : "/_Lib/Js/Generated/Menu/"+MenuSettings.shop+"-"+MenuSettings.language+"_Menu.js?ts="+MenuSettings.timestamp;
	document.body.appendChild(MenuScript);
  ProductImagePreview.Init();
  MailingList.Init();
	InitHoverController();
});


$.fn.animateHighlight = function(highlightColor, duration) {
	var highlightBg = highlightColor || "#FFFF9C";
	var animateMs = duration || 1500;
	var originalBg = this.css("backgroundColor");
	this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
