$(document).ready(function () {

	//aligh set to the middle
	if($('#bottomSetsX').length > 0){
		//make sure the width is the same. for some reason on resize the width changes
        var WbottomSetsX = $('#bottomSetsX').width();
        $('#bottomSetsX').width(WbottomSetsX);
		// number by class all the items
		$.each( $("#carousel ul li"), function(i, n){
		  $(this).attr("class",i);
		});
		//show work type
		$('div.work span').after(" " + $('li.0 div.pieceWorkType').text() );
	}

	$("#carousel").jCarouselLite({
	    btnNext: ".nextPiece",
	    btnPrev: ".previousPiece",
	    visible: 1,
	    circular: true,
	    afterEnd: function(el) {
	    	//elId = Number(el.attr("class")) + 1;
	    	elId = Number(el.attr("class"));
	    	//change counter
            $("span#currentItem").text(elId+1);
	    	
	    	$('div.work').empty();
	    	$('div.work').html("<span>WORK:</span> " + $('li.'+elId+':first div.pieceWorkType').text() );
	    	
            //make images as background
	    	var img = el.children('.imagePieceNoText').children();
	    	var imgW = el.children('.imagePieceNoText').children().attr('width');
	    	var imgSrc = el.children('.imagePieceNoText').children().attr('src');
	    	if(imgW > 1500){
	    		var cssObj = {'background' : 'url('+imgSrc+') no-repeat center center'}
			    $('body').css(cssObj);
			    img.hide();
			    
			    //$('.imagePieceNoText img').hide();
			    
			}else{
	    		$('body').removeAttr('style');
	    		
	    		//$('.imagePieceNoText img').show();
	    		
	    		var windowh = $(window).height();
				var topH = 85;
				var bottomH = 114;
				var liH = windowh - topH - bottomH;
				$('#carousel li.' + elId).css('height',liH+'px');
	    	}
	    }
	});
	
	
	
	$.each( $("#extCtrlCarousel div"), function(i, n){
        //add number class + 1 and convert to string. for external controls of the carousel	   
        $(this).addClass("" + (i + 1));
	});

    // number by class all the items
	$.each( $("#homeCarousel ul li"), function(i, n){
        $(this).attr("class",i);
	});

    //select first item form home carousel nav
    $('#extCtrlCarousel div:first img').addClass('selected');
    
	$("#homeCarousel").jCarouselLite({
	    btnNext: ".fpNextPiece",
	    btnPrev: ".fpPreviousPiece",
	    visible: 1,
	    circular: true
	});
	
	
	
	
    if($('.imagePieceNoText').length > 0){
		var img = $('#carousel ul li.0:first .imagePieceNoText img');
		var imgW = img.attr('width');
		var imgH = img.attr('height');
		var imgSrc = img.attr('src');
		if(imgW > 1500){
			var cssObj = {'background' : 'url('+imgSrc+') no-repeat 50% 50% #072A02'}
		    $('body').css(cssObj);
		    
		    img.hide();
            //$('.imagePieceNoText img').hide();
            
            $('#carousel ul li').css('height','100px');
		}else{
			$('body').removeAttr('style');
			//$('#carousel ul li').css('height', imgH + 'px');
			
			var windowh = $(window).height();
			var topH = 85;
			var bottomH = 114;
			
			var liH = windowh - topH - bottomH;
			$('#carousel li.0').css('height',liH+'px');
		}
	}
	
	if($('.imagePiece').length > 0){
		
		var windowh = $(window).height();
		var topH = 85;
		var bottomH = 114;
		var liH = windowh - topH - bottomH;
		$('#carousel li.0').css('height',liH+'px');
	}
	
	
	
	//hide prev next if on element in carousell
	if( $('#carousel').length > 0){
		if( $('#carousel ul li').length == 1 ){
			$('.previousPiece').css("visibility", "hidden");
			$('.nextPiece').css("visibility", "hidden");
		}
	}
	
	$('#projectsTrigger div a').hover(
          function() {
               var idx = $('#projectsTrigger div a').index(this);
               $('#bottomSetsX div:eq('+idx+') img').addClass('over');
          },
          function() {
               var idx = $('#projectsTrigger div a').index(this);
               //$('#content div').eq(idx).hide();
               $('#bottomSetsX div:eq('+idx+') img').removeClass('over');
          }
      );
      

	//about open close
	
	$('h3.title').click(function() {
		$(this).next().slideToggle();
		$(this).toggleClass('opened');
	}).next().hide();
	
	
	//add separators
	$('div.service').after('<img src="assets/comp/service-sep.png" alt=""/>');
	//$('div.service h3').addClass('closed');
	
	moveStuff();
});


