/**
 * Copyright (C) 2009 Drumbit - www.drumbit.com
 * 
 * This file is part of ExperienceGifts
 *
 * ExperienceGifts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ExperienceGifts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ExperienceGifts.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

$(document).ready(function() {
    
    $('#home_rotator LI A').each(function(){
      $('<div class="rotator_active" id="rotator_'+$(this).attr('rel')+'"><div><p>'+
          '<a href="'+$(this).attr('href')+'">'+$(this).html()+'</a>'+
        '</p></div></div>')
        .prependTo('#home_rotator')
        .css({'top': $(this).parent().parent().offset().top,'left': ($(this).parent().parent().offset().left -1 )})
        .hide();

      var href = $(this).attr('href');

      $('.home_rotator_img[rel="'+$(this).attr('rel')+'"]').click(function(){ window.location = href; });
          
      $(this).click(function(){
      	$().stopTime('rotator');
        rotatorActivate($(this).attr('rel'));
        $().oneTime(5000, startRotator);
        return false; 
       });
      
      var zIndexNumber = 1000;
      $('.ie #afold div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
      });
      
    });
    
    var tabContainers = $('.tab_content');
       $('#tabs a').click(function () {
          tabContainers.hide().filter(this.hash).show();
          $('#tabs li').removeClass('active');
          $(this).parent().addClass('active');
          $(this).blur();
          return false;
      });
    $('#tabs a:first').click();
  
    rotatorActivate(0);
    startRotator();
    
    $(window).resize(function(){
    	 $('#home_rotator LI A').each(function(){
    	   $('#rotator_'+$(this).attr('rel')).css({'top': $(this).parent().parent().offset().top,'left': ($(this).parent().offset().left - 1)});
      });
    });

    function rotatorActivate(index)
    {
      $('.rotator_active.active, .home_rotator_img.active').fadeOut(500).removeClass('active');
      $('#rotator_'+index).fadeIn(500).addClass('active');
      $('.home_rotator_img[rel="'+index+'"]').fadeIn(500).addClass('active');
    }
    
    function rotatorNext()
    {
      var index = $('.home_rotator_img.active').attr('rel');
      if (index == 3)
        return 0;
      else 
        return ++index;
    }
    
    function startRotator()
    {
    	$().everyTime(3000,'rotator' , function(){rotatorActivate(rotatorNext())})
    }
 
})

