var LinkAccordion = Accordion.extend({
	initialize: function(handles, drawers, options) {
		this.addEvent('onActive', function(handle, i) {
			handle.addClass('selected');
		});
		this.addEvent('onBackground', function(handle, i) {
			handle.removeClass('selected');
		});
		
		// run parent initializer
		this.parent.apply(this, arguments);
		
		// this next part adds the automatic opening magic to each “Handle”
		this.togglers.each(function(handle, index, array) {
			// and the magic hover opening dealie!
			handle.hoverOpenTimer = null
			handle.getElement('a').addEvents({
				mouseover: function(thisHandle) {
					thisHandle.hoverOpenTimer = $clear(thisHandle.hoverOpenTimer);
					thisHandle.hoverOpenTimer = this.display.delay(100, this, index);
				}.bind(this, handle),
				mouseout: function(thisHandle) {
					thisHandle.hoverOpenTimer = $clear(thisHandle.hoverOpenTimer);
				}.bind(this, handle),
				focus: this.display.pass(index, this) // supports tab based keyboard navigation
			});
		}.bind(this));
	}
});

var HeaderAccordion = Accordion.extend({
	initialize: function(handles, drawers, options) {
        var x = readCookie('ps_contrast')
	    if (x != 'contrast') {
		this.addEvent('onActive', function(handle, i) {
			handle.addClass('selected');
		    handle.setStyles({background:'url(/design/images/bg_grey.gif) top left no-repeat'});
		});
		this.addEvent('onBackground', function(handle, i) {
			handle.removeClass('selected');
			handle.setStyles({background:'none'});
		});
         } 
		
		// run parent initializer
		this.parent.apply(this, arguments);
		
		// this next part adds the automatic opening magic to each “Handle”
		this.togglers.each(function(handle, index, array) {
			// and the magic hover opening dealie!
			handle.hoverOpenTimer = null
			handle.getElement('h5').addEvents({
				mouseclick: function(thisHandle) {
					thisHandle.hoverOpenTimer = $clear(thisHandle.hoverOpenTimer);
					thisHandle.hoverOpenTimer = this.display.delay(100, this, index);
				}.bind(this, handle),
				focus: this.display.pass(index, this) // supports tab based keyboard navigation
			});
		}.bind(this));
	}
});

var CookieLinkAccordion = LinkAccordion.extend({
  initialize: function(togglers, elements, options) {
    this.options.cookieName = 'accordion-place';
    this.options.cookieOptions = {path: '/', duration: 30};
    
    this.setOptions(options);
    
    var cookieValue = Cookie.get(this.options.cookieName);
    
    if (cookieValue != false) {
      this.options.show = cookieValue.toInt();
    }
    
    // run parent initializer
		this.parent.apply(this, arguments);
		
		this.addEvent('onActive', function(toggler, element) {
		  Cookie.set(this.options.cookieName, this.togglers.indexOf(toggler), this.options.cookieOptions);
		});
  }
});

var CookieHeaderAccordion = HeaderAccordion.extend({
  initialize: function(togglers, elements, options) {
    this.options.cookieName = 'accordion-place';
    this.options.cookieOptions = {path: '/', duration: 30};
    
    this.setOptions(options);
    
    var cookieValue = Cookie.get(this.options.cookieName);
    
    if (cookieValue != false) {
      this.options.show = cookieValue.toInt();
    }
    
    // run parent initializer
		this.parent.apply(this, arguments);
		
		this.addEvent('onActive', function(toggler, element) {
		  Cookie.set(this.options.cookieName, this.togglers.indexOf(toggler), this.options.cookieOptions);
		});
  }
});

window.addEvent('domready', function () {
var x = readCookie('ps_contrast')
if (x != 'contrast') {
	if(document.getElementById("news")){
		var hidenews = $('news').setStyles({display:'none'});
	}
	if(document.getElementById("feature_nav")){
		var hidenews = $('feature_nav').setStyles({display:'none'});
	}
}
});
	
window.addEvent('domready', function () {
var x = readCookie('ps_contrast')
if (x != 'contrast') {
	if(document.getElementById("news")){
		var shownews = $('news').setStyles({display:''});
	}
	if(document.getElementById("feature_nav")){
		var shownews = $('feature_nav').setStyles({display:''});
	}
	var nav = new CookieLinkAccordion($$('.nav_item', 'feature_nav'), $$('.nav_info', 'feature_nav'));
}
	var news = new CookieHeaderAccordion($$('.news_item', 'news'), $$('.news_info', 'news'));
});

