﻿/* K I R O W S K I */

// add multiple loadevents: use it AFTER the start of body tag
// to preserve inline functions!
// (source: http://simon.incutio.com/archive/2004/05/26/addLoadEvent)

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) { oldonload(); }
			func();
		}
	}
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = =
// SPECIAL EVENTS
// = = = = = = = = = = = = = = = = = = = = = = = = = = =

//mouseenter/leave emulation based on pattern 
//recognition instead of cancelling the bubble
function onMouseLeave( pTarget, pAction ) {
	pTarget.realLeaveFunction = pAction; //store action
	pTarget.leaveFunction = function() { pTarget.realLeaveFunction(); }

	if (pTarget.onmouseout) pTarget.onmouseoutSave1 = pTarget.onmouseout; //Save1 previous mouseout
	pTarget.onmouseout = function() {
		if (pTarget.onmouseoutSave1) pTarget.onmouseoutSave1();
		this.ltimer = setTimeout(this.leaveFunction.bind(this),100);
	}
	
	if (pTarget.onmouseover) pTarget.onmouseoverSave1 = pTarget.onmouseover; //Save1 previous mouseover
	pTarget.onmouseover = function() {
		if (pTarget.onmouseoverSave1) pTarget.onmouseoverSave1();
		clearTimeout(this.ltimer);
	}

}

