/* Grenbell Javascript utils */

function checkIE6 () {
	if (WSystem.ieVersion<7) {
	  document.getElementById('warning_ie6').style.display='block';
	}
}

/* Text size stuff */
function incTextSize () {
  var currentFontSize = $('#content').css('font-size');
  var currentFontSizeNum = parseFloat(currentFontSize, 10);
  var newFontSize = currentFontSizeNum+2;
  if (newFontSize>=20)
  	return;
  $('#content').css('font-size', newFontSize);
  if (newFontSize>11) {
		$('.person_list td').css('font-size', newFontSize-2);
		$('.person_list td h3').css('font-size', newFontSize+1);

		$('.press_description').css('font-size', newFontSize-1);
	}
  $.cookie('grenbellFontSize', $('#content').css('font-size'), {path:'/'});

}

function decTextSize () {
	var currentFontSize = $('#content').css('font-size');
  var currentFontSizeNum = parseFloat(currentFontSize, 10);
  var newFontSize = currentFontSizeNum-2;
  if (newFontSize<=10)
  	return;
  $('#content').css('font-size', newFontSize);
  if (newFontSize>11) {
		$('.person_list td').css('font-size', newFontSize-2);
		$('.person_list td h3').css('font-size', newFontSize+1);

		$('.press_description').css('font-size', newFontSize-1);
	}
  $.cookie('grenbellFontSize', $('#content').css('font-size'), {path:'/'});

}

function initTextSize () {
	if ($.cookie('grenbellFontSize')) {
	  
	  $('#content').css('font-size', $.cookie('grenbellFontSize'));
    var currentFontSize = $('#content').css('font-size');
	  var newFontSize = parseFloat(currentFontSize, 10);

	  if (newFontSize>11) {
		  $('.person_list td').css('font-size', newFontSize-2);
			$('.person_list td h3').css('font-size', newFontSize+1);

			$('.press_description').css('font-size', newFontSize-1);
		}
	}
}


/* Popup menu stuff */
menuTimer = null;
menuTimer2 = null;
menuHideId = 0;
menuOpenId = 0;
menuHideTimeout = 250;
menuOpenTimeout = 150;
menuIsFilter = false;

function showPopupMenuFilter () {
	if (menuIsFilter)
		return;
	menuIsFilter = true;		
	var wndDim = WSystem.getWindowDim ();
	var filterWidth  = wndDim.clientWidth;
	var filterHeight = wndDim.clientHeight;
	if (wndDim.clientHeight<wndDim.windowHeight)
		filterHeight  = wndDim.windowHeight;
	var obj = document.getElementById ('main_frame');
	var coords = WSystem.getAbsoluteCoords (obj);
	if (WSystem.ieVersion<7) {
		$('#popupMenuFilter').attr('style', 'display:none;left:-'+coords.x+'px;top:0px;width:'+filterWidth+'px;height:'+filterHeight+'px;');	
	}
	else
		$('#popupMenuFilter').attr('style', 'display:none;left:-'+coords.x+'px;top:0px;min-width:'+filterWidth+'px;min-height:'+filterHeight+'px;');	

	if (WSystem.isIE)
		$('#popupMenuFilter').css('filter', 'alpha(opacity=40)');


	$('#popupMenuFilter').fadeIn('1000', function() {
    // Animation complete
  });
	// $('#popupMenuFilter').show();
}

function hidePopupMenuFilter () {
	if (menuIsFilter) {
		$('#popupMenuFilter').hide();	
		menuIsFilter = false;
	}
}

function clearPopupMenuTimeout () {
	if (menuTimer!=null) {	
		clearTimeout (menuTimer);
		menuTimer = null;

	}
}
function clearPopupMenuTimeout2 () {
	if (menuTimer2!=null) {	
		clearTimeout (menuTimer2);
		menuTimer2 = null;
	}
}


function hidePopupMenu (id) {
	if (menuTimer!=null) {
		// Reset close timer if quick mouse moving
		clearTimeout (menuTimer);
		menuTimer = null;
		
		// Dont open again
		if (menuOpenId==id && menuTimer2) {
			clearPopupMenuTimeout2 ();
		}
	}

	menuHideId = id;
	menuTimer = setTimeout(function () { 		
		menuTimer=null; 
		$('#menu_dup').hide (); 
		$('#popup_menu_'+id).hide();
		hidePopupMenuFilter ();  
		menuOpenId = 0;
	}, menuHideTimeout);
}

