var currentFontSize = 4;

function revertStyles(fontsize){
	currentFontSize = fontsize;
	changeFontSize(0);
}

function changeFontSize(sizeDifference){
	//get css font size
	var rule = getRuleByName("body.fs" + (currentFontSize + sizeDifference));
	if (rule){
		document.body.style.fontSize = rule.style.fontSize;
		currentFontSize = currentFontSize + sizeDifference;
		createCookie("FontSize", currentFontSize, 365);
		equalHeight();
	}
	return;
	
};

function getRuleByName(ruleName){
	for (i=0; i<document.styleSheets.length; i++){
		var style = document.styleSheets[i];
		var rules = style.cssRules?style.cssRules:style.rules;
		if (rules){
			for (j = 0; j<rules.length; j++){
				if (rules[j].selectorText.trim().toUpperCase() == ruleName.trim().toUpperCase()){
					return rules[j];
				}
			}
		}
	}
	return null;
}

function setActiveStyleSheet(title) {
	createCookie("ColorCSS", title, 365);
	//window.location.reload();
	window.location.reload();
	return;
}

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=/";
}

function setScreenType(screentype){
	bclass = document.body.className.trim();
	if (bclass.indexOf(' ') > 0){
		bclass = bclass.replace(/^\w+/,screentype);
	}else{
		bclass = screentype + ' ' + bclass;
	}

	document.body.className = bclass;
	equalHeightInit();
	createCookie("ScreenType", screentype, 365);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

function changeToolHilite(oldtool, newtool) {
	if (oldtool != newtool) {
		if (oldtool) {
			oldtool.src = oldtool.src.replace(/-hilite/,'');
		}
		newtool.src = newtool.src.replace(/.gif$/,'-hilite.gif');
	}
}

//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 (elems){
	if (!elems) return;
	var maxh = 0;
	for (var i=0; i<elems.length; i++)
	{
		if (elems[i] && elems[i].scrollHeight > maxh) maxh = elems[i].scrollHeight;
	}

	for (i=0; i<elems.length; i++){
		if (elems[i]) elems[i].style.height = (maxh-8) + "px";
	}
}

function getElem (id) {
	var obj = document.getElementById (id);
	if (!obj) return null;
	var divs = obj.getElementsByTagName ('div');
	if (divs && divs.length >= 1) return divs[divs.length - 1];
	return null;
}

function getFirstDiv (id) {
	var obj = document.getElementById (id);
	if (!obj) return null;
	var divs = obj.getElementsByTagName ('div');
	if (divs && divs.length >= 1) return divs[0];
	return obj;
}

function getDivElemsByClass (parent, className) {
	var objs = parent.getElementsByTagName ('div');
	var elems = new Array();
	var j = 0;
	for (var i=0; i<objs.length; i++)
	{
		if (instr(objs[i].className, className) )
		{
			elems[j++] = objs[i];
		}
	}
	return elems;
}

function instr(str, item){
	var arr = str.split(" ");
	for (var i = 0; i < arr.length; i++){
		if (arr[i] == item) return true;
	}
	return false;
}

function equalHeightInit (){
	var objs = new Array();
	objs[0] = getFirstDiv ("ja-topsl1");
	objs[1] = getFirstDiv ("ja-topsl2");
	objs[2] = getFirstDiv ("ja-topsl3");
	equalHeight (objs);
	
	var janewsblocks = getDivElemsByClass (document, "ja-newsblock");
	for (var i=0; i<janewsblocks.length; i++)
	{
		var janewsitems = getDivElemsByClass (janewsblocks[i], "ja-newsitem-inner");
		if (janewsitems.length > 1)
			equalHeight (janewsitems);
	}
}

jaAddEvent (window, 'load', equalHeightInit);

jaToolsHover = function() {
	var jautw = document.getElementById("ja-usertoolswrap");	
	if (!jautw) return;

	jautw.onmouseover=function() {
		this.className="ja-toolswraphover";
	}
	jautw.onmouseout=function() {
		this.className="";
	}
}

jaAddEvent (window, 'load', jaToolsHover);

function adjustVMCatList (className, cols){
	var vmCatList = getDivElemsByClass (document, className);
	k = 0;
	var items = new Array();
	var vmCatProduct = null;
	var objsep = null;
	var divwidth = Math.floor(998/cols)/10 + "%";
	var p;
	for (var i=0; i<vmCatList.length; i++)
	{
		vmCatProduct = vmCatList[i];
		p = vmCatProduct.parentNode;
		p.style.width = divwidth;
		p.style.margin = "0";
		items[k] = vmCatProduct;
		k++;
		if (i==0)
		{
			objsep = document.createElement("SPAN");
			objsep.className = "ja-separator ja-firstsep";
			p.parentNode.insertBefore(objsep, p);
		}
		if (k<cols && i < vmCatList.length - 1)
		{
			vmCatProduct.className += " ja-rightseparator";
		}else{
			objsep = document.createElement("SPAN");
			objsep.className = "ja-separator";
			p.parentNode.insertBefore(objsep, p.nextSibling.nextSibling);
			if (items.length > 1)
			{
				equalHeight (items);
			}
			items = new Array();
			k = 0;
		}
	}
}

function addStyleToFeatureProduct (){
	var obj = document.getElementById ('ja-feature-products');
	if (!obj)
	{
		return;
	}
	var objtable = obj.getElementsByTagName ('table');
	if (objtable && objtable.length > 0)
	{
		objtable = objtable[0];
		var rows = objtable.rows;
		for (i=0; i<rows.length; i++)
		{
			if (rows[i].cells.length > 0)
			{
				for (j=0; j<rows[i].cells.length-1; j++)
				{
					rows[i].cells[j].className += " ja-rightseparator";
				}
			}
		}
	}
}

jaAddEvent (window, 'load', addStyleToFeatureProduct);

