var $j=jQuery.noConflict();

$j(document).ready(function(){
  if($j.browser.msie){
    $j(".main-nav li:last-child").addClass("off");
  }
  
  if($j("body.ttype-serveis").length>0){
    
    if(!$j("html").hasClass("lt-ie8")){
      $j("#theloop")
        .find("h1")
        .each(function(i){
          $j(this)
            .nextUntil("h1,.clear")
            .wrapAll('<section id="tad-item-'+(i+1)+'" class="tad-item '+((i==0)?'no-transparent':'transparent')+'"></section>');
          
          $j(this).wrapInner('<a href="#" rel="#tad-item-'+(i+1)+'"></a>');
          if(i==0){
            $j(this).addClass("s-selected");
          }
          
        });
      
      $j("#theloop")
        .find("h1,.tad-item")
        .wrapAll('<div class="tad"></div>');
        
      var $tad=$j("#theloop .tad");
    
      //igualem alçades i reposicionem target
      var highest=0;
      $tad.find("h1").each(function(i,val){
          //reposicionem target
        var position=$j(this).find("a").position();
        var $tadItem=$j("#tad-item-"+(i+1));
        var nu_top=($j("html").hasClass("ie8"))?position.top-25:position.top-2;
        $tadItem.css("top",nu_top);
        if(highest<$tadItem.outerHeight()) highest=$tadItem.outerHeight();
      });
      $tad.height(highest);
  
      //acció
      $tad.find("h1 a")
        .click(function(e){
          var $target=$j($j(this).attr("rel"));
          
          //unselect, selct
          $tad.find(".s-selected").removeClass("s-selected");
          $j(this).closest("h1").addClass("s-selected");
          
          
            
          if($target.hasClass("no-transparent")){
            return true
          }else{
            $tad.find(".tad-item")
              .addClass("transparent")
              .removeClass("no-transparent");
            $target
              .removeClass("transparent")
              .addClass("no-transparent");
              
            //--------------IE8
            if($j("html").hasClass("ie8")){
            	$j("#theloop .tad > .transparent").fadeOut();
            	$j("#theloop .tad > .no-transparent").fadeIn();
            }
            
            //---- scroll to top
            $tad.data("scrolling",true);
            $j.scrollTo(0, 1000, {
              axis: 'y',
              easing: '',
              onAfter:function(){
              	var $tad=$j("#theloop .tad");
              	$tad.data("scrolling",false);
              	$j(window).scroll();
              }
            });
            return false;
            
          }
        });
       $tad.data("scrolling",false);//no hi ha scrolling
    }
    
    //reposicionem menú
   	$j("#theloop .entry_content .tad").append('<span class="debug"></span>');
   	var default_padding_top=$j(".tad h1").first();
    $j(window).scroll(function(){
    	var $tad=$j("#theloop .tad");
    	if(!$tad.data("scrolling")){
	    	var $debug=$j("#theloop .entry_content .tad > .debug");
	    	var $h1=$j(".tad h1").first();
	    	var $scrollTop=$j(window).scrollTop()-400;
	    	if($scrollTop<0) $scrollTop=0;
	    	$h1.animate({paddingTop:$scrollTop+'px'},{duration:500,queue:false}); 
    	}
    });
  }
  
  if($j("body.single-proyectos").length>0){
    
    $j(".single-proyectos #feature-area").click(function(e){
      var posicio=e.pageX-$j(this).offset().left-$j(this).width()/2;
    	var $active_slide=$j("#feature-footer.dots #featurenav a.activeSlide");
    	var $first=$active_slide.closest("div").children("a").first();
    	var $last=$active_slide.closest("div").children("a").last();
      if(posicio>0){
      	var $next=$active_slide.next();
      	if($next.length==0) $next=$first;
      	$next.click();  
      }else{
      	var $prev=$active_slide.prev();
      	if($prev.length==0) $prev=$last; 
        $prev.click(); 
      }
    });
    
    
  }
  
  //--------------IE8
  if($j("html").hasClass("ie8")){
  	$j("#theloop .tad > .transparent").fadeOut();
  	$j("#theloop .tad > .no-transparent").fadeIn();
  }  
  

});


