$(document).ready(function(){
	
	tooltip();
	// alert($('body').attr('class'));
	if($('body').attr('class') === undefined){
		circles();
		works();	
		section();
		blank();
	}

	carousel();
	
	$(window).bind('blur', function() {
		clearTimeout(run);
	}); 
	$(window).bind('focus', function() {
		run = setTimeout('rotate()', speed);   
	});
	
	$(document).scroll(function(e){
		scroll(); 
	});
	
	if($('#goto').length > 0){
		$('#goto').delay(500).click().remove();
		
	}
});


/* MENU FIXED */
function scroll(){
	
	if($(document).scrollTop() > $('#nav-util-space').offset().top){
		if($('#nav-util .menu').css('display') != 'block'){
			$('#nav-util').css('position','fixed').css('top','0');
			$('#nav-util .menu').fadeIn('1000');
			$('#nav-util .telefono').animate({left: '50',opacity: '1'},500);
			$('#nav-util a img').fadeIn('3000');
		}
	}
	else{
		if($('#nav-util .menu').css('display') != 'none'){
			$('#nav-util').css('position','absolute').css('top','auto');
			$('#nav-util .menu').fadeOut('1000');
			$('#nav-util .telefono').animate({left: '0',opacity:'0'},500);
			$('#nav-util a img').fadeOut('3000');
		}
	}
	
}

/* TOOLTIP */
function tooltip(){

	$('.tooltip').mouseenter(function(){
		$(this).find('.over').fadeIn(800);
		$(this).find('.info').animate({top:'+=10px',opacity:'1'},500);
		$(this).find('.tooltip .out').fadeOut(800);
	});
	
	$('.tooltip').mouseleave(function(){
		$(this).find('.out').fadeIn(500);
		$(this).find('.info').animate({top:'-=10px',opacity:'0'},500);
		$(this).find('.over').fadeOut(500);
	});
}

/* CIRCLES */
function circles(){

		$('.inner').hover(function(){
			$(this).find('span').fadeIn('slow');
			$(this).next('.circle').animate({left:'0px',top:'0px',width:'290px',height:'290px'},250);
			// $(this).find('.inner').animate({left:'20px',top:'20px'},250);
		} ,
			function(){
				$(this).find('span').fadeOut('slow');
				$(this).next('.circle').animate({left:'10px',top:'10px',width:'270px',height:'270px'},250);
				// $(this).find('.inner').animate({left:'10px',top:'10px'},250);
			}
		);
			
}

/* WORKS */
function works(){

		$('#trabajos a').hover(function(){
			$(this).find('.on').fadeIn('slow');
			$(this).find('.info').animate({left:'10px',opacity:1},400);
			
		} ,
			function(){
				$(this).find('.on').fadeOut('slow');
				$(this).find('.info').animate({left:'-150px',opacity:0},400);
			}
		);
			
}

/* SECTION */
function section(){
	$('.section').click(function(e){
		e.preventDefault();
		div = '#'+$(this).attr('href').substring(1);
		// alert(div);
		if(div == '#')
			$('html,body').animate({scrollTop: 0}, 1000);
		else
			$('html,body').animate({scrollTop: $(div).offset().top-40}, 1000);
	});
}

/* LINKS ON NEW WINDOW */
function blank(){
	$('.blank').click(function(e){
		e.preventDefault();
		url = $(this).attr('href');
		window.open(url);
	});
}

/* INFINITE CAROUSEL */
function carousel(){ 
     
    //grab the width and calculate left value
    var item_width = $('#slides .slide').outerWidth(); 
    var left_value = item_width * (-1); 
         
    //move the last item before first item, just in case user click prev button
    $('#slides .slide:first').before($('#slides .slide:last'));
     
    //set the default item to the correct position 
    $('#slides #slides-wrapper').css({'left' : left_value});
 
    //if user clicked on prev button
    $('#prev').click(function() {
 
		 clearTimeout(run); 
		 run = setTimeout('rotate()', speed);
		
        //get the right position            
        var left_indent = parseInt($('#slides #slides-wrapper').css('left')) + item_width;
 
        //slide the item            
        $('#slides #slides-wrapper').animate({'left' : left_indent}, 500,function(){    
 
            //move the last item and put it as first item               
            $('#slides .slide:first').before($('#slides .slide:last'));           
 
            //set the default item to correct position
            $('#slides #slides-wrapper').css({'left' : left_value});
			
			active = $('#slide-nav .active');
			prevSlide = active.prev('span');
			active.removeClass('active');
			
			if(prevSlide.length > 0)
				prevSlide.addClass('active');
			else
				$('#slide-nav span:last').addClass('active');
         
        });
 
        //cancel the link behavior            
        return false;
             
    });
 
  
    //if user clicked on next button
    $('#next').click(function() {
         
		 clearTimeout(run); 
		 run = setTimeout('rotate()', speed);   
		 
        //get the right position
        var left_indent = parseInt($('#slides #slides-wrapper').css('left')) - item_width;
         
        //slide the item
        $('#slides #slides-wrapper').animate({'left' : left_indent}, 500, function () {
             
            //move the first item and put it as last item
            $('#slides .slide:last').after($('#slides .slide:first'));                  
             
            //set the default item to correct position
            $('#slides #slides-wrapper').css({'left' : left_value});
			
			active = $('#slide-nav .active');
			nextSlide = active.next();
   			active.removeClass('active');
			if(nextSlide.length > 0)
				nextSlide.addClass('active');
			else
				$('#slide-nav span:first').addClass('active');
         
        });
                  
        //cancel the link behavior
        return false;
         
    });        
     
    //if mouse hover, pause the auto rotation, otherwise rotate it
    $('#slides').hover(
         
        function() {
            clearTimeout(run);
        }, 
        function() {
            run = setTimeout('rotate()', speed);   
        }
    ); 

}

//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
function rotate() {
    $('#next').click();
}

//rotation speed and timer
var speed = 5000;
var run = setTimeout('rotate()', speed); 
