/* Homepage Panel
####################################################################################### */

var panel = {
   
   timer: 10, // seconds
   panelInterval: 0,
   panel_no: 0,
   selected: 0,
   selector: -1,

   init: function() {
      panel.panel_no = $('#north_america_panel div.panel').size();
      panel.priority();
      panel.navigation();
      panel.first();
      //panel.initInterval();
   },
   
   /*initInterval: function() {
      clearInterval(panel.panelInterval);
      panel.panelInterval = setInterval("panel.switcher()",(panel.timer*1000));
   },*/
   
   first: function() {
      $('#north_america_panel ul li.panel:eq('+panel.selector+') a').css('color','#ed087f');
      $('#north_america_panel div.panel:eq('+panel.selector+')').fadeIn('slow');
      panel.selected = panel.next();
      panel.selector = panel.next();
   },
    
   change: function() {
      $('#north_america_panel div.panel:eq('+(panel.selected-1)+')').fadeOut('slow', function() {
         $('#north_america_panel div.panel:eq('+(panel.selector-1)+')').fadeIn('slow');
         $('#north_america_panel ul li.panel a').css('color','#7797c0');
         $('#north_america_panel ul li.panel:eq('+(panel.selector-1)+') a').css('color','#ed087f');
      });
      panel.selected = panel.selector;
   },
   
   switcher: function() {
      panel.selector = panel.next();
      panel.change();
   },
   
   next: function() {
      // alert(panel.panel_no);
      var next = panel.selector+1;
      if (next > panel.panel_no) next = 1;
      return next;
   },
   
   prev: function() {
      var next = panel.selector-1;
      if (next < 1) next = panel.panel_no;
      return next;
   },
   
   navigation: function() {
      //$('#panel_nav_container ul').append('<li class="previous"><a href="javascript:void(0);" onclick="panel.link(\'prev\')">&laquo;</a></li>');
      $("#north_america_panel").find("div.panel").each(function(i) {
         $('#panel_nav_container ul').append('<li class="panel"><a href="javascript:void(0);" onclick="panel.link('+(i+1)+')">'+(i+1)+'</a></li>');
      });
      $('#panel_nav_container ul').append('<li class="pause"><a href="javascript:void(0);" title=""></a></li>');
      $('#panel_nav_container ul li.pause a').text('Pause').attr("title","Pause").unbind('click').click(function (){
                                             panel.link('pause');
                                           });
      // onclick="panel.link(\'pause\')" Pause
   },
   
   link: function(i) {
	  
      if (i=='prev')
      {
         panel.selector = panel.prev();
      }
      else if (i=='next')
      {
         panel.selector = panel.next();
      }
      else if (i=='pause')
      {
         clearInterval(panel.panelInterval);
         $('#panel_nav_container ul li.pause').addClass('play');
         $('#panel_nav_container ul li.play').removeClass('pause');
         $('#panel_nav_container ul li.play a').text('Play').attr("title","Play").unbind('click').click(function (){
                                                panel.link('play');
															 });
      }
      else if (i=='play')
      {
         panel.switcher();
         panel.initInterval();
         $('#panel_nav_container ul li.play').addClass('pause');
         $('#panel_nav_container ul li.pause').removeClass('play');
         $('#panel_nav_container ul li.pause a').text('Pause').attr("title","Pause").unbind('click').click(function (){
                                                panel.link('pause');
															 });
      }
      else 
      {
		 $('#north_america_panel_nav li').removeAttr('class');
		 $('#north_america_panel_nav li:eq('+ (i-2) +')').addClass('active');
         clearInterval(panel.panelInterval);
         panel.selector = i;
         panel.change();
         //panelInterval = setTimeout("panel.sequence()",30000);
      }
   },

   sequence: function() {
      panel.initInterval();
   },
   
   priority: function() {
      $("#north_america_panel").find("div.panel").each(function(i) {
         if ( $(this).is('.first') )
         {
            panel.selector = i;
         }
      });
      
      if (panel.selector == -1)
      {
         var rand = Math.floor(Math.random()*panel.panel_no);
         panel.selector = rand;
      }
   }
};