/***************************

	requires( jquery 1.5, jquery.validate 1.8, jquery ui 1.8, swfobject.js)
	v2.5
	
***************************/

/***************************

	PROMPT DIALOG

***************************/

function mfwAlert(title,text)
{
	var $dialog = $('<div></div>')
	.html(text)
	.dialog({
		autoOpen: false,
		modal:true,
		width:'auto',
		title: title ? title : 'Alert'
	});
	$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"
				}
			},	
			submitHandler: function(form){
				var jqxhr = $.ajax({ url: $(form).attr('action'), data: $(form).serialize(), dataType: 'json', type: 'POST' })
					.success(function(data) { mfwData(data); })
					.error(function() {mfwAlert('<div class="ui-state-error ui-corner-all"><p class="Alert">Error',' Missing controller :(</p></div>'); });
			}
		});
	});
}


$(document).ready(function(){
	
	jQuery.each($('form.validate'),function(){
		$(this).validate();
	});
	
/***************************

	ANIMACION
		
***************************/

/***************************

	DATE PICKER 
	
***************************/

	$(".datepicker").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'mm-dd-yy'
	});

/***************************

		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('600');
			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){	
		
		var swf = new SWFObject("videos/player.swf",divId,"575","335",'9.0.124','false');
		
		swf.addParam('menu','false'); swf.addParam('scale','noborder'); swf.addParam('quality','high');
		swf.addParam('allowfullscreen','true'); swf.addParam('allowscriptaccess','always'); swf.addParam('wmode','transparent');
		swf.addParam('backcolor','0x000000'); swf.addParam('frontcolor','0xFFFFFF'); swf.addVariable('overstretch','true');
		swf.addVariable('displaywidth','575'); swf.addVariable('displayheight','335'); swf.addVariable('file',url);
		swf.addVariable('autostart','true'); swf.addVariable('bufferlength','5');
			  
		swf.write(divId);
		
	}
	
	$('.videoLink').click(function(e){
		// <a class="videoDiv" title="id de la para el video" href="url del video"></a>
		e.preventDefault();
		mostrarVideo($(this).attr('href'),$(this).attr('title'));
	});
	
	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
	 
*************************/	
	$('.indexGallery').cycle({
		fx:     'scrollHorz'
	},'slow');
	
	$('#ingreso').click(function(e){
		e.preventDefault();
		mfwAlert('Victoria del Agua','Proximamente...');
	});
	
	$('.sections li').hover(function(){
		$(this).find('span img').fadeOut(1000);
	}, function(){
		$(this).find('span img').fadeIn(1000);
	});	
	
	$('.verGallery').click(function(e){
		e.preventDefault();
		$('#content,.casas,.info,.infoTxt').fadeToggle();
		if($('.verGallery.bottom').html()=='VOLVER'){
			$('.verGallery.bottom').html('ver galeria');
		}
		else
			$('.verGallery.bottom').html('VOLVER');
		if($('.verGallery.top').html()=='VOLVER'){
			$('.verGallery.top').html('Quitar menu');
		}
		else
			$('.verGallery.top').html('Mostrar Menu');
	});
	
	$('.info').fadeIn(2000);
	
	$('#scrollbar1').tinyscrollbar();
		
	$('.linkModal').click(function(e){
		e.preventDefault();
		mfwAlert($(this).attr('title'),$($(this).attr('href')).html());
	});
	
});
