$(document).ready(function(){
 
 (function(){
 if(!$('#clients-pics').length)
  return;
 
 var pics={here:[],there:[]};
 
 $('#clients-pics').find('a').each(function(i){
  var here=$(this).children(':first');
  var there=$('#clients-pics').children('.hidden').children(':eq('+i+')');
  pics.here[i]=here.prop('src');
  pics.there[i]=there.prop('src');
  
  $(this).children(':first').mouseover(function(){
   $(this).prop('src',pics.there[i]);
  }).mouseout(function(){
   $(this).prop('src',pics.here[i]);
  });
 });
 }());

(function(){
 if(!$('#under1,#under2,#under3,#under4').length)
  return;
 var gray={go:$('#gray-on'),gu:$('#gray-under')};
 gray.go.css('top','50%');
 var cls=$('#gray-on').children(':first');
 var data=$('#gray-on').children('div.data');

 $('#under1,#under2,#under3,#under4').click(function(){
  gray.gu.css('visibility','visible');
  data.css('display','block');
  gray.go.css('margin-left',-gray.go.innerWidth()/2).css('margin-top',-gray.go.innerHeight()/2).css('visibility','visible');

  return false;
 });
 
 cls.click(function(){
 data.css('display','none');
  gray.gu.css('visibility','hidden');
  gray.go.css('visibility','hidden');
  
  return false;
 });
}());

 (function(){
 if(!$('#for-gallery').length)
  return;
 
 var active=0;
 var gal=$('#gallery1').children('.pics');
 var panel=gal.children('.panel');
 var forPanel=$('#for-panel');
 var items=gal.children('a.item');
 var gray={go:$('#gray-on'),gu:$('#gray-under')};
 gray.go.css('top','50%');
 var cls=$('#gray-on').children(':first');
 var opened=0;
 var data=$('#gray-on').children('div.data');
 gal.css('margin-left',0);
 
 var time;
 
 panel.prop('href',gal.children('.item:eq('+active+')').prop('href'));
 
 $('#for-gallery').children().each(function(){
  $(this).children(':first').click(function(){
   var self=$(this);
   if(!self.is('.active')){
    self.addClass('active');
	self.prop('src','images/gallery-buttonA.jpg');
	self.parent().parent().children(':eq('+active+')').children(':first').removeClass('active').prop('src','images/gallery-button.jpg');
	active=self.parent().index();
	
	clearTimeout(time);
	
	gal.stop(true);
	gal.animate({
	 marginLeft:parseInt(gal.css('margin-left'))-940*(active+parseInt(gal.css('margin-left'))/940)
	},{
	 duration:1500,
	 complete:function(){
	  go();
	 }
	});
	
	panel.stop(true);
	panel.animate({
	 opacity:0
	},{
	 duration:750,
	 complete:function(){
	  panel.children(':first').children('span').each(function(i){
	   $(this).text(forPanel.children(':eq('+active+')').children(':eq('+i+')').text());
	  });
	  
	  panel.prop('href',gal.children('.item:eq('+active+')').prop('href'));
	 }
	}).animate({
	 opacity:1
	},{
	 duration:750
	});
   }
  });
 });
 
 panel.click(function(){
  opened=panel.prop('href').match(/#\d+/)[0].replace('#','');
  gray.gu.css('visibility','visible');
  $(data[opened]).css('display','block');
  gray.go.css('margin-left',-gray.go.innerWidth()/2).css('margin-top',-gray.go.innerHeight()/2).css('visibility','visible');
  
  return false;
 });
 
 items.each(function(){
  $(this).click(function(){
   opened=$(this).index()-1;
   
   gray.gu.css('visibility','visible');
   $(data[opened]).css('display','block');
   gray.go.css('margin-left',-gray.go.innerWidth()/2).css('margin-top',-gray.go.innerHeight()/2).css('visibility','visible');
   
   return false;
  });
 });
 
 cls.click(function(){
  $(data[opened]).css('display','none');
  gray.gu.css('visibility','hidden');
  gray.go.css('visibility','hidden');
  
  return false;
 });
 
 function go(){
  time=setTimeout(function(){
   $('#for-gallery').children(':eq('+((active+1<$('#for-gallery').children().length)?active+1:0)+')').children(':first').trigger('click');
  },8000);
 }
 
 go();
 }());
 //work-gallery
 (function(){
  var gal=$('#work-gallery-long');
  var arrows={left:$('#left-arrow'),right:$('#right-arrow')};
  
  if(!gal.length)
   return;
  
  gal.children(':eq('+window.active+')').addClass('active');
  if(window.active>=2)
   arrows.left.css('display','block');
  if(window.active<=gal.children().length-3)
   arrows.right.css('display','block');
  var k=(window.active==0)?0:((window.active==gal.children().length-1)?gal.children().length-3:window.active-1);
  gal
   .css('margin-left',-k*294)
   .data('active',k);
  
  gal.data('blocks',gal.children().length);
  
  arrows.left.click(function(){
   if(gal.is(':animated'))
    return false;
   arrows.right.css('display','block');
   gal.data('active',gal.data('active')-1);
   if(gal.data('active')==0)
    arrows.left.css('display','none');
   gal.animate({
    marginLeft:'+=294'
   },
   {
    
   });
   
   return false;
  });
  arrows.right.click(function(){
   if(gal.is(':animated'))
    return false;
   arrows.left.css('display','block');
   gal.data('active',gal.data('active')+1);
   if(gal.data('active')==gal.data('blocks')-3)
    arrows.right.css('display','none');
   gal.animate({
    marginLeft:'-=294'
   },
   {
    
   });
   
   return false;
  });
 }());
 //scroll
 (function(){
  var clickers=$('#services-scrollers a');
  
  if(!clickers.length)
   return;
  
  var refs=$('#main .top-ref');
  var match=document.location.href.match(/(?:#)([^\d]*)(\d+)/);
  var active=(match)?match[2]-1:-1;
  
  clickers.each(function(i){
   $(this).click(function(e){
    if(i>=refs.length)
     return false;
	
    $(($.browser.webkit)?'body':'html').animate({
     scrollTop:$(refs[i]).offset().top
    },1000);
    
    if(window.history&&history.pushState)
	{
     match=document.location.href.match(/.*(?=#)/);
     history.replaceState("", "", (match?match[0]:document.location.href)+"#tab"+(i+1));
	 
	 return false;
	}
   })
  });
  
  if(!(window.history&&history.pushState))
  {
   clickers.each(function(i){
    match=document.location.href.match(/.*(?=#)/);
    $(this).attr('href',(match?match[0]:document.location.href)+'#tab'+(i+1));
   });
   
   if(active>=0)
    clickers.eq(active).get(0).click();
  }else
  {
   if(active>=0)
    clickers.eq(active).trigger('click');
   match=document.location.href.match(/.*(?=#)/);
   if(active>=0)
    history.replaceState("", "", (match?match[0]:document.location.href)+"#tab"+(active+1));
  }
  
  refs.click(function(){
   $(($.browser.webkit)?'body':'html').animate({
    scrollTop:0
   },1000);
   
   return false;
  });
 }());
 //studio
 (function(){
  var tabs=$('#float1 a.h3');
  
  if(window.studio==undefined)
   return;
  
  var match=document.location.href.match(/(?:#)([^\d]*)(\d+)/);
  var active=match?(match[2]-1):window.studio;
  var data=[$('#float2,#st1,#st11,#proud'),$('#years,#st22'),$('#st33'),$('#st44')];
  
  $(data[0]).css('display','none');
  $(data[active]).css('display','block');

 if(!(window.history&&history.pushState))
   tabs.each(function(i){
    match=document.location.href.match(/.*(?=#)/);
    $(this).attr('href',(match?match[0]:document.location.href)+'#tab'+(i+1));
   });else
   $(tabs[active]).addClass('studio-tab').trigger('click');
  if(window.history&&history.pushState)
  {
   match=document.location.href.match(/.*(?=#)/);
   history.replaceState("", "", (match?match[0]:document.location.href)+"#tab"+(active+1));
  }else
   $(tabs[active]).addClass('studio-tab').get(0).click();
  
  tabs.each(function(i){
   $(this).click(function(){
    if(active!=i)
	{
	 $(tabs[active]).removeClass('studio-tab');
	 data[active].css('display','none');
	 $(tabs[i]).addClass('studio-tab');
	 data[i].css('display','block');
	 if(i==1)
	  $(this).closest('#float1').css('width','100%');else
	  $(this).closest('#float1').css('width','650px');
	 active=i;
	}
        if(window.history&&history.pushState)
	{
         match=document.location.href.match(/.*(?=#)/);
         history.replaceState("", "", (match?match[0]:document.location.href)+"#tab"+(active+1));
	 return false;
	}
   });
  });
 }());
 //showreel
 (function(){
  if(!$('#main').is('.showreel'))
   return;
   
  var refs=$('#main').find('.item');
  var cls=refs.parent();
  
  refs.click(function(){
   $(this).parent().prev().css('display','block');
   
   return false;
  });
  
  cls.each(function(){
   $(this).prev().children(':first').click(function(){
    $(this).parent().css('display','none');
    
    return false;
   });
  });
 }());
});
