// Property of YKM
// ***********************************************************
// Collection of useful functions and methods for general use
// ***********************************************************
// Short documentation:
// --------------------
// The YKM object is a container with several properties, functions and methods that allow quick
// access to the most commonly used functions used in projects.
// Usage within the JS:  YKM.property_or_method_name[(params...)]
// --------------------
// (function) browserType() - returns "IE", "MZ" or "OP" (Explorer, Mozilla/Netscape, Opera) or "false" for neither.
// (function) getRealX(obj) - returns the real X from top left (beta version). obj parameter can be object reference
//							  or id string.
// (function) getRealY(obj) - same as above, returns Y
// (function) isChildOf(obj1,obj2) - returns true if obj1 is a child in any generation of obj2.
// (property) mX, mY - contain the current X and Y positions of the mouse. Need YKM.Init(true); to be active.
// (property) mouseTarget - contains the reference to the current object being pointed by the mouse. 
//						    Needs YKM.Init(true); to be active.
// (function) Init(track_mouse) - Initializes special function(s) of the YKM object. Must be onLoad.


var YKM = {
	browserType: function() {
		// This funciton determines the type of browser engine 
		var isDOM = (document.getElementById)?true:false;
		var isOpera = (window.opera)?true:false;
		var isOpera5 = (isOpera && isDOM);
		var isOpera6 = (isOpera5 && window.print)?true:false;
		var isMSIE = isIE = (document.all && document.all.item && !isOpera);
		var isNC = (navigator.appName=="Netscape");
		var isNC4 = (isNC && !isDOM);
		var isNC6 = isMozilla = (isNC && isDOM);
		if (isDOM && isMSIE && isIE) {
			return "IE";
		} else if (isDOM && (isOpera || isOpera5 || isOpera6)) {
			return "OP";
		} else if (isDOM && (isNC || isNC4 || isNC6)) {
			return "MZ";
		} else {
			return false;
		}
	},
	getRealX: function(ofWho) {
		// Returns the "real" X-coordinate of the top left corner of the object's bounding box
		// The function tries to take into consideration the margins of containers, which normally are not taken into
		// the account when using the conventional properties.
		who = ((typeof ofWho == "string")?(document.getElementById(ofWho)):(ofWho));
		if (typeof who == "undefined" || who == null) {
			return 0;
		} else {
			if (who.tagName.toUpperCase() == "BODY" || who.tagName.toUpperCase() == "HTML") {
				return 0;
			} else {
				var reg = /px/g;
				var marL = who.style.marginLeft;
				if (marL!="") {
					marL = parseInt(marL.replace(reg,""));
				} else {
					marL = 0;
				}
				if (this.browserType()=="IE") {
					return who.offsetLeft + this.getRealX(who.offsetParent);
				} else if (this.browserType()=="OP" && marL!=0) {
					return who.offsetLeft + this.getRealX(who.offsetParent) - marL;
				} else {
					return who.offsetLeft + this.getRealX(who.offsetParent);
				}
			}
		}
	},
	getRealY: function(ofWho) {
		// Returns the "real" Y-coordinate of the top left corner of the object's bounding box
		// The function tries to take into consideration the margins of containers, which normally are not taken into
		// the account when using the conventional properties.
		who = ((typeof ofWho == "string")?document.getElementById(ofWho):ofWho);
		if (typeof who == "undefined" || who == null) {
			return 0;
		} else {
			if (who.tagName.toUpperCase() == "BODY" || who.tagName.toUpperCase() == "HTML") {
				return 0;
			} else {
				var reg = /px/g;
				var marT = who.style.marginTop;
				if (marT!="") {
					marT = parseInt(marT.replace(reg,""));
				} else {
					marT = 0;
				}
				if (this.browserType()=="IE") {
					return who.offsetTop + this.getRealY(who.offsetParent) + marT;
				} else if (this.browserType()=="OP" && marT!=0) {
					return who.offsetTop + this.getRealY(who.offsetParent) - marT;
				} else {
					return who.offsetTop + this.getRealY(who.offsetParent);
				}
			}
		}
	},
	isChildOf: function(whois,ofwho) {
		// This function returns "true" if the "whois" object is a child of "ofwho" object on any level of hierarchy.
		// Both parameters can be either string IDs of the objects or references to the objects
		who = ((typeof whois == "string")?document.getElementById(whois):whois);
		par = ((typeof ofwho == "string")?document.getElementById(ofwho):ofwho);
		if (typeof who == "undefined" || who == null || typeof par == "undefined" || par == null) {
			return false;
		} else {
			if ((par.tagName.toUpperCase() == "HTML" || par.tagName.toUpperCase() == "BODY") && (who.tagName.toUpperCase() != "HTML" && who.tagName.toUpperCase() != "BODY")) {
				return true;
			} else 
			if ((par.tagName.toUpperCase() != "HTML" && par.tagName.toUpperCase() != "BODY") && (who.tagName.toUpperCase() == "HTML" || who.tagName.toUpperCase() == "BODY")) {
				return false;
			} else 
			if (who.parentNode == par) {
				return true;
			} else {
				return this.isChildOf(who.parentNode,par);
			}
		}
	},
	mX: 0,		// This variable contains the X-position of the mouse pointer when tracking is active
	mY: 0,		// This variable contains the Y-position of the mouse pointer when tracking is active
	mouseTarget: null,
	trackMouse: function(ev) {
		// This funciton is used to track the mouse movements and report the location of the cursor to the YKM object
		if (YKM.browserType() == "IE") {
			YKM.mouseX = event.x;
			YKM.mouseY = event.y;
			YKM.mouseTarget = event.srcElement;
		} else {
			YKM.mouseX = ev.pageX;
			YKM.mouseY = ev.pageY;
			YKM.mouseTarget = ev.target;
		}
	},
	startTracking: function() {
		// Initiates tracking of the mouse position
		document.onmousemove = this.trackMouse;
	},
	_HOUR: -1,
	_DAY: -2,
	_WEEK: -3,
	_MONTH: -4,
	_YEAR: -5,
	SetCookie: function(cname, cvalue, expiration, path, dom, secure) {
		// The "expires" parameter can receive:
		// - string value of the desired expiration date, in which case it is written to the cookie as is.
		// - null value meaning no expiration date
		// - preset constants (see above the function) add to the current datetime
		// - greater than 0 numeric value of milliseconds to add to the current datetime
		var cookiestr = cname+"="+cvalue;
		if (expiration != null) {
			if (typeof expiration == "string") {
				// Custom expiration date received
				cookiestr += (";expires="+expiration);
			} else if (expiration <0) {
				switch (cookiestr) {
					case -1: var addon = (1000 * 60 * 60); break;
					case -2: var addon = (1000 * 60 * 60 * 24); break;
					case -3: var addon = (1000 * 60 * 60 * 24 * 7); break;
					case -4: var addon = (1000 * 60 * 60 * 24 * 30); break;
					case -4: var addon = (1000 * 60 * 60 * 24 * 365); break;
					default: var addon = 0;
				}
				var d = new Date();
				var expdate = d.getTime() + addon;
				cookiestr += (";expires="+expdate);
			} else {
				var d = new Date();
				var expdate = d.getTime() + expiration;
				cookiestr += (";expires="+expdate);
			}
		}
		if (path && path != null) {
			cookiestr += (";path="+path);
		}
		if (dom && dom != null) {
			cookiestr += (";domain="+dom);
		}
		if (secure && secure == true) {
			cookiestr += (";secure");
		}
		document.cookie = cookiestr;
	},
	GetCookie: function(cname) {
		// Returns the string value of the desired cookie or boolean "false" if the cookie not found
		var temparr = document.cookie.split("; ");
		for (var i=0;i<temparr.length;i++) {
			var tempitem = temparr[i].split("=");
			if (tempitem[0] == cname) {
				return tempitem[1];
			}
		}
		return false;
	},
	CookiesSupported: function() {
		// Returns true if cookie set/get cycle was succesful
		this.SetCookie("cookietest","1");
		var test = this.GetCookie("cookietest");
		if (test == "1") {
			this.SetCookie("cookietest","");
			return true;
		} else {
			return false;
		}
	},
	getDocHeight: function() {
		// Returns the true height of the document (including the scroll)
		if (this.browserType() == "IE") {
			var s1 = document.body.scrollHeight;
			var s2 = document.body.parentNode.scrollHeight;
			if (s1<s2) return s2; else return s1;
		} else {
			return document.body.scrollHeight;
		}
	},
	getViewportTop: function() {
		// Returns the top coordinate of the currently viewport
		return document.body.scrollTop;
	},
	getViewportHeight: function() {
		// Returns the height of the client visible area of the browser
		if (this.browserType() == "IE") {
			return document.body.clientHeight;
		} else {
			return document.body.clientHeight;
		}
	},
	Init: function(track) {
		if (track) {
			this.startTracking();
		}
	}
}

function test() {
}

function Init() {
	YKM.Init();
	alert(YKM.getViewportHeight());
}




