/**************************************************************/
$(document).ready(function(){
	$("a,img").click(function(){					
		if($(this).attr("target") == "_blank"){
	        $("#jquery_jplayer").jPlayer("stop");  
		} 
	});

	/*var playItem = 0;

	var myPlayList = [
		{name:"",mp3:"musica/BlackEyedPeas-IGottaFeeling.mp3",ogg:""},
		{name:"",mp3:"musica/AxeBahia-OndaOnda.mp3",ogg:""},
		{name:"",mp3:"musica/bob_sinclair_love_generation.mp3",ogg:""}
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		oggSupport: false
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	function displayPlayList() {
		$("#jplayer_playlist ul").empty();
		for (i=0; i < myPlayList.length; i++) {
			var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
			listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a></li>";
			$("#jplayer_playlist ul").append(listItem);
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
				$(this).blur();
				return false;
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
	}*/
	
});

function PlayMusic(mp3){
	$(document).ready(function(){
		$("#jquery_jplayer").jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", mp3, "").jPlayer("play");
			},
			volume: 25,
			oggSupport: false
		})
		.jPlayer("onSoundComplete", function() {
			this.element.jPlayer("play");
		});	
	});	
}


/****************************************************************/




function ShowDialog(id){
    $.ui.dialog.defaults.bgiframe = true;
    $(function() {       
        $("#dialog_"+id).dialog({  
            bgiframe: true, 
            modal: true,
            draggable: false,
            resizable: false,
            closeOnEscape: true,
            buttons: {
                Ok: function() {
                    $(this).dialog('destroy');
                }
            },
            close: function() {
                    $(this).dialog('destroy');            
            }                                        
        });
    });
}

/*Formulario de contacto desde columna derecha o izquierda*/
function validarFormContactoColumna(){
	//var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	msg = "";
	
	if($("#txtNombre").val() == ""){
		msg += "Complete su Nombre<br>";
	}	
		
	if($("#txtTelefono").val() == ""){
		msg += "Complete su Tel&eacute;fono<br>";
	}		
	/*if($("#txtEmail").val() == ""){
		msg += "Complete su Email<br>";
	}	
	if(!emailReg.test($("#txtEmail").val())){
		msg += "Email no valido<br>";
	}*/
	if($("#txtConsulta").val() == ""){
		msg += "Complete su Consulta<br>";
	}	

	if(msg != ""){
		//Hubo un error	
		ShowDialog("form_contacto");
		$(".ui-dialog-titlebar").html("Validación");		
		$("#dialog_form_contacto").html(msg);
	}else{
		$("#btn_EnviarConsulta").hide();
		$("#loading_columna").show("normal");		
		
		/////////se envia el mail////////
		$.ajax({ 
			 url: "includes/mail.php", 
			 type: "POST",
			 data: "sec=form_columna&txtNombre=" + $("#txtNombre").val() + "&txtTelefono=" + $("#txtTelefono").val() 
			 		+ "&txtConsulta=" + $("#txtConsulta").val(),
			 error:function(xhr, ajaxOptions, thrownError){
			 	alert("Error");			
				var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				$(".ui-dialog-titlebar").html("Error");
				$("#dialog_form_contacto").html(obj.msg); 
			 },
			 success: function(r){
			 	$("#loading_columna").hide();
				$("#btn_EnviarConsulta").show();
				 
				 var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				if(obj.success){
	        		$(".ui-dialog-titlebar").html("Envio Correcto");
				}
				else{
					$(".ui-dialog-titlebar").html("Error al enviar el email");
				}
					
				$("#dialog_form_contacto").html(obj.msg);
				limpiarCamposFormColumna();
      		}
		});
		
	}
}

function limpiarCamposFormColumna(){
	$("#txtNombre").val("");
	$("#txtTelefono").val("");	
	$("#txtConsulta").val("");
}

