function switchFontSize (ckname,val){
	var bd = $E('body');
	switch (val) {
		case 'inc':
			if (CurrentFontSize+1 < 7) {
				bd.removeClass('fs'+CurrentFontSize);
				CurrentFontSize++;
				bd.addClass('fs'+CurrentFontSize);
			}		
		break;
		case 'dec':
			if (CurrentFontSize-1 > 0) {
				bd.removeClass('fs'+CurrentFontSize);
				CurrentFontSize--;
				bd.addClass('fs'+CurrentFontSize);
			}		
		break;
		default:
			bd.removeClass('fs'+CurrentFontSize);
			CurrentFontSize = val;
			bd.addClass('fs'+CurrentFontSize);		
	}
	Cookie.set(ckname, CurrentFontSize,{duration:365});
}

function switchTool (ckname, val) {
	createCookie(ckname, val, 365);
	window.location.reload();
}

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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

//addEvent - attach a function to an event
function jaAddEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

/*function equalHeight(){
	makeEqualHeight ($('ja-botsl').getChildren());
	makeEqualHeight ($$(['ja-content','ja-col1-bot','ja-col2']));
}

function makeEqualHeight(divs) {
	if(!divs || divs.length < 2) return;
	var maxh = 0;
	divs.each(function(el, i){
		var ch = el.getCoordinates().height;
		maxh = (maxh < ch) ? ch : maxh;		
	},this);
	divs.each(function(el, i){
		el.setStyle('height', maxh-el.getStyle('padding-top').toInt()-el.getStyle('padding-bottom').toInt());		
	},this);
}*/

function getDeepestDiv (div) {
	while (div.getChildren().length==1 && (div.getChildren()[0].tagName == 'DIV'))
	{
		div = div.getChildren()[0];
	}
	return div;
}

function preloadImages () {
	var imgs = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var imgsrc = arguments[i];
		imgs[i] = new Image();
		imgs[i].src = imgsrc;
	}
}

//Add span to module title

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	var j = 0;
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (var i = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	//alert(searchClass + j);
	return classElements;
}

//Add 1st item identity
jaAddFirstItemToTopmenu = function() {
	li = $E('#ja-footer ul li');
	if(li) {
		li.addClass('ja-firstitem');
	}
}

tooglebody = function () {
	mainbody = $('ja-contentwrap');
	if (!mainbody) return;
	maxwidth = $('ja-container').offsetWidth - 50;
	mainbody.setStyles({position:'absolute'});
	new Fx.Style (mainbody, 'width').start(mainbody.offsetWidth, maxwidth); 
}

MainBodyToogle = new Class({
	initialize: function(){
		this.mainbody = $('ja-contentwrap');
		if (!this.mainbody) return;
		this.handler = $('ja-mainbody-resize');		
		if (!this.handler) return;
		this.minwidth = this.mainbody.offsetWidth;
		this.maxwidth = $('ja-container').offsetWidth;
		
		if (this.minwidth > this.maxwidth -50) return;
		
		this.handler._expand = new Element('img',{'src':tmplurl + "/images/icon-expand.gif",'title':'Expand Body','alt':'Expand Body'});
		this.handler._collapse = new Element('img',{'src':tmplurl + "/images/icon-collapse.gif",'title':'Collapse Body','alt':'Collapse Body'});
		this.handler._expand.setStyles({'cursor':'pointer'});
		this.handler._collapse.setStyles({'display':'none','cursor':'pointer'});
		this.handler.adopt (this.handler._expand);
		this.handler.adopt (this.handler._collapse);
		
		this.colwrap = $('ja-colwrap');
		if (this.colwrap) this.colwrap.setStyles({position: 'relative','z-index':5});
		this.mainbody.setStyle('z-index',10);
		this.handler.addEvent('click', this.tooglewidth.bind(this));
	},
	
	tooglewidth: function() {
		if (!this.isopen) {
			this.updateposition();
			this.isopen = true;
			new Fx.Style (this.mainbody, 'width', {onComplete:this.updatefinish.bind(this)}).start(this.mainbody.offsetWidth, this.maxwidth);
			this.handler.className = 'close';
			this.handler._collapse.setStyle('display','block');
			this.handler._expand.setStyle('display','none');
			if (this.colwrap) new Fx.Style (this.colwrap, 'opacity').start(1, 0.1); 
		} else {
			this.isopen = false;
			new Fx.Style (this.mainbody, 'width', {onComplete:this.updateback.bind(this)}).start(this.mainbody.offsetWidth, this.minwidth);
			this.handler.className = 'open';
			this.handler._expand.setStyle('display','block');
			this.handler._collapse.setStyle('display','none');
			if (this.colwrap) new Fx.Style (this.colwrap, 'opacity').start(0.1, 1); 
		}
		return false;
	},
	
	updateposition: function () {
		if (document.body.className.test ('rightlayout')) {
			this.mainbody.setStyles({top:0,left:0});
		}else{
			this.mainbody.setStyles({top:0,right:0});
		}
		this.mainbody.setStyle('position','absolute');
	},
	
	updatefinish: function() {
		this.mainbody.getParent().setStyle('height', this.mainbody.offsetHeight);
		window.fireEvent('resize');
	},
	
	updateback: function() {
		this.mainbody.setStyle('position','relative');
		this.mainbody.getParent().setStyle('height', 'auto');
		window.fireEvent('resize');
	}
});

window.addEvent ('load', function() {
	//equalHeight();
	jaAddFirstItemToTopmenu();
	//new MenuIcon($E('#ja-mainnav'));
	new MainBodyToogle();
});
