var iex = navigator.appName == "Microsoft Internet Explorer" ? true : false;
var fi = iex ? 'filters.alpha.opacity' : 'style.MozOpacity'
var opa = iex ? 100 : 1;

function Menu(nodeId)
{
	var element = document.getElementById(nodeId);
	element.style.display = element.style.display == 'none' ? 'block' : 'none';
}

function openMainMenuCompany()
{
	WindowManager.loadElement("leftMenuDiv", context + "jsp/company/MenuCompany.jsp");
	//WindowManager.loadElement("centralPanelDiv", context + "jsp/company/History.jsp");
	document.getElementById("centralPanelDiv").innerHTML = "<IMG src='" + context + "images/company.jpg" + "' width='100%'>";
	document.getElementById("centralPanelDiv").align = "left";

	resetBreadCrumb();
	addBreadCrumb("Principal", "location.reload()");
	addBreadCrumb("Empresa", null);

	showLeftPanel(true);
}

function openMainMenuProducts()
{
	// Hide left panel
	showLeftPanel(false);

	WindowManager.loadElement("centralPanelDiv", context + "productos.do");

	resetBreadCrumb();
	addBreadCrumb("Principal", "location.reload()");
	addBreadCrumb("Productos", null);
}

function openModel(productId, styleId, modelId)
{
	WindowManager.loadElement("centralPanelDiv", context + "modelo.do?productId=" + productId + "&styleId=" + (styleId != null ? styleId : "") + "&modelId=" + (modelId != null ? modelId : ""));

	resetBreadCrumb();
	addBreadCrumb("Principal", "location.reload()");
	addBreadCrumb("Productos", "openMainMenuProducts()");
	addBreadCrumb(productId, "selectProduct(\"" + productId + "\")");
	addBreadCrumb(modelId, null);
}

function selectStyle(productId, styleId)
{
	// Show left menu
	WindowManager.loadElement("leftMenuDiv", context + "menuProductos.do?productId=" + productId + "&styleId=" + styleId);
	showLeftPanel(true);

	// Load random images in the central pane
	WindowManager.loadElement("centralPanelDiv", context + "jsp/products/RandomImages.jsp");
	//openModel(productId, null, null);

	resetBreadCrumb();
	addBreadCrumb("Principal", "location.reload()");
	addBreadCrumb("Productos", "openMainMenuProducts()");
	addBreadCrumb(productId, null);
}

function openMainMenuContacts()
{
	// Hide left panel
	showLeftPanel(false);
	WindowManager.loadElement("centralPanelDiv", context + "contacto.do");

	resetBreadCrumb();
	addBreadCrumb("Principal", "location.reload()");
	addBreadCrumb("Contactos", null);
}

function showMenuTitle(/*string*/title)
{
	document.getElementById("menuTitle").innerHTML = title;;
}

function resetBreadCrumb(/*string*/text)
{
	document.getElementById("breadCrumb").innerHTML = "";
}

function getBreadCrumb()
{
	return document.getElementById("breadCrumb").innerHTML;
}

function addBreadCrumb(text, link)
{
	var separator = " -> ";
	var text = (link != null ? ("<A href='#' onclick='" + link + "'>" + text + "</A>") : text);

	document.getElementById("breadCrumb").innerHTML += getBreadCrumb() == "" ? text : separator + text;
}

function showLeftPanel(/*boolean*/show)
{
	document.getElementById("leftMenuDiv").style.display = show ? "" : "none" ;
}

function previousImage()
{
	if (arrayImages[indexImages - 1] != null)
		selectImage(arrayImages, --indexImages);
}
function nextImage()
{
	if (arrayImages[indexImages + 1] != null)
		selectImage(arrayImages, ++indexImages);
}

function setImageOpacity(img, opacity /*0-1*/)
{
	// Line for IExplorer (if not, does not work!! ¿?¿?¿?
	if (iex)
		img.style.filter = "alpha(opacity = " + (opacity * 100) + ")";
	else
		img.style.opacity = opacity;
}

function manageArrows()
{
	var disabled;

	// Left arrow
	disabled = (indexImages == 0 ? true : false);
	document.getElementById("leftArrowButton").disabled = disabled;
	setImageOpacity(document.getElementById("leftArrowImg"), disabled ? 0.3 : 1);
	
	// Right arrow
	disabled = (indexImages == arrayImages.length - 1 ? true : false)
	document.getElementById("rightArrowButton").disabled = disabled;
	setImageOpacity(document.getElementById("rightArrowImg"), disabled ? 0.3 : 1);
}
function selectImage(array, index)
{
	var image = document.getElementById("modelImage");
	image.src = array[index];

	// Resize image accordingly
	//resizeImage(image);

	manageArrows();
}
function resizeImage(image)
{
	(image.width/image.parentNode.clientWidth) > (image.height/image.parentNode.clientHeight) ? image.style.width = "100%" : image.style.height = "100%";
}

function zoomImage(imgPath, title)
{
	var w = window.open('','',
			//	'type=fullWindow'
			   //+',fullscreen'
			   ',menubar=0'
			+',addressbar=0'
			+ ',location=0'
			+',toolbar=0'
			   +',status=0'
			   +',scrollbars=1'
			   +',resizable=1');
	w.document.write('<html><head><title>' + title + '</title></head><body style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload=""><img src="' + imgPath + '" alt="' + title + '" title="' + title + '" onLoad="opener.reDimension(window, this.width, this.height)">');
}

function reDimension(w, width, height)
{
	if (iex)
		w.resizeTo(width + 12, height + 100);
	else
		w.resizeTo(w.outerWidth - w.innerWidth + width, w.outerHeight - w.innerHeight + height)

	w.moveTo((screen.width-width)/2,(screen.height-height)/2);
}
function shineImage(img, isSelected, opacity)
{
	if (iex)
		img.style.filter = "alpha(opacity=" + (isSelected ? 100 : 90) +")";
	else
		img.style.opacity = (isSelected ? 1 : 0.9);
}

function validateForm(form)
{
	if (form.name.value == "" || form.mail.value == "")
	{
		alert("Por favor, rellene los campos obligatorios marcados con el asterisco \'*\'");
		return false
	}
	else
	{
		return true;
	}
}