/*

Cuca Design

CSS by
http://

RUBY ON RAILS
http://

DESIGN
http://

*/


(function($,sr){
 
  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;
 
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');


function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true;
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

addEvent(window, 'resize', adjustLayout);

$(window).resize(function(){
	adjustLayout();
})

addEvent(window, 'smartresize', adjustLayoutSmart);

$(window).smartresize(function(){
	adjustLayoutSmart();
})


$(document).ready(function(){
	
	adjustLayout();
	startSlider();
	
	
	/* PNG FIX FOR IE6 */
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) { // if IE6
		$("#logo, .go-to-work").pngFix();
	}
	
	$(".circle-mask").each(function(){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) { // if IE6
			var src = $(this).attr("src");
			var new_src = src.substring(0, src.length-3)+"gif";
			$(this).attr("src", new_src);
		}
	});


	/* THUMBS */
	
	/* body e list */
	
	$("body.home .thumb, body.list .thumb").mouseenter(function(evt){
		$(this).addClass("half-hover");
	});
	
	$("body.home .thumb, body.list .thumb").mouseleave(function(evt){
		$(this).removeClass("half-hover");
	});

	$("body.home .thumb, body.list .thumb, #info-box .thumb").click(function(evt){
		link = $(this).find("a").attr("href");
		location.href = link;
	});
	
	/* equipe */
	
	$("body.half-list.left .thumb").mouseenter(function(evt){
		$(this).addClass("half-hover");
	});
	
	$("body.half-list.left .thumb").mouseleave(function(evt){
		$(this).removeClass("half-hover");
		$(this).removeClass("full-hover");
	});
	
	$("body.half-list.left .thumb").click(function(evt){
		$(this).addClass("full-hover");
		$(this).removeClass("half-hover");
	});
	
	
	/* EXPERTISE */
	
	var hiddenExpertiseTop = ($(".expertise-inner").innerHeight()*-1)-112;
	
	$(".expertise-inner").prepend('<h3><a class="replace expertise" href="#expertise">Filtrar Projetos</a></h3>');
	$(".expertise-inner").css("marginTop", hiddenExpertiseTop+"px");
	
	$(".expertise-top h3 a").click(function(evt){
		$(".expertise-inner").animate({marginTop: "-97px"}, 300, function(){
			$(".expertise-inner ul").addClass("ready");
		});
		evt.preventDefault();
	});
	
	$(".expertise-inner h3 a").click(function(evt){
		$(".expertise-inner ul").removeClass("ready");
		$(".expertise-inner").animate({marginTop: hiddenExpertiseTop+"px"}, 300);
		evt.preventDefault();
	});
	
	$(".expertise-inner a").click(function(evt){
		$.cookie("last-group", null);
	});
	
	$("#project-btn").click(function(evt){
		$.cookie("last-group", null);
	});
	
	
	/* WORK ZOOM */
	
	/* $("#work img").click(function(){
		$("#work").toggleClass("full");
		$("#info").toggleClass("hidden");

		if( $("#work").hasClass("full") ){
			$("#zoom-hover").addClass("out");
			$("#zoom-hover").removeClass("in");
		} else {
			$("#zoom-hover").addClass("in");
			$("#zoom-hover").removeClass("out");
		}

		doImageBoxResize();
	});
	
	$("#work img").mousemove(function(evt){		
        $("#zoom-hover").show();
        $("#zoom-hover").css({
            top: (evt.pageY - 15) + "px",
            left: (evt.pageX - 35) + "px"
        });
    });

	$("#work img").mouseleave(function(e){
        $("#zoom-hover").hide();
    }); */

});

function setZoom(){
	//
}


function startSlider(){
	
	if( $(".group").length > 1 ){
		
		disableButtons();
		
		$("#arrow-right a, #arrow-right-mini a").click(function(evt){
			var target = findNext();
			sliderScrollTo( target, true );
			disableButtons();
			evt.preventDefault();
		});

		$("#arrow-left a, #arrow-left-mini a").click(function(evt){
			var target = findPrev();
			sliderScrollTo( target, true );
			disableButtons();
			evt.preventDefault();
		});
	} else {
		$("#arrow-right a, #arrow-right-mini a, #arrow-left a, #arrow-left-mini a").css("visibility", "hidden");
	}
}

scrollToTarget = 0;

function sliderScrollTo( target, anima ){
	if(anima == true){
		speed = 400;
	}else{
		speed = 0;
	}
	scrollToTarget = target;
	$("#slider-wrap").scrollTo( target, speed );
	markSelectedGroup( target );
	if( $("body").hasClass("home")){
		$.cookie("last-group", target);
	}
}

function markSelectedGroup( target ){
	$(".group").removeClass("selected");
	$(target).addClass("selected");
}

function findSelectedGroup(){
	if( !$(".group").hasClass("selected") ){
		$(".slider .group:first-child").addClass("selected");
	}
	return( $(".group.selected").attr("id") );
}


function adjustLayout(){
	adjustContent();
	adjustMethod();	
	doImageBoxResize();
	adjustFooter();
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) { // if IE6
		$(".v, .full-bg").each(function(){
			$(this).css("height", $(this).closest(".thumb").height()+"px");
		});
	}
}

