window.addEvent('domready', function () {
	if(document.getElementById("home_page")){
		var hidenews = $('home_page').setStyles({visibility:'hidden'});
	}
});

//  RIGHT NEWS
var HeaderAccordion = Accordion.extend({
	initialize: function(handles, drawers, options) {
		this.addEvent('onActive', function(handle, i) {
			handle.addClass('selected');
		    handle.setStyles({background:'url(images/bg_white.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('h4').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 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 () {
	if(document.getElementById("news")){
		var hidenews = $('right').setStyles({display:'none'});
		new Fx.Style('home_page','opacity').set(0);
	}
});
	
window.addEvent('load', function () {
	if(document.getElementById("news")){
		var shownews = $('right').setStyles({display:'block'});
		setTimeout("fadeInHome()",500);
                var shownews = $('home_page').setStyles({visibilty:'visible'});
	}
	var news = new CookieHeaderAccordion($$('.news_item', 'news'), $$('.news_info', 'news'));
});

function fadeInHome(){
	new Fx.Style('home_page','opacity',{duration:1000}).start(0,1);
}

//  HIDE/SHOW CONTACTS
function display(obj,id1,id2,id3,id4,id5,id6) {
	txt = obj.options[obj.selectedIndex].value;
		document.getElementById(id1).style.display = 'none';
		document.getElementById(id2).style.display = 'none';
		document.getElementById(id3).style.display = 'none';
		document.getElementById(id4).style.display = 'none';
		document.getElementById(id5).style.display = 'none';
		document.getElementById(id6).style.display = 'none';
	if ( txt.match(id1) ) {
		document.getElementById(id1).style.display = 'block';
	}
	if ( txt.match(id2) ) {
		document.getElementById(id2).style.display = 'block';
	}
	if ( txt.match(id3) ) {
		document.getElementById(id3).style.display = 'block';
	}
	if ( txt.match(id4) ) {
		document.getElementById(id4).style.display = 'block';
	}
	if ( txt.match(id5) ) {
		document.getElementById(id5).style.display = 'block';
	}
	if ( txt.match(id6) ) {
		document.getElementById(id6).style.display = 'block';
	}
};

window.addEvent('domready', function () {
	contacts=['services_contact','isign_contact','insight_contact','premex3d_contact','accuro_contact']
	for (var i_tem = 0; i_tem < contacts.length; i_tem++)
	
		if(document.getElementById(contacts[i_tem])){
			var hideContacts = $(contacts[i_tem]).setStyles({display:'none'});
		}
});

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) })
		}
	}
		
});

// HIDE/SHOW HOMEPAGE COMPANY LOGOS
var subs_array = new Array("group_info","services_info","isign_info","insight_info","premex3d_info","accuro_info");// Put the id's of your hidden divs in this array

function displaySubs(the_sub){
	 if (document.getElementById(the_sub).style.display==""){
	   document.getElementById(the_sub).setStyles({display:''});return
	    
  }
  for (i=0;i<subs_array.length;i++){
	   var my_sub = document.getElementById(subs_array[i]);
	   my_sub.style.display = "none";
	 }
  new Fx.Style(the_sub,'opacity',{duration:1000}).set(0);
  document.getElementById(the_sub).style.display = "";
  new Fx.Style(the_sub,'opacity',{duration:1000}).start(0, 1);
}

window.addEvent('domready', function () {
	contacts=['services_info','isign_info','insight_info','premex3d_info','accuro_info']
	for (var i_tem = 0; i_tem < contacts.length; i_tem++)
	
		if(document.getElementById(contacts[i_tem])){
			var hideContacts = $(contacts[i_tem]).setStyles({display:'none'});
			new Fx.Style(contacts[i_tem],'opacity',{duration:1000}).set(0);
		}
});

//  LOAD 1024 CSS STYLESHEET ON BROWSER SIZE
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 news = new CookieHeaderAccordion($$('.news_item', 'news'), $$('.news_info', 'news'));
	}else{
		setStylesheet("");
		document.cookie = "tmib_res_layout=";
		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', 'http://www.premex.com/css/default.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('pg_textSize','default',1);
}
function setbigtext(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', 'http://www.premex.com/css/big.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('pg_textSize','big',1);
}
function setbiggertext(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', 'http://www.premex.com/css/bigger.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('pg_textSize','bigger',1);
}
function setcontrast(){
	var cssNode = document.createElement('link');
		cssNode.setAttribute('rel', 'stylesheet');
		cssNode.setAttribute('type', 'text/css');
		cssNode.setAttribute('href', 'http://www.premex.com/css/contrast.css');
		document.getElementsByTagName('head')[0].appendChild(cssNode); 
		createCookie('pg_contrast','contrast',1);
}
function setnormal(){
		createCookie('pg_contrast','normal',1);
}

window.addEvent('domready', function(){
	var x = readCookie('pg_textSize')
	if (x == 'default') {
		setdefault();
		//alert('default');
	}
	else if (x == 'big') {
		setbigtext();
		//alert('big');
	}
	else if (x == 'bigger') {
		setbiggertext();
		//alert('bigger');
	}
});

window.addEvent('domready', function(){
	var x = readCookie('pg_contrast')
	if (x == 'contrast') {
		setcontrast();
		//alert('bigger');
	}
	else if (x == 'normal') {
		setnormal();
		//alert('default');
	}

});

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();
        });
});

function dropdown_redirect(obj){
    var url = obj.options[obj.selectedIndex].value;

    if (url == '-1')
        return false;

    document.location.href = url;
}

//popup box

function homePopUps(){
	var popupCookie = readCookie('should_pop')
	
	if(popupCookie != 'popup'){
		popup_welcome_boxes = '';
		popup_welcome_boxes += '<div id=\"popup_welcome\">';
		popup_welcome_boxes += '<div id=\"popup_inner\">';
                popup_welcome_boxes += '<p>As part of the on going web developments at Premex, a new Group website has been launched @ www.premex.com. To view the new group site click the Premex Group button below.</p>';
                popup_welcome_boxes += '<p>If you wish to visit the Premex Services web site, it is accessible via the Premex Services button below or @ www.premexservices.co.uk.</p>';
		popup_welcome_boxes += '<a href=\"#\" onclick=\"homePopUpsCookie();iBox.hide();\" onkeypress=\"homePopUpsCookie();iBox.hide();\" title=\"Contine to Premex Group\">';
		popup_welcome_boxes += '<img src=\"css/images/popup_images/group_button.gif\" alt=\"Group Button\" id=\"group_button\" /\></\a>';
		popup_welcome_boxes += '<a href=\"http://www.premexservices.co.uk\" title=\"Forward to Premex Services\">';
		popup_welcome_boxes += '<img src=\"css/images/popup_images/services_button.gif\" alt=\"Services Button\" id=\"services_button\"/\></\a>';
		popup_welcome_boxes += '</\div>';
		popup_welcome_boxes += '</\div>';

		iBox.show(popup_welcome_boxes,'',params = {height: 350});
	}
}

function homePopUpsCookie(){
        createCookie('should_pop','popup',1);
}

window.addEvent('load', function(){
	var showPopUp = setTimeout("homePopUps();",1100);
});