﻿var inwindow_minimum_height = 350;
var inwindow_minimum_width = 500;

function OpenPrintImage(applicationPath)
{
	var lis = htmlObjs.other_menu.getElementsByTagName('LI');
	if(lis.length != 0)
	{
		var li = lis[0];
		var init_width = 600;
		var init_height = 600;
		var init_left = Math.floor((screen.availWidth - init_width)/2);
		var init_top = Math.floor((screen.availHeight - init_height)/2);

		var newWin = window.open(applicationPath + '/Popup.aspx?imageUrl=' + li.imageUrl + "&lineName=" + li.lineName + "&productName=" + li.productName,
			'_new',
			'width=' + init_width + ',height=' + init_height + ',top=' + init_top + ',left=' + init_left + ',scrollbars=no,resizable=yes,status=no,location=no,toolbar=no');
	}
}

function MakePrintImageVisible(imageUrl, lineName, productName)
{
	var lis = htmlObjs.other_menu.getElementsByTagName('LI');
	if(lis.length != 0)
	{
		var li = lis[0];
		li.style.display = '';
		li.imageUrl = imageUrl;
		li.lineName = lineName;
		li.productName = productName;
	}
}

function LoadBigImage(imageUrl, lineName, productName)
{
	//alert(parseInt(htmlObjs.ph_image.style.height) + " - " + inwindow_minimum_height);
	//alert(parseInt(htmlObjs.ph_image.style.width) + " - " + inwindow_minimum_width);
	
	MakePrintImageVisible(imageUrl, lineName, productName);

	if(parseInt(htmlObjs.ph_image.style.height) < inwindow_minimum_height || 
		parseInt(htmlObjs.ph_image.style.width) < inwindow_minimum_width)
	{
		OpenImagePopup(imageUrl, lineName, productName);
		return;
	}

	htmlObjs.side_image.onload = function()
	{
		if(this.src != '')
		{
			if(parseInt(this.width) > parseInt(htmlObjs.ph_image.style.width) || parseInt(this.height) > parseInt(htmlObjs.ph_image.style.height))
				OpenImagePopup(imageUrl, lineName, productName);
		}
	};
	htmlObjs.side_image.src = imageUrl;

	// dida
	if(htmlObjs.dida != null)
	{
		// clear
		while(htmlObjs.dida.hasChildNodes())
			htmlObjs.dida.removeChild(htmlObjs.dida.firstChild);
		// add dida
		htmlObjs.dida.innerHTML = '<b>' + lineName + ' - ' + productName + '</b>';
	}
}

function OpenImagePopup(imageUrl, lineName, productName)
{
	htmlObjs.side_image.src = '';
	htmlObjs.popup_container.style.display = 'block';
	htmlObjs.popup_image.src = imageUrl;
	htmlObjs.popup_image.onload = function()
	{
		this.parentNode.style.marginTop = ((document.documentElement.clientHeight - this.height) / 2).toString() + 'px';
	};
	
	// dida
	if(htmlObjs.popup_dida != null)
	{
		// clear
		while(htmlObjs.popup_dida.hasChildNodes())
			htmlObjs.popup_dida.removeChild(htmlObjs.popup_dida.firstChild);
		// add dida
		htmlObjs.popup_dida.innerHTML = '<b>' + lineName + ' - ' + productName + '</b>';
	}
};

function CloseImagePopup()
{
	htmlObjs.popup_container.style.display = 'none';
	htmlObjs.popup_image.src = '';
};

function OpenGallery(IDnode, IDlanguage, path)
{
	var init_width = 556;
	var init_height = 504;
	var init_left = Math.floor((screen.availWidth - init_width)/2);
	var init_top = Math.floor((screen.availHeight - init_height)/2);
	
	var lang = 'IT';
	if(IDlanguage == 2) lang = 'EN';

	var newWin = window.open(path + '/Gallery.aspx?lang=' + lang + '&IDnode=' + IDnode,
		'_new',
		'width=' + init_width + ',height=' + init_height + ',top=' + init_top + ',left=' + init_left + ',scrollbars=no,resizable=yes,status=no,location=no,toolbar=no');
}


// used by ShowImage.htm to dynamically show the images
function RenderImage()
{
	var url = document.URL;
	if(url.indexOf('?') != -1)
	{
		var queryString = url.substring(url.indexOf('?') + 1);
		var imageUrl = queryString.substring(queryString.indexOf('=') + 1);
		var img = document.getElementById('ph');
		var loading = document.getElementById('loading');
		if(loading != null) loading.style.display = 'none';

		if(img != null)
			img.src = imageUrl;
		else
			alert('An error occurred, the image cannot be shown.');
	}
	else
	{
		alert('An error occurred, the image cannot be shown.');
	}
};


// used by ShowImage.htm to dynamically adapt window size and position
// based to image size
function SetWindowSizeSize(img)
{
	var windowWidth = Math.min(img.width + 8, screen.availWidth);
	var windowHeight = Math.min(img.height + 56, screen.availHeight);
	window.resizeTo(
		windowWidth,
		windowHeight
	);
	window.moveTo(
		Math.floor((screen.availWidth - windowWidth) / 2),
		Math.floor((screen.availHeight - windowHeight) / 2)
	);
};