function adjustLayoutSmart(){
	$("#slider-wrap").scrollTo( scrollToTarget, 400);	
}


function adjustContent(){
	if( $("body").hasClass("home") || $("body").hasClass("list") || $("body").hasClass("half-list") ){
		var p = ($("body").hasClass("half-list")) ? 50 : 100;
		var k = p*( $(window).height()-130 )/( $(window).width()*0.48 );
		if( k<p ){
			$("#content").css("width", k+"%");
		} else {
			$("#content").css("width", p+"%");
		}
	}
}


function adjustMethod(){
	var k = 13.33-($(window).width()*0.0083);
	$(".separation-arrow").css("left", -50+k+"px");
}


function adjustFooter(){
	$("#footer").css("position", "absolute");
	$("#footer").css("top", ($(document).height()-50)+"px");
	$("#footer").css("bottom", "auto");
}


function doImageBoxResize(){
	imageBoxResize(70, 50);
}


function imageBoxResize(innerMargin, txtBoxHeight) {

	if( $("#image-box") ){
		
		$("#image-box").css("width", ($(window).width()*0.96-235)+"px");
		
		var imgBoxWidth = $("#image-box").width();
		var windowHeight = $(window).height();
		var imgBoxHeight = windowHeight-txtBoxHeight;

		var imgHeight = $("#image-box img").height();
		var imgWidth = $("#image-box img").width();
		var imgAspect = imgWidth/imgHeight;

		var imgMaxHeight = imgBoxHeight-(2*innerMargin);
		var imgMaxWidth = imgBoxWidth-(2*innerMargin);
		var imgMaxAspect = imgMaxWidth/imgMaxHeight;

		$("#image-box").css("height", imgBoxHeight+"px");
		//$("#info-box").css("height", imgBoxHeight+"px");

		if (imgAspect<=imgMaxAspect) {
			$("#image-box img").css("height", imgMaxHeight+"px");
			$("#image-box img").css("width", "auto");
			$("#image-box img").css("margin", innerMargin+"px 0 0");
		};

		if (imgAspect>imgMaxAspect) {
			$("#image-box img").css("height", "auto");
			$("#image-box img").css("width", imgMaxWidth+"px");
			$("#image-box img").css("margin", innerMargin+((imgMaxHeight-$("#image-box img").height())/2)+"px 0 0");
		};
		
		if( $("#arrow-box").length ){
			arrowBoxResize(50);
		}
		
	}
}

function arrowBoxResize(txtBoxHeight) {	
	
	var imgBoxWidth = $("#image-box").width();
	var windowHeight = $("#canvas").height();
	var imgBoxHeight = windowHeight-txtBoxHeight;
	
	var imgHeight = max_h;
	var imgWidth = max_w;
	var imgAspect = imgWidth/imgHeight;

	var imgMaxHeight = imgBoxHeight;
	var imgMaxWidth = imgBoxWidth;
	var imgMaxAspect = imgMaxWidth/imgMaxHeight;
		
	if (imgAspect<=imgMaxAspect) {	
		imgMaxWidth = parseInt((max_w * imgMaxHeight) / max_h);
		$("#arrow-box").css("width", imgMaxWidth+"px");		
		boxLeft = (imgBoxWidth - imgMaxWidth)/2;
		$("#arrow-box").css("left", boxLeft+"px");
	};

	if (imgAspect>imgMaxAspect) {			
		$("#arrow-box").css("width", imgMaxWidth+"px");		
		boxLeft = (imgBoxWidth - imgMaxWidth)/2;
		$("#arrow-box").css("left", boxLeft+"px");
	};	
	
	max = $("#image-box img").height();
	$("#arrow-box").css("height", "100%");		
	
}

function findNext(){
	var total = $(".group").length;
	var selected_id = findSelectedGroup();
	var selected_index = parseFloat( selected_id.substr(2) );
	var target = ( selected_index == total-1 ) ? "#g-"+selected_index : "#g-"+(selected_index+1);
	return target;
}

function findPrev(){
	var total = $(".group").length;
	var selected_id = findSelectedGroup();
	var selected_index = parseFloat( selected_id.substr(2) );
	var target = ( selected_index == 0 ) ? "#g-"+selected_index : "#g-"+(selected_index-1);
	return target;
}

function disableButtons(){
	var total = $(".group").length;
	var selected_id = findSelectedGroup();
	var selected_index = parseFloat( selected_id.substr(2) );
	
	if( selected_index == 0 ){
		$("#arrow-left a, #arrow-left-mini a").addClass("disabled");
		$("#arrow-right a, #arrow-right-mini a").removeClass("disabled");
	} else if( selected_index == total-1 ){
		$("#arrow-left a, #arrow-left-mini a").removeClass("disabled");
		$("#arrow-right a, #arrow-right-mini a").addClass("disabled");
	} else {
		$("#arrow-left a, #arrow-left-mini a").removeClass("disabled");
		$("#arrow-right a, #arrow-right-mini a").removeClass("disabled");
	}
}