var itemvisibility = [];

function hideModalWindow(id) {
	if ( itemvisibility[id] && (itemvisibility[id] == true) )
	{
		$('#'+id).fadeOut("normal");
		itemvisibility[id] = false;
	}
}

var nextzindexid = 11111;

function showModalWindow(id, title, x, y, width, height, style, dontCenter ) {

	if ($('#'+id).get(0)) {
		try
		{
			adjustModalWindowPosition(id,width);
		}
		catch(err) { /* ignore */ }
		$('#'+id).fadeIn("normal");
	} else {
		if (!style)
			style = "normal";
		var newModalWindow = '';
		newModalWindow += '<div class="dw" id="' + id + '" style="position: absolute; z-index: ' + nextzindexid + '; width: '+(width+8) +'px; height: '+(height+8) +'px; left: '+x+'px; top: '+y+'px;">';
		newModalWindow += '<div class="dialogwindow ' + style + '" id="'+id+'_main" style="width: '+(width)+'px; height: '+height+'px; left: 0; top: 0; position:absolute; z-index: 100;">';
		newModalWindow += '<div class="titlebar ' + style + '" style="width: ' + (width-1) + 'px;"><span class="title" style="font-family:Arial;font-size:10pt;font-weight:bold;">'+title+'</span><div class="x"><a href="#" onclick="hideModalWindow(\''+id+'\');" title="close">close</a></div></div>';
		newModalWindow += '<div class="modal-content" style="width: ' + (width - 12) + '"></div></div>';
		// newModalWindow += '<div class="modal-shadow" style="z-index:99; position:absolute; top: 7px; left: 7px; width: '+(width)+'px; height: '+height+'px;" ></div>';
		newModalWindow += '</div>';

		var dw_modalHolder = $g('dw_modalHolder');
		if (dw_modalHolder == null) {
			dw_modalHolder = document.createElement("div");
			dw_modalHolder.setAttribute("id", "dw_modalHolder");
			document.body.appendChild(dw_modalHolder);
		}
		$('#dw_modalHolder').append(newModalWindow);
		
		// $('body').append(newModalWindow);
		try
		{
			if(dontCenter == null || ! dontCenter)
				adjustModalWindowPosition(id,width);
		}
		catch(err) { /* ignore */ }
		$('#'+id).fadeIn("normal");
		nextzindexid ++;
	}
	itemvisibility[id]= true;
	return $('#'+id);
}

function modalWindowSample()
{
	var acctmaintWindow = showModalWindow('acctmaint-modal-panel', 'Your account', mouseX - 100 < 0 ? 0 : mouseX - 100, mouseY - 16, 200, 140, "error");
	var modalContent = "<p>This is a test</p>";
	acctmaintWindow.find('.modal-content').html(modalContent);
}

var IE = document.all?true:false;
var mouseX = 0;
var mouseY = 0;

