/***************************



	requires( jquery 1.4, jquery.validate, jquery ui )

	v2.3

	

***************************/



/***************************



	PROMPT DIALOG



***************************/



function mfwAlert(title,text,width)

{

	var $dialog = $('<div></div>')

	.html(text)

	.dialog({

		autoOpen: false,

		modal:true,

		title: title ? title : 'Alert',

		width: width ? width : 'auto',
		
		resizable: false, 
		
		close: function(event, ui)
        {
            $(this).dialog('destroy').remove();
        }


	});

	$dialog.dialog('open');

}



/***************************



	JSON DATA



***************************/



function mfwData(data)

{

	if(data.status == true){							

		if(data.redirect)

			window.location = data.redirect;

		if(data.response)

			mfwAlert(data.title ? data.title : 'Message' ,data.response);

		if(data.id)

			$('#id'+data.id).remove(); 

		if(data.resetForm){

			$('#'+data.resetForm+' input').val('');

			$('#'+data.resetForm+' textarea').val('');

		}		

	}

	else

		mfwAlert(data.message,data.description);	

}



/***************************



	VALIDATION, forms.ajax



***************************/





jQuery.validator.addMethod("defaultText", function(value, element) {

    return this.optional(element) || ( $(element).attr('title') != value );

}, "Change default value.");





function ajaxForms()

{ 

	jQuery.each($('form.ajax'),function(ajaxForm){

		$(this).validate({

			rules: {

				password: {

					minlength: 5	

				},

				confirmPassword: {

					required: true,

					equalTo: "#password"

				},

				confirmemail: {

					required: true,

					equalTo: "#email"

				}

			},	
			submitHandler: function(form){

				$.ajax({

					type: 'post',

					url: $(form).attr('action'),

					data: $(form).serialize(),

					dataType: 'json',

					error:function(){

						mfwAlert('<div class="ui-state-error ui-corner-all"><p class="Alert">Error',' Missing controller :(</p></div>');

					},

					success: function(data){  

						mfwData(data);

					}

				});

			}

		});

	});

}



/***************************



	mfwswf (swfobject);



***************************/



	function mfwswf(location,id,width,height){

		var params = {wmode:"transparent"};

		swfobject.embedSWF(location, id, width, height, "9.0.0",'', '', params);

	}

	



	function mudaFecha(){



	  dia = document.deshas.fechas.value.substr(0, 2);

	  mes = document.deshas.fechas.value.substr(3, 2);

	  ano = document.deshas.fechas.value.substr(8, 2);

	

	  document.deshas.fecha.value = ano+dia+mes;

	

	  dia_ = document.deshas.fechas_vuelta.value.substr(0, 2);

	  mes_ = document.deshas.fechas_vuelta.value.substr(3, 2);

	  ano_ = document.deshas.fechas_vuelta.value.substr(8, 2);

	

	  document.deshas.fecha_vuelta.value = ano_+dia_+mes_;

	

	}	



