var isOpenGuardaVideo = 0;
var isOpenInviaAmico = 0;
var isFeedbackOpen = 0;
var idVideo = -1;
var openInviaAmico = 0;
var video = Array();
var schedeVideo = Array();
var idVideoCorrente = -1;
var titoloVideoCorrente = -1;
var urlInvitaJSP = 'http://www-proxy.it.corp.leroymerlin.com/jsp/invitaAmico.jsp';

function addVideo(pVideo, pScheda)
{
	video.push(pVideo);	
	schedeVideo.push(pScheda);
}

function getParameter()
{
	var pg = "id=";
    var url = document.location.toString(); 
    var splot = url.split(pg);
	
	if(location.host != "as") urlInvitaJSP = location.protocol + "//" + location.host + "/jsp/invitaAmico.jsp";
	//alert(urlInvitaJSP);
	var hu = window.location.search.substring(1);
	var gy = hu.split("&");

	for (i=0;i<gy.length;i++) 
	{
		ft = gy[i].split("=");
		if (ft[0] == "id") idVideo = ft[1];
		if (ft[0] == "is") openInviaAmico = ft[1];
	}
}
getParameter();

$(document).ready(function()
{
	$(".guarda_immagine a img").each(
		function()
		{
			$(this).hover(
				function()
				{
					$(this).parent().parent().parent().children("a.title").css('text-decoration', 'underline');
				}, 
				function()
				{
					$(this).parent().parent().parent().children("a.title").css('text-decoration', 'none');
				}
			);
		});
	
	$(".guarda_singolo_video a.title").each(
		function()
		{
			$(this).hover(
				function()
				{
					$(this).css('text-decoration', 'underline');
				}, 
				function()
				{
					$(this).css('text-decoration', 'none');
				}
			);
		});
	
	if(idVideo > -1)
	{
		openGuardaVideo(idVideo);
		if(openInviaAmico == 1)  openInviaAmicoPanel();
	}
});

function openGuardaVideo(indexVideo)
{
	if(isOpenGuardaVideo == 0)
	{
		idVideoCorrente = indexVideo;
		titoloVideoCorrente = $("#video_"+indexVideo).children("a.title").attr('title');
		
		setTopGuardaVideo();
		document.getElementById('layer_opacity').style.display = 'inline';	
		document.getElementById('guarda_popup_video').style.display = 'inline';	
		isOpenGuardaVideo = 1;
		
		if(schedeVideo[indexVideo] != '')
		{
			$("#testoScheda").attr('href', schedeVideo[indexVideo]);
			$("#testoScheda").show();
			$("#bottoneScheda").attr('href', schedeVideo[indexVideo]);
			$("#bottoneScheda").show();
		}else
		{
			$("#testoScheda").attr('href', '#');
			$("#bottoneScheda").attr('href', '#');
			$("#testoScheda").hide();
			$("#bottoneScheda").hide();
		}
		
		$("a#playerVideo").attr('href', video[indexVideo]);
		$f("playerVideo", "/flowplayer/flowplayer.commercial-3.1.5.swf",
		{ 
			key: '$69ded43689a28c34ef4',
			plugins: {
			   controls: {
				  volumeSliderGradient: 'none',
				  durationColor: '#58585A',
				  bufferGradient: 'none',
				  volumeSliderColor: '#CDE5B1',
				  borderRadius: '0px',
				  timeBgColor: 'transparent',
				  backgroundGradient: 'none',
				  progressGradient: 'medium',
				  progressColor: '#6BB025',
				  buttonColor: '#6BB025',
				  sliderColor: '#D8D9DA',
				  tooltipTextColor: '#FFFFFF',
				  tooltipColor: '#6BB025',
				  bufferColor: '#CDE5B1',
				  backgroundColor: '#FFFFFF',
				  buttonOverColor: '#A1E551',
				  sliderGradient: 'none',
				  timeColor: '#58585A',
				  height: 24,
				  opacity: 1.0,
				  play:true, 
				  volume:true, 
				  mute:true, 
				  time:true, 
				  stop:false, 
				  playlist:false, 
				  fullscreen:true,
				  background: 'url(/images/background_guarda_video.jpg) repeat-x',url: 'flowplayer.controls.swf'
			   }
			},
			canvas:
			{
				background: 'url(/images/background_load_guarda_video.jpg) no-repeat',
				backgroundColor: "transparent"
			},
			clip: 
			{ 
				autoPlay: true,
				autoBuffering: true
			},
			play: { 
				label: null, 
				replayLabel: "premi per rivedere il video" 
			}
		});
	}
}

