function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}

function mfwAlert(title,text)
{
	var $dialog = $('<div></div>')
	.html(title+' '+text)
	.dialog({
		autoOpen: false,
		modal:true,
		title: 'Alert'
	});
	$dialog.dialog('open');
}

function defaultTextSubmit()
	{
		jQuery.each($('input.defaultTextSubmit'),function(n,defaultText){
			var dValue = $(defaultText).attr('title');
			$(defaultText).val(dValue);
		});
	}

function mfwData(data)
{
	if(data.status == true){							
		if(data.redirect)
			window.location = data.redirect;
		if(data.description)
			mfwAlert(data.message,data.description);
		if(data.response)
			mfwAlert('',data.response);
		if(data.resetForm){
			$('#'+data.resetForm+' input').val('');
			$('#'+data.resetForm+' textarea').val('');
			defaultTextSubmit();
		}
		else if(data.id){
			$('#id'+data.id).remove(); }
	}
	else
		mfwAlert(data.message,data.description);	
}

/*****************/	
/* VALIDATION  */	
/***************/	

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(){
	
	$("a.ajaxLink").click(function(e) {
		e.preventDefault();
		if(confirm('Please, confirm to proceed.'))
		{
			$.post(this.href, this.rel ,
				function(data)
				{ 
					mfwData(data);
				}, "json");
		}
	});

	ajaxForms();

	$("form").validate();

	$('.innerfade').innerfade({containerheight: '220px' });
		
	$( ".accordion" ).accordion();

/*****************/	
/* DATE PICKER   */	
/*****************/		

	$(".datepicker").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'mm-dd-yy'
	});

/*****************/	
/* TABS */	
/*****************/		
	
	$("#tabs").tabs({fx: { height: 'toggle', opacity: 'toggle' }});
	$('#tabs *,.ui-accordion *').removeClass('ui-corner-bottom ui-corner-top ui-corner-all');
	
/*****************/	
/* tooltip */	
/*****************/		
	
//	$('.tooltiper[title]').tooltip();

/*****************/	
/* 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($('input.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();
		
   //***********************/	
  //	 cdsSlideshow  	   /	
 //***********************/
//

		var cantidadElementos = $('#cdSlideshow .cdCover').length;
		var posicionActual=7;
		var cantidadAMover=0;
		
		$('#content a.arrow').click(function(){ 
		
			if($(this).hasClass('right')){  
				if(cantidadElementos - posicionActual > 7){		
					$('#cdSlideshow ul').animate({
						right: '+=889'
						}, 
						{ duration: 1200, specialEasing: 
							{
								right: 'easeInQuad'
							}
						}
					);
					posicionActual += 7;
				} else {
					cantidadAMover = cantidadElementos - posicionActual;
					$('#cdSlideshow ul').animate({
							right: '+='+(cantidadAMover * 127)
						}, 
						{ duration: 800, specialEasing: 
							{
								right: 'easeInQuad'
							}
						}
					);
					posicionActual += cantidadAMover;
				}					
			}else if ((posicionActual > 0)){ 
				if(posicionActual > 14){		
					$('#cdSlideshow ul').animate({
						right: '-=889'
						}, 
						{ duration: 1200, specialEasing: 
							{
								right: 'easeInQuad'
							}
						}
					);
					posicionActual -= 7;
				} else {
					cantidadAMover = posicionActual-7;
					$('#cdSlideshow ul').animate({
							right: '-='+(cantidadAMover * 127)
						}, 
						{ duration: 800, specialEasing: 
							{
								right: 'easeInQuad'
							}
						}
					);
					posicionActual -= cantidadAMover;
				}					

			}		
		});
//		
//		$('#cdSlideshow .cdCover').mouseover(function() {  
//			$(this).animate({
//				width: '+20'
//				}, { duration: 700, specialEasing: {
//				 right: 'easeInQuad'
//				 }
//				}
//			);		
//		});
//		
   //***********************/	
  //	 			 	   /	
 //***********************/
 
 
	$('a.demoPlayer').click(function(e) {
		e.preventDefault();
		var videoDialog = '<div id="ply"></div>';
		var dialog = $('<div></div>')
		.html(videoDialog)
		.dialog({
			autoOpen: false,
			modal: true,
			height: 321,
			width: 405,
			title: 'Playing: ' + this.title,
			beforeclose: function(event, ui) { 
				$('#ply').remove();
			}

		});
		var tVideo = new SWFObject(site.content+'/images/player.swf','ply','376','256','9','#ffffff');
		tVideo.addParam('allowfullscreen','true');
		tVideo.addParam('autoPlay','true');
		tVideo.addParam('allowscriptaccess','always');
		tVideo.addParam('wmode','opaque');
		tVideo.addVariable('file',this.href);
		tVideo.write('ply');			
		dialog.dialog('open');
	});
	
	$(".accordion").accordion();

	$('#slider1').tinycarousel({ animation: false });
	
	$(".oembed").oembed(null, 
                        {
                        embedMethod: "append", 
                        vimeo: { autoplay: true, maxWidth: 654, maxHeight: 532}                 
                        });

});
