/* *******************************************************
J  a  v  a  c  a  t  z
Filename:   common.js
Purpose:    This file is created to hold common functions
            that are useful to other projects.
            Please do not put other codes in this file.
Modified by: KY 2010.05.04
******************************************************* */

/* kyMenu 2010.08.31 */
var kyMenu = {
	showCurrent: true,
	statusEffect: false,
	currentStayOn: true,
	init: function(action, mouseEvent) {
		if(action) action = action.toLowerCase();
		if(mouseEvent) mouseEvent = mouseEvent.toLowerCase();
		$$('.kyMenu ul ul').each( function(node) {
			$(node).hide();
		} );
		$$('.kyMenu ul').each( function(node) {
			$(node).hide();
		} );
		$$('.kyMenu').each( function(menu) {
			if($(menu).hasClassName('2rows')) {
				$(menu).select('li .topSifr').each( function(node) {
					var wordArr = $(node).innerHTML.split(' ');
					var text = '';
					for(var i=0; i<wordArr.length; i++) {
						if(i == wordArr.length - 1) {
							if(i == 0) text += '&nbsp;';
							text += '<br />' + wordArr[i];
						} else {
							text += wordArr[i] + ' ';
						}
					}
					$(node).update(text);
				} );
			}
			if($(menu).hasClassName('center')) {
				var padding = parseInt(($(menu).up().getWidth() - $(menu).getWidth()) / 2) + 'px';
				$(menu).up().setStyle({'paddingLeft': padding,'paddingRight': padding});
			}
		} );
		$$('.kyMenu li').each( function(node) {
			var kyMenuUL = $(node).up('.kyMenu');
			if($(kyMenuUL).hasClassName('fade') || $(kyMenuUL).hasClassName('appear')) action = 'fade';
			else if($(kyMenuUL).hasClassName('blind')) action = 'blind';
			if($(kyMenuUL).hasClassName('click')) {
				mouseEvent = 'click';
				if($(node).down('ul')) $(node).addClassName('close');
			} else if($(kyMenuUL).hasClassName('hover')) {
				mouseEvent = 'hover';
				if($(node).down('ul')) $(node).down('a').addClassName('moreNav');
			} else if($(kyMenuUL).hasClassName('noSub')) {
				mouseEvent = 'noSub';
			}
			if($(node).hasClassName('current') || $(node).down('a').hasClassName('current')) {
				$(node).addClassName('active');
				$(node).down('a').addClassName('active');
				if($(node).down('ul')) {
					if(mouseEvent=='click') {
						$(node).addClassName('open');
					}
					new Effect.Appear($(node).down('ul'), { duration: 0 });
				}
			}
			if(mouseEvent != 'noSub') {
				if(!mouseEvent || mouseEvent == 'hover') {
					if(!(kyMenu.showCurrent && $(node).down('a').hasClassName('current'))) {
						$(node).observe('mouseenter', menuover = function(e) {
							$(node).addClassName('active');
							$(node).down('a').addClassName('active');
							if($(node).down('ul') && !$(node).down('ul').visible()) {
								$(node).removeClassName('close').addClassName('open');
								if(!action || action == 'fade' || action == 'appear') {
									new Effect.Appear($(node).down('ul'), { duration: 0.3, delay: 0.2 });
								} else if(action == 'blind') {
									$(node).setStyle({'visibility':'visible'});
									new Effect.BlindDown($(node).down('ul'), { duration: 0.3, delay: 0.2, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}).show(); } });
								}
							}
						} );
						$(node).observe('mouseleave', menuleave = function(e) {
							$(node).removeClassName('active');
							$(node).down('a').removeClassName('active');
							if($(node).down('ul') && !$(node).down('ul').hasClassName('currentUl')) {
								$(node).removeClassName('open').addClassName('close');
								if(!action || action == 'fade' || action == 'appear') {
									new Effect.Fade($(node).down('ul'), { duration: 0.3 });
								} else if(action == 'blind') {
									new Effect.BlindUp($(node).down('ul'), { duration: 0.3, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}).hide(); } });
								}
							}
						} );
					}
				} else if(mouseEvent == 'click') {
					if($(node).down('ul')) {
						$(node).down('a').href = 'javascript:void(0);';
						$(node).down('a').title = 'Click to toggle subpages';
						if(!$(node).hasClassName('current')) $(node).addClassName('close');
						else $(node).addClassName('open');
					}
					$(node).down('a').observe('click', menuclick = function(e) {
						if($(this).hasClassName('current') && kyMenu.currentStayOn) return;
						if(!kyMenu.statusEffect) {
							kyMenu.statusEffect = true;
							if(!$(node).hasClassName('active')) {
								$(node).addClassName('active');
								$(node).down('a').addClassName('active');
								if($(node).down('ul')) {
									$(node).removeClassName('close');
									$(node).addClassName('open');
									if(!action || action == 'fade' || action == 'appear') {
										new Effect.Appear($(node).down('ul'), { duration: 0.3, delay: 0.2, afterFinish: function() { kyMenu.statusEffect = false; } });
									} else if(action == 'blind') {
										new Effect.BlindDown($(node).down('ul'), { duration: 0.3, delay: 0.2, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}).show(); kyMenu.statusEffect = false; } });
									}
								}
							} else {
								$(node).removeClassName('active');
								$(node).down('a').removeClassName('active');
								if($(node).down('ul')) {
									$(node).removeClassName('open');
									$(node).addClassName('close');
									if(!action || action == 'fade' || action == 'appear') {
										new Effect.Fade($(node).down('ul'), { duration: 0.3, afterFinish: function() { kyMenu.statusEffect = false; } });
									} else if(action == 'blind') {
										new Effect.BlindUp($(node).down('ul'), { duration: 0.3, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}).hide(); kyMenu.statusEffect = false; } });
									}
								}
							}
						}
					} );
				}
			}
		} );
		if(kyMenu.showCurrent) {
			$$('.kyMenu .current').each( function(node) {
				$(node).up('ul').show();
				$(node).up('li').addClassName('open').removeClassName('close');
			} );
		}
	}
};