function openInviaAmicoPanel()
{
	if(isOpenInviaAmico == 0)
	{
		isOpenInviaAmico = 1;
		var heightVideo = $("#guarda_popup_video").height();
		var marginTopVideo = $("#guarda_popup_video").css('margin-top');
		if(marginTopVideo.indexOf('px') != -1) marginTopVideo = marginTopVideo.substring(0, marginTopVideo.indexOf('px'));
		$("#invia_amico_video").css('margin-top', (parseInt(marginTopVideo) + heightVideo));
		$("#tuonome").val('Il tuo nome');
		$("#tuaemail").val('La tua mail');
		$("#suonome").val('Il suo nome');
		$("#suaemail").val('La sua mail');
		$("#messaggio").val('Scrivi un messaggio');
		$("#invia_amico_video").show();
	}
}

function closeInviaAmicoPanel()
{
	if(isOpenInviaAmico == 1)
	{
		isOpenInviaAmico = 0;
		closeFeedbackInviaAmicoVideo();
		$("#invia_amico_video").hide();
	}
}

function findPageHeight()
{
	if (typeof window.innerHeight!='undefined') 
		return window.innerHeight;
	if (document.documentElement && typeof document.documentElement.clientWidth!='undefined' && document.documentElement.clientHeight!=0) 
		return document.documentElement.clientHeight;
	if (document.body && typeof document.body.clientWidth!='undefined') 
		return document.body.clientHeight;
	return (null);
}

function setTopGuardaVideo()
{
	var Y = getScrollXY();
	var pageHeight = findPageHeight();
	var pannello_y_default = -300;
	if(pageHeight <= 800) pannello_y_default = -270;
	if(pageHeight <= 500) pannello_y_default = -200;
	document.getElementById('guarda_popup_video').style.marginTop = pannello_y_default+'px';
	if(Y > 0)
	{
		var newTop = pannello_y_default + Y;
		pannello_y_default = newTop;
		document.getElementById('guarda_popup_video').style.marginTop = pannello_y_default+'px';
	}
}

function closeGuardaVideo()
{
	$f(0).stop();
	closeInviaAmicoPanel();
	closeFeedbackInviaAmicoVideo();
	document.getElementById('layer_opacity').style.display = 'none';	
	document.getElementById('guarda_popup_video').style.display = 'none';
	isOpenGuardaVideo = 0;
}

function checkTextGuardaVideo(id, defaultText, errorText) 
{
	document.getElementById(id).style.color = "#868789";
 	if(trim(document.getElementById(id).value) == defaultText)
 	{
 		document.getElementById(id).value = "";
 	} else {
	    if(trim(document.getElementById(id).value) == "" || trim(document.getElementById(id).value) == errorText)
  		{
   			document.getElementById(id).value = defaultText;
  		}
 	}	
}

var count_error = 0;