function showPopupMenu (id) {	
	if (menuTimer!=null) {
		// Same menu try to close
		if (id==menuOpenId) {
			clearPopupMenuTimeout ();
			return;
		}
	}
	
	if (menuTimer2!=null) {
		// Reset open timer if quick mouse moving
		clearTimeout (menuTimer2);
		menuTimer2 = null;
		if (id==menuOpenId) {
			// Its just open
			return;
		}
	}
	// Open timed
	menuTimer2 = setTimeout(function () { 
		menuTimer2 = null;
		// Mark as open
		menuOpenId = id;
		// Hide if there is something to hide
		$('#popup_menu_'+menuHideId).hide(); 
		var obj = document.getElementById ('menu');
		showPopupMenuFilter ();		
		$('#menu_dup').attr('style', 'width='+obj.offsetWidth+'px;height='+obj.offsetHeight+'px;z-index:12;position:absolute;right:0px;');
		$('#menu_dup').show();
		$('#popup_menu_'+id).show();
	}, menuOpenTimeout);

}


/* Ajax popup window stuff */
popupWindowId = '';

function showPopupWindowFilter () {
	var wndDim = WSystem.getWindowDim ();
	var filterWidth   = wndDim.documentWidth;
	var filterHeight  = wndDim.documentHeight;
	if (WSystem.ieVersion<7)
		$('#popupWindowFilter').attr('style', 'display:block;width:100%;height:'+wndDim.windowHeight+'px;');	
	else
		$('#popupWindowFilter').attr('style', 'display:block;min-width:'+filterWidth+'px;min-height:'+filterHeight+'px;');
	$('#popupWindowFilter').show();	
}

function hidePopupWindowFilter () {
	$('#popupWindowFilter').hide();	
}

function closePopupWindow ()
{
	var obj = document.getElementById (popupWindowId);
	if (obj) {
		obj.style.display="none";
		obj.style.visibility = "hidden";
		hidePopupWindowFilter ();	
		WSystem.removeEvent (document,'keyup', onDocumentKeyPress);
		WSystem.removeEvent (document,'mousedown', onDocumentMouseClick);
	}
}

function onDocumentKeyPress (evt) {
	var keyCode = evt.which;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		keyCode = window.event.keyCode;
	if (keyCode==27) { // ESC
		closePopupWindow ();
	}
	return true;
}

this.onDocumentMouseClick = function (evt)  {
	var mouseCoords = WSystem.getMouseCoords (evt);
	var obj = document.getElementById (popupWindowId);
	if (obj) {
		popupCoords = WSystem.getAbsoluteCoords (obj);
		var x  = popupCoords.x;
		var y  = popupCoords.y;
		var x2 = popupCoords.x+parseInt (obj.offsetWidth);
		var y2 = popupCoords.y+parseInt (obj.offsetHeight);
		if (!(
				mouseCoords.x>x && 
		    mouseCoords.x<x2 && 
		    mouseCoords.y>y && 
		    mouseCoords.y<y2 )) {
			closePopupWindow ();
		}				
	}		
}

function showPopupWindow (id, popupURL, fAutoPrint) {
	var obj = document.getElementById (id);
	if (!obj)
		return;
	
	popupWindowId = id;
	var scrollPos = WSystem.getScrollPos ();
	var wndDim		= WSystem.getWindowDim ();
	var wndHeight = wndDim.windowHeight;
	var wndWidth  = wndDim.windowWidth;
	
	var x0 = scrollPos.x;			
	var x1 = x0 + wndWidth;
	var y0 = scrollPos.y;	
	var y1 = y0 + wndHeight;
	
  showPopupWindowFilter ();		
	obj.style.display="block";			

	// $('#popup_content').jScrollPane({scrollbarWidth: 0, dragMaxHeight:0});
	// Progress bar
	$('#'+id+'_content').html ('<div style="text-align:center;"><img src="images/progress.gif"></div>');
	obj.style.left = (x0+(wndWidth  - parseInt (obj.offsetWidth))  /2)+"px";
	obj.style.top  = (y0+(wndHeight - parseInt (obj.offsetHeight)) /2)+"px";
	obj.style.visibility = "visible";	

	// Esc key functions
	WSystem.addEvent (document,'keyup', onDocumentKeyPress, false);
	WSystem.addEvent (document,'mousedown', onDocumentMouseClick, false);

	// Load ajax content
	$.ajax({ url: popupURL, success: function(data) {
		$('#'+id+'_content').html (data); 								
		// $('#popup_content').jScrollPane({scrollbarWidth: 8, dragMaxHeight:120});			
		if (fAutoPrint) {
			window.print ();
			closePopup ();
		}
	}});
}
