﻿var promotions = function($){
    var priv = {
    
        promoBlocks			: "#promotion-blocks",
		promoBlockAnchor   : ".promo-acco-name a",

showPromoBlocks : function(listPosition){
		    //first make sure we have the promotion blocks available
			if($(priv.promoBlocks).length == 0){
				return false;
			}
			logic.writeDebug("doing logic for promo blocks");
			
			//get the PromoItems
			$promoItems = $(priv.promoBlocks + ">li");
			
			//create the new list to insert halfway the page
			$newList = $("<ul class='promotion-blocks'>");
			
			//make sure we have at least one itemm otherwise exit the method
			if($promoItems.length == 0){
			    return false;
			}
			
			var position = 1;
			var maxBlockItems = 3;
			var infloop = 0;
			
			//show maximum of 2 other random items for curent block
			while($promoItems.length > 0 && $newList.find("li").length < maxBlockItems && infloop < 25){
                infloop++;
                //pool from which to grab depends on position (and style)
                $currPool = $promoItems.parent().find(".promo-type-" + position);
                position = (position < maxBlockItems ? parseInt(position + 1) : 1);
                
                if($currPool.length == 0){
                    //if we do not have any items left in the current pool, go to next
                    continue;
                }
                
                //get the new random index
			    var rnd = Math.round(Math.random() * ($currPool.length - 1));
			    logic.writeDebug(rnd);
    			$newList.append($currPool[rnd]);
    			
    			//rebind list to keep the counters in order
			    $promoItems = $(priv.promoBlocks + ">li");
			}
			
			//remove right-margin of the most right visible element
			$newList.children("li:nth-child(3)").css("margin-right", "0px");
			
			//now append the $newList to the list page
			//write the block at the 3rd position if not empty
			if($newList.length > 0){
			    $("#random-promos").append($newList);
			}
            
            //bind events
            $newList.children("li").hover(
                function(){$(this).addClass("hover");},
		        function(){$(this).removeClass("hover");}
            ).bind("click",
                function(){
                    //redirect to the correct page
                    var url = $(this).find(priv.promoBlockAnchor).attr("href");
                    
                    document.location.href = url;
                    //prevent event bubbling
                    return false;
                }
            );
            
            //show the $newList
            $newList.show();
            
            //finnaly make sure all blocks have the same height
			//images stretch the height and additional correction is needed after the body has loaded
			$(window).bind("load",
				function() {
					priv.setPromoBlockHeights();
				}
			);
			
		},
				
		/**
		 * (re)sets the heights of the promotion blocks, so they have teh same height.
		 */
		setPromoBlockHeights	: function(){		
			logic.writeDebug("(re)setting the height of the promotion blocks");
			var maxHeight = 0;			
			$("ul.promotion-blocks li:visible").each(
				function(i){
					//wobble the height a bit... so the browser recalculates its actual height...
					$(this).height($(this).height() + 1);
					$(this).height($(this).height() - 1);
					
					maxHeight = Math.max(maxHeight, $(this).height());
				}
			);
			$("ul.promotion-blocks li:visible").height(maxHeight);
		}
		
		
		};
    
    return {
    
        OnReady	: function(){
            priv.showPromoBlocks(3);
        }
    
     };
}(jQuery);