$(window).resize(function() {if (isOpen == true) {sizeDivsWindow();}});
$(window).scroll(function () {if (isOpen == true) {sizeDivsWindow();}});
$(window).load(function (e) {if (isOpen == true) {sizeDivsWindow();}});

var borderWin = 7;
$(document).ready(function() 
{
	if (typeof colorBg ==  'undefined')
	{
		colorBg = '#036';
	}
	$('#nWindowBg').click(function() {
		/// cerra fondo con hacer clic en el mismo fondo/////////////
		 //close_nWin();
	});
	sizeDivsWindow();
	$('#nWindowBg').css({'background':colorBg});
	
	$('#winAjaxBackGroundLoading').css({'background-color' : colorBg});
	$('#nWindow').css({'border': borderWin+'px', 'border-style':'solid', 'border-color':colorBg});
	$('#nWindowBg').fadeTo("fast", 0.70);
	$('#nWindowBg').hide();
	$('#nWindow').hide();

});

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// START BIND MouseEnter & MouseLeave ///////////////////////

var prevMouseEnterHandler = function() 
{
	$("div#row_left").fadeTo(0, 1, function() {$("div#row_left").css('filter', 'none');});
	$("div#photoPrev").show();
};

var prevMouseLeaveHandler = function() 
{
	$("div#row_left").fadeTo(0, 0.30, function() {$("div#row_left").css('filter', 'none');});
	$("div#photoPrev").hide();
};

var nextMouseEnterHandler = function() 
{
	$("div#row_right").fadeTo(0, 1, function() {$("div#row_right").css('filter', 'none');});
	$("div#photoNext").show();
};

var nextMouseLeaveHandler = function() 
{
	$("div#row_right").fadeTo(0, 0.30, function() {$("div#row_right").css('filter', 'none');});
	$("div#photoNext").hide();
};

function bindRowLeft()
{
	$("div#row_left").bind('mouseenter', prevMouseEnterHandler);
	$("div#row_left").bind('mouseleave', prevMouseLeaveHandler); 
}

function unbindRowLeft()
{
	$("div#row_left").unbind('mouseenter', prevMouseEnterHandler);
	$("div#row_left").unbind('mouseleave', prevMouseLeaveHandler); 
}

function bindRowRight()
{
	$("div#row_right").bind('mouseenter', nextMouseEnterHandler);
	$("div#row_right").bind('mouseleave', nextMouseLeaveHandler); 
}

function unbindRowRight()
{
	$("div#row_right").unbind('mouseenter', nextMouseEnterHandler);
	$("div#row_right").unbind('mouseleave', nextMouseLeaveHandler); 
}

// END BIND MouseEnter & MouseLeave ///////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
function sizeBrowser()
{
	var browserSize = [];
	
	var widthViewport;
	if (typeof window.innerWidth != 'undefined'){widthViewport= window.innerWidth;}
	else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
	{widthViewport=document.documentElement.clientWidth;}
	else{widthViewport= document.getElementsByTagName('body')[0].clientWidth;}
	browserSize["w"] = widthViewport;
	browserSize["h"] = $(window).height();
	//alert("browserSize[w]: "+browserSize["w"]+" - browserSize[h]:"+browserSize["h"]);
	return browserSize;
}

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 sizeDivsWindow()
{
	BrowserH = sizeBrowser()['h'];
	BrowserW = sizeBrowser()['w'];

	$('#nWindowBg').css({'width' : BrowserW+'px', 'height' : BrowserH+'px'});
	
	divW = $('#nWindow').width();
	divH = $('#nWindow').height();
	center_divW = Number(Number(BrowserW)/2);
	center_divH = Number(Number(BrowserH)/2);

	centerWidth = center_divW- Number(divW/2);
	centerHeight = center_divH- Number(divH/2);
	
	x = getScrollXY();
	positionWindow = $('#nWindow').position();
	
	$('#nWindowBg').css({'top' : x[1]+'px', 'left' : x[0]+'px'});
	$('#nWindow').css({'left' : Number(centerWidth+x[0])+'px', 'top' : Number(centerHeight+x[1])+'px'});
}

