var popupEnabled = 0;
var popupWidth = 0;
var popupHeight = 0;

$(document).ready(function () {
    
   //Esconder el popup 
   $("#JSPopup").hide();
    
   //Función para cerrar el popup 
   $("#JSPopup").click(function (){ 
      $(this).fadeOut('slow');
	  $(".flashBannerHome").show();
	  $("#JSPopupBackground").hide();
	  popupEnabled = 0;
   });
   
   $(window).scroll(function () { 
		if (popupEnabled == 1) {
			setSizeAndCenterPopup(popupWidth, popupHeight)
		}
    });
   
});

function setSizeAndCenterPopup(width,height) {
   var winSizeXY = getWindowSize();
   var scrollXY = getScrollXY();
   var w = winSizeXY[0]; 
   var h = winSizeXY[1];
      	  
   $("#JSPopup").css('width', width + 'px'); 
   $("#JSPopup").css('height', height + 'px');
   $("#JSPopupBackground").css('width', '100%');
   $("#JSPopupBackground").css('height', '100%');
   $("#JSPopupBackground").css('top', scrollXY[1] + 'px');
   
   //Centra el popup    
   w = (w/2) - (width/2) + scrollXY[0]; 
   h = (h/2) - (height/2) + scrollXY[1]; 
   $("#JSPopup").css("left",w + "px"); 
   $("#JSPopup").css("top",h + "px");
   
}

function showSWFVideo(videoUrl, videoWidth, videoHeight, caption) {
	popupWidth = videoWidth;
	popupHeight = videoHeight + 70;
	
	var html;
	html = ""
			+	"<div><object codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"" +
			/*+		"width=\"" + videoWidth + "\" height=\"" + videoHeight + "\">"*/
			+		"<param name=movie value=\"" + videoUrl + "\">"
			+		"<param name=quality value=high>"
			+		"<param name=width value=\"" + videoWidth + "\">"
			+		"<param name=height value=\"" + videoHeight + "\">"
			+		"<embed src=\"" + videoUrl + "\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"" + videoWidth + "\" height=\"" + videoHeight + "\"> "
			+		"</embed>"
			+	"</object></div>"
			+	"<div class=\"caption\">"
			+		"<div id=\"cerrar\"></div>"
			+		"<div class=\"text\">" + caption + "</div></div>";
			
	$("#JSPopup #content").html(html);
			
	showJSPopup();
	setSizeAndCenterPopup(popupWidth, popupHeight);
}

function showJSPopup() {
	$("body").append("<div id='JSPopupBackground' style='display: none;'></div>");
    $("#JSPopupBackground").show(); 
    $("#JSPopup").fadeIn('slow'); 
	$(".flashBannerHome").hide();
	popupEnabled = 1;
}





function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
	scrOfY = window.pageYOffset;
	scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant
	scrOfY = document.body.scrollTop;
	scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
	scrOfY = document.documentElement.scrollTop;
	scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
} 

function getWindowSize() {
	var viewportwidth, viewportheight;
	if (typeof window.innerWidth != 'undefined')
	{
	viewportwidth = window.innerWidth,
	viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
	&& typeof document.documentElement.clientWidth !=
	'undefined' && document.documentElement.clientWidth != 0)
	{
	viewportwidth = document.documentElement.clientWidth,
	viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE
	else
	{
	viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return [viewportwidth, viewportheight]
} 
