// Toggle fade categories
dsg.fn.initFadeCategories = function(){
	var $allContainers = $j("div.box-categories div.box-content > div").each(function(){
		
		var $container = $j(this);
		var $toggleControler = $container.find("h2").not(".no-sub-level");
		var $toggleContent = $container.find("ul").hide();
		var categoryTimer;
		
		if ( $container.find("ul").length > 0 ) {
			
			$container.addClass("enabled")
			.hover(
				function () {
					window.clearTimeout(categoryTimer);
				},
				function () {
					categoryTimer = window.setTimeout(function(){
						$toggleContent.fadeOut(300,function(){
							$toggleControler.removeClass("expanded");
						});
					},3000);
				});
			
			$toggleControler
			.hover(
				function () {
					$toggleControler.addClass("hover");
				},
				function () {
					$toggleControler.removeClass("hover");
				}
			)
			.click( function () {
				
				// Hide others
				$allContainers.not($container).find("ul:visible").hide();
				
				if($toggleContent.is(":visible") && $toggleContent.not(":animated")){
					$toggleContent.fadeOut(300);
					$toggleControler.removeClass("expanded");
				}
				else {
					$toggleContent.fadeIn(300);
					$toggleControler.addClass("expanded");
				}
			});
			
		}
		
	});
};

// Must have
dsg.fn.initMustHave = function(ajaxUrl){
	
	var $mustHave = $j("#col-main div.must-have");
	var $mustHaveContent = $mustHave.find("div.box-content");
	
	var $loader = $j('<div class="loader"></div>').css({
		width: $mustHave.outerWidth(),
		height: $mustHave.outerHeight(),
		opacity: 0
	}).hide().appendTo($mustHave);
	
	initEffects();
	
	// Bind the "change" event to the select box
	$mustHave.find("div.box-header select").change(function(e){
		
		// Get the ajax url for the option selected
		var completeAjaxUrl = ajaxUrl + '&' + $j(this).serialize();
		// Load the new block
		loadBlock(completeAjaxUrl);
	});
	
	// Load the block from Ajax response
	function loadBlock(ajaxUrl){
		
		// Show the loader
		$loader.show().fadeTo(300, 0.6);
		
		// Send the Ajax request
		$j.get(ajaxUrl, function(data){
			
			// Change the block display with the new content
			$mustHaveContent.html(data);
			
			// Re-init Must-have effects
			initEffects();
			
			// Loader fade
			$loader.fadeTo(300, 0, function(){
				$loader.hide();
			});
		});
	};
	
	// Must-have effects
	function initEffects(){
		
		// Slider
		$mustHave.find("div.must-have-minis-slide").carousel({
			direction: "vertical",
			dispItems: 3,
			combinedClasses: true
		});
		
		// Pack available
		var $packAvailable = $j("#pack-available").hide();
		
		$mustHave.find("p.pack-avail a").click(function(e){
			e.preventDefault();
			$packAvailable.fadeIn(300)
			.find("a.close-window").click(function(e){
				e.preventDefault();
				$packAvailable.fadeOut(300);
			});
		});
	};
};

// Init top seller link on Star UCMS box
dsg.fn.loadTopSellersLink = function(){
	$j(function() {
		$oLink = $j("div.box-categories p.see-top-sellers");
		if ($oLink.length > 0 && $j("a[@name=top-top-sellers]").length === 0){
			$oLink.hide();
		}
	});
};

