if(typeof sevenSummits == "undefined" || !sevenSummits){
	sevenSummits = {};
}

/**
 * @namespace sevenSummits.sliderBar
 */
sevenSummits.sliderBar = (function(){	
		
	var SLIDER_BAR_CONTAINER_HANDLE = "#sliderContent";
	var SLIDER_BAR_HANDLE  = "#slider";
	var SLIDER_CONVEYOR_HANDLE = ".content-conveyor";
	var SLIDER_ITEM_HANDLE = ".item";    
	var SLIDER_ITEM_TEXT_HANDLE = ".item-text";
	var SLIDER_ITEM_ACTIVE_CLASS = "item-active";
	var SLIDER_ITEM_ID_PRE = "item-";
	var SLIDER_OBJECT;
	var SLIDER_ITEMS;
	var CONVEYOR;
	var CONVEYOR_VISIBLE_WIDTH;
	
	var HEIGHT_OFFSET = 16; // height
	var ITEM_START_HEIGHT = 113;
	var mX, mY;
	
	/**
	 * @function initSlider
	 * @description initialize and setup default parameters
	 * of the slider.
	 */
  function initSlider() {	
    
    // get the conveyor, width of the convyer and the number of items in the conveyor
    CONVEYOR = $((SLIDER_BAR_CONTAINER_HANDLE + " " + SLIDER_CONVEYOR_HANDLE));  
    CONVEYOR_VISIBLE_WIDTH = parseInt($(SLIDER_BAR_CONTAINER_HANDLE).css("width"));
    SLIDER_ITEMS = $((SLIDER_BAR_CONTAINER_HANDLE + " " + SLIDER_ITEM_HANDLE));	
	  
    // loop through all the items in the conveyor and set id's
	  for(var x = 0; x < SLIDER_ITEMS.length; x++){
	    $(SLIDER_ITEMS[x]).attr("id", (SLIDER_ITEM_ID_PRE + (x + 1)));  	    
	  }
	  
	  // get conveyor parameters
	  var conveyorWidth = parseInt(SLIDER_ITEMS.css("width"));
	  var itemMarginRight = parseInt(SLIDER_ITEMS.css("marginRight"));
	  var conveyorMarginRight = parseInt(CONVEYOR.css("marginRight"));
	  var conveyorMarginLeft = parseInt(CONVEYOR.css("marginLeft"));	  
	  
	  // get the maxValue of the conveyor, which will be passed into the slider object
	  var maxValue = SLIDER_ITEMS.length * (conveyorWidth + itemMarginRight) + conveyorMarginLeft + conveyorMarginRight 
	    - parseInt($((SLIDER_BAR_CONTAINER_HANDLE + " .viewer")).css("width")) - itemMarginRight; 
	 
	  //set length of conveyor	 
	  CONVEYOR.css("width", (SLIDER_ITEMS.length + 1) * (conveyorWidth + itemMarginRight + conveyorMarginLeft + conveyorMarginRight));	 
	  	  
	  //config the slider	
	  var sliderOpts = {
		  animate: true,
	    max: maxValue,
		  slide: function(e, ui) {	
		    CONVEYOR.css("left", "-" + ui.value + "px");
		  }
    };
		
	  //create slider
	  SLIDER_OBJECT = $(SLIDER_BAR_HANDLE).slider(sliderOpts);  
	} // end initSlider
	
  /**
   * @function initItemActions
   * @description attach the mouseover/mouseout events
   */
	initItemActions = function(){		
		
	  //set the current mouse positions
	  $(SLIDER_BAR_CONTAINER_HANDLE).mousemove(function(e){
	    mX = e.pageX; // e.pageX - gives you X position
		  mY = e.pageY; // e.pageY - gives you Y position
	  });
		
	  // attach the mouse actions
	  $((SLIDER_BAR_CONTAINER_HANDLE + " " + SLIDER_ITEM_HANDLE)).each(function(){			  
		  $(this).mouseenter(function(){	 
		    displayHover(this);
		  });
	    $(this).mouseleave(function(){
	        hideHover(this);
		  });	    
	  });	  
	 		  
	} // end initItemActions
	
	/**
	 * @function displayHover
	 * @description mouse over events action
	 */
	function displayHover(el){ 
		
	  // make sure all elements are closed
    closeAllElements();
		
	  // get the dimensions of the slider container	
	  var leftSideContainer = $(SLIDER_BAR_CONTAINER_HANDLE).offset().left;
    var rightSideContainer = $(SLIDER_BAR_CONTAINER_HANDLE).offset().left + parseInt($(SLIDER_BAR_CONTAINER_HANDLE).width());
      
    // get the dimensions of the element that was hovered over
    var leftSideEl = $(el).offset().left;
    var rightSideEl = $(el).offset().left + parseInt($(el).width());          
      
    // is the element partially outside the left edge of the container
    // if so move it
    if(leftSideContainer > leftSideEl){
      $(CONVEYOR).stop(); // stop the conveyor    
      //get the position of the element
      var idString = $(el).attr("id")
      var pos = idString.substring(SLIDER_ITEM_ID_PRE.length, idString.length) - 1;         
      var valPerElement = (CONVEYOR_VISIBLE_WIDTH + SLIDER_OBJECT.slider("option" , "max")) / (SLIDER_ITEMS.length);
      var sliderOffset = (CONVEYOR_VISIBLE_WIDTH + SLIDER_OBJECT.slider("option" , "max")) - (valPerElement * pos);
      var sliderValue = (CONVEYOR_VISIBLE_WIDTH + SLIDER_OBJECT.slider("option" , "max")) -  sliderOffset;      
           
      // move the conveyor to bring the element fully into the view
      SLIDER_OBJECT.slider("value" , [sliderValue]);       
      
      var leftOffset = leftSideContainer - leftSideEl; // find out how far left we are     
      var currentLeft = parseInt(CONVEYOR.css("left")) || 0; // find out how far the conveyor is to the left  
      var conveyorMarginLeft = parseInt(CONVEYOR.css("marginLeft"));    	
	   
    	$(CONVEYOR).animate({
 	        left: (currentLeft + leftOffset + conveyorMarginLeft)	
 	    }, 300, function(){ 	      
 	      
 	      // once the element is in the view we
        // will now display the hover state.
 	    	displayHoverHelper(el);  	    	
 	    }); 	 	   
    	
    }
    // is the element partially outside the right edge of the container
    // if so move it into view
    else if(rightSideContainer < rightSideEl) {           
      $(CONVEYOR).stop(); // stop the conveyor    
      //get the position of the element
      var idString = $(el).attr("id")
      var pos = idString.substring(SLIDER_ITEM_ID_PRE.length, idString.length);        
      var valPerElement = (CONVEYOR_VISIBLE_WIDTH + SLIDER_OBJECT.slider("option" , "max")) / (SLIDER_ITEMS.length);
      var sliderOffset = (CONVEYOR_VISIBLE_WIDTH + SLIDER_OBJECT.slider("option" , "max")) - (valPerElement * pos);
      var sliderValue = SLIDER_OBJECT.slider("option" , "max") - sliderOffset;
      
      // move the conveyor to bring the element fully into the view
      SLIDER_OBJECT.slider( "value" , [sliderValue] );      
      
    	var rightOffset = rightSideEl - rightSideContainer; // find out how far right we are 	    	
    	var currentRight = parseInt(CONVEYOR.css("left")) || 0; // find out how far the conveyor is to the left     	
    	var conveyorMarginRight = parseInt(CONVEYOR.css("marginRight"));    	
    
    	$(CONVEYOR).animate({
  	      left: (currentRight - rightOffset - conveyorMarginRight) + "px"	
  	    }, 300, function(){
  	    
  	    // once the element is in the view we
 	    	// will now display the hover state.  	    	
	      displayHoverHelper(el);	        
  	  });     
    	  
    }
    // the element is in the normal view so just display the
    // hover state
    else {
      displayHoverHelper(el);    	  
    }     	  	
	  
	} // displayHover	
	
	/**
	 * @function displayHoverHelper
	 * @description animate the item's hidden content into view
	 * @param {object} el - element that has been hovered over
	 */
	function displayHoverHelper(el) {	
	  // get the hidden child element
		$(el).children(SLIDER_ITEM_TEXT_HANDLE).each(function(){
			
		  // stop the element if its currently animating,
		  // as well as set the active class
			var child = this;	   		  
			$(el).stop(); 				  
			$(el).addClass(SLIDER_ITEM_ACTIVE_CLASS); //
					
			// we need to set the display to block in order to get the height of the
			// hidden element. We will use the height to determine the top position of
			// the elements
			$(child).css({"display":"block", "visibility":"hidden"});	
			var childItemTextHeight = parseInt($(child).css("height")) + HEIGHT_OFFSET;		
			$(child).css({"display":"none", "visibility":"visible"});
			var elTopValue = "-" + childItemTextHeight;
			
			// turn on the hidden child
			$(child).show();
			
			// animate the height of the child and the top of the el,
			// in order to bring it into view
			$(el).animate({
		        top: ("-" + childItemTextHeight),	
		        height: (ITEM_START_HEIGHT + childItemTextHeight)
		    }, 300, function(){	   
		    	
		      // after we have animated we need to make sure the mouse is still within the
		      // elements dimensions if not hide it.
		    	var leftSideEl = $(el).offset().left;
		      var rightSideEl = $(el).offset().left + parseInt($(el).width());
		      var topSideEl = $(el).offset().top;
		      var bottomSideEl = $(el).offset().top + parseInt($(el).height());		  
		    	
		    	if((mX < leftSideEl || mX > rightSideEl) | (mY < topSideEl || mY > bottomSideEl)) {
		    		hideHover(el, true);		    		
		    	}		    	
		    	
		    });		 
			
		  });	
		
	} // end displayHoverHelper
	
	/**
	 * @function closeAllElements
	 * @description close all the elements in the conveyor
	 */
	function closeAllElements(){
	  //make sure all other elements are hidden	
	  $(SLIDER_ITEMS).each(function(){
	    if(0 != parseInt($(this).css("top"))) {
	      hideHover(this);				  
	    }		 
      });		
	} // end close all elements
	
	/**
	 * @function hideHover
	 * @description mouse out events action
	 * @param {object} el - element hovered over
	 * @param {boolean} manCalled - true if the function was called
	 * via javascript as opposed to an actual action.
	 */
	function hideHover(el, manCalled){	   	
		
	  // get the child to hide
	  $(el).children(SLIDER_ITEM_TEXT_HANDLE).each(function(){
		  var child = this;  		 
		  
		  // was this called manually inside the script if so
		  // don't stop the animation
		  if(typeof manCalled  == "undefined" || !manCalled){
		    $(el).stop();	
	    }
		  
		  // remove the active class
		  $(el).removeClass(SLIDER_ITEM_ACTIVE_CLASS);
		  
		  // animate the height of the child and the top of the el,
      // in order to take the item out of view
		  $(el).animate({
		    top: "0px",
		    height: ITEM_START_HEIGHT
		  }, 300, function(){
			  $(child).hide();
		  });		  
	  });	
	  
	} // end hideHover
	
	return {
		
		init : function(){			
		  initSlider(); // create the jquery slider
		  initItemActions(); // init the mouseover and mouseout states		 
		}	
		
	}; // end return
	
})(); // end sevenSummits.sliderBar	
