var current_slide = 0;
var next_slide = 0;
var animation_stopped = 1;
var intervalID;
var wrap_height;
var wrap_width;
var background_position = '0% 0%';
	

function homepage_animation() {
    
    animation_stopped = 0;
    if(next_slide != 0) {
	$('#home_circle').css({backgroundPosition: $('#home_circle #a'+next_slide).data('background_position')});
	background_position = $('#home_circle #a'+next_slide).data('background_position');
    }

    
    $('.slide').removeClass('current')
	// current anim
	$('#slide_'+current_slide).stop().animate({
		left: -wrap_width
		    }, 1600);
    
    $('#slide_'+current_slide+' .txt').animate({
	    left: -800
		}, 1600);
    // next anim
    
    $('#slide_'+next_slide+' .bg2').stop().show().css('left', 0);
    $('#slide_'+next_slide).show().addClass('current').css('left', wrap_width);
    $('#slide_'+next_slide+' .txt').show().css('left', wrap_width);
    // bg
    
    $('#slide_'+next_slide).animate({
	    left: 0
		}, 1600, function(){
	    animation_stopped = 1;
	    $('#slide_'+current_slide+' .bg2').hide();
	    $('#slide_'+current_slide).hide();
	    $('#slide_'+current_slide+' .txt').hide();
	    current_slide = next_slide;
	});			
    // bg2
    $('#slide_'+next_slide+' .bg2').animate({
	    left: -770
		}, 1600, function(){ 		    
	});
    // txt
    $('#slide_'+next_slide+' .txt').animate({
	    left: 0
		}, 1600);
}


