// JavaScript Document

jQuery(document).ready(function(){
    var totWidth=0;
	var current = 1;
    var positions = new Array();
 
    jQuery('#slides .slide').each(function(i) {
      positions[i]= totWidth;
      totWidth += jQuery(this).width();
 
      if(!jQuery(this).width()) {
        alert("Please, fill in width & height for all your images!");
        return false;
      }
    });
 
    jQuery('#slides').width(totWidth);
 	
    jQuery('#menu ul li a').blur(function(e){
      jQuery('li.menuItem').removeClass('act').addClass('inact');
      jQuery(this).parent().addClass('act');
      var pos = jQuery(this).parent().prevAll('.menuItem').length;
      jQuery('#slides').stop().animate({marginLeft:-positions[pos]+'px'},750)
      e.preventDefault();
    });
	
	jQuery('#menu ul li a').click(function(e){
	  clearInterval(itvl);
      jQuery('li.menuItem').removeClass('act').addClass('inact');
      jQuery(this).parent().addClass('act');
      var pos = jQuery(this).parent().prevAll('.menuItem').length;
      jQuery('#slides').stop().animate({marginLeft:-positions[pos]+'px'},750)
      e.preventDefault();
    });
	
	// Add Active Class to our First Element, and Inactive Class to its Siblings
	jQuery('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');

	// Auto Advance Function called by setInterval method
	function autoAdvance() {
	  if(current == -1) return false;
	  jQuery('#menu ul li a').eq(current % jQuery('#menu ul li a').length).blur();
	  current++;
	}
	
	// setInterval Method to Auto Advance elements
	var itvl = setInterval(function(){ autoAdvance() }, 5 * 1000);
});