var currentVideo='testimoniovideo1';
var stopExperiencias = false;

var current_form=null; //Contiene el formulario actual para distinguir entre formularios que se llaman a partir de la misma clase
var timer_sendForm;
var is_callme_sent; //var bool para indicar si un form tipo 'calmme' ha sido enviado, usada en funcionalidad: 'elección tipo cliente'.
var active_form_type;// var string usada para establecer el valor del tipo de formulario activo (en uso por el cliente), usada en funcionalidad: 
$(document).ready(function(){	
	
	/*$('#oferta1, #oferta2, #oferta3').click(function(){
		$('a.regalos_opener').trigger('click');
	});*/
	
	//Controla la entrada de datos en el comprobador de teléfono
	$('#comp_tel_telefono').focus(function()
	{
		$(this).val(($(this).val()=='Escribe tu teléfono') ? '' : $(this).val());
	});
	
	$('#comp_tel_telefono').blur(function()
	{
		$(this).val(($(this).val()=='') ? 'Escribe tu teléfono' : $(this).val());
	});
	
	//Controla la entrada de datos en el callme
	$('#callme_telefono').focus(function()
	{
		$(this).val(($(this).val()=='Escribe tu teléfono') ? '' : $(this).val());
	});
	
	$('#callme_telefono').blur(function()
	{
		$(this).val(($(this).val()=='') ? 'Escribe tu teléfono' : $(this).val());
	});

	//Muestra el formulario de comprobación de dirección
	$('#comp_dir_opener').click(function(){
		$('#comprobador_step1').hide();
		$('#comprobador_step2').show();
		$('#compdir_fijo_texto').hide();
	});
	
	
	//Envía el formulario de comprobador de teléfono
	$('#comp_tel').submit(function(){
		var tel = $('#comp_tel_telefono').val();
		var tsource = $('#tsource').val();
		var producto = $('#producto').val();
		$.ajax({
			url: '/helper/comptel',
			type: 'POST',
			data: { telefono: tel, tsource: tsource, producto: producto},
			dataType: 'json',
			success: function(data)
			{
				if(data.status)
				{
					$('#comprobador_step1').hide();
					$('#comprobador_step2').show();
					$('#comprobador1').hide();
					$('#comprobador2').show();
					$('#comprueba_fijo').val(tel);
					var url = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/'+$('#distribuidor').val()+'/'+$('#tsource').val()+'/c/0';
					document.getElementById('tracker').contentWindow.location.href = url;
					
					var url_tracking = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/' + jQuery('#distribuidor').val() + '/default/remarketing/';
					document.getElementById('tracking_remark').contentWindow.location.href = url_tracking;
					
					$.fancybox({
						'type' : 'iframe',
						'href' : '/jazztel/experiencias_c2b?telefono='+tel+'&producto='+producto,
						'hideOnOverlayClick' : false,
						'height' : 425
					});
				}
				else
				{
					alert(data.msg);
				}
			}
		});
		return false;
	});
	
	//Envía el formulario de comprobador de dirección
	$('.compdir_form').submit(function(){
		if($('.compdir_acepto',this).attr('checked') != false){	
			var movil = jQuery('.compdir_movil').val();
			var nombre = jQuery('.compdir_nombre').val();
			var apellidos = '';
			var cp = jQuery('.compdir_cp').val();
			var provincia = '';
			var localidad = '';
			var tipo_via = '';
			var telefono = $('.compdir_fijo').val();
			var calle = jQuery('.compdir_calle').val();
			var numero = jQuery('.compdir_numero').val();
			var email = '';
			var horario = '';
			var piso = jQuery('.compdir_piso').val();
			var tsource = jQuery('#tsource').val();
			var producto = jQuery('#producto').val();
			//alert('hola');
			jQuery.ajax({
				url: '/helper/compdir/',
				global: false,
				type: 'POST',
				data: ({movil : movil, telefono: telefono, nombre: nombre, apellidos: apellidos, cp: cp, provincia: provincia, localidad: localidad, tipovia: tipo_via, direccion: calle, numero: numero, tsource: tsource, producto:producto, horario: horario, email: email, piso: piso}),
				dataType: 'json',
				success: function(data){
					if(data.status)
					{
						$('#comprobador_step1').hide();
						$('#comprobador_step2').hide();
						$('#comprobador_step3').show();
						$('#comprobador1').hide();
						$('#comprobador2').hide();
						$('#comprobador3').show();
						//$('#comprueba_fijo').val(tel);
						var url = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/'+$('#distribuidor').val()+'/'+$('#tsource').val()+'/dir/0';
						document.getElementById('tracker').contentWindow.location.href = url;
						var url_tracking = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/' + jQuery('#distribuidor').val() + '/default/remarketing/';
						document.getElementById('tracking_remark').contentWindow.location.href = url_tracking;
					}
					else
					{
						alert(data.msg);
					}
				}
			});
			
		}else{
		
			alert('Debes aceptar la política de privacidad');			
		
		}
		return false;
	});
	
	
	window.onbeforeunload = userExit;//Ejecuta la función de salida de la página. Funcionalidad: 'elección tipo cliente'.
	
	//Control formulario tipo Callme - 2011/08/09 ABM
	$('#callme').submit(function(){
		is_callme_sent = false;
		active_form_type = 'callme';
		current_form=this;
		$.fancybox({
			'hideOnOverlayClick'	: false,
			'showCloseButton'		: false,
			'enableEscapeButton'	: false,
			'content'				: $('#div_tipocliente').html()
		});
		timer_sendForm = window.setTimeout('sendFormData(\'callme\',0)',20000);
		return false;
	});
	
	//Envía el formulario de callme
	$('#callme_old').submit(function(){
		var tel = $('#callme_telefono').val();
		var tsource = $('#tsource').val();
		var producto = $('#producto').val();
		//Se quita la política de privacidad. 2011-10-28 ICM
		/*if ($('.solinfo_acepto',this).attr('checked') != false)
		{*/
			$.ajax({
				url: '/helper/callme',
				type: 'POST',
				data: { telefono: tel, tsource: tsource, producto: producto},
				dataType: 'json',
				success: function(data)
				{
					if(data.status)
					{
						$('#callme_info').hide();
						$('#callme_feedback').show();
						var url = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/'+$('#distribuidor').val()+'/'+$('#tsource').val()+'/callme/0';
						document.getElementById('tracker').contentWindow.location.href = url;
						
						var url_tracking = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/' + jQuery('#distribuidor').val() + '/default/remarketing/';
						document.getElementById('tracking_remark').contentWindow.location.href = url_tracking;
					}
					else
					{
						alert(data.msg);
					}
				}
			});
		/*}else{
			alert('Debes aceptar la política de privacidad');
		}*/
		return false;
	});
	
	$('.solinfo_form').submit(function(){
		var nombre = $('.solinfo_nombre').val();
		//var apellidos = $('.solinfo_apellidos').val();
		var telefono = $('.solinfo_telefono').val();
		//var movil = $('.solinfo_movil').val();
		var email = $('.solinfo_email').val();
		var tsource = $('#tsource').val();
		var producto = $('#producto').val();
		var linea = 0;
		if ($('.solinfo_acepto',this).attr('checked') != false)
		{
			$.ajax({
				url: '/helper/solinfo',
				type: 'POST',
				//data: { nombre: nombre, apellidos:apellidos, telefono: telefono, movil:movil, email:email, linea:linea, tsource: tsource, producto: producto},
				data: { nombre: nombre, telefono: telefono, email:email, tsource: tsource, producto: producto},
				dataType: 'json',
				success: function(data)
				{
					if(data.status)
					{
						$('#solinfo_form').hide();
						$('#solinfo_feedback').show();
						var url = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/'+$('#distribuidor').val()+'/'+$('#tsource').val()+'/b/0';
						document.getElementById('tracker').contentWindow.location.href = url;
						
						var url_tracking = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/' + jQuery('#distribuidor').val() + '/default/remarketing/';
						document.getElementById('tracking_remark').contentWindow.location.href = url_tracking;
					}
					else
					{
						alert(data.msg);
					}
				}
			});
		}else{
			alert('Debes aceptar la política de privacidad');
		}
		return false;
	});
	
	// Envío de Email para newsletter
	$(".newsletter_button").click(function(){
	
		if($('.newsletter_acepto').is(':checked') == true)
		{	
			$('.newsletter_button').hide();
		
			var email = $('.newsletter_email').val();	
			
			jQuery.ajax({
				url: "/helper/email/",
				global: false,
				type: "POST",
				data: ({email: email, section:'_ADSL'}),
				dataType: "json",
				success: function(data){
					if(data.status)
					{
						$('.newsletter_form').hide();
						$('.newsletter_feedback').show();	
					}
					else
					{
						alert(data.msg);
						$('.newsletter_button').show();
					}
				}
			});
		}
		else
		{
			alert('Debes aceptar la política de privacidad');
		}	
	
		return false;
	});
	
	$('a.pol_priv_opener').fancybox({});
	$('a.regalos_opener, a.ofertacontratar').fancybox({});
	$('a.alta_opener,#oferta1, #oferta2, #oferta3').fancybox({
		'href'		: '/jazztel/alta?tsource='+$('#tsource').val()+'&distribuidor='+$('#distribuidor').val()+'&code='+$('#producto').val(),
		'type'		: 'iframe',
		'height' 	: 518,
		'width' 	: 820
	});
	
	$('area.alta_opener').click(function(){
		$('#oferta1').trigger('click');
	});
	
	$('a.tutocontratar_si,#como_contratar_opener').fancybox({
		'href'		: '/jazztel/como_contratar?tsource='+$('#tsource').val()+'&distribuidor='+$('#distribuidor').val()+'&code='+$('#producto').val()+'&ya_tengo=1',
		'type'		: 'iframe',
		'height' 	: 518,
		'width' 	: 820
	});
	
	$('a.tutocontratar_no').fancybox({
		'href'		: '/jazztel/como_contratar?tsource='+$('#tsource').val()+'&distribuidor='+$('#distribuidor').val()+'&code='+$('#producto').val()+'&ya_tengo=0',
		'type'		: 'iframe',
		'height' 	: 518,
		'width' 	: 820
	});
	
	$('.vm_prev').click(function () {
		if( currentVideo == 'testimoniovideo1')
		{ 
			/*$(this).parent().attr('id', 'video3'); 
			$('.vm_video').attr('href','/videos?default=video3');*/
			$('.testimoniovideo1').hide();
			$('.testimoniovideo4').show();
			currentVideo = 'testimoniovideo4';
			return false;
		}
		if( currentVideo == 'testimoniovideo2')
		{ 
			/*$(this).parent().attr('id', 'video1'); 
			$('.vm_video').attr('href','/videos?default=video1');*/
			$('.testimoniovideo2').hide();
			$('.testimoniovideo1').show();
			currentVideo = 'testimoniovideo1';
			return false;
		}
		if( currentVideo == 'testimoniovideo3')
		{
			/*$(this).parent().attr('id', 'video2'); 
			$('.vm_video').attr('href','/videos?default=video2');*/
			$('.testimoniovideo3').hide();
			$('.testimoniovideo2').show();
			currentVideo = 'testimoniovideo2';
			return false;
		}
		if( currentVideo == 'testimoniovideo4')
		{
			/*$(this).parent().attr('id', 'video2'); 
			$('.vm_video').attr('href','/videos?default=video2');*/
			$('.testimoniovideo4').hide();
			$('.testimoniovideo3').show();
			currentVideo = 'testimoniovideo3';
			return false;
		}
		return false;
	});
	
	$('.vm_next').click(function () {
		if( currentVideo == 'testimoniovideo1')
		{
			/*$(this).parent().attr('id', 'video2');
			$('.vm_video').attr('href','/videos?default=video2');*/
			$('.testimoniovideo1').hide();
			$('.testimoniovideo2').show();
			currentVideo = 'testimoniovideo2';
			return false;
		}
		if( currentVideo == 'testimoniovideo2')
		{
			/*$(this).parent().attr('id', 'video3');
			$('.vm_video').attr('href','/videos?default=video3');*/
			$('.testimoniovideo2').hide();
			$('.testimoniovideo3').show();
			currentVideo = 'testimoniovideo3';
			return false;
		}
		if( currentVideo == 'testimoniovideo3')
		{
			/*$(this).parent().attr('id', 'video1');
			$('.vm_video').attr('href','/videos?default=video1');*/
			$('.testimoniovideo3').hide();
			$('.testimoniovideo4').show();
			currentVideo = 'testimoniovideo4';
			return false;
		}
		if( currentVideo == 'testimoniovideo4')
		{
			/*$(this).parent().attr('id', 'video1');
			$('.vm_video').attr('href','/videos?default=video1');*/
			$('.testimoniovideo4').hide();
			$('.testimoniovideo1').show();
			currentVideo = 'testimoniovideo1';
			return false;
		}
		return false;
	});
	
	/*$('.vm_video').fancybox({
		'type'	:	'iframe',
		'height' : 450,
		'width' : 535
	});
	$('.vm_video').click(function(){return false;});*/
	
	$('.vervideo').click(function(){
		$('#oferta1').addClass('ofertavideo2');
		$('.oferta_info').hide();
		$('.ver_video').show();
		$('.cerrarvideo').show();
		$('.vervideo').hide();
		$('.header_oferta').hide();
		return false;
	});
	
	$('.cerrarvideo').click(function(){
		$('#oferta1').removeClass('ofertavideo2');
		$('.ver_video').hide();
		$('.oferta_info').show();
		$('.cerrarvideo').hide();
		$('.vervideo').show();
		$('.header_oferta').show();
		return false;
	});
	
	setTimeout('mostrar_precio_tv()',2000);
	
	$('.experiencias_flotante').animate({
		left: '50%',
		'margin-left': '-425px',
		opacity:'100'
	},1500,ocultar_experiencia());
	
	$('a#experiencias_opener').fancybox({
		'href'		: '/experiencias',
		'type'		: 'iframe',
		'height' 	: 503,
		'width' 	: 802,
		'padding'	: 0
	});
	
	$('.experiencias_flotante iframe')
		.mouseover(function(){
			stopExperiencias=true;
		})
		.mouseout(function(){
			stopExperiencias=false;
			ocultar_experiencia();
		});
		
	$('#cerrar_experiencias').click(function(){
		stopExperiencias=false;
		$('.experiencias_flotante').animate({left: '0',opacity:'0'},1000);
		return false;
	});
});