function checkInviaAmicoVideoForm()
{
	$(".chiudi_guarda_video").attr('href', 'javascript:void(0)');
	count_error = 0;
	checkTextInputGuardaVideo('tuonome', 'Il tuo nome', 'Valore richiesto');
	checkEmailGuardaVideo('tuaemail', 'La tua mail', 'Valore errato');
	checkTextInputGuardaVideo('suonome', 'Il suo nome', 'Valore richiesto');
	checkEmailGuardaVideo('suaemail', 'La sua mail', 'Valore errato');
	checkTextInputGuardaVideo('messaggio', 'Scrivi un messaggio', 'Valore richiesto');
	if(count_error == 0)
	{
		var url = document.location.toString(); 
		if(url.indexOf("?id=") != -1) 
		{
			url = url.substring(0, url.indexOf("?id="));
			url = url + "?id=" + idVideoCorrente;
		}else if(url.indexOf("&id=") != -1) 
		{
			url = url.substring(0, url.indexOf("&id="));
			url = url + "&id=" + idVideoCorrente;
		}else
		{
			url = url + "&id=" + idVideoCorrente;
		}
		 
		var queryString = "type=sezVideo";
		queryString = queryString + "&sender=" + escape($("#tuonome").val());
		queryString = queryString + "&sender_email=" + escape($("#tuaemail").val());
		queryString = queryString + "&receiver=" + escape($("#suonome").val());
		queryString = queryString + "&receiver_email=" + escape($("#suaemail").val());
		queryString = queryString + "&text=" + escape($("#messaggio").val());
		queryString = queryString + "&link=" + escape(url);
		queryString = queryString + "&title=" + escape(titoloVideoCorrente);
		
		$.ajax({
		   type: "POST",
		   url: urlInvitaJSP,
		   data: queryString,
		   success: function(msg)
		   {
			   $(".chiudi_guarda_video").attr('href', 'javascript:closeGuardaVideo()');
	 		   openFeedbackInviaAmicoVideo();
		   }
		 });
	}else
	{
		$(".chiudi_guarda_video").attr('href', 'javascript:closeGuardaVideo()');
	}
}

function checkTextInputGuardaVideo(id, defaultValue, errorValue)
{	
	document.getElementById(id).style.color = "#868789";
	var value = jQuery.trim(document.getElementById(id).value);
	if( value == "" || value == errorValue || value == defaultValue)
	{
		count_error = count_error + 1;
		//document.getElementById(id).value = errorValue;
		document.getElementById(id).style.color = '#FF0000';
	}
}

function checkEmailGuardaVideo(id, defaultValue, errorValue) //vuole l'ID del campo
{
	var value = jQuery.trim(document.getElementById(id).value);
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if( value == "" || value == defaultValue)
	{
		count_error = count_error + 1;
		//document.getElementById(id).value = errorValue;
		document.getElementById(id).style.color = '#FF0000';	
	}else
	{
		if(!value.match(emailExp))
		{
			count_error = count_error + 1;
			//document.getElementById(id).value = errorValue;
			document.getElementById(id).style.color = '#FF0000';
		}
	}
}

function openFeedbackInviaAmicoVideo()
{
	if(isFeedbackOpen == 0)
	{
		var widthForm = $("#invia_amico_video").width();
		var heightForm = $("#invia_amico_video").height();
		var marginTopForm = $("#invia_amico_video").css('margin-top');
		var marginLeftForm = $("#invia_amico_video").css('margin-left');
		isFeedbackOpen = 1;
		$("#invia_amico_video").css('z-index', 20);
		if(marginTopForm.indexOf('px') != -1) marginTopForm = marginTopForm.substring(0, marginTopForm.indexOf('px'));
		$("#feedback_invia_amico_guarda_video").css('margin-top', (parseInt(marginTopForm) + 27));
		$("#feedback_invia_amico_guarda_video").css('margin-left', (parseInt(marginLeftForm) + 212));
		$("#feedback_invia_amico_guarda_video").show();
	}
}

function closeFeedbackInviaAmicoVideo()
{
	if(isFeedbackOpen == 1)
	{
		isFeedbackOpen = 0;
		$("#feedback_invia_amico_guarda_video").hide();
		$("#invia_amico_video").css('z-index', 3005);
		closeInviaAmicoPanel()
	}
}