/*formulario de contacto desde la pagina de ventas de un producto*/
function validarFormContactoVentas(id_producto){
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	msg = "";
	
	if($("#txtNombre").val() == ""){
		msg += "Complete su Nombre<br>";
	}	
	
	if($("#txtApellido").val() == ""){
		msg += "Complete su Apellido<br>";
	}	
		
	if($("#txtTelefono").val() == ""){
		msg += "Complete su Telefono<br>";
	}	

	if($("#txtEmail").val() == ""){
		msg += "Complete su Email<br>";
	}	
	if(!emailReg.test($("#txtEmail").val())){
		msg += "Email no valido<br>";
	}
	if($("#txtConsulta").val() == ""){
		msg += "Complete su Consulta<br>";
	}	

	if(msg != ""){
		//Hubo un error	
		ShowDialog("form_contacto");
		$(".ui-dialog-titlebar").html("Validación");		
		$("#dialog_form_contacto").html(msg);
	}else{
		$("#btn_EnviarConsulta").hide();
		$("#loading_columna").show("normal");		
		
		/////////se envia el mail////////
		$.ajax({ 
			 url: "includes/mail.php", 
			 type: "POST",
			 data: "sec=form_prd_venta&txtNombre=" + $("#txtNombre").val() + "&txtEmail=" + $("#txtEmail").val() + "&txtApellido=" + $("#txtApellido").val() 
					 + "&txtTelefono=" + $("#txtTelefono").val() + "&txtConsulta=" + $("#txtConsulta").val()  + "&id_producto=" +  id_producto,
			 error:function(xhr, ajaxOptions, thrownError){
			 	alert("Error");			
				var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				$(".ui-dialog-titlebar").html("Error");
				$("#dialog_form_contacto").html(obj.msg); 
			 },
			 success: function(r){
			 	$("#loading_columna").hide();
				$("#btn_EnviarConsulta").show();
				 
				 var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				if(obj.success){
	        		$(".ui-dialog-titlebar").html("Envio Correcto");
				}
				else{
					$(".ui-dialog-titlebar").html("Error al enviar el email");
				}
					
				$("#dialog_form_contacto").html(obj.msg);
				limpiarCamposFormVentas();
      		}
		});
		
	}
}

function limpiarCamposFormVentas(){
	$("#txtNombre").val("");
	$("#txtEmail").val("");	
	$("#txtConsulta").val("");
	$("#txtTelefono").val("");
}


/*Formulario de contacto desde la pagina de contacto.php*/
function validarFormContacto(){
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	msg = "";
	
	if($("#txtNombre").val() == ""){
		msg += "Complete su Nombre<br>";
	}	
	if($("#txtApellido").val() == ""){
		msg += "Complete su Apellido<br>";
	}	
	if($("#txtTelefono").val() == ""){
		msg += "Complete su Telefono<br>";
	}
	if($("#txtEmail").val() == ""){
		msg += "Complete su Email<br>";
	}	
	if(!emailReg.test($("#txtEmail").val())){
		msg += "Email no valido<br>";
	}
	if($("#txtConsulta").val() == ""){
		msg += "Complete su Consulta<br>";
	}	

	if(msg != ""){
		//Hubo un error	
		ShowDialog("form_contacto");
		$(".ui-dialog-titlebar").html("Validación");		
		$("#dialog_form_contacto").html(msg);
	}else{
		$("#btn_EnviarConsultaContacto").hide();
		$("#loading_columna").show("normal");		
		
		/////////se envia el mail////////
		$.ajax({ 
			 url: "includes/mail.php", 
			 type: "POST",
			 data: "sec=form_contacto&txtNombre=" + $("#txtNombre").val() + "&txtApellido=" + $("#txtApellido").val() 
					 + "&txtCiudad=" + $("#txtCiudad").val() + "&txtTelefono=" + $("#txtTelefono").val()
					 + "&txtEmail=" + $("#txtEmail").val() + "&txtConsulta=" + $("#txtConsulta").val(),
			 error:function(xhr, ajaxOptions, thrownError){
			 	alert("Error");			
				var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				$(".ui-dialog-titlebar").html("Error");
				$("#dialog_form_contacto").html(obj.msg); 
			 },
			 success: function(r){
			 	$("#loading_columna").hide();
				$("#btn_EnviarConsultaContacto").show();
				 
				 var obj = eval("("+r+")");
			 	ShowDialog("form_contacto");
				if(obj.success){
	        		$(".ui-dialog-titlebar").html("Envio Correcto");
				}
				else{
					$(".ui-dialog-titlebar").html("Error al enviar el email");
				}
					
				$("#dialog_form_contacto").html(obj.msg);
				limpiarCamposFormContacto();
      		}
		});
		
	}
}


function limpiarCamposFormContacto(){
	$("#txtNombre").val("");
	$("#txtApellido").val("");	
	$("#txtEmail").val("");	
	$("#txtCiudad").val("");	
	$("#txtTelefono").val("");	
	$("#txtConsulta").val("");
}