$(window).resize(function(){
  	moveStuff();		
});



$(window).load(function () {
    moveStuff();	
});

function moveStuff(){
	
	var windowh = $(window).height();
	var topH = 85;
	var bottomH = 114;
	var bottomSimple = 89;
	
	//align home
    if($('#homeCarousel').length > 0){
        //get height of the middle piece
		var homeCarousel = $('#homeCarousel').height();
		var moveH = (homeCarousel)/2
		
		$('.fpPreviousPiece').css("margin-top",parseInt(moveH - 31/2));
		$('.fpNextPiece').css("margin-top",parseInt(moveH - 31/2));
    }

	//align home carousel to the middle
	if($('#homeLeft').length > 0){
		var homeLeft = $('#homeLeft').height();
		var moveH = (windowh - homeLeft)/2 - bottomSimple + 40;
		$('#homeLeft').css('padding-top', moveH +'px')
	}
	
	//align home content to the middle
	if($('#homeRight').length > 0){
		var homeRight = $('#homeRight').height();
		var moveH = (windowh - homeRight)/2 - bottomSimple + 40;
		$('#homeRight').css('padding-top', moveH +'px')
	}
    
    //align about
   
	if($('#aboutW').length > 0){
    	var aboutWH = $('#aboutW .left').height();
		var moveAbout = parseInt( (windowh - aboutWH- 100)/2 - bottomSimple + 40 );
    	if(moveAbout > 0){
			$('#aboutW').css('padding-top',moveAbout + 'px');
		}
    }

 	//align contact
    if($('#contactW').length > 0){
    	var contactWH = $('#contactW .left').height();
		var moveContact = parseInt( (windowh - contactWH - 100)/2 - bottomSimple + 40 );
		if(moveContact > 0){
			$('#contactW').css('padding-top', moveContact + 'px');
		}
    	
    }

    if($('#bottomSets').length > 0){
        var WbottomSetsX = $('#bottomSetsX').width();
        //align projects thumb to the middle
        $('#bottomSets .thumbSet:first').css('margin-left', parseInt((1175-WbottomSetsX)/2) );
        
        //layered fix for thumbs to be clickable
	   var offset = $("#bottomSets div.thumbSet:first").offset();
	   $('#projectsTrigger').css({'left' : parseInt(offset.left) + 'px', 'top' : parseInt(offset.top) + 'px'});
    }
    
    //align arrows image and text
	if($('.previousPiece').length > 0){
		
		//get height of the middle piece
		var windowh = $(window).height();
		var topH = 85;
		var bottomH = 114;
		
		var moveH = (windowh - topH - bottomH)/2;
		
		$('.previousPiece').css("margin-top",parseInt(moveH - 31/2));
		$('.nextPiece').css("margin-top",parseInt(moveH - 31/2));
		
		//align text vertical center
		if($('.textPiece').length > 0){
			var textPieceH = $('.textPiece').height();
			$('.textPiece').css("margin-top", parseInt(moveH - textPieceH/2) );
		}
		
		//align vertical the image
		if($('.imagePiece').length > 0){
			var imagePieceH = $('.imagePiece img').height();
			//alert(imagePieceH);
			var moveImg = moveH - imagePieceH/2;
			//alert(moveH);
			if(moveImg > 0){
				$('.imagePiece').css("padding-top", parseInt(moveImg));
			}else{
				$('.imagePiece').css("padding-top",0);
			}
		}
		
		if($('.imagePieceNoText').length > 0){
			var imagePieceNoTextH = $('.imagePieceNoText img').height();
			var moveImg = moveH - imagePieceNoTextH/2;
			if(moveImg > 0){
				$('.imagePieceNoText').css("padding-top",  parseInt(moveImg));
			}else{
				$('.imagePieceNoText').css("padding-top",0);
			}
		}
	}
}