function resizeDivWindowInfo(w, h)
{	
	sizeDivsWindow(true);
	divW = Number($('#nWindow').width());
	divH = Number($('#nWindow').height());
	
	position = $('#nWindow').position();
	
	centerLeft = Number(position.left+divW/2);
	centerTop = Number(position.top+divH/2);
	
	animateLeft = Number(centerLeft-w/2);
	animateTop = Number(centerTop-h/2);

	$('#nWindowInfo').hide().css({width : w+'px', height : h+'px'});

	$('#nWindow').stop(false, true);

	$('#nWindow').animate
	({width : Number(w)-(30)+'px', height : Number(h)-(30)+'px', left : Number(animateLeft-10)+'px', top : Number(animateTop-3)+'px'}, 400);

	$('#nWindow').animate
	(
		{
			width : w+'px', 
			height : h+'px',
			left : animateLeft+'px',
			top : animateTop+'px'
		}, 
		200, function(){
			$('#nWindowInfo').fadeIn('fast');
		}
	);
	$('#nWindowInfo').css({'overflow-x':'hidden'});
}

function validationLoadingSize(form)
{
	W_divCarrruselContent = $('#'+form+' #carrusel_content').css('width').split('px');
	W_divCarrruselContent = W_divCarrruselContent[0];
	
	H_divCarrruselContent = $('#'+form+' #carrusel_content').css('height').split('px');
	H_divCarrruselContent = H_divCarrruselContent[0];
	
	$('#'+form+' #validationLoading').show();
	$('#'+form+' #validationLoading').fadeTo("fast", 0.8);

	$('#'+form+' #validationLoading').css({'width' :  Number(W_divCarrruselContent)+'px', 'height' : Number(H_divCarrruselContent)+'px'});
}

var isOpen = false;

function close_nWin()
{
	ajaxVar.abort();
	isOpen = false;
	$('#nWindowBg').fadeOut("fast");
	$('#nWindow').fadeOut("fast");
	$('body').css({'overflow-x':'auto'});
}

function open_nWin(param, type, pathOpenWin, pathFormSend, nameFormDiv, isPopup)
{
	$('body').css({'overflow-x':'hidden'});

	isOpen = true;
	$('#nWindowBg').fadeIn("fast");
	$('#nWindow').fadeIn("fast");

	switch (type)
	{
		case "galleryInfoCarrusel": 
			//$("#content_carrusel").animate({top: '0px'}, 300);
			loading(param, "#nWindowInfo", "nWidgets/nWindows/load_photo.php", type);
		break;
		
		case "formBasic":
			loading("id="+param, "#nWindowInfo", "menu_contact.php", type);
		break;
		
		case "anything":
			loading(param, "#nWindowInfo", pathOpenWin, type, pathFormSend, nameFormDiv, isPopup);
		break;

		default:
		break;
	}
}

var next;
var prev;
var type;

//para el bind y unbind de los botones NEXT y PREV
var openPREVHandler = function() {open_nWin(prev, type);};
var mouseMovePREVHandler = function() {$("div#row_left").fadeTo(0, 1, function() {$("div#row_left").css('filter', 'none');});};

var openNEXTHandler = function() {open_nWin(next, type);};
var mouseMoveNEXTHandler = function() {$("div#row_right").fadeTo(0, 1, function() {$("div#row_right").css('filter', 'none');});};

var ajaxVar;