function onMouseEnter( pTarget, pAction ) {
	
	pTarget.enterFunction = pAction; //store action
	
	pTarget.stepPattern = function() { this.epattern = -1; } //reset pattern

	if (pTarget.onmouseover) pTarget.onmouseoverSave2 = pTarget.onmouseover; //save previous mouseover
	pTarget.onmouseover = function() {
		if (pTarget.onmouseoverSave2) pTarget.onmouseoverSave2();
		if (this.epattern == 1) { this.epattern = 0; clearTimeout(this.etimer); }
			else { (this.epattern = 0); this.enterFunction(); }
	}
	
	if (pTarget.onmouseout) pTarget.onmouseoutSave2 = pTarget.onmouseout; //save previous mouseout
	pTarget.onmouseout = function() {
		if (pTarget.onmouseoutSave2) pTarget.onmouseoutSave2();
		if (this.epattern == 0) this.epattern = 1;
		this.etimer = setTimeout(this.stepPattern.bind(this),100);
	}
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = =
// PROTOTYPE ENHANCEMENTS
// = = = = = = = = = = = = = = = = = = = = = = = = = = =


//shorthand for getElementsByTagName
function $T() {
	var elements = new Array();
	var elStrings = new Array();
	var elObjects = new Array();
	
	for (var i = 0; i < arguments.length; i++) { 
		if (arguments[i] != null)
			if (typeof arguments[i] == 'string') elStrings.push(arguments[i])
				else elObjects.push(arguments[i]); 
	}
	
	elObjects.each( function(eo){ 
		elStrings.each( function(es){ 
			elements = elements.concat($A( eo.getElementsByTagName(es) ));
		});
	});

	return elements;
}

//string.trim -- remove whitespace
String.prototype.ltrim = function () { var re = /\s*((\S+\s*)*)/; return this.replace(re, "$1"); }
String.prototype.rtrim = function () { var re = /((\s*\S+)*)\s*/; return this.replace(re, "$1"); }
String.prototype.trim = function () { return this.rtrim().ltrim(); }

//string.toArray
String.prototype.toArray = function() {
	var pString = this;
	var retArr  = new Array();
	for (var i = 0; i < pString.length; i++) retArr.push(pString.substr(i,1));
	return retArr;
}

//+array.toString -- only for valid indexes
Array.prototype.toString = function() {
	var retString = new String();
	var pArr  = this;
	pArr.each( function(ae) { retString += ae });
	return retString;
}


//array.has (uses proto's indexOf)
Array.prototype.has = function( pItem) {
	if (this.indexOf( pItem ) != -1) return true
		else return false;
}

//array.remove (remove item, NOT the item at index)
Array.prototype.remove = function( pItem ) {
	var pos = this.indexOf( pItem );
	if (pos != -1) this.splice(pos,1);
}

//string.has
String.prototype.has = function ( pSubStr ) { if (this.indexOf(pSubStr)!=-1) { return(true); } else return(false); }

//string.flip -- change first substring to second and vice versa
String.prototype.flip = function( pFrom, pTo ) {
	if (this.has(pFrom)) return(this.replace(pFrom,pTo))
		else return(this.replace(pTo,pFrom));
}

//string.addOrRemove -- removes if exists, add otherwise
String.prototype.addOrRemove = function( pSubString ) {
	if (this.has(pSubString)) return(this.replace(pSubString,''))
		else return(this+pSubString);
}

//string.remove
String.prototype.remove = function( pSubString ) { return(this.replace(pSubString,'')); }

//string.addonce -- add if does not exist
String.prototype.addonce = function( pSubString ) { if (!this.has(pSubString)) return (this+pSubString); else return (this); }

//string.toXML
String.prototype.toXML = function () { return ((new DOMParser()).parseFromString(this, "text/xml")); }

//xmlEntities
String.prototype.xmlEntities = function() {
	var pString = this;
	pString = pString.replace('&nbsp;','\u00a0'); //nbsp
	pString = pString.replace('&amp;','\u0026'); //amp
	return pString;
}

//string.filter -- filter out characters based on regexp
String.prototype.filter = function( pRegexp ) {
	var validChars = pRegexp;
	var stringBuff = this;
	var failed = 0;
	var selChar = new String();
	
	for (var i = 0; i < stringBuff.length; i++)
	{
		var selChar = stringBuff.substr(i,1);
		if (selChar.match(validChars)==null)
		{
			failed = 1;
			stringBuff = stringBuff.replace(selChar,'?');
		}
	}
	if (failed == 1) { stringBuff = stringBuff.replace(/\?/gi,''); }
	return(stringBuff);
}


//window.setHeight without prototyping
window.setHeight = function( pNum ) { window.resizeBy( 0, pNum - document.documentElement.clientHeight ); }

//event.parent wo proto (IE still sucks)
Event = new Object();
Event.parent = function( pEvent ) {
	if (pEvent.srcElement) return pEvent.srcElement
			else if (pEvent.target) return pEvent.target
				else return null;
}

//enhance proto's Element (singletonish)
Object.extend(Element, {

	//Element.append - the cleaner way... proto 1.5 will be better with its Element.Methods
	//can accept text when pChild's first char is "$"
	append: function( pTarget, pChild ) { 
		if (pChild.indexOf('$') == 0) return pTarget.appendChild( document.createTextNode( pChild.substr(1,pChild.length-1).xmlEntities() ) );
			else return pTarget.appendChild( document.createElement( pChild ) ); 
	},
	
	//Element.genocide - kill all children 
	genocide: function( pTarget ){
		if (typeof(pTarget) == "string") pTarget = IDG$(pTarget); //is it okay? other methods are still dumb!!
		if (pTarget == null) return false;
		
		while (pTarget.childNodes[0]) { pTarget.removeChild(pTarget.childNodes[0]); }
		return true;
	},
	
	//Element.suicide - destroy self, the nice way
	suicide: function( pTarget ){ pTarget.parentNode.removeChild(pTarget); },
	
	//Element.alpha (aka setOpacity in Scripta)	
	alpha: function( pTarget, opacity ) {
		opacity = (opacity == 100)?99.999:opacity;
		pTarget.style.filter = "alpha(opacity:"+opacity+")";
		pTarget.style.KHTMLOpacity = opacity/100;
		pTarget.style.MozOpacity = opacity/100;
		pTarget.style.opacity = opacity/100;
	}
	
});

//kill events
function destroyOnClick( pItem ) {
	pItem.onclick = function(){}
}



// = = = = = = = = = = = = = = = = = = = = = = = = = = =
// ADD BROWSER DETECTION ROUTINES HERE
// = = = = = = = = = = = = = = = = = = = = = = = = = = =

var isIE = false;
if (navigator.userAgent.has("MSIE")) isIE = true;


// = = = = = = = = = = = = = = = = = = = = = = = = = = =
// COOKIE SINGLETON
// = = = = = = = = = = = = = = = = = = = = = = = = = = =

var tCookie = Class.create();
Object.extend(tCookie.prototype, {

	initialize: function() {
	},
	

	set: function(name,value,days)
	{
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	get: function(name)
	{
		var nameEQ = name + "=";
		var retVal = null;
		var cookies = document.cookie.split(';');
		cookies.each(function(cookieItem){
			while (cookieItem.charAt(0)==' ') cookieItem = cookieItem.substring(1,cookieItem.length);
			if (cookieItem.indexOf(nameEQ) == 0) retVal = cookieItem.substring(nameEQ.length,cookieItem.length);
		});
		return retVal;
	},

	erase: function(name)
	{
		this.set(name,"",-1);
	}

});
var simpleCookie = new tCookie();


