$(function()
{

	$("#sms-loading").hide();

	// Escondemos formularios
	$('#back, #formCommentVideo, #formCommentFoto, #formCommentAudio').hide();
	
	$('form#formCommentTxt .formInspEnviar').show();
	$('#formCommentTxt').show();
	
	// Posicionamos formularios
	$('#back').css({
		zIndex		: 100,
		position	: 'absolute',
		top			: '0',
		left		: '0',
		width		: '100%',
		height		: $(document).height(),
		background	: '#000',
		opacity		: 0.2,
		filter		: 'alpha(opacity=20)'
	});

	$('#widgetLayer').css({
		zIndex		: 101,
		top			: '75px',
		position	: 'absolute',
		left		: ($(window).width()/2) - ($('#widgetLayer').width()/2),
		opacity		: 1,
		display		: 'none'
	});

	$('#widgetLayerHoy').css({
		zIndex		: 101,
		top			: '75px',
		position	: 'absolute',
		left		: ($(window).width()/2) - ($('#widgetLayerHoy').width()/2),
		opacity		: 1,
		display		: 'none'
	});

	$('#sms-not-loggedin').css({
		zIndex		: 101,
		position	: 'absolute',
		left		: ($(window).width()/2) - ($('#sms-not-loggedin').width()/2),
		opacity		: 1,
		display		: 'none'
	});

	$('#sms-not-sms').css({
		zIndex		: 101,
		top			: '75px',
		position	: 'absolute',
		left		: ($(window).width()/2) - ($('#sms-not-sms').width()/2),
		opacity		: 1,
		display		: 'none'
	});

	// Posicionamos stiker
	$('#stikerComment')
		.css({
			top : (400 + $(document).scrollTop()) + 'px',
			left: (($(window).width()/2) - 107) + 'px'
		})
		.click(function(){
			$('#back').hide();
			$(this).hide();
			$('.comment_item').find('embed').css('display','block')
			$('#IDformOrder').find('select').show()
			return false;
		});
		//.find('p a').click(function(){
			//window.open(this.href,'Evax_VIP','width=700,height=550,scrollbars=NO')
			//return false;
		//});


	// Configuramos boton de vista de formularios
	$('.btn_comentalaDetalle a').click(function()
	{
		$('#back').css({
			height:$(document).height()+'px',
			width:$(window).width()+'px'
		});

		$('.comment_item').find('embed').css('display','none');
		$.ajax({
			url		: _GLOBAL.context+'isLogged.xml',
			data	: 'nocache=' + Math.random(),
			success : function(xml)
			{
				//if ( $('data userName',xml).length )
				//{
					// multi track
					//dcsMultiTrack("WT.cg_n", "Cuentaselo a una amiga");
					$('.errorForm p').html('');
					$('#formInspira input, #formInspira textarea').removeClass('borderRed');
					if ( $.browser.name == 'ie' && $.browser.version == '6.0' )
					{
						$('#IDformOrder').find('select').hide();
						$('.comment_item').find('embed').css('display','none');
					}
					$('#formInspira').css('top',( 75 + $(document).scrollTop() ) + 'px');
					$('#back, #formInspira, #formCommentTxt').show();

					$('#formCommentTxt *, #formCommentVideo *, #formCommentFoto *, #formCommentAudio *').attr('disabled',false);
					$('#formCommentTxt input[type=checkbox], #formCommentVideo input[type=checkbox], #formCommentFoto input[type=checkbox], #formCommentAudio input[type=checkbox]').attr('checked',false);
					$('#formCommentTxt input[type!=hidden], #formCommentTxt textarea, #formCommentVideo input[type!=hidden], #formCommentVideo textarea, #formCommentFoto input[type!=hidden], #formCommentFoto textarea, #formCommentAudio input[type!=hidden], #formCommentAudio textarea').each( function(){
						$(this).val( this.title );
					} );
					$('.formVideo').html('<div id="previewSWF"></div>');

					$('.errorForm p').html('');
					$('.formImagen').html('');

					$('#TrefreshCaptcha').click()

			}
		});
		return false;
	});

	$('#vIDproposalComment_video').blur(function(){
		if ($.trim(this.value)!="")
		{
			var reg = /^http:\/\/.{2,3}\.youtube\.com\/watch\?v=(.*)$/;
			if (reg.test(this.value))
			{
				if (id == $('#vIDproposalComment_video').val().split('=')[1])
				{
					$('.formVideo').html('<div id="previewSWF"></div>');
					var flashvars = {};
					var params = {wmode: "transparent"};
					var attributes = {};
					try {
						swfobject.embedSWF('http://www.youtube.com/v/' + id, "previewSWF", "425", "344", "9.0.0", "false", flashvars, params, attributes);
					} catch(e) {}
				}
				else
				{
					$('.formVideo').html('<div id="previewSWF"></div>');
				}
			}
		}
	});
	$('#fIDproposalComment_image').blur(function(){
		if ($.trim(this.value)!="")
		{
			var reg = /^http:\/\/(.*)\.(jpg|gif|png|JPG|GIF|PNG)(.*)$/
			if (reg.test(this.value)) $('.formImagen').html('<img src="'+this.value+'" style="width:400px" />');
		}
	});


	$('a.form_video').click(function(e)
	{
		$('#formCommentTxt, #formCommentFoto, #formCommentAudio').hide();

		$('form#formCommentTxt .formInspEnviar').hide();
		$('form#formCommentFoto .formInspEnviar').hide();
		$('form#formCommentAudio .formInspEnviar').hide();
		$('form#formCommentVideo .formInspEnviar').show();

		$('#formCommentVideo').find('.errorForm p').html('');
		$('#formCommentVideo').show();


		return false;
	});
	$('a.form_foto').click(function(e)
	{
		$('#formCommentTxt, #formCommentVideo, #formCommentAudio').hide();

		$('form#formCommentTxt .formInspEnviar').hide();
		$('form#formCommentAudio .formInspEnviar').hide();
		$('form#formCommentVideo .formInspEnviar').hide();
		$('form#formCommentFoto .formInspEnviar').show();

		$('#formCommentFoto').find('.errorForm p').html('');
		$('#formCommentFoto').show();

		return false;
	});
	$('a.form_audio').click(function(e)
	{
		$('#formCommentTxt, #formCommentVideo, #formCommentFoto').hide();

		$('form#formCommentTxt .formInspEnviar').hide();
		$('form#formCommentVideo .formInspEnviar').hide();
		$('form#formCommentFoto .formInspEnviar').hide();
		$('form#formCommentAudio .formInspEnviar').show();

		$('#formCommentAudio').find('.errorForm p').html('');
		$('#formCommentAudio').show();

		return false;
	});
	$('a.form_txt').click(function(e)
	{
		$('#formCommentAudio, #formCommentVideo, #formCommentFoto').hide();

		$('form#formCommentVideo .formInspEnviar').hide();
		$('form#formCommentFoto .formInspEnviar').hide();
		$('form#formCommentAudio .formInspEnviar').hide();
		$('form#formCommentTxt .formInspEnviar').show();

		$('#formCommentTxt').find('.errorForm p').html('');
		$('#formCommentTxt').show();

		$('#TrefreshCaptcha').click()

		return false;
	});
/*
	$('a.form_cerrar').click(function(){
		$('#back, #formInspira, #formCommentTxt, #formCommentVideo, #formCommentFoto, #formCommentAudio, #widgetLayer, #widgetLayerHoy, #sms-not-loggedin, #sms-not-sms').hide();
		if ( $.browser.name == 'ie' && $.browser.version == '6.0' )
		{
			$('#IDformOrder').find('select').show()
			$('.comment_item').find('embed').css('visibility','visible');
		}
		return false;
	})
*/

	$('#tIDproposalComment_comment, #vIDproposalComment_comment, #fIDproposalComment_comment, #aIDproposalComment_comment').each(function()
	{
		$(this).keyup(function()
		{
			var v = this.value;
			$('#tIDproposalComment_comment, #vIDproposalComment_comment, #fIDproposalComment_comment, #aIDproposalComment_comment').each(function(){ this.value = v });
		});
	});

	$('.formInspEnviar').hover(
		function(){ this.src = '/media/img/botones/btn_enviar.gif'; },
		function(){ this.src = '/media/img/botones/btn_enviar_over.gif'; }
	);

	$('.btnCierre a, #sticker a').click(function(){return false});

	$('#tIDproposalComment_comment, #vIDproposalComment_comment, #fIDproposalComment_comment, #aIDproposalComment_comment')
		.keyup(function(e){
			if ( this.value.length > 255 ) this.value = this.value.substr(0,255);
		})
		.blur(function(e){
			if ( this.value.length > 255 ) this.value = this.value.substr(0,255);
		})

// WIDGET
	$('.ico_entuBlogSemanal').click(function()
	{
		$('#back').css({
			height:$(document).height()+'px',
			width:$(window).width()+'px'
		});
		$('#back').show();
		$('#widgetLayer').show();
		if ( $.browser.name == 'ie' && $.browser.version == '6.0' )
		{
		$('#IDformOrder').find('select').hide()
		}
	})

	$('.ico_entuBlog').click(function()
	{
		$('#back').css({
			height:$(document).height()+'px',
			width:$(window).width()+'px'
		});
		$('#back').show();
		$('#widgetLayerHoy').show();

		if ( $.browser.name == 'ie' && $.browser.version == '6.0' )
		{
			$('#IDformOrder').find('select').hide()
		}
	})

	$("#altRSS, #altWidget, #altFacebook").css("display", "none");
	$(".ico_entuBlogSemanal").hover( function()
		{
			$("#altWidget").show()
		}, function() {
			$("#altWidget").hide();
		});


		$(".ico_rss").hover( function()
		{
			$("#altRSS").show()
		}, function() {
			$("#altRSS").hide();
		});

		$(".ico_facebook").hover( function()
		{
			$("#altFacebook").show()
		}, function() {
			$("#altFacebook").hide();
		});

	function blockLayer(){
		var idLayer = "#sms-loading";
		showSMSErrorMsg(idLayer);
		$('#back').css({
			height:$(document).height()+'px',
			width:$(window).width()+'px'
		}).show();
	}
	function showSMSErrorMsg(el){
		var offsetY = document.documentElement.scrollTop;
		var offsetX = document.documentElement.scrollLeft;
		var topH = ($(window).height()/2) - ($(el).height()/2) + offsetY;
		var leftW = ($(window).width()/2) - ($(el).width()/2) + offsetX;
		$(el).css({"top":topH,"left":leftW}).show();
	}
	$("#check-sms").click(function(){
		$.ajax({
			beforeSend: function(){
				blockLayer();
			},
			url : _GLOBAL.context + 'promopin/hasSMS.xml',
			//url		: 'http://branch.tuvidaeshoy.dy/xml/test_hasSMS.xml',
			data	: 'nocache=' + Math.random(),
			success : function(xml) {
				var redirectUrl, nickName, vipId, country, sms;
				$(xml).find("data").each(function(index){
					redirectUrl = $(this).children("url").text();
					nickName = $(this).children("nick").text();
					vipId = $(this).children("vip").text();
					country = $(this).children("pais").text();
					sms = $(this).children("hasSMS").text();
				});
				$("#sms-loading").hide();
				switch(sms){
					case "-1":
						//alert('no estas logueado');
						var idLayer = "#sms-not-loggedin";
						showSMSErrorMsg(idLayer);
						break;
					case "0":
						//alert('no has canjeado por sms');
						var idLayer = "#sms-not-sms";
						showSMSErrorMsg(idLayer);
						break;
					case "1":
						//alert('redirect: ' + redirectUrl);
						location.href=redirectUrl + "?nick=" + nickName + "&vip=" + vipId + "&pais=" + country;
						break;
					default:
						alert("error general");
				}
			}
		});
		return false;
	});


});
