/* JAVASCRIPT DOCUMENT */

/* COMMON FUNCTIONS */
/* CREATED: FRIDAY DECEMBER 16TH 2005 | 8:59AM - LEEVI GRAHAM */
/* UPDATED: TUESDAY FEBUARY 14TH 2006 | 12:12PM - LEEVI GRAHAM */


/*****************************************************************************/
/* 			EVENT REGISTRATION AND METHODS									 */
/*****************************************************************************/

// cross browser event registration
// object 		=  	the element the event is registered on
// eventName 	= 	is the event eg: click, mouseup
// funcName 	= 	the fucntion to be clalled on the event
// eventOrder	=  	True the event handler is set for the capturing phase,
//				   	False the event handler is set for the bubbling phase.
//					By default IE implements bubbling

function addEvent(object, eventName, funcName, eventOrder){
	eventOrder = (!eventOrder)?true:eventOrder;
	if(window.attachEvent){ object.attachEvent('on'+eventName, funcName); } // IE
	else if(window.addEventListener){ object.addEventListener(eventName, funcName, eventOrder); } // W3C
}

// Cross browser event de-registration
function removeEvent(object, eventName, funcName, eventOrder){
	eventOrder = (!eventOrder)?true:eventOrder;
	if(window.detachEvent){ object.detachEvent('on'+eventName, funcName); } // IE
	else if(window.removeEventListener){ object.removeEventListener(eventName, funcName, eventOrder); } // W3C
}

// This stops all propagation of the event in the bubbling phase.
// Stopping event propagation in the capturing phase is impossible.
function cancelEventBubble(e){
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

// Cross browser event target registration
// Returns an element object that event was registered on
// Include cancel bubble as an optional variable :: default = true
function getEventTarget(e, cancelBubble){
	cancelBubble = (!cancelBubble)?true:cancelBubble;
	if(cancelBubble){
		cancelEventBubble(e);
	}
	var targ;
	if (!e) var e = window.event; 
	if (e.target) targ = e.target; // W3C
	else if (e.srcElement) targ = e.srcElement; // IE
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

// adds function to window onload event
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


/*****************************************************************************/
/* 					GET ELEMENTS BY CLASS NAME								 */	
/*****************************************************************************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/* END GETS ELEMENTS BY CLASS NAME */


/*****************************************************************************/
/* 					GET ELEMENTS BY TAG NAME(s)								 */	
/*****************************************************************************/
function getElementsByTagNames(list,obj)
{
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++)
	{
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++)
		{
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (testNode.sourceIndex)
	{
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition)
	{
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}
/* END GETS ELEMENTS BY TAG NAME(s) */


/*****************************************************************************/
/* 						CLASS MANIPULATION METHODS							 */
/*****************************************************************************/

// add class to the element
function addClass(o,c){
	if(!checkClass(o,c)){o.className+=o.className==''?c:' '+c;}
}

// remove class to the element
function removeClass(obj, c) {
		var oldClass = obj.className;
		var regExp = new RegExp('\\s?'+c+'\\b');
		if (oldClass.indexOf(c) != -1) {
			obj.className = obj.replace(regExp,'');
		} 
}

// swap classes
function swapClass(o,c1,c2){
	var cn=o.className;
	
	// old swap class function.
	//o.className=!checkClass(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	
	if(!checkClass(o,c1))
	{
		var myExpression = new RegExp("\\b"+c2+"\\b", "gi");
		sNewClass = cn.replace(myExpression,c1);
	}
	else
	{
		var myExpression = new RegExp("\\b"+c1+"\\b", "gi");
		sNewClass = cn.replace(myExpression,c2);
	}
	o.className = sNewClass;
}

// check if an element has the defined class
function checkClass(o,c){
	return new RegExp('\\b'+c+'\\b').test(o.className);
}


/*****************************************************************************/
/* 						TOGGLE FUNCTION 									 */
/*****************************************************************************/
function toggle(target, ref, effect, targetVisibileClass, targetHiddenClass, refActiveClass, refInActiveClass){
	
	// check if the elements are passed to the function	
	if(!$(target)){alert('no target'); return;} else {var target = $(target);}
	if(!$(ref)){alert('no referer'); return;} else {var ref = $(ref);}

	// if no classes are provided to function
	// make the target visible class visible
	targetVisibileClass = (!targetVisibileClass)?'visible':targetVisibileClass;
	// make the target hidden class hidden
	targetHiddenClass = (!targetHiddenClass)?'hidden':targetHiddenClass;
	// make the target refer active class active
	refActiveClass = (!refActiveClass)?'active':refActiveClass;
	// make the target refer inactive class inactive
	refInActiveClass = (!refInActiveClass)?'inactive':refInActiveClass;
	
	// if the object doesnt have the active or inactive it has no been initialised.
	// give the object the class of active by default
	if(!checkClass(ref,refActiveClass) && !checkClass(ref,refInActiveClass)){addClass(ref,refActiveClass);}
	// toggle parent and active deteriming state
	swapClass(ref,refActiveClass,refInActiveClass);

	// if the object doesnt have the visible or hidden it has no been initialised.
	// give the object the class of open as default
	if(!checkClass(target,targetVisibileClass) && !checkClass(target,targetHiddenClass)){addClass(target,targetVisibileClass);} 
	// toggle visible and hidden deteriming state
	swapClass(target,targetVisibileClass,targetHiddenClass);
	
	// requires scriptaculous
	if(effect)
	{
		switch (effect)
		{
			case 'fade': (checkClass(target, 'visible')) ? Effect.Fade(target) : Effect.Appear(target) ; break;
			case 'slide': (checkClass(target, 'visible')) ? Effect.SlideUp(target) : Effect.SlideDown(target) ; break;
		}
		
	}

}

/* END TOGGLE FUNCTION */


/*****************************************************************************/
/* 						COOKIE FUNCTIONS									 */
/*****************************************************************************/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


