﻿
/* Initialisation ----------------------------------------------------------------------*/

var d = document, dE = d.documentElement, dB = d.body, dA = d.all;
IE5 = navigator.userAgent.match('MSIE 5.');

var modules = new Object();
var library = new Object();

library={
	init:function(){
		if(d.getElementById){
			modules.styleSwitcher.init('styleSwitcher');
		};
		rollovers( "_o" , "_h" );
	}
};

window.onload = library.init;

/* Functions --------------------------------------------------------------------------*/

rollovers=function(off,on){img=document.getElementsByTagName("img");p=[];off_reg=new RegExp(off+"(\.[a-z]+$)","i");on_reg=new RegExp(on+"(\.[a-z]+$)","i");for(var x=0,i;i=img[x];x++){if(i.src.match(off_reg)){p[x]=new Image();p[x].src=i.src.replace(off_reg,on+"$1");i.onmouseover=function(){this.src=this.src.replace(off_reg,on+"$1");};i.onmouseout=function(){this.src=this.src.replace(on_reg,off+"$1");};};};};

if(!Array.prototype.push){
	Array.prototype.push = function(){
		for(var i = 0; i < arguments.length; i++){ this[this.length] = arguments[ i ]; }
		return this.length;
	}
}
function $($key, $parent){
	var elements = [];
	var tags = 'A LINK IMG P DIV OL UL LI DL DT DD SPAN EM STRONG LINK TABLE THEAD TBODY TR TD TH H5 H4 H3 H2 H1 OBJECT';
	$parent = (typeof $parent == 'undefined') ? false : $parent;
	keys = $key.split(',');
	for (var i = 0; i < keys.length; i++) {
		var key = keys[i];
		if(tags.match(key)){
			$parent = $parent ? $parent : d;
			elements = $parent.getElementsByTagName(key);
			return elements;
		} else if(d.getElementById(key)){
			element = d.getElementById(key);
			return element;
		} else {
			limit = 5;
			IE5 = navigator.userAgent.match('MSIE 5.');
			if(!IE5){
				$parent = $parent ? $parent : d;
				all = $parent.getElementsByTagName('*');
				for(var j=0, el; el=all[j]; j++){ if(el.className.match(' '+key) || el.className==key){ elements.push(el); } }
			} else {
				all = d.all;
				for(var j=0, el; el=all[j]; j++){ 
					if($parent){
						if(el.className.match(' '+key) || el.className==key){
							var $node = el;
							for(k=0; k<=limit; k++){ if($node.parentNode){ $node = $node.parentNode; } if($node == $parent){ elements.push(el); break; } }
						}
					} else { if(el.className.match(' '+key) || el.className==key){ elements.push(el); } }
				}
			}
		}
	}
	if(elements.length == 0){ return false }
	return elements;
}


/* Modules --------------------------------------------------------------------------*/


modules.cookies = {
	set: function(name,val,days) {
		var expires = '';
		if (days) {
			var date =new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = '; expires=' + date.toGMTString();
		}
		d.cookie = name + '=' + val +expires + '; path=/';
	},
	get: function(name) {
		name = name + '=';
		var cs=d.cookie.split('; ');
		for (var i = 0, cv; cv = cs[i]; i++){
			if (cv.indexOf(name) == 0){ return cv.substring(name.length,cv.length); }
		}
		return null
	}
};

modules.styleSwitcher={
	cur_size: 2,
	init: function(idName){
		this.load();
		if($(idName)){
			var _this = this;
			var parent = $(idName);
			var queryStr = 'style';
			buttons = $('A',parent);
			for (var j=0, curBtn; curBtn=buttons[j]; j++){
				curBtn.onclick=function(){
					if( this.id.match(/^style/) ){
						this.blur();
						var action = this.id.substring(queryStr.length+1);
						if( action == "plus" && _this.cur_size<3 ){ _this.cur_size = 3; }
//						if( action == "plus" && _this.cur_size<3 ){ _this.cur_size++; }
						else if( action == "minus" && _this.cur_size>1 ){ _this.cur_size = 1; }
//						else if( action == "minus" && _this.cur_size>1 ){ _this.cur_size--; }
						else if( action == "default" ){ _this.cur_size = 2; }
						_this.setActive( _this.cur_size );
						return false;
					}
				};
			}
		}
	},
	setActive: function(title){
		var found = false;
		var slinks = $('LINK');
		for (var i = 0, slinks; l = slinks[i]; i++){
			if(l.getAttribute('rel').indexOf('style')!=-1 && l.getAttribute('title')){
				l.disabled = true;
				if (l.getAttribute('title') == "size"+title){
					l.disabled = false;
					found = true;
				}
			}
		}
		if(!found){
			var title = this.getPreferred();
			if(title){
				this.setActive(title);
			}
		}
		this.save();
	},
	getActive: function() {
		var slinks = $('LINK');
		for (var i = 0, l; l = slinks[i]; i++) {
			if (l.getAttribute('rel').indexOf('style')!=-1 && l.getAttribute('title') && !l.disabled){
				return l.getAttribute('title');
			}
		}
		return null;
	},
	getPreferred: function() {
		var slinks = $('LINK');
		for (var i = 0, l; l = slinks[i]; i++){
			if (l.getAttribute('rel').indexOf('style')!=-1 && l.getAttribute('rel').indexOf('alt')==-1 && l.getAttribute('title')){ return l.getAttribute('title').split('size')[1]; }
		}
		return null
	},
	load: function() {
		if (d.getElementById) {
			var cookie = modules.cookies.get('style');
			var title = cookie || this.getPreferred();
			this.cur_size = title;
			this.setActive(this.cur_size);
		}
	},
	save: function(){
		if (d.getElementById) {
			var title = this.getActive();
			modules.cookies.set('style', this.cur_size, 365);
		}
	}
};
modules.styleSwitcher.load();


modules.ie={
	end:function(){
		if (d.all && window.attachEvent) {
			var elProps = [
				'data', 'onmouseover', 'onmouseout',
				'onmousedown', 'onmouseup', 'ondblclick',
				'onclick', 'onselectstart', 'oncontextmenu'
			];
			var all = d.all;
			for (var i = 0, el; el = all[i]; i++) {
				for (var j = 0, elProp; elProp = elProps[j]; j++) { el[elProp] = null; }
			}
		}
	}
}