
$(function(){
	
	// $('.scrollable').scrollable().autoheight();
	$.setScrollableRootHeight = function(api, index, method) {
		method = method || "animate";
		
		var item = api.getItems().eq(index),
			root = api.getRoot();
		
		root.stop()[method]({ height: item.height() });
	}
	$.fn.autoheight = function() {
		return this.each(function() {
		var api = $(this).data("scrollable");
		
		api.onSeek(function(e, index) {
			$.setScrollableRootHeight(api, index);
		}); 
		
		$(window).load(function() {
			$.setScrollableRootHeight(api, 0, "css");
			api.seekTo(0, 0);
		});
		});
	};
	
	
	// target blank for W3C
	$("a[rel='external']").click(function(){
		this.target = "_blank";
	});
	
	
	// IE6 PNG fix
	if (window['DD_belatedPNG'] !== undefined)
		DD_belatedPNG.fix('img');

	// Ajax Form
	$("#quote-form").ajaxForm({
		"url":"presupuesto.php",
		"type":"POST",
		"dataType":"json",
		"success":function(response,status) {
			if (!response) return;
			if (response.success) {
				$("#quote-form").clearForm();
				$("#quote-success").show();
				setTimeout(toggleForm,1500);
			} else if (response.errorFields) {
				$.each(response.errorFields, function(i,val) {
					$("label[for=lb_"+val+"]").addClass("error");
					$("#quote-error").show();
				});
			} else if (response.errMsg) {
				alert(response.errMsg);
			}
		},
		"beforeSubmit": function(arr, $form, options) {
			$form.find("label").removeClass("error");
			$("#quote-success").hide();
			$("#quote-error").hide();
			return true;
		}
	}); 

	//	Home Projects & Partners - Carousel	
	$("#carousel .scrollable").scrollable({ 
		circular:true,
		next:'#carousel .next', 
		prev:'#carousel .prev'
	}).autoheight();
	
	// testimonios
	$(".testimoniales .scrollable").scrollable({ 
		preload:true,
		preloadImage:'/image/loading.gif',
		circular:true,
		next:'.testimoniales .next', 
		prev:'.testimoniales .prev'
	});


	// accordion servicios
	$(".accordion h5:first").addClass("active");
	$(".accordion ul:not(:first)").hide();

	$(".accordion h5").click(function(){
		var rel = $(this).attr('rel');
		
		$(this).next('ul').slideToggle().siblings('ul:visible').slideUp();
		$(this).toggleClass('active');
		$(this).siblings('h5').removeClass('active');
		
		$('.accordion ul li:first-child').addClass('current');
		$('.sh:visible').hide();
		$('.'+rel).fadeIn('fast'); 
		
	});
	
	$('.sh').hide();
	$('.sh:first').show();
	
	$('.accordion li a').bind('click', function(e) { 
		var rel = $(this).attr('rel');
		
		if($('.sh').is(':visible') == true) {
			$('.accordion li').removeClass('current');
			$(this).parent().addClass('current');
			$('.sh:visible').hide();
			$('.'+rel).fadeIn('fast'); 
		}
		else {
		}
		return false;
	});

	if ($(".accordion h5").length && window.location.hash) {			
		var hash = window.location.hash.replace('#','');
		if (hash) {
			var $h = $(".accordion h5."+hash);
			if ($h.length>0) $h.first().click();
		}
	}
	

	// Contacto tabs
	$(".tabs").tabs(".panes > div");

	// Team
	$('.team li').hover(function(){
		$(".cover", this).stop().animate({top:'164px', left:'184px'},{queue:false,duration:400});
	}, function() {
		$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:400});
	});
	
	
	// Form presupuesto
	$('.btn-quote').click(toggleForm);


	// Galeria de clientes
	$('.client-list li a').each(function(i, el) {
		$(el).find('img').clone().appendTo(el).addClass('color').css('opacity', 0);
	});

	$('.client-list li').live('mouseenter', function(e) {
		$(this).find('img.color').show().animate({'opacity': 1}, {queue: false});
	});

	$('.client-list li').live('mouseleave', function(e) {
		$(this).find('img.color').animate({'opacity': 0}, {queue: false});
	});

	$('.clients #subnav a').click(function(e) {
		e.preventDefault();
				
		// Change State
		$(this).parents('ul').find('.current-page').removeClass('current-page');
		$(this).parent('li').addClass('current-page');

		// Sort it out
		var list = $($(this).attr('href'));
		sortIt('#client-list', list);
	});


	// input placeholder support

	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
	
	setInterval( slideSwitch, 2600 );
	
});



//	Home Partners - fadein fadeout slide
function slideSwitch() {
	var $active = $('#slide img.active');
	if ( $active.length == 0 ) $active = $('#slide img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#slide img:first');

	// random order
	var $sibs  = $active.siblings();
	var rndNum = Math.floor(Math.random() * $sibs.length );
	var $next  = $( $sibs[ rndNum ] );

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}


function toggleForm() {
	$('#quote').slideToggle(700, 'easeOutExpo');
	$('.btn-quote').toggleClass('open');
	if ($('.btn-quote').hasClass('open')){
		$('.btn-quote').text('Cerrar');
	}else{
		$('.btn-quote').text('Pídenos presupuesto');
	};
}

