var menuTimeout;
var subMenuTimeout;

/*
Prevent JS error from appearing on the client browser
*/
function stopErrors() {
	return true;
}
//window.onerror = stopErrors;


/*
Trim a string from leading and trailing spaces
*/
function Trim(iString) {
  iString = iString.replace( /^\s+/g, "" );
  return iString.replace( /\s+$/g, "" );
}


/*
Pre-load image functions
*/
function preloadBaseImages() {
	var imageArray = new Array('loading.gif', 
							   'progress_dots.gif');
							   
    preloadImages(imageArray, '/images/');
}


/*
Pre-load the image array
*/
function preloadImages(imageArray, dirPrefix) {
	for(i = 0; i < imageArray.length; i++) {
		var imageName = new Image();
        imageName.src = dirPrefix + imageArray[i];
	}
}


/*
Find an object on the page
*/
function findObj(objName) {
	var theObj = document.getElementById(objName);
	
	if (!theObj) {
		//theObj = document.all[objName];
	}
	
	return theObj;
}


/*
Replaces text with by in string
*/
function replace(string, text, by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


/*
"width=480,height=350,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0"
*/
function launchWin(urlPath, winName, params) {	
	var paramArray = params.split(",");
	var windowWidth = Trim(paramArray[0].split("=")[1]);
	var windowHeight = Trim(paramArray[1].split("=")[1]);

	var windowLeft = (self.screen.width-windowWidth) / 2;
	var windowTop = (self.screen.height-windowHeight) / 2;
	
	params = params + ',top=' + windowTop + ',left=' + windowLeft;
	//alert(params);
	
	var winName = window.open(urlPath,winName,params);
	
	//winName.moveTo((self.screen.width-windowWidth) / 2, (self.screen.height-windowHeight) / 2);
	winName.focus();

	/*
	var paramArray = params.split(",");
	var windowWidth = Trim(paramArray[0].split("=")[1]);
	var windowHeight = Trim(paramArray[1].split("=")[1]);	
	
	winDialog = window.showModalDialog(urlPath, winName,"dialogHeight:" + windowHeight + "px;dialogWidth=" + windowWidth + "px;status:0;help:0;center:1");
	*/
}


/*
Reload the current window after a set time
*/
function reloadWindowTimeout() {
	setTimeout('window.location.reload();', 250);
}


/*
Redirect the current window after a set time
*/
function redirectWindowTimeout(iLocation) {
	setTimeout('window.location.href=\'' + iLocation + '\'', 250);
}


/*
Detect ESC key in a text field
*/
function handleEdit(e, iCancelFunction, iEnterFunction) {
	var kC = (window.event) ? event.keyCode : e.keyCode;
	var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;

	if (kC == Esc) {
    	//handleEdit = true;
    	eval(iCancelFunction);
	}
	
	if (kC == 13) {
		eval(iEnterFunction);
	}
}


/*
Popup the menu for the specified parent
*/
function popupMenu(iParent) {
	clearTimeout(menuTimeout);
	
	// popup_toolbar_btn_2
	var tmpTop = $(iParent).offset().top + $(iParent).height() - 2;
	
	var tmpID = iParent.id.replace('toolbar_btn_', '');
	
	$.each($('[id*=popup_]'), function(i, item) {
		if (item.id != 'popup_' + tmpID) {
			$(item).slideUp(500);
		}
	});	

	$('#popup_' + tmpID).css('top', tmpTop);
	$('#popup_' + tmpID).css('left', $(iParent).offset().left - 2);
	if ($(iParent).width() > $('#popup_' + tmpID).width()) {
		$('#popup_' + tmpID).css('width', $(iParent).width());
	}
	
	$('#popup_' + tmpID).slideDown(500);
}


/*
Popup a sub menu (if it exists)
*/
function popupSubMenu(iParent) {
	var tmpID = iParent.id.replace('menu_item_', '');
	
	var tmpSubMenu = $('#sub_menu_' + tmpID);
	if (tmpSubMenu.size()) {
		tmpSubMenu.css('top', $(iParent).offset().top);
		tmpSubMenu.css('left', $(iParent).offset().left + $(iParent).width() + 10);
		
		tmpSubMenu.show();
	}
}


/*
Popup the menu for the specified parent
*/
function closeMenus() {
	menuTimeout = setTimeout('$(\'[id*=popup_]\').slideUp(500);', 500);
}


/*
Open a window maximized
*/
function openWinMax(aURL, aWinName, params) {
   var wOpen;
   var sOptions;

   sOptions = params;
   sOptions = sOptions + ',width=' + (screen.availWidth - 10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight - 122).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';

   wOpen = window.open('', aWinName, sOptions);
   wOpen.location = aURL;
   wOpen.focus();
   wOpen.moveTo(0, 0);
   wOpen.resizeTo(screen.availWidth, screen.availHeight);
   return wOpen;
}


/*

*/
function scrollNews() {
	$('#pageContent-1').css({'marginTop': $('#main_content').height() - 40});
	
	var tmpModifier = $('#pageContent-1').height() / 210;
	var tmpSpeed = 30000 + (tmpModifier * 7500);
	
	$('#pageContent-1').startAnimation({'marginTop': "-" + $('#pageContent-1').height()}, tmpSpeed, 'linear', function() { 
		scrollNews();
	});
}

