// S2 Javascript functions v0.1

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	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 (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function insertElement( container, element ) {
   if(getRef(container).insertAdjacentElement == null)
      getRef(container).appendChild( element )
   else
      getRef(container).insertAdjacentElement("beforeEnd", element); //Internet Explorer
}

function setStyle( obj, property, value ) {
	var element = getRef( obj );
	if( element ) {
		if( element.style ) element.style[property] = value;
		else element[property] = value;
	}
}

function getStyle( obj, property ) {
	var element = getRef( obj );
	if( element.style ) return element.style[property];
	else return element[property];
}

function is_function(obj) {
	if( !obj ) return false;
    return (obj.constructor.toString().indexOf("Function")!= -1);
}

function getCompStyle( obj, cAttribute ){
	var element = getRef( obj );

	//if IE
	if ( element.currentStyle ) {
		var curVal = eval( 'element.currentStyle.' + cAttribute );
	}
	else {
		//if Mozilla/FF
		var curVal = eval( 'document.defaultView.getComputedStyle( element, null ).' + cAttribute );
	}

	return curVal;
}

function setClass( obj, classname ) {
	getRef(obj).className = classname;
}

function getRef(obj){
	return (typeof obj == "string") ?
	document.getElementById(obj) : obj;
}

function objectExists( obj ) {
	return (document.getElementById(obj) != null);;
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function fixSWF(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName] = new Object();
	} else {
		return document[movieName] = new Object();
	}
}

// Gets a reference to the specified SWF file by checking which browser is
// being used and using the appropriate JavaScript.
// Unfortunately, newer approaches such as using getElementByID() don't
// work well with Flash Player/ExternalInterface.
function getSWF(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function pageHeight() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return myHeight;
}

function pageWidth() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return myWidth;
}

function resize()
{
	myWidth = pageWidth();
	myHeight = pageHeight();

}

if(window.attachEvent)
    window.attachEvent("onload",setListeners);

function setListeners(){
//---------------------------------------------------------------------------------
//Google Toolbar stuff
//---------------------------------------------------------------------------------
  inputList = document.getElementsByTagName("INPUT");
  for(i=0;i<inputList.length;i++){
	addEvent( inputList[i], "onpropertychange", restoreStyles, false );
	inputList[i].style.backgroundColor = "";
  }
  selectList = document.getElementsByTagName("SELECT");
  for(i=0;i<selectList.length;i++){
	addEvent( selectList[i], "onpropertychange", restoreStyles, false );
    selectList[i].style.backgroundColor = "";
  }

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = getElementsByClass( 'hover' );

	for (var i = 0; i < aImages.length; i++) {
		var src = aImages[i].getAttribute('src');
		var ftype = src.substring(src.lastIndexOf('.'), src.length);
		var hsrc = src.replace(ftype, '_hover'+ftype);

		aImages[i].setAttribute('hsrc', hsrc);
		
		aPreLoad[i] = new Image();
		aPreLoad[i].src = hsrc;
		
		aImages[i].onmouseover = function() {
			sTempSrc = this.getAttribute('src');
			this.setAttribute('src', this.getAttribute('hsrc'));
		}	
		
		aImages[i].onmouseout = function() {
			if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_hover'+ftype, ftype);
			this.setAttribute('src', sTempSrc);
		}
	}
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "") {
		event.srcElement.style.backgroundColor = "";
	}
}

function modifyForIE6() {

}

function checkIE() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ieversion>=8) {}
	 else if (ieversion>=7) {}
	 else {
		modifyForIE6();
	 }
	}
}

function opacity( id, opacStart, opacEnd, millisec ) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
	if( opacStart > opacEnd ) {
		for( i = opacStart; i >= opacEnd; i-- ) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd) {
		for( i = opacStart; i <= opacEnd; i++ ) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers 
function changeOpac( opacity, id ) {
    var object = getRef( id ).style; 
    object.opacity = ( opacity / 100 ); 
    object.MozOpacity = ( opacity / 100 ); 
    object.KhtmlOpacity = ( opacity / 100 ); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

// The following block implements the string.parseJSON method
(function (s) {
  // This prototype has been released into the Public Domain, 2007-03-20
  // Original Authorship: Douglas Crockford
  // Originating Website: http://www.JSON.org
  // Originating URL    : http://www.JSON.org/JSON.js

  // Augment String.prototype. We do this in an immediate anonymous function to
  // avoid defining global variables.

  // m is a table of character substitutions.

  var m = {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '"' : '\\"',
    '\\': '\\\\'
  };

  s.parseJSON = function (filter) {

    // Parsing happens in three stages. In the first stage, we run the text against
    // a regular expression which looks for non-JSON characters. We are especially
    // concerned with '()' and 'new' because they can cause invocation, and '='
    // because it can cause mutation. But just to be safe, we will reject all
    // unexpected characters.

    try {
      if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.
        test(this)) {

          // In the second stage we use the eval function to compile the text into a
          // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
          // in JavaScript: it can begin a block or an object literal. We wrap the text
          // in parens to eliminate the ambiguity.

          var j = eval('(' + this + ')');

          // In the optional third stage, we recursively walk the new structure, passing
          // each name/value pair to a filter function for possible transformation.

          if (typeof filter === 'function') {

            function walk(k, v) {
              if (v && typeof v === 'object') {
                for (var i in v) {
                  if (v.hasOwnProperty(i)) {
                    v[i] = walk(i, v[i]);
                  }
                }
              }
              return filter(k, v);
            }

            j = walk('', j);
          }
          return j;
        }
      } catch (e) {

      // Fall through if the regexp test fails.

      }
      throw new SyntaxError("parseJSON");
    };
  }
) (String.prototype);
// End public domain parseJSON block