/* getSiteDomain */
function getSiteDomain(url) {
	var siteDomain = url.toString().substring(url.toString().indexOf('://'));
	if(siteDomain.length == url.length) return null;
	else {
		siteDomain = siteDomain.substring(3, siteDomain.length);
		siteDomain = siteDomain.substring(0, (siteDomain.indexOf('/')!=-1)?(siteDomain.indexOf('/')):(siteDomain.length));
		siteDomain = siteDomain.substring(0, (siteDomain.indexOf(':')!=-1)?(siteDomain.indexOf(':')):(siteDomain.length));
		var tempDomain = siteDomain.substring(0, siteDomain.lastIndexOf('.'));
		tempDomain = tempDomain.substring(tempDomain.lastIndexOf('.')+1);
		siteDomain = tempDomain + siteDomain.substring(siteDomain.lastIndexOf('.'));
		return siteDomain;
	}
}

/* Get URL Parameter */
function getParameter(name) {		// get value of an ? parameter in url
	var url = window.location.href;
	var paramsStart = url.indexOf("?");
	if(paramsStart != -1) {
		var paramString = url.substr(paramsStart + 1);
		var tokenStart = paramString.indexOf(name);
		if(tokenStart != -1) {
			paramToEnd = paramString.substr(tokenStart + name.length + 1);
			var delimiterPos = paramToEnd.indexOf("&");
			if(delimiterPos == -1) {
				return paramToEnd;
			} else {
				return paramToEnd.substr(0, delimiterPos);
			}
		} else {
			return "";
		}
	}
}


/* Set, Retrieve, Delete Cookies */
function setCookie(name, value, expires) { 
	var expDate = new Date(expires);
	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expDate.toGMTString());
}
function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}
function delCookie(name) {
	document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}

/* Print Page */
function printPage() {
	var this_url = document.location.href;
	if(this_url.lastIndexOf('?') == -1) {	// there is no ? parameters
		this_url = this_url + "?screenmode=print";
	} else {
		this_url = this_url + "&screenmode=print";
	}
	var day = new Date();
	var id = day.getTime();
	eval("page" + id + " = window.open(this_url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=640,height=480'); page" + id + ".focus();");
}
function printPageWithURL(ExtraURL) {
	var day = new Date();
	var id = day.getTime();
	eval("page" + id + " = window.open(ExtraURL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=640,height=480'); page" + id + ".focus();");
}
function printPage2(){
	print();
}


/* Browser Detection */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

/* Fix PNG in MSIE, used in bodyOnLoad in needed */
function fixPNG() {
	// var arVersion = navigator.appVersion.split("MSIE");
	// var version = parseFloat(arVersion[1]);

	// if ((version >= 5.5) && (version < 7)) {
	if( BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 5.5 && BrowserDetect.version < 7 && BrowserDetect.OS == "Windows" ) {
	   for(var i=0; i<document.images.length; i++) {
		  var img = document.images[i];
		  var imgName = img.src.toUpperCase();
		  if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			 var imgID = (img.id) ? "id='" + img.id + "' " : "";
			 var imgClass = (img.className) ? "class='" + img.className + " fixPNG' " : "class='fixPNG'";
			 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			 var imgStyle = "display:inline-block;" + img.style.cssText ;
			 if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			 if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			 var strNewHTML = "<span " + imgID + imgClass + imgTitle
			 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle // + ";"
			 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			 + "(src=\'" + img.src + "\', sizingMethod='scale');\">"
			 + "<span class='unfixPNG' style='display: none;'>" + img.src + ":::" + img.style.cssText + "</span>"	// moded by KY
			 + "</span>";
			 img.outerHTML = strNewHTML;
			 i = i-1;
		  }
	   }
	}
}
function unfixPNG() {	// undo fixPNG -- added by KY, not 100% foolproof yet.
	var itemFixPNG = $$('.fixPNG');
	var itemFixPNG_span = $$('.fixPNG .unfixPNG');
	var counter = 0;
	if(itemFixPNG.length > 0) {
		itemFixPNG.each( function(node) {
			var tmpText = itemFixPNG_span[counter].innerHTML.split(":::");
			var imgSrc = tmpText[0];
			var imgID = ($(node).id) ? "id='" + $(node).id + "' " : "";
			var imgClass = ($(node).className) ? "class='" + $(node).className + "' " : "";
			imgClass = imgClass.replace("fixPNG", "");
			var imgTitle = ($(node).title) ? "title='" + $(node).title + "' " : "";
			var imgStyle = (tmpText[1]!="") ? "style='" + tmpText[1] + "' " : "";
			$(node).outerHTML = "<img src='" + imgSrc + "' alt='beforePrintPNG' " + imgID + imgClass + imgTitle + imgStyle + " />";
			counter++;
		} );
	}
}



/* *** adjustText *** */
function adjustText(pc) {
	if(pc) {
		// $('main').setStyle({ 'fontSize': pc+'%' });
		new Effect.Morph($('main'), { style: { 'fontSize': pc+'%' }, duration: 0.5 });
	}
}



/* *** IE Update for ActiveX click to activate *** */
function ieupdate() {
	objects = document.getElementsByTagName("object");
	for (var i = 0; i < objects.length; i++)
	{
		objects[i].outerHTML = objects[i].outerHTML;
	}
}

