/* RR 20090202 Added the code to random sort the items in the image slider. */
var IMAGESLIDER = {};
 
IMAGESLIDER.Options = {
   objToSlide: 'imagesToSlide',
   sliderElements: 'a', /* change this to 'div' if you decide to use divs instead of links */
   numOfElementsToSlide: 2, /* this is the number of elements that slide when the button is hit */
   numOfElementsShown: 4, /* this is the total number of elements shown at any give time */
   xmlToLoad: '/slider/imageslider.xml',
   leftBtn: 'isLeftBtn',
   rightBtn: 'isRightBtn',
   easing: 'easeInOutExpo' /* uses any easing function from http://gsgd.co.uk/sandbox/$uery/easing/ */
};

IMAGESLIDER.Setup = {
	
	/* Create a number to sort the array */
	randOrd: function(){
		return (Math.round(Math.random())-0.5);
	},
	
	/* Just a creator to build an item object */
   	item: function(img, href,title, width, height){
		this.img = img;
        this.href = href;
        this.title = title;
        this.width = width;
        this.height = height;
	},
	
	loadXML: function(){
      var o = IMAGESLIDER.Options;
      if($("#"+o.objToSlide).length == 0){return false;};
      $.ajax({ 
         type: "GET", 
         url: o.xmlToLoad, 
         dataType: "xml", 
         success: function(xml){
            var _img = "";
            var _href = "";
            var _title = "";
            var _width = "";
            var _height = "";
			var list = new Array();
			
            //This function will loop for each match on link and add it to an array
            $("link", xml).each(function(){
               _img = $("img", this).text();
               _href = $("href", this).text();
               _title = $("title", this).text();
               _width = $("width", this).text();
               _height = $("height", this).text();
				
               list.push(new IMAGESLIDER.Setup.item(_img,_href,_title,_width,_height));
			 });
			
			//Random sort the array 
			list.sort(IMAGESLIDER.Setup.randOrd);
			 for(i in list)
			 {
               //build and append the dom element
               $('#'+o.objToSlide).appendDom([{
                  tagName : 'a',
                  href : list[i].href,
                  title : list[i].title,
                  childNodes : [{
                     tagName : 'img',
                     src : list[i].img,
                     width : list[i].width,
                     height : list[i].height,
                     alt : ''
                  }]
               }]);
			 }
         IMAGESLIDER.Setup.config();
         }
      }); 
   },
   
   config: function(){
      var o = IMAGESLIDER.Options;
      o.objToSlide = $("#"+o.objToSlide);
      if(o.objToSlide.length > 0){
         o.sliderElements = (o.objToSlide.find('a'));
         o.leftBtn = $("#"+o.leftBtn);
         o.rightBtn = $("#"+o.rightBtn);
         o.objToSlide.css('position', 'relative');
         o.leftBtn.bind("click", IMAGESLIDER.Events.leftClick);
         o.rightBtn.bind("click", IMAGESLIDER.Events.rightClick);
         var element = $(o.sliderElements[0]);
         var margins = parseFloat(element.css('margin-left')) + parseFloat(element.css('margin-right'));
         o.elementWidth = element.width() + margins;
         o.index = 0;
         o.total = o.sliderElements.length - o.numOfElementsShown;
		 o.duration = 1500;
      }
   }
};

IMAGESLIDER.Events = {
   leftClick: function(){
      var o = IMAGESLIDER.Options;
      var distance;
      if(o.index - o.numOfElementsToSlide >=0){
         distance = o.elementWidth * o.numOfElementsToSlide;
         o.index -= o.numOfElementsToSlide;
      }else{
         distance = o.elementWidth * o.index;
         o.index = 0;
      }
      if(distance == 0){return false;}
      o.objToSlide.animate({left: '+='+distance}, o.duration , o.easing);
      return false;
   },
   
   rightClick: function(){
      var o = IMAGESLIDER.Options;
      var distance;
      if(o.index + o.numOfElementsToSlide <= o.total){
         distance = o.elementWidth * o.numOfElementsToSlide;
         o.index += o.numOfElementsToSlide;
      }else{
         distance = o.elementWidth * (o.total - o.index);
         o.index = o.total;
      }
      if(distance == 0){return false;}
      o.objToSlide.animate({left: '-='+distance}, o.duration , o.easing);
      return false;
   } 
};


$(document).ready(function(){
   IMAGESLIDER.Setup.loadXML();
});