function loading(param, loadDiv, path, types, pathFormSend, nameFormDiv, isPopup)
{
	if(pathFormSend != undefined){sendPath = pathFormSend;}
	if(isPopup != undefined)
	{
		isPopup = isPopup.split('=');
		sendisPopup = isPopup[1];
	}

	ajaxVar = $.ajax
	({
		type: "POST",
		url: path,
		data: param+"&nameFormDiv="+nameFormDiv,
		beforeSend: function(objeto)
		{
			$(loadDiv).hide();
			$("div#nWindow").css({'background-position': 'center center'});
		},
		success: function(datos)
		{	
			$("div#nWindow").css({'background-position': '-100px center'});

			var paddingBottomPX = 50;
			switch (types)
			{
				case "galleryInfoCarrusel": 
					splitArr = datos.split("//");
					path = splitArr[0];
					size = splitArr[1].split("x");
					
					name = splitArr[2];
					description = splitArr[3];
					prev = splitArr[4];
					next = splitArr[5];
	
					type = types;

					/////////////////////////////
					unbindRowLeft();
					unbindRowRight();
					$("div#nWindowInfo").css({overflow: "hidden"});
					$("div#nWindow").css({'padding-bottom': paddingBottomPX+'px', 'margin-top': '-30px'});
					$("div#row_left").fadeTo(0, 0);
					$("div#row_right").fadeTo(0, 0);
					
					prevSplit1 = prev.split("&");
					prevSplit2 = prevSplit1[0].split("=");
					previf = prevSplit2[1];

					nextSplit1 = next.split("&");
					nextSplit2 = nextSplit1[0].split("=");
					nextif = nextSplit2[1];

					if (previf == "")
					{
						$('div#row_left').unbind('click', openPREVHandler);
						$("div#row_left").css({cursor:"default"});
						
						$("div#photoPrev").hide();
						$('div#row_left').unbind('mousemove', mouseMovePREVHandler);
					}
					else
					{
						$('div#row_left').unbind('click', openPREVHandler);
						$("div#row_left").bind('click', openPREVHandler);
						$("div#row_left").css({cursor:"pointer"});
						bindRowLeft();
						
						$('div#row_left').bind('mousemove', mouseMovePREVHandler);
					}

					if (nextif == "")
					{
						$('div#row_right').unbind('click', openNEXTHandler);
						$("div#row_right").css({cursor:"default"});
						
						$("div#photoNext").hide();
						$('div#row_right').unbind('mousemove', mouseMoveNEXTHandler);
					}
					else
					{
						$('div#row_right').unbind('click', openNEXTHandler);
						$("div#row_right").bind('click', openNEXTHandler);
						$("div#row_right").css({cursor:"pointer"});
						bindRowRight();
						
						$('div#row_right').bind('mousemove', mouseMoveNEXTHandler);
					}
					
					$(loadDiv).html('<img src="'+path+'"/>');
					
					resizeDivWindowInfo(size[0], size[1]);
					
					$("div#nWindow").css({'background-position': 'center center'});

				break;
				
				case "formBasic":
					$("div#nWindowInfo").css({overflow: "auto"});
					$("#row_left").hide();
					$("#row_right").hide();

					$(loadDiv).html(datos);
					resizeDivWindowInfo(600, 600);
				break;
				
				case "anything":
					$("div#nWindowInfo").css({overflow: "auto"});
					$("div#nWindow").css({'padding-bottom': '0px', 'margin-top': '-5px'});
					$("#row_left").hide();
					$("#row_right").hide();

					$(loadDiv).html(datos);

//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////Obtiene el tamaño de la Window por medio del ID "#carrusel_content" que
///////////////////////Está ubicado en el mismo formulario justo con la medida que debe tener

					W_divCarrruselContent = $('#'+nameFormDiv+' #carrusel_content').css('width').split('px');
					W_divCarrruselContent = W_divCarrruselContent[0];
					
					H_divCarrruselContent = $('#'+nameFormDiv+' #carrusel_content').css('height').split('px');
					H_divCarrruselContent = H_divCarrruselContent[0];

					resizeDivWindowInfo(W_divCarrruselContent, H_divCarrruselContent);

///////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
					searchFCKeditor(nameFormDiv);

				break;
			}
		}
	});
}

function searchFCKeditor(nameFormDiv)
{
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Busca en el formulario si exite un  "editor = fckeditor",
//////////////////// Si lo encuentra, entonces pone la opción de editor de html

	var fckeditorID = $("#"+nameFormDiv+" :input[editor='fckeditor']").attr("id");
	var numEditor = $("#"+nameFormDiv+" :input[editor='fckeditor']").length;

//////////////////// Con este while, busca si en el mismo formulario existe más de un editor
//////////////////// Si lo encuentra, entonces realiza uno editor independiente de los demás
	var count = 0;
	while(count < numEditor)
	{
		//alert($("#formNew :input[editor='fckeditor']")[count].id);
		startEditor($("#"+nameFormDiv+" :input[editor='fckeditor']")[count].id, nameFormDiv);
		count++;
	}	
}