// function to allow javascript to programmatically load a CSS stylesheet
function loadStylesheet( filename, media ){
	// ensure that the browser has all the DOM methods/properties we need
	if( !(document.getElementById) ||
		!(document.childNodes) ||
		!(document.createElement) ||
		!(document.getElementsByTagName) ){
		return;
	}

	// make a new link node to the stylesheet
	var link = document.createElement( "link" );
	link.href = filename;
	link.rel = "stylesheet";
	link.type = "text/css";
	link.media = media;

	// insert the stylesheet link into the document head
	//document.getElementsByTagName('head')[0].appendChild(link);
}


// load listener to load multiple fns onload
function addLoadEvent( func ){
	// if there isn't currently an onload function...
	if( typeof window.onload != "function" ){
		// ...then just set it to this one
		window.onload = func;
	} else {
		// otherwise, remember the old onload function
		var oldOnloadFn = window.onload;
		// and make a new onload function that will call the old one, and then func
		window.onload = function() {
			if( oldOnloadFn ) {
				oldOnloadFn();
			}
			func();
		};
	}
}

window.addEvent('domready', function(){
	//get all external links
	var anchors = $(document).getElements('a[rel=external]');
	// loop over all external links
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		// set target attribute to open in new window
		$(anchor).target = "_blank"; 
	}
	
	// rhs fix for multiline h1 text when wide flash element is used
	var size = 0;
	var h1 = $(document).getElement('h1')
	if (h1) { size = h1.clientHeight; }
	if (size > 60) {
		// update bottom margin on search form in rhs
		var formSearch = $$('div.wideFlash').getElementById('topSearch');
		if (formSearch) {
			var oldMargin = $('topSearch').getStyle('margin-bottom').toInt();
			$('topSearch').setStyle('margin-bottom', (oldMargin + size/2));
		}
	}
});



/* Calendar Date validator */
function vaidateDate(){

	d1 = $('date_from').value;
	d2 = $('date_to').value;
	
	var fD = d1.substring(0,2);
	var fM = d1.substring(3,5);
	var fY = d1.substring(6,10);
	
	var tD = d2.substring(0,2);
	var tM = d2.substring(3,5);
	var tY = d2.substring(6,10);	
	var fMDY = new Date(fM + '/' + fD + '/' + fY);
	var tMDY = new Date(tM + '/' + tD + '/' + tY);
	//alert(fMDY+"\n"+tMDY);
	if(fMDY > tMDY){$('date_to').value = "";alert("Invalid 'From' OR 'To' Date(s) \nPlease Select again");}
}

// calendar date picker listeners

window.addEvent('load', function() {
		if($('date_from')){	myCal = new Calendar({ date_from: 'd/m/Y' }, { direction: 0.5, onHideComplete: vaidateDate}); }
		if($('date_to')){	myCal = new Calendar({ date_to: 'd/m/Y' }, { direction: 0.5, onHideComplete: vaidateDate}); }		
	});
	
startList = function() {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion<7){
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("subNav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className=this.className.replace("dropdown", "over");
	  			}
				node.onmouseout=function() {
	  			this.className=this.className.replace("over", "dropdown");
	   			}
	   		}
	 	}
	 }
 }// navigator is < IE7
}
}// End startList
window.onload=startList;


/* START: Expandable content for Accodian */
	function additionalSetup(){
		var toggleAdditional = $('placesToGo').getElements('a.toggleAdditional');
		var addDivs = $('placesToGo').getElements('div.additionalHide');
		for (var i=0; i<toggleAdditional.length; i++){
			toggleAdditional[i].href = "javascript: showAdditional("+i+")";
		}
		var addDivs = $('placesToGo').getElements('div.additional');
		for (var i=0; i<addDivs.length; i++){
			addDivs[i].style.display = "none";
		}
	}
	function showAdditional(k){
		var toggleAdditional = $('placesToGo').getElements('a.toggleAdditional');
		var addDivs = $('placesToGo').getElements('div.additional');		
		//alert(addDivs[k].parentNode.parentNode.parentNode.offsetHeight+"    ... 2lev up");
		addDivs[k].style.display = "block";
		var tempNode = addDivs[k];
		var tempNodeIndex = addDivs[k].className.indexOf('atStart');
		while(tempNodeIndex == -1){
				tempNode = tempNode.parentNode;
				if(null == tempNode) break;
				tempNodeIndex = tempNode.className.indexOf('atStart');
			}
		if(null != tempNode){
			var parentHeight = tempNode.offsetHeight;
			var additionalHeight = addDivs[k].offsetHeight;
			tempNode.style.height = parentHeight+additionalHeight+"px";	
			toggleAdditional[k].style.display = "none";
		}
	}
/* END: Expandable content for Accodian */


/* START: News Scroller
-----------------------------------------*/
var Ticker = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			speed: 500,
			delay: 1000,
			scrollItems : 1,
			direction: 'vertical',
			onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
	
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getSize().size.y;
				this.items.each(function(li,index) {
				w += li.getSize().size.x;
			});
		} else {
			w = this.el.getSize().size.x;
			this.items.each(function(li,index) {
				h += li.getSize().size.y;
			});
		}
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		this.fx = new Fx.Styles(this.el,{duration:this.options.speed,onComplete:function() {
			var i = (this.current == 0) ? this.items.length : this.current;
			for(j=this.options.scrollItems; j>=1; j--){
				this.items[i-j].injectInside(this.el);
			}
			
			this.el.setStyles({
				left:0,
				top:0
			});
		}.bind(this)});
		
		this.current = 0;
		this.delayedNext();
	},
	
	pause: function() {
	    $clear(mytimer);
	    mytimer = null;
	},
	resume: function() {
	    if (mytimer == null) {
	    	this.delayedNext();
	    }
	},
	next: function() {
		this.current = this.current + this.options.scrollItems;
		if (this.current >= this.items.length) this.current = 0;
		var pos = this.items[this.current];
		this.fx.start({
			top: -pos.offsetTop,
			left: -pos.offsetLeft
		});
		this.delayedNext();
	},
	delayedNext: function(){
		mytimer = this.next.bind(this).delay(this.options.delay+this.options.speed);
	}
});

var mytimer = null;

window.addEvent('load', function() {
   if($('newsTicker')){
	   	var hor = new Ticker('newsTickerVertical', {
	    	speed : 1500, delay : 5000, direction : 'vertical', scrollItems : 1});
   
   		$('newsTicker').addEvent('mouseenter', function() {hor.pause();});
   		$('newsTicker').addEvent('mouseleave', function() {hor.resume();});
   }
   
   if($('newsTickerFast')){
   	   	var hor = new Ticker('newsTickerVertical', {
   	    	speed : 1000, delay : 1000, direction : 'vertical', scrollItems : 1});
      
      		$('newsTickerFast').addEvent('mouseenter', function() {hor.pause();});
      		$('newsTickerFast').addEvent('mouseleave', function() {hor.resume();});
   }
});

