/*
/*	Dynamic design functions and onLoad events
/*	----------------------------------------------------------------------
/* 	Creates added dynamic functions and initializes loading.
*/
var $j = jQuery.noConflict();
//---- google analitycs for button submits
function CallPage( $page, $pageAnalitycs ){ //alert( "CallPage("+ $page +","+ $pageAnalitycs+");" );
	/*pageTracker._trackPageview($pageAnalitycs);
	if( $page ){
		location.href='./'+ $page +'/';
	}
	*/
}

// ======================================================================
//
//	On document ready functions
//
// ======================================================================

jQuery(document).ready(function() {
	// initialise slide show (Cycle plugi-n)
	// -------------------------------------------------------------------
		//header
	$j('#slide-header .slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		next: '#arrow_next',
		prev: '#arrow_prev',
		pager:  '#nav',
		slideExpr: 'img'
	});
	//modal window
	$j("a#modal-form").fancybox({
		'padding'		: 0,
		'overlayShow'	: true,
		'scrolling'     : 'no',
		'frameWidth': 264,
		'frameHeight': 180,
		'autoDimensions':true,
		'hideOnContentClick': false,
		'hideOnOverlayClick': false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	//
	formContact();
	formBrochure();

});

function formBrochure(){ //alert('formBrochure');
	/* Brochure form */
	 var $frm = "#formDownload";
	// input lable replacement -------------------------------------------
	$j( $frm + " label.overlabel").overlabel();	 //jQuery( $frm + " :input.textInput").val("test@todo.com");
	
	$j.metadata.setType("attr", "validate");
	// form validation ---------------------------------------------------
	$j( $frm ).validate({
		messages: {
			name_b: "!", email_b: "!"
		},
		submitHandler: function(form) { // form is valid, submit it
			ajaxSendForm(form, $frm, $j( $frm + " #URL").val() );
			return false;
		}
	});
}

function formContact(){ //alert('formContact');
	/* contact form */
	 var $frm = "#formContact";
	// input lable replacement -------------------------------------------
	$j( $frm + " label.overlabel").overlabel();	//jQuery( $frm + " :input.textInput").val("test@todo.com");
	$j.metadata.setType("attr", "validate");
	// form validation ---------------------------------------------------
	$j( $frm ).validate({
		messages: {
			name: "!", lastname: "!", phone: "!", provincia:"!", email: "!"
		},
		submitHandler: function(form) { // form is valid, submit it
			ajaxSendForm(form, $frm, $j( $frm + " #URL").val() );
			return false;
		}
	});
}


// ======================================================================
//	Proceses functions
// ======================================================================
// -------------------------------------------------------------------     
function ajaxSendForm(theForm, $idForm, $file) { //alert( "ajaxSendForm("+ theForm +", "+ $idForm +", "+ $file +")"); return; 
	$j($idForm+' button').hide();
	$j($idForm+' #loader').fadeIn();	
	var formData = $j(theForm).serialize(),
		$URL = $file,
		note = $j( $idForm+'-msg'),
		result;
	note.hide();
	//alert( "formData:"+formData ); return;
	$j.ajax({
		type: "POST",
		url: $URL,
		data: formData,
		success: function(response) { //alert('response' + response);
			$j($idForm+' #loader').hide();
			var $data = response.split(',');
			if ( $data[0] == 1 ) { //alert('$data[0]='+$data[0]);
				if( $data[1] == "location" ){
					document.cookie='ebio-boletos=' + $data[3];
					document.location= $data[2];
				}else{
					$j(theForm).animate({opacity: 0},'fast',function(){ 
					$j(theForm).remove();
					note.fadeIn();
					});
				}
			}else{ //alert("else="+response);
				$j($idForm+' button').fadeIn();
				var i = setInterval(function() {
					if ( $j($idForm+' button').is(':visible') ) {
						result = response;
						note.html(result);
						note.fadeIn('slow');
						clearInterval(i);
					}
				}, 40);
			}
		}
	});
	return false;
}

