// JS util functions
// used in: menu.js, accordion.js and tabs.js
// Copyright 2009 AutumnPress Inc.  All rights reserved.

///////
function et2(d){
	d = $(d);
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/3);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
/*		d.style.opacity = (v/d.maxh);
		d.style.MozOpacity = (v/d.maxh);
		d.style.KhtmlOpacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';*/
		d.style.visibility = 'visible';
		/*if width is modified then overflow is showed in IE, but mouseIn event will fire all the way 800px :( */
		//d.style.width='800px';	
/*		for (var o=0; o<d.childNodes.length; o++){
			d.childNodes[o].style.overflow='visible';
			d.childNodes[o].style.display='inline';
		}*/
			/* in IE only by filtering inside tags, but is a mess */
			/*var uls = d.getElementsByTagName("a");
			for (var o=0; o<uls.length; o++){
				uls[o].style.visibility = 'visible';
			}
			var uls = d.getElementsByTagName("span");
			for (var o=0; o<uls.length; o++){
				uls[o].style.visibility = 'visible';
			}
			var uls = d.getElementsByTagName("ul");
			for (var o=0; o<uls.length; o++){
				uls[o].style.opacity = (v/d.maxh);
				uls[o].style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
			}*/
	}else{
		/* back to width*/
		//d.style.width='auto';
		sh(d,d.maxh);
		clearInterval(d.ti);
	}
};


function changecss(theClass,element,value) {
	var cssRules;
	if (document.all) {
		cssRules = 'rules';
	}
	else if (document.getElementById) {
		cssRules = 'cssRules';
	}
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}
	//	alert(document.styleSheets[1]['cssRules'][18].cssText);
	//	alert(document.styleSheets[1][cssRules][19].style['width']);
	//	document.styleSheets[1][cssRules][19].style['height'] = '0px';
	//	document.styleSheets[1][cssRules][19].style['display'] = 'block';
/*	var theRules = new Array();
	if (document.styleSheets[0].cssRules) {
		theRules = document.styleSheets[0].cssRules;
	} else if (document.styleSheets[0].rules) {
		theRules = document.styleSheets[0].rules;
	}*/
//	theRules[theNumber].style.backgroundColor = '#FF0000';

};

//Expand Initializer
function ex2(d,tar){
//	if(dsp(d)=='none'){
//		dsp(d,'block');
	d.style.height='0px';
	clearInterval(d.ti);
	d.ti=setInterval('et2("'+d.id+'")',10);
//	}
};

function ct2(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/3);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
/*		d.style.opacity = (v/d.maxh);
		d.style.MozOpacity = (v/d.maxh);
		d.style.KhtmlOpacity = (v/d.maxh);
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';	*/
		d.style.visibility = 'visible';		
	}else{
		sh(d,0);
		//dsp(d,'none');
		d.style.visibility = 'hidden';
		//accordion only
		//d.parentNode.opened = false;
		//
		clearInterval(d.to);
	}
};

function cl2(d, anim){
	if (!anim) {
		sh(d,0);
		dsp(d,'none');
	} else {
//		if(dsp(d)=='block'){
			clearInterval(d.to);
//			d.to=setInterval('ct2("'+d.id+'","'+targ+'")',5);
			d.to=setInterval('ct2("'+d.id+'")',10);
//		}
	}
};

