jQuery.preloadImages("/images/newsite1024/images/tour-control/tb-ln.png",
                "/images/newsite1024/images/tour-control/tb-la.png",
                "/images/newsite1024/images/tour-control/tb-mn.png",
                "/images/newsite1024/images/tour-control/tb-ma.png",
                "/images/newsite1024/images/tour-control/tb-rn.png",
                "/images/newsite1024/images/tour-control/tb-ra.png");
                
var tourDoc = null;
var tourSections = null;
var isSliding = false;
var tourControlWidth = 0;
var tourControlHeight = 0;
var tourAutoRotate = 0;
var tourRotateHandler = null;
var tourCurSlide = 0;
var tourTotalSlides = 0;


function tourAutoSlide() 
{   
   gotoTourTab(tourCurSlide < (tourTotalSlides-1) ? ++tourCurSlide : 0);   
   tourRotateHandler = setTimeout(tourAutoSlide,tourAutoRotate*1000);   
}

function gotoTourTab(slidenum)
{
   if(!isSliding && jQuery("#tour #sections-container").css("left") != ((slidenum*-tourControlWidth) + "px"))
   {
      isSliding = true;
      
      if(tourSections[slidenum])
      {      
         jQuery("#tour .legend").removeClass("active");
         jQuery("#tour .legend").css("color","#4a8ab9");
         jQuery("#tour #button" + tourSections[slidenum].getAttribute("id") + " .legend").addClass("active");
         jQuery("#tour #button" + tourSections[slidenum].getAttribute("id") + " .legend").css("color","#ffffff");
      }
   
      var amt = (slidenum*-tourControlWidth);
   
      jQuery("#tour #sections-container").animate(
   		{ left: amt },
   		1000,
   		"easeOutExpo",
   		function() { isSliding = false; }
   	);
   	
   	tourCurSlide = slidenum;
   }
}

function TourControl(width,height) {
   
   this.width = width; //952
   this.height = height; //297
   
   tourControlWidth = this.width;
   tourControlHeight = this.height;
   
   //Attach main container
   jQuery("#tour").append('<div id="sections-screen"><div id="sections-container"></div></div><div id="buttons-container"></div>');
   
   //Setup our environment....
   jQuery("#tour, #tour #sections-screen").width(this.width + "px");
   jQuery("#tour, #tour #sections-screen, #tour #sections-container").height(this.height + "px");
   
   jQuery("#tour #buttons-container").width((this.width - 10) + "px");
   jQuery("#tour #buttons-container").css("left","5px");
   jQuery("#tour #buttons-container").css("top",(this.height - 44) + "px");
      
   this.processResponse = function(data) {
            		
		if (window.ActiveXObject)
		{
			tourDoc = new ActiveXObject("Microsoft.XMLDOM");
			tourDoc.async="false";
			tourDoc.loadXML(data);
		}
		else
		{
			var parser = new DOMParser();
			tourDoc = parser.parseFromString(data,"text/xml");
		}

		if(tourDoc != null)
		{
		   tourControl = tourDoc.getElementsByTagName("tourcontrol")[0];
		   tourSections = tourDoc.getElementsByTagName("section");		   		   
		   
		   //Attach our table with buttons....
		   var btnsHTML = "";
		   var sectionsHTML = "";
		   var screensWidth = 0;
		   
		   for(x=0;x<tourSections.length;x++)
		   {		      
		      jQuery.preloadImages(tourSections[x].getAttribute("background"));
		      
		      var htmlContent = tourSections[x].getElementsByTagName("content")[0]
		      
		      sectionsHTML += '<td class="section-spot">' + 
		         '<div id="tab' + tourSections[x].getAttribute("id") + '" class="sectiondiv" style="background-image: url(\'' + tourSections[x].getAttribute("background") + '\')">' + 
		            '<div>' + (htmlContent.textContent ? htmlContent.textContent : htmlContent.text) + '</div>' + 
		         '</div>' +
		      '</td>';
		      
		      btnsHTML += '<td class="button-spot">' +
               '<table id="button' + tourSections[x].getAttribute("id") + '" class="tour-button" onclick="gotoTourTab(\'' + x + '\')">' +
                  '<tr>' +
                     '<td class="l"><div class="lhoverer">&nbsp;</div></td>' +
                     '<td><div class="hoverer"><div class="legend' + (x==0?' active':'') + '">' + tourSections[x].getAttribute("label") + '</div></div></td>' +
                     '<td class="r"><div class="rhoverer">&nbsp;</div></td>' +
                  '</tr>' +
               '</table>' +
            '</td>';
            screensWidth += tourControlWidth;
		   }
		   		   
		   jQuery("#tour #buttons-container").append('<table class="buttons-layout"><tr>' + btnsHTML + '</tr></table>');		   
		   jQuery("#tour #sections-container").width(screensWidth + "px");
		   jQuery("#tour #sections-container").append('<table class="sections-layout"><tr>' + sectionsHTML + '</tr></table>');		   
		   
		   jQuery("#tour .sectiondiv").width(tourControlWidth + "px");
		   jQuery("#tour .sectiondiv").height(tourControlHeight + "px");
		   
         //Render Buttons ...
         var totalButtons = jQuery("#tour #buttons-container table.tour-button").length;
         tourTotalSlides = totalButtons;

         jQuery("#tour #buttons-container table.buttons-layout td.button-spot").css("width",totalButtons > 0 ? ((100/totalButtons)+"%") : "auto");
         jQuery("#tour #buttons-container table.buttons-layout td.button-spot").css("padding-right","3px");
         jQuery("#tour #buttons-container table.buttons-layout td.button-spot:last").css("padding-right","0px");

         jQuery("#tour #buttons-container table.tour-button").hover(function () {

            if(!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version >= 7))
				jQuery("td .hoverer, td .lhoverer, td .rhoverer",this).addClass("hovered");
            jQuery(".legend",this).css("color","#4a8ab9");
            jQuery(".legend",this).css("color","#ffffff");

         },
         function () {

            if(!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version >= 7))
				jQuery("td .hoverer, td .lhoverer, td .rhoverer",this).removeClass("hovered");
            jQuery(".legend",this).css("color","#4a8ab9");
            jQuery(".active",this).css("color","#ffffff");

         });
         
         //Attach timer, if any...
         if(tourControl && tourControl.getAttribute("autorotate") > 0)
         {
            tourAutoRotate = tourControl.getAttribute("autorotate");
            
            tourRotateHandler = setTimeout(tourAutoSlide,tourAutoRotate*1000);
            
            jQuery("#tour").hover(function (){
               clearTimeout(tourRotateHandler);
            },function (){
               tourRotateHandler = setTimeout(tourAutoSlide,tourAutoRotate*1000);
            });
         }
      }
      
   };
      
   this.render = function(xmlfilepath) {
     
      //Pull our XML
      jQuery.ajax({
         type: "GET",
         url: xmlfilepath,
         dataType: "text",
         success: this.processResponse
      });	
      
   };
   
}


