$(document).ready(function() {
	/**
	 * TOP SLIDER
	 */
	if(!$('#wraper').hasClass('empresa')) {
		$('.kwicks').kwicks({
			//min: 150,
			min: 240,
			sticky : true,
			duration : 700
		});
		
		$('li:not(.active) .faixa-h').stop().animate({opacity : 0}, 500);
		$('li:not(.active) .bg').stop().animate({width : 0}, 500);
		$('li:not(.active) .faixa-v').stop().animate({/*opacity : 1,*/ height: 451}, 600);	
		
		$('li.active .faixa-h').stop().animate({opacity : 1}, 500);
		//$('li.active .bg').stop().animate({/*opacity : .75,*/ width: 850}, 600);
		$('li.active .bg').stop().animate({/*opacity : .75,*/ width: 760}, 600);
		$('li.active .faixa-v').stop().animate({/*opacity : 0,*/ height: 0}, 600);
		
	    $(".kwicks li").hover(function() {
	    	elmParent = $(this).parent();
	    	$('li:not(.active) .bg', elmParent).stop()
	        .animate({
	            //opacity : 0,
	            width: 0
	        }, 600);
	    	$('li:not(.active) .faixa-v', elmParent).stop()
	        .animate({
	            //opacity : 1,
	            height: 451
	        }, 500);
	    	$('li:not(.active) .faixa-h', elmParent).stop()
	        .animate({
	            opacity : 0
	        }, 600);
	    	
	    	elmImgWpc = $('#wpc img');
	    	if($(this).attr('id') == 'wpc') {
	    		elmImgWpc.stop().animate({left : 0}, 600);
	    	} else {
	    		//elmImgWpc.stop().animate({left : -351}, 600);
	    		elmImgWpc.stop().animate({left : -260}, 600);
	    	}
	        $('li.active .bg', elmParent).stop()
	        .animate({
	           // opacity : .75,
	            //width: 850
	        	width: 760
	        }, 600);
	        $('li.active .faixa-v', elmParent).stop()
	        .animate({
	            //opacity : 0,
	            height: 0
	        }, 500);
	        $('li.active .faixa-h', elmParent).stop()
	        .animate({
	            opacity : 1
	        }, 600);
	    });
	}
	
    /**
     * SLIDER INTERNAS
     */
    $('.internas:not(#aplicacoes) .slider').tabbed_interface({
    	switch_duration   : 2500,
    	auto_switch_delay : 5000,
    	auto_switch       : true, 
    	contents_tag      : 'img',
    	specific_list     : '.tabs'
    });
    
    // carousel dos sliders
	$('.carousel').each(function() {
		$(this).jcarousel();
	});
    
    // slider Ecompound/Aplicacoes
    $('#aplicacoes .slider').tabbed_interface({
    	switch_duration   : 2500,
    	auto_switch_delay : 5000,
    	auto_switch       : false, 
    	contents_tag      : 'figure',
    	//carousel          : true,
    	specific_list     : '.tabs'
    });
        
    // carousel dos sliders
	$('.carousel').each(function() {
		$(this).jcarousel({
			//scroll: 1
			//wrap: 'last'
		});
	});
    
    
    /**
     * CONTATO
     */
    $('#contato form.ajax').submit(function(e) {
		e.preventDefault();		
		processAjaxForms($(this), $('#alert'));
	});
    
    /**
     * REFLORESTAMENTO - SUSTENTABILIDADE
     */
    $('.pin').hover(function() {    	
    	$(this).prev().css('visibility', 'visible').stop().animate({opacity:1}, 500);
    }, function() {
    	$(this).prev().css('visibility', 'hidden').stop().animate({opacity:0}, 500);
    });
    
});

/**
 * Processa forms usando ajax
 * @param form
 * @param alertElement
 * @return
 */
function processAjaxForms(form, alertElement) {
	if(typeof(alertElement) == 'undefined') {
		alertElement = $('#alert');
	}
	
	$.ajax({
		type       : form.attr('method'),
		data       : form.serialize(), 
		url        : form.attr('action'),
		dataType   : 'json',
		beforeSend : function() {
			appendLoader(alertElement);
			$('#submit', form).attr('disable', 'disable');
		},
		complete   : function() {
			$('#submit', form).removeAttr('disable');
		},
		success    : function(data) {
			if(data) {
				if(data.message) {
					alertElement.removeClass().addClass(data.type).html(data.message);
				}
				if(data.type == 'success') {
					form[0].reset;
					unmarkErrorFields(form);					
				} else if(data.type == 'error') {
					if(data.errorFields) {						
						unmarkErrorFields(form);
						markErrorsFields(form, data.errorFields);
						if(data.errorFields.length == 1 && data.formated[0].errors.error.recordFound) {							
							alertElement.html('Erro! Este e-mail já está cadastrado');
						}
					}
				}
			}
		},
		error : function() {
			alertElement.removeClass().addClass('error').html('Erro! Ocorreu um erro na aplicação');
		}
	});
}

/**
 * Append loader
 * 
 * @param DOMElement
 * @return void
 */
function appendLoader(element, text, img) {
	if(typeof(text) == 'undefined') {
		text = 'Carregando...';
	}
	if(typeof(img) == 'undefined') {
		img = $('<img/>').attr('src', site_url+'/imgs/loader.gif');
	}
	element.removeClass().addClass('loading').html(text).append(img);
	
}

/**
 * Marca os campos com erro
 * 
 * @param DOMElement form
 * @param JSON       objErrors
 * 
 * @return void
 */
function markErrorsFields(form, objErrors) {
	form.find('input, textarea, select').each(function() {		
		sInputId = $(this).attr('id');
		for (i = 0; i <= objErrors.length; i++) {
			if(objErrors[i] == sInputId) {
				$(this).addClass('error-border');
				$(this).parent('li').addClass('error');
			}
		}
	});
}

/**
 * Desmarca os campos com erro
 * 
 * @param DOMElement form Especifica um form
 * 
 * @return void
 */
function unmarkErrorFields(form) {	
	if(typeof(form) !== undefined) {		
		form.find('.error-border').removeClass('error-border');
		form.find('li.error').removeClass('error');
	} else {
		$('.error-border').removeClass('error-border');
		$('li.error').removeClass('error');
	}
}
