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(text)
	.dialog({
		autoOpen: false,
		modal:true,
		title: title
	});
	$dialog.dialog('open');
}

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){  
						if(data.status == false)
							mfwAlert(data.message,data.description);
						else
						{
							if(data.redirect)
								window.location = data.redirect;
							if(data.response)
								mfwAlert('',data.response);
							if(data.id == "all")
								form.reset();
						}
					}
				});
			}
		});
	});
}

function notice_information()
{
	$('#notice_information').css('display','block');
	$('#notice_comments').css('display','none');
}

function notice_comments()
{
	$('#notice_information').css('display','none');
	$('#notice_comments').css('display','block');
}

/*****************/	
/* VALIDATIONS */	
/*****************/	
	
$(document).ready(function(){
		
		$('form.ajaxOrder').submit(function(e){
			
			e.preventDefault();
			
			jQuery.each($('#shown_notices option'),function(i, option){ 
				$(option).attr("selected","selected");
			});
			
					$.ajax({
						type: 'post',
						url: $('form.ajaxOrder').attr('action'),
						data: $('#shown_notices').serialize(),
						dataType: 'json',
						success: function(data){  
							if(data.status == false)
								mfwAlert(data.message,data.description);
							else
							{
									window.location = data.redirect;
							}
						}
					});
		
		});
		
		$('#right-arrow').click(function() {
			return !$('#not_shown_notices option:selected').remove().appendTo('#shown_notices'); 
		}); 
		
		$('#left-arrow').click(function() {  
			return !$('#shown_notices option:selected').remove().appendTo('#not_shown_notices');  
		});
  
		$('#up-arrow').bind('click', function() {
			$('#shown_notices option:selected').each( function() {
				var newPos = $('#shown_notices option').index(this) - 1;
				if (newPos > -1) {
					$('#shown_notices option').eq(newPos).before("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
					$(this).remove();
				}
			});
		});
		$('#down-arrow').bind('click', function() {
			var countOptions = $('#shown_notices option').size();
			$('#shown_notices option:selected').each( function() {
				var newPos = $('#shown_notices option').index(this) + 1;
				if (newPos < countOptions) {
					$('#shown_notices option').eq(newPos).after("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
					$(this).remove();
				}
			});
		}); 
	
	function ajaxLink(){
		$("a.ajaxLink").click(function(e) {
			e.preventDefault();
			if(confirm('Please, confirm to proceed.'))
			{
				$.post(this.href, this.rel ,
					function(data)
					{ 
						if(data.status == true){
							if(data.id)
								$('#id'+data.id).remove();
							if(data.redirect)
								window.location = data.redirect;
							/* SÓLO PARA AVANZAR */
							if(data.attachments)
							{
								$('#attachLink').remove();
								$('<strong>Links para copiar en el editor de texto.</strong><br /><br />').appendTo($('#tdTexto'));
								for (i=0;i<data.attachments.length;i++){
									$('<span>'+data.attachments[i]+'</span><br />').appendTo($('#tdTexto'));									
								} 
							}
						}
						else
							mfwAlert(data.message,data.description);
					}, "json");
			}
		});
	}
	
	ajaxLink();
	
	jQuery.each($('form.validate'),function(id, formVal){ 
		$(formVal).validate();
	});
/*****************/	
/* ADDTHIS CODE  */	
/*****************/	
	
	jQuery.each($('.coupon .header'),function(slink){ 
		var shareLinks = '<div class="addthis_toolbox addthis_default_style"><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_favorites"></a><span class="addthis_separator">|</span><a href="http://www.addthis.com/bookmark.php?v=250&amp;username=30efe" class="addthis_button_expanded">More</a></div>';																		
		if(slink == 0)
			$('.coupon .header').append(shareLinks);
	});

	ajaxForms();
	
/*****************/	
/* DATE PICKER   */	
/*****************/		

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

/*****************/	
/* TABS */	
/*****************/		
	
	$(".tabs").tabs({ fx: { opacity: 'toggle', duration: 'fast' } });
	
/*****************/	
/* TOOLTIP */	
/*****************/		
	
	$('.tooltip').tooltip({showURL: false});
	$('.admin-tooltip').tooltip({showURL: false});
	
	$(".adminTable tbody tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$(".adminTable tbody tr:even").addClass("alt");
	
/*************************/	
/* 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();
	
/*************************/	
/* wysiwyg			 	*/	
/***********************/
	
	$('.wysiwyg').htmlarea();

/*************************/	
/* cycle modal			*/	
/***********************/
	
/*	function cycleModal()
	{
		var $dialog = $('<div class="galeria"></div>')
		.html($('.notice-imgs').html().replace(/thumb/g,'big'))
		.dialog({
			autoOpen: false,
			modal:true,
			title: 'Galeria',
			minWidth: 660,
			minHeight: 600
		});
		$dialog.dialog('open');
		$('.galeria').cycle();
	}
	
	$(".notice-img").click(function(e) {
		e.preventDefault();
		cycleModal();
	});
*/	

	$('.galeria').cycle();
	
	$('.notice-img').lightBox();
	
	$("#type").change(function(e) {
		if($(this).val()=='testimonio'){
			$('#file').removeClass('required');
		}else{
			$('#file').addClass('required');	
		}
	});
	
	$('#category').change(function(e) {
								   
		$.post(site.path+'admin/noticeAvailable', 'id_category='+$(this).val() ,
			function(data)
			{
				if(data && data.status==true || ($('#actual_category').val() ==  $('#category').val()  ))
				{ 
					$.post(site.path+'admin/categoryAttachments', 'id_category='+$('#category').val() ,
						function(data)
						{	
							if(data)
							{ 
								if(data.attachments.length!=0)
								{
									$('.attLinks').remove();
									$('<div class="attLinks"><strong>Links para copiar en el editor de texto.</strong><br /><br /></div>').appendTo($('#tdTexto'));
									for (i=0;i<data.attachments.length;i++){
										$('<span class="attLinks">'+data.attachments[i]+'</span><br />').appendTo($('#tdTexto'));									
									} 
								}
								else
								{
									$('.attLinks').remove();
								}								
							}
						}, "json");
				}
				else
				{
					if(data.message)
					{
						mfwAlert('',data.message);
					}
				}
			}, "json");
		
		
	});
	
	$('.mfwAlertLink').click(function(e){
		e.preventDefault();
		mfwAlert($('#'+$(this).attr('rel')).attr('title'),$('#'+$(this).attr('rel')).html());
	});
	
	$('.mediaLink').tooltip({showURL: false,  fade: 250  });
	
});