/*********************************************************************************************/
function getCSSRule(ruleName, deleteFlag) {               // Return requested style obejct
   ruleName=ruleName.toLowerCase();                       // Convert test string to lower case.
   if (document.styleSheets) {                            // If browser can play with stylesheets
      for (var i=0; i<document.styleSheets.length; i++) { // For each stylesheet
         var styleSheet=document.styleSheets[i];          // Get the current Stylesheet
         var ii=0;                                        // Initialize subCounter.
         var cssRule=false;                               // Initialize cssRule. 
         do {                                             // For each rule in stylesheet
            if (styleSheet.cssRules) {                    // Browser uses cssRules?
               cssRule = styleSheet.cssRules[ii];         // Yes --Mozilla Style
            } else {                                      // Browser usses rules?
               cssRule = styleSheet.rules[ii];            // Yes IE style. 
            }                                             // End IE check.
            if (cssRule)  {                               // If we found a rule...
               if (cssRule.selectorText.toLowerCase()==ruleName) { //  match ruleName?
                  if (deleteFlag=='delete') {             // Yes.  Are we deleteing?
                     if (styleSheet.cssRules) {           // Yes, deleting...
                        styleSheet.deleteRule(ii);        // Delete rule, Moz Style
                     } else {                             // Still deleting.
                        styleSheet.removeRule(ii);        // Delete rule IE style.
                     }                                    // End IE check.
                     return true;                         // return true, class deleted.
                  } else {                                // found and not deleting.
                     return cssRule;                      // return the style object.
                  }                                       // End delete Check
               }                                          // End found rule name
            }                                             // end found cssRule
            ii++;                                         // Increment sub-counter
         } while (cssRule)                                // end While loop
      }                                                   // end For loop
   }                                                      // end styleSheet ability check
   return false;                                          // we found NOTHING!
}                                                         // end getCSSRule 

function killCSSRule(ruleName) {                          // Delete a CSS rule   
   return getCSSRule(ruleName,'delete');                  // just call getCSSRule w/delete flag.
}                                                         // end killCSSRule

function addCSSRule(ruleName) {                           // Create a new css rule
   if (document.styleSheets) {                            // Can browser do styleSheets?
      if (!getCSSRule(ruleName)) {                        // if rule doesn't exist...
         if (document.styleSheets[0].addRule) {           // Browser is IE?
            document.styleSheets[0].addRule(ruleName, null,0);      // Yes, add IE style
         } else {                                         // Browser is IE?
            document.styleSheets[0].insertRule(ruleName+' { }', 0); // Yes, add Moz style.
         }                                                // End browser check
      }                                                   // End already exist check.
   }                                                      // End browser ability check.
   return getCSSRule(ruleName);                           // return rule we just created.
} 

/********************************************************************************/
function $(d){
	return document.getElementById(d);
};

// Efectos for the Tab menu

// set or get the current display style of the div
function dsp(d,v){
    if (d != undefined) {
		if(v==undefined){
			return d.style.display;
		}else{
			d.style.display=v;
		}
	}
};
// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.height=v;
	}
};
function sw(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetWidth;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetWidth);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.widtht=v;
	}
};

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		content = $('content'+tab_act);
		content.style.visibility = 'hidden';		
		d.style.opacity = (v/d.maxh);
		d.style.MozOpacity = (v/d.maxh);
		d.style.KhtmlOpacity = (v/d.maxh);
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
};

//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		content = $('content'+tab_act);
		content.style.visibility = 'hidden';
		d.style.visibility = 'visible';
/*		if (v>d.maxh-10) {
			content.style.visibility = 'visible';
		} else {
			content.style.visibility = 'hidden';
		}*/
/*		var lis = content.getElementsByTagName("UL");
		for (var j=0; j<lis.length; j++){
				var table=lis[j];
				table.style.opacity = (v/d.maxh);
				table.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
		}*/
		d.style.opacity = (v/d.maxh);
		d.style.MozOpacity = (v/d.maxh);
		d.style.KhtmlOpacity = (v/d.maxh);
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		content = $('content'+tab_act);
		content.style.visibility = 'visible';
		sh(d,d.maxh);
		clearInterval(d.t);
	}
};

// Collapse Initializer
function cl(d, anim){
	if (!anim) {
		sh(d,0);
		dsp(d,'none');
	} else {
		if(dsp(d)=='block'){
			clearInterval(d.t);
			d.t=setInterval('ct("'+d.id+'")',t);
		}
	}
};

//Expand Initializer
function ex(d){
//	if(dsp(d)=='none'){
		d.style.visibility = 'hidden';
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
//	}
};
function ex2(d,tar){
//	if(dsp(d)=='none'){
//		dsp(d,'block');
	d.style.height='0px';
	clearInterval(d.ti);
	d.ti=setInterval('et2("'+d.id+'")',5);
//	}
};

// Removes Classname from the given div.
function cc(n,v){
	var s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
};


///////////////////////////////////////////