$(document).ready(function(){

	

	ajaxForms();



	jQuery.each($('form.validate'),function(){

		$(this).validate();

	});



/***************************



	mfwswf (swfobject);



***************************/

	jQuery.each($('.mfwswf'),function(){

		

		var vars = $(this).attr('title').split(',');

		var location = vars[0] ? vars[0] : '';

		var width = vars[1] ? vars[1] : '';

		var height = vars[2] ? vars[2] : '';

		

		mfwswf(location,$('.mfwswf').attr('id'),width,height);

		

	});

		

/***************************



	DATE PICKER 

	

***************************/



	$(".datepicker").datepicker({

		changeMonth: true,

		changeYear: true,

		dateFormat: 'mm/dd/yy'

	});



	$("#fechas").datepicker({

		changeMonth: true,

		changeYear: true,

		dateFormat: 'mm/dd/yy'

	});



	$("#fechas_vuelta").datepicker({

		changeMonth: true,

		changeYear: true,

		dateFormat: 'mm/dd/yy'

	});



	$("#dateClick1").click(function(e) {	

		e.preventDefault();

		$("#fechas").datepicker("show");

	});



	$("#dateClick2").click(function(e) {	

		e.preventDefault();	

		$("#fechas_vuelta").datepicker("show");

	});

	

/***************************



		TABS & ACCORDION

		

***************************/

	

	$("#tabs").tabs({fx: { height: 'toggle', opacity: 'toggle' }});

	$( ".accordion" ).accordion();

	



/***************************

	

	HIDE AND SHOW 

	

***************************/

	

	jQuery.each($('.hider'),function(){

		if(this.checked == true)

			$('.'+this.id).show();

		else

			$('.'+this.id).hide();	

	});

	

	$('a.hider span').html('+');

	

	$('.hider').click(function(e){

		if($(this).attr('type') == "checkbox")

		{

			if(this.checked == true)

			{

				$('.'+this.id).show(); 

			}

			else

			{

				$('.'+this.id).hide();

				if( $('.'+this.id+' input').length>0)

					$('.'+this.id+' input').val('');

			}

		} 

		else

		{

			e.preventDefault();

			$('.'+this.id).slideToggle('400');

			if(jQuery("span", this).html()=='-')

				jQuery("span", this).html('+');

			else

				jQuery("span", this).html('-');

		}			

	});



/***************************



	INPUT DEFAULT TEXT 	 	

 

*************************/



	function defaultText()

	{

		jQuery.each($('.defaultText'),function(n,defaultText){

			var dValue = $(defaultText).attr('title');

			$(defaultText).val(dValue);

			$(defaultText).focus(function(){

				if($(defaultText).val() == dValue)

					$(defaultText).val('');

				$(defaultText).blur(function(inp){

					if($(defaultText).val() == '')

						$(defaultText).val(dValue);

				});

			});

		});

	}

	

	defaultText();



/***************************



	AJAX LINKS

 

*************************/	

	

	$("a.ajaxLink").click(function(e) {

		e.preventDefault();

		if(confirm('Please, confirm to proceed.'))

		{

			$.post(this.href, this.rel ,

				function(data)

				{ 

					mfwData(data);

				}, "json");

		}

	});

	

/***************************

	

	HTML LOADER

	 

*************************/	

	

	jQuery.each($('.htmlLoader'),function(){

		// <div class="htmlLoader" title="{URL DEL HTML PARA CARGAR}" ></div>

		

		$(this).load($(this).attr('title'));

		

	});



/***************************

	

	GALLERY MAKER

	 

*************************/	



	jQuery.each($('.cycle'),function(){

		// <div class="cycle" title="CANTIDAD DE IMAGENES,CARPETA,(WIDTH),(HEIGHT)" ></div>

		

		var imgs = '';

		var vars = $(this).attr('title').split(',');

		var widther = vars[2] ? 'width="'+vars[2]+'"' : '';

		var heighter = vars[3] ? 'height="'+vars[3]+'"' : '';



		for(var n = 1; n <= vars[0]; n++) {

		 	imgs += '<img src="../'+vars[1]+'/'+n+'.jpg" '+vars[2]+' '+vars[3]+' alt="" />';

		}

		$(this).html(imgs);

		

	});

	
	
/***************************
	
	VIDEO display, requires jwplayer + swfobject.js
	 
*************************/	

	function mostrarVideo(url,divId,el){	

		var params = {wmode:"transparent",menu:"false",scale:"noborder",quality:"high",allowfullscreen:"true",allowscriptaccess:"always",backcolor:"0x000000",frontcolor:"0xFFFFFF",overstretch:"true",displaywidth:"320",displayheight:"240",flashvars:"file="+url,autostart:"true",bufferlength:"5"};
		mfwAlert('RedCoach','<div id="'+divId+'"></div>','350' );
		
		var porSiNoEsIngles = el.attr('rel')=="lang" ? '../' : '';
		
		swfobject.embedSWF(porSiNoEsIngles+"videos/player.swf",divId,"320","280",'9.0.124','','',params);
		
	}
	
	$('.videoLink').click(function(e){
		// <a class="videoLink" title="id de la para el video" href="url del video"></a>
		e.preventDefault();
		mostrarVideo($(this).attr('href'),$(this).attr('title'),$(this));
	});
	
	jQuery.each($('.videoDiv'),function(){
		// <div class="videoDiv" title="url del video" id="el id del div... y si" ></div>
		mostrarVideo($(this).attr('title'),$(this).attr('id'));
	});


/***************************

	

	MISC

	 

*************************/	 



	$('.gallery').cycle();

	

	$('#portuguese').click(function(){

	//	mfwAlert('Alert','Coming soon... Proximamente... Em breve');

	});

	

	$('#mapaDest').click(function(e){

		e.preventDefault();

		mfwAlert('Red Coach',$('.section .hide').html());

	});

	

	var sPath = window.location.pathname;

	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

	

	$('.menu a[href="'+sPage+'"]').addClass('current');

	

	if(document.getElementById('txt_desde')!=null){

		if (document.getElementById('txt_desde').value == 2)	

			document.getElementById('txt_hasta').value = 1;

	}

		

});