function mostrar_precio_tv()
{
	$('#pest_tv1').hide();
	$('#pest_tv2').show();
	setTimeout('mostrar_titulo_tv()',5000);
}

function mostrar_titulo_tv()
{
	$('#pest_tv2').hide();
	$('#pest_tv1').show();
	setTimeout('mostrar_precio_tv()',5000);
}

function ocultar_experiencia(){
	setTimeout("if (!stopExperiencias) $('.experiencias_flotante').animate({left: '0',opacity:'0'},1000, function(){$('.experiencias_flotante').hide();});",4000);
}

function popup_experiencias(){
	$('a#experiencias_opener').fancybox({
		'href'		: '/experiencias',
		'type'		: 'iframe',
		'height' 	: 503,
		'width' 	: 802,
		'padding'	: 0
	}).trigger('click');
}

function popup_impresora(){
	$('a.regalos_opener').trigger('click');
}

function sendFormData(formType,clientType)
{
	window.clearTimeout(timer_sendForm); //Quitamos el setTimeout, ya que se va a enviar el formulario
	
	$.fancybox.close();
	
	var clientTypeText = new Object();
       
	clientTypeText[0] = '#TIPO_CLIENTE_0# El cliente no ha indicado el tipo.';
	clientTypeText[1] = '#TIPO_CLIENTE_1# No es cliente.';
	clientTypeText[2] = '#TIPO_CLIENTE_2# Es cliente ADSL.';
	clientTypeText[3] = '#TIPO_CLIENTE_3# Es cliente Móvil.';
	
	switch (formType)
	{
		case 'callme':
		{
			active_form_type = '';
			
			var tel = $('#callme_telefono').val();
			var tsource = $('#tsource').val();
			var producto = $('#producto').val();
			$.ajax({
				url: '/helper/callme',
				type: 'POST',
				data: { telefono: tel, tsource: tsource, producto: producto, observaciones:clientTypeText[clientType] },
				dataType: 'json',
				success: function(data)
				{
					if(data.status)
					{
						$('#callme_info').hide();
						$('#callme_feedback').show();
						var url = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/'+$('#distribuidor').val()+'/'+$('#tsource').val()+'/callme/0';
						document.getElementById('tracker').contentWindow.location.href = url;
						
						var url_tracking = 'http://www.cobaltskull.com/pixels/get_tracking/jazztel/' + jQuery('#distribuidor').val() + '/default/remarketing/';
						document.getElementById('tracking_remark').contentWindow.location.href = url_tracking;
						
						is_callme_sent = true;
						window.onbeforeunload = null;
						/*$.fancybox({
							'type' : 'iframe',
							'href' : '/jazztel/experiencias_c2b?telefono='+tel+'&producto='+producto,
							'hideOnOverlayClick' : false,
							'height' : 390
						});*/
					}
					else
					{
						alert(data.msg);
					}
				}
			});	
			break;
		}
	}
}

// Función utilizada en el evento 'onbeforeunload' para realizar distintas acciones. Funcionalidad: 'elección tipo cliente'.
function userExit(){
	
	//Envío los datos del formulario 'callme' de un usuario si estos no han sido enviados anteriormente.
	if(active_form_type=='callme' && is_callme_sent==false)
	{
		sendFormData('callme',0);
		return "";
	}

}
