/**
 * @fileoverview
 * @author			Michael Ord <michael@think.eu>
 * @version			0.1
 * @class
 * @requires		YAHOO.util.Dom
 * @requires		YAHOO.util.Event
 *
 * @file			init.js
 * @description		To be included globally across the site, therefore it should
 * 					include calls to functions and scripts that need to	be run
 * 					on each page.
 */

/**
 *
 */
YAHOO.util.Event.onAvailable ('iRoot', function ( ) {
	ThinkCo.Enhance ( 'enhance' );
} );

/**
 * TODO Description
 * @method TH_NK_PageLoad
 */
function TH_NK_PageLoad_Init () {

	if ( ThinkCo.component.Collapse ) {
		ThinkCo.component.Collapse ();
	};

	var tmp_links	= document.getElementsByTagName ( 'a' );

	var tmp_event	= function ( ev ) {

		var iFlash	= YAHOO.util.Dom.get ( 'iFlash' );
		var flashOb	= iFlash.childNodes [ 0 ];

		if ( ( this.href.indexOf ( '#' ) > -1 ) && ( ( this.pathname == location.pathname ) || ( '/' + this.pathname == location.search ) ) && ( this.search == location.search ) ) {
			return;
		};

		if ( YAHOO.util.Dom.hasClass ( this, 'jsToggle' ) ) {
			return;
		}

		if ( flashOb.tagName.toString ( ).toLowerCase ( ) == 'object' || flashOb.tagName.toString ( ).toLowerCase ( ) == 'embed' ) {

			var frame	= new Number ( flashOb.CurrentFrame ( ) )
			var suffix	= ( this.href.indexOf ( "?" ) < 0 ) ? "?" : "&";
			var mailto	= this.href.indexOf ( "mailto:" );

			if ( mailto <= -1 ) {
				this.href = this.href + suffix + 'f=' + frame;
			};
		};
	};

	for ( var i = 0; i < tmp_links.length; i++ ) {
		YAHOO.util.Event.addListener ( tmp_links [ i ], "click", tmp_event );
	};
};

function getFrameParam ( ) {

	var tmp_location	= String ( location.search );
	var tmp_matches		= tmp_location.match (/([^&?=]*[^&?=])/g );

	if ( !tmp_matches ) {
		return null;
	};

	for ( var i = 0; i < tmp_matches.length; i+= 2 ) {
		if ( tmp_matches [ i ] == 'f' ) {
			return Number ( tmp_matches [ i + 1 ] );
		};
	};

	return null;
}

// add the event call
YAHOO.util.Event.onDOMReady(TH_NK_PageLoad_Init);