$(document).ready(function() {
  
// COLORS
$('.colors').each(function(){
	var arr = $(this).html().split('');	
	var new_str = '';
	var tci = 0;
	var tcic = -1;
	for(var i in arr)
	{
		i = parseInt(i);
		if (i%6 == 0) tcic++;
		tci = (i + 1) - tcic*6;
		if (i > 0 && arr[i] == arr[i-1]) tci--;
		new_str += '<span class="tc'+tci+'">'+arr[i]+'</span>';
	}
	$(this).html(new_str);
})



// PLACEHOLDER
$('input, textarea').placeholder();


// NAV INIT
    var arrow_speed = 300;
    var down_speed = 200;
    $('nav').data({current_pos: 0});
    $('.ul_wrap').data({current_pos: 0});
    $('#who_we_are').data({current_pos: -1497.5});
    $('#what_we_do').data({current_pos: -1395.5});
    $('#our_clients').data({current_pos: -1296.5});
    $('#inspiration').data({current_pos: -1200.5});
    $('#contact_us').data({current_pos: -1106.5});
    function calculateBackgroundArrowPosition(object_for_position){
	var first_position = 1813;
	var a_position = object_for_position.offset().left - ($(window).width()-1000)/2;
	var a_width = object_for_position.width();

        return -(first_position - a_position - a_width/2);
    }
    function animateBackgroundArrowPosition(object_for_position, background_tag) {
	var pos = calculateBackgroundArrowPosition(object_for_position);
	background_tag.stop().animate({backgroundPosition: pos + 'px 100%'},arrow_speed);
    }
    function setBackgroundArrowPosition(object_for_position, background_tag) {
	var pos = calculateBackgroundArrowPosition(object_for_position);
	background_tag.data({current_pos: pos});
	background_tag.css('background-position', pos+'px 100%');
    }
    function setNavBackgroundArrowPosition(object_for_position, background_tag) {
	var pos = object_for_position.data('current_pos');
	background_tag.data({current_pos: pos});
	background_tag.css('background-position', pos+'px 100%');
    }
    
    var $current_li = $('#nav > li.current');
    if ($current_li.length) {
	setNavBackgroundArrowPosition($current_li, $('nav'));
	// if SUB
	var $sub_li = $('#nav > li.sub.current');
	if ($sub_li.length) {
	    $current_li.find('.Snav').css({height: 40})	
	    $sub_li_li = $sub_li.find('li.current');
	    if ($sub_li_li.length) {		
		setBackgroundArrowPosition($sub_li_li.find('a'), $sub_li.find('.ul_wrap'));
     	    }
	}
    }

    // NAV
    $('#nav > li.sub > a').hover(
	function () {
	    var $hovered_li = $(this).parent();
	    animateBackgroundArrowPosition($(this),$('nav'));
	    $('#nav li .Snav, #nav li .ul_wrap').not($hovered_li.find('.Snav, .ul_wrap')).stop().animate({height: 0},down_speed);
	    $hovered_li.find('.Snav').stop().animate({height: 40},down_speed);
	    $hovered_li.find('.ul_wrap').stop().animate({height: 40, backgroundPosition: $hovered_li.find('.ul_wrap').data('current_pos') + 'px 100%'},down_speed);
	    
	});
    
    $('#nav > li.notsub > a').hover(
	function () {
	    animateBackgroundArrowPosition($(this),$('nav'));
	    $('.Snav, .ul_wrap').stop().animate({height: 0},down_speed);
	});
    
    $('#nav ul a').hover(
	function () {
	    animateBackgroundArrowPosition($(this),$(this).parent().parent().parent());
	}
    );
    
    // NAV leave
    $('#nav').mouseleave(function(){
	$('#nav .Snav, .ul_wrap').not('li.sub.current .Snav, li.sub.current .ul_wrap').stop().animate({height: 0});
	$('#nav li.sub.current .Snav').stop().animate({height: 40}, down_speed);
	$('#nav li.sub.current .ul_wrap').stop().animate({height: 40, backgroundPosition: $('li.sub.current .ul_wrap').data('current_pos') + 'px 100%'}, arrow_speed)
	
	$('nav').stop().animate({backgroundPosition: $('nav').data('current_pos')+'px 100%'}, arrow_speed);

    });
    
    // TABS 
    $('ul.tabs').tabs('div.tab')
    
    
    // MORE TEXT
    $('.more_text').hide();
    $('.btn_less_text').hide();
    $('.btn_more_text').click(function(){
        $(this).parent().parent().find('.more_text').slideToggle();
	$(this).parent().find('.btn_less_text').show();
	$(this).hide();
	return false;
    });
    
    $('.btn_less_text').click(function(){
	$(this).parent().parent().find('.more_text').slideToggle();
        $(this).parent().find('.btn_less_text').hide();
	$(this).parent().find('.btn_more_text').show();
	$(this).hide();
	return false;
    });
    
          
    // GAYSCALE
    if($.browser.msie) {
     $('.clients_small img').load('',function(){
		var img = new Image();
		img.src = $(this).attr('src');
		Pixastic.process(img, "desaturate", {average : false});
		$(this).addClass('colored').hide();
		$(this).parent().append(img);
    });
    }
    else {
        $('.clients_small img').load(function(){
		var clone = $(this).clone().addClass('colored').hide();
		$(this).parent().append(clone);
		$(this).pixastic("desaturate");
        });
    }
    
    $('.clients_small li').hover(function() { 
	$(this).find('img.colored').show();
    }, function() { 
	$(this).find('img.colored').hide();
    }); 
    
    
    // TEAM
    var scrool_pos = 0;
    $('ul.team > li > a').click(function(){
	scrool_pos = $('html, body').scrollTop();
	$('html, body').scrollTop(0);
	var height = $('#main').height();
	$($(this).attr('href')).show().css('height', height);
	
	return false;
    })
    $('.person .btn').click(function(){
	$('html, body').scrollTop(scrool_pos);
	$(this).parent().parent().parent().parent().hide();
	return false;
    })
    
    
    
// CLIENTS 
    var scrool_pos = 0;
    
    $('#page-clients ul.clients a').click(function(){
        
	scrool_pos = $('html, body').scrollTop();
	$('html, body').scrollTop(0);
	current = $($(this).attr('href'));
        current.show();
	
        current.find('div.spec').each(function() {
            $(this).data('move', 1);
        });
	var height = $('#page-clients').height();
	current.css('height', height);	
	
	return false;
	
    })
    $('#page-clients .client .btn').click(function(){
	    var parent = $(this).parents('.client');
	    parent.find('div.spec').each( function() {$(this).data('move',0);} );
	
	$('html, body').scrollTop(scrool_pos);	
        parent.hide();        
	
	return false;
    })

// INSIGHT KIDS    
    var scrool_pos_kids = 0;
    
    $('#page-kids ul.clients a').click(function(){
        
	scrool_pos_kids = $('html, body').scrollTop();
	$('html, body').scrollTop(510);
	current = $($(this).attr('href'));
        current.show();
	
        current.find('div.spec').each(function() {
            $(this).data('move', 1);
        });
	var height = $('#page-kids').height();
	current.css('height', height);	
	
	return false;
	
    })
    $('#page-kids .client .btn').click(function(){
	    var parent = $(this).parents('.client');
	    parent.find('div.spec').each( function() {$(this).data('move',0);} );
	
	$('html, body').scrollTop(scrool_pos_kids);	
        parent.hide();        
	
	return false;
    })
    
    /* HOME */
    

    //wrap_height = $('body').height() - 169;
    wrap_height = $('body').height()-152;
    wrap_width = $('body').width()+15;
        
    if ($('#home').length) {
	
	/* animation */
	
	$('#home').height(wrap_height);
	//$('#home .slide .bg').height(wrap_height);
	$('#home .slide .bg').width(wrap_width);	
	$('#home .slide .bg2').height(wrap_height);
	
	$('#a1').data({'background_position': '0% 0%'})
	$('#a2').data({'background_position': '-153px 0%'})
	$('#a3').data({'background_position': '-306px 0%'})
	$('#a4').data({'background_position': '-459px 0%'})
	$('#a1, #a2, #a3, #a4').hover(function(){
		//background_position = $('#home_circle').css('backgroundPosition');
	    $('#home_circle').css({backgroundPosition: $(this).data('background_position')});
	}, function(){
		$('#home_circle').css({backgroundPosition: background_position});
	})
	$('#home_circle a').click(function(event)
				  {		
				      clearInterval(intervalID);
				      intervalID = setInterval(function(){
					      if(current_slide==4){
						  next_slide=0;
					      }
					      next_slide++;
					      homepage_animation($("#home_circle #a"+ next_slide.toString()));
					  }, 8000);


				      var object = $(this);
				      var a_class = object.attr('id');
				      var a_id = a_class.replace('a', '');
				      
				      if (current_slide != a_id && animation_stopped)  {
					  next_slide = parseInt(a_id);
					  homepage_animation();
				      }
				      return false;
				  });
	
	intervalID = setInterval(function(){
		if(current_slide==4){
		    next_slide=0;
		}
		next_slide++;
		homepage_animation($("#home_circle #a"+ next_slide.toString()));
	    }, 8000);

    }
    
    if(($('#home').length)>0)
    {
	var img = $('img.bg');
	var const_width;
	var propotion = img[0].width / img[0].height;
	$(window).resize(function(){
		$('.slide').css({width: '100%'});
		wrap_width = $(this).width();
		var animation_height = $(this).height() - $('header.home').height();- $('footer').height();
		$('div.bg2').css({height: animation_height});
		
		img.css({minHeight: animation_height,minWidth: '100%'});
		if($('.slide:visible img.bg')[0].height == animation_height) {
		   
		    img.css({width: parseInt($('.slide:visible img.bg').css('height')) * propotion});
		    
		} else {
		    img.css({width: '100%'});
		     
		}
		$('.spec').data("vel", false);
		
	    });
	
        $('#footer_wrap').css({'width':'100%'});
        $('html').css({'overflow':'hidden'});
    }
    

    
    $('.area').focusin(function(){
        if($('#page-on_your_mind .area').length>0)
            $('#page-on_your_mind .area').css({'background-position':'0px -129px'})
        else    
            $('.area').css({'background-position':'-4px -194px'})
        
    }) 
    
    $('.area').focusout(function(){
        if($('#page-on_your_mind .area').length>0)
            $('#page-on_your_mind .area').css({'background-position':'0px -2px'})
        else
            $('.area').css({'background-position':'-4px -5px'})
    })
    
    $('.single').focusin(function(){
        $(this).css({'background-position':'0px -37px'})
    })
    
    $('.single').focusout(function(){
        $(this).css({'background-position':'0px 0px'})
    })
    
    $('.logo').mouseover(function(){
        $('img.png').hide();
        $('img.gif').show();
    });
    
    $('.logo').mouseout(function(){
        $('img.png').show();
        $('img.gif').hide();
    });
});