var stingHover = new Array();

window.addEvent('domready', function(){

	for(var i = 1; i < 4; i++){		
	    if(document.getElementById("hover_" +i)){	
		    var setopacity = new Fx.Style('sting_' +i, 'opacity');
		    setopacity.set(0.8);
    		
		    stingHover['hover_' +i] = new Fx.Style('sting_' +i, 'opacity', {duration: 200, wait: false});
    		
		    $('hover_' +i).addEvent('mouseenter', function(){ stingHover[this.id].start(1) })
		    $('hover_' +i).addEvent('mouseleave', function(){ stingHover[this.id].start(0.8) })
		}
	}
		
});

/*
window.addEvent('domready', function(){

	var list = $$('#main_nav li a');
	list.each(function(element) {
	 
		var fx = new Fx.Styles(element, {duration:200, wait:false});
	 
		element.addEvent('mouseenter', function(){
			fx.start({
				'backgroundPosition': '0 3px',
				'paddingLeft': '12px'
			});
		});
	 
		element.addEvent('mouseleave', function(){
			fx.start({
				'backgroundPosition': '-12px 3px',
				'paddingLeft': '0'
			});
		});
	 
	});
});
*/


checkBrowserWidth();

attachEventListener(window, "resize", checkBrowserWidth, false);


function checkBrowserWidth(){
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0){
		var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);

		if (resolutionCookie != null){
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth > 964){
		setStylesheet("1024 x 768");
		document.cookie = "tmib_res_layout=" + escape("1024 x 768");
		var nav = new CookieLinkAccordion($$('.nav_item', 'feature_nav'), $$('.nav_info', 'feature_nav'));
		var news = new CookieHeaderAccordion($$('.news_item', 'news'), $$('.news_info', 'news'));
	}else{
		setStylesheet("");
		document.cookie = "tmib_res_layout=";
		var nav = new CookieLinkAccordion($$('.nav_item', 'feature_nav'), $$('.nav_info', 'feature_nav'));
		var news = new CookieHeaderAccordion($$('.news_item', 'news'), $$('.news_info', 'news'));
	}
	
	return true;
};


function getBrowserWidth()
{
	if (window.innerWidth){
		return window.innerWidth;
	}else if (document.documentElement && document.documentElement.clientWidth != 0){
		return document.documentElement.clientWidth;
	}else if (document.body){
		return document.body.clientWidth;
	}
	
	return 0;
};

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined'){	
		window.attachEvent('onload', fn);
	}else{
		return false;
	}
	
	return true;
};




function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined"){
        target.addEventListener(eventType, functionRef, capture);
    }else if (typeof target.attachEvent != "undefined"){
        target.attachEvent("on" + eventType, functionRef);
    }else{
        return false;
    }

    return true;
};


function setStylesheet(styleTitle){
	var currTag;

	if (document.getElementsByTagName){
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++){
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")){
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle){
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};
function createCookie(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=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setdefault(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', '/css/default.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('ps_textSize','default',1);
}
function setbigtext(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', '/css/big.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('ps_textSize','big',1);
}
function setbiggertext(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', '/css/bigger.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('ps_textSize','bigger',1);
}

function setcontrast(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', '/css/contrast.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('ps_contrast','contrast',1);
}
function setnormal(){
		createCookie('ps_contrast','normal',1);
}

window.addEvent('domready', function(){
	var x = readCookie('ps_textSize')
	if (x == 'default') {
		setdefault();
		//alert('default');
	}
	else if (x == 'ps_big') {
		setbigtext();
		//alert('big');
	}
	else if (x == 'ps_bigger') {
		setbiggertext();
		//alert('bigger');
	}
});

window.addEvent('domready', function(){
	var x = readCookie('ps_contrast')
	if (x == 'contrast') {
		setcontrast();
	}
	else if (x == 'normal') {
		setnormal();
	}

});

window.addEvent('domready', function(){
        var mySlide = new Fx.Slide('accessibility_inner', {duration: 500, transition: Fx.Transitions.Sine}).hide();

        $('toggle').addEvent('click', function(e){
			e = new Event(e);
			mySlide.toggle();
			e.stop();
        });
});