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



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

	

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



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



	PROMPT DIALOG



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



function mfwAlert(title,text)

{

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

	.html(text)

	.dialog({

		autoOpen: false,

		modal:true,

		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[type=text]').val('');

			$('#'+data.resetForm+' input[type=select]').val('');

			$('#'+data.resetForm+' input[type=checkbox]').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){

				$.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);

					}

				});

			}

		});

	});

}



$(document).ready(function(){

	

	ajaxForms();



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

		$(this).validate();

	});



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

		e.preventDefault();

		mfwAlert('Alert','Coming soon... Proximamente... 即将推出');

	});

		

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



	DATE PICKER 

	

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



	$(".datepicker").datepicker({

		changeMonth: true,

		changeYear: true,

		dateFormat: 'mm-dd-yy'

	});

	

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

	

	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");

		}

	});

	

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

	

	MISC

	 

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

	

	$('.cycle').cycle({timeout:  10000});

	$('.busCycle').cycle();



	$('.notice .galeria').cycle({ 

	    timeout:  5000,

		pager:  '#nav', 

		next:   '#galNext', 

	    prev:   '#galPrev',

	    pause:   1,

		// callback fn that creates a thumbnail to use as pager anchor 

		pagerAnchorBuilder: function(idx, slide) {

			var src = $(slide).attr('src').replace('notices/','notices/thumbs/');

			return '<li><a href="#"><img src="' + src + '" /></a></li>';

		},

		before: function (){

			if(this.alt!=''){

				$('.galeriaText').remove();

				$('#nav').before('<div class="galeriaText" style="display:none">'+this.alt+'</div>');

				$('.galeriaText').fadeIn();

			}

			else{

				$('.galeriaText').fadeOut(function(){$('.galeriaText').remove();});

				

			}

		}

	});

	

	function mostrarVideo(url){	

		

		var swf = new SWFObject(site.content+"uploads/videos/player.swf","slider","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("slider");

		

	}

	

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

		e.preventDefault();

		mostrarVideo($(this).attr('href'));

	});

	

	/* MENU DESPLEGABLE */

	

	 $("#header .menuLi").mouseover(function(){  

	 var h = jQuery("a", this).length*50+40; 

         $(this).stop().animate({height:h+'px'},{queue:false, duration:600, easing: 'easeOutSine'})  

     });  

   

     //When mouse is removed  

     $("#header .menuLi").mouseout(function(){  

         $(this).stop().animate({height:'40px'},{queue:false, duration:600, easing: 'easeOutSine'})  

     });  

	

	$('.button').button();

	

//	$('#englishLang').click(function(e){

//		e.preventDefault();

//		mfwAlert('Alert','English release coming soon!');

//	});

	

	$('.tooltip').tooltip({showURL: false});

	



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



		TABS & ACCORDION

		

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

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

	$('.acordion').accordion({autoHeight: false});

	if($('.scroller').length>0)
		$('.scroller').tinyscrollbar();
	

});