function getMouseXY(e) 
{
	if (IE) 
	{ // grab the x-y pos.s if browser is IE
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	}	
	else 
	{  // grab the x-y pos.s if browser is NS
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	return true;
}

// start capturing mouse movement
function mcstart()
{
	if (!IE) 
		document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getMouseXY;
}

// stop capturing mouse movement
function mcstop()
{
	document.onmousemove = null;
}
var NotificationAutoTimeout = 8000; // 3 secs
var NotificationTimeout;
var NotificationVisible = false;

var errorwindow;
var notewindow;

$(document).ready(function()
	{
		var o = document.getElementById("ErrorMsg");
		if (o && o.innerHTML.length > 0)
		{
			if ( ! isIE )
			{
				if ( o.innerHTML.length > 500 )
					errorwindow = showModalWindow('error-modal-panel', 'Error!', 200, 180, 400, 300, "error");
				else if ( o.innerHTML.length > 200 )
					errorwindow = showModalWindow('error-modal-panel', 'Error!', 200, 180, 350, 250, "error");
				else
					errorwindow = showModalWindow('error-modal-panel', 'Error!', 200, 180, 300, 200, "error");				
				errorwindow.find('.modal-content').html(o.innerHTML);
			}
			else
				alert(o.innerHTML);
			
		}
		else
		{
			o = document.getElementById("NoteMsg");
			if (o && o.innerHTML.length > 0)
			{
				if ( ! isIE )
				{
					if ( o.innerHTML.length > 500 )
						notewindow = showModalWindow('note-modal-panel', 'Notification', 200, 180, 300, 250);
					else if ( o.innerHTML.length > 200 )
						notewindow = showModalWindow('note-modal-panel', 'Notification', 200, 180, 250, 180);
					else
						notewindow = showModalWindow('note-modal-panel', 'Notification', 200, 180, 200, 140);
					notewindow.find('.modal-content').html(o.innerHTML);
					NotificationTimeout = setTimeout("NotificationClose()", NotificationAutoTimeout);
					NotificationVisible = true;
				}
				else
					alert(o.innerHTML );
			}
		}
	});

	
function ErrorMessage(msg,caption)
{
	if ( isIE )
	{
		alert(msg);
		return;
	}
	
	if(! caption)
	{
		caption="Error";	
	}
	
	if (msg.length > 500 )
		errorwindow = showModalWindow('error-modal-panel', caption, 250, 150, 350, 280, "error");
	else if (msg.length > 200 )
		errorwindow = showModalWindow('error-modal-panel', caption, 250, 150, 300, 230, "error");
	else
		errorwindow = showModalWindow('error-modal-panel', caption, 250, 150, 250, 200, "error");
	errorwindow.find('.modal-content').html(msg);
}
function Notification(msg,caption)
{
	if ( isIE )
	{
		alert(msg);
		return;
	}

	if( ! caption)
	{
		caption = "Notification";	
	}
	if (msg.length > 500 )
		notewindow = showModalWindow('note-modal-panel', caption, 250, 150, 350, 280);
	else if (msg.length > 200 )
		notewindow = showModalWindow('note-modal-panel', caption, 250, 150, 300, 230);
	else
		notewindow = showModalWindow('note-modal-panel', caption, 250, 150, 250, 200);
	notewindow.find('.modal-content').html(msg);
	NotificationVisible = true;
}

function NotificationClose()
{
	if (NotificationVisible && itemvisibility["note-modal-panel"] )
	{
		$('#note-modal-panel').fadeOut('slow');
		NotificationVisible = false;
	}
}

function show_veil()
{
	if (veil_count == 0)
	{
		try
		{
			var ww = getwindowsize();
			var st = getscrolltop();
			$("#veil").css("top", "" + st.y + "px");
			$("#veil").css("width", "" + (ww.x - 1) + "px");
			$("#veil").css("height", "" + (ww.y - 1) + "px");
			$('#veil').css("filter", "alpha(opacity=20)");
			$('#veil').fadeIn('slow');
			try { after_showing_veil(); } catch (err) { }
			veil_visible = true;
		}
		catch (err) { alert(err.description ? err.description : err); }
	}
	veil_count++;
}

function hide_veil()
{
	if (veil_count == 1)
	{
		if (veil_visible)
		{
			$('#veil').fadeOut('slow');
			veil_visible = false;
			try { after_hiding_veil(); } catch (err) { }
		}
		if (in_submit_order)
		{
			in_submit_order = false;
			document.getElementById("btn_submit_order").value = submit_order_button_text;
		}
	}
	veil_count--;
}

//
function ie6selectfix(i)
{
	if (isIE6 && i == 'hide' && selectsvisible) //isIE6 defined at the top of the masterpage
	{
		$("select.ie6select").hide();
		selectsvisible = false;
	}
	else if (isIE6 && (!shipping_visible) && (!user_info_visible) && i == 'show' && (!selectsvisible))
	{
		$("select.ie6select").show();
		selectsvisible = false;
	}
}

function ie6selectfix2(i)
{
	if (isIE6 && i == 'hide' && selects2visible) //isIE6 defined at the top of the masterpage
	{
		$("select.ie6select2").hide();
		selects2visible = false;
	}
	else if (isIE6 && (!login_visible) && i == 'show' && (!selects2visible))
	{
		$("select.ie6select2").show();
		selects2visible = false;
	}
}

