// This is the implementation of SimpleSwap
// by Jehiah Czebotar
// Version 1.1 - June 10, 2005
// Distributed under Creative Commons
//
// Include this script on your page
// then make image rollovers simple like:
// <img src="/images/ss_img.gif" oversrc="/images/ss_img_over.gif">
//
// http://jehiah.com/archive/simple-swap
// 
//added by Bijan Nejad on June 07 2006
// _w : which ID (1) or (2)
// _h : (h)ide or (s)how

function createCookie(value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = "keilbg="+value+expires+"; path=/";
	//if (days != -1) alert('Cookie\n' + ck + '\ncreated');
	document.cookie = ck;
}

function readCookie()
{
	var nameEQ = "keilbg=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie("keilbg","",-1);
}

function checkCookie()
{
	var bg = readCookie();
	
	//alert('cookie value is: ' + bg);
	if(bg=='')
	{
	switchbg(0);
	} else {
		switchbg(bg);
	}
}
function switchbg(bg)
{
    eraseCookie();
	createCookie(bg,7);
  if(bg==0)
  {
    document.body.background = "images/scribblewhite.gif";
	  document.bgColor = 'white';
	} else {
    document.body.background = "images/scribbleblack.gif";
	  document.bgColor = 'black';
  }
}

function setActiveStyleSheet(title) {
   var i, a, main;
   
   
   eraseCookie();
   createCookie(title,7);
   cookie = readCookie();
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) {
	   	a.disabled = false;
		//getGradeImages();
	   }
     }
   }
}


function getActiveStyleSheet() {
var i, a;
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  if(a.getAttribute("rel").indexOf("style") != -1
  && a.getAttribute("title")
  && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a, title;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
	   
  }
  return null;
}



window.onload = function(e) {
  var cookie = readCookie();
  var title = (cookie!="null") ? cookie : getPreferredStyleSheet();
 
  setActiveStyleSheet(title);
  createCookie(title, 365);
  
  
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie(title, 365);
}

var cookie = readCookie();
var title = cookie ? cookie : getPreferredStyleSheet();

setActiveStyleSheet(title);


function ShowHideDiv(_w,_h) {
//alert("Cookie value is:" + cookie);

var el = document.getElementById(_w);
if (_h=='s') el.style.visibility = "visible";
if (_h=='h') el.style.visibility = "hidden";
}

function ShowHideRightMenu(_h){
var cookie = readCookie();

var m=(cookie=="whitebg") ? "rightMenuWhite" : "rightMenuBlack";
var el = document.getElementById(m);
if (_h=='s') el.style.visibility = "visible";
if (_h=='h') el.style.visibility = "hidden";
}


function findLinkByHref(href) {
  for (var i=0; i<document.links.length; i++) {
    if (document.links[i].href == href) return i;
  }
  return -1;
}

function changeLinkHref1(id,newHref) {
  if (document.links.length > 0) {
    if (document.getElementById) {
      document.getElementById(id).href = newHref;
    }
    else if (document.all) {
      document.all[id].href = newHref;
    }
    else {
      /*var index = findLinkByHref(oldHref);
      if (index > -1)
        document.links[index].href = newHref;
    */
    
    document.getElementById(id).href = newHref;
    
    }
  }
}

	function roll(img_name1, img_src1)
	{
	document[img_name1].src = img_src1;
	}

function change(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}

function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}

function SimpleSwap1(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup1(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrcc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    //x[i].oversrc_img = new Image();
    //x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap1(this,'oversrcc');");
    x[i].onmouseout = new Function("SimpleSwap1(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}

/*function SimpleSwap2(e2,which2){
  e2.height=e2.getAttribute(which2 || "origsrc2");
  e2.width=e2.getAttribute(which2 || "origsrc2");
}
function SimpleSwap3(e3,which3){
  e3.width=e3.getAttribute(which3 || "origsrc1");
}
function SimpleSwapSetup2(){
  var y = document.getElementsByTagName("img");
  for (var j=0;j<y.length;j++){
    var overheight = y[j].getAttribute("overheight");
    if (!overheight) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
   // x[i].overheight_img = new Image();
   //x[i].overheight_img.height=overheight;
    // set event handlers
    y[j].onmouseover = new Function("SimpleSwap2(this,'overheight');");
    y[j].onmouseout = new Function("SimpleSwap2(this);");
    // save original src
    y[j].setAttribute("origsrc2",y[j].height);
	y[j].setAttribute("origsrc2",y[j].width);
  }
}
*/
function showlayer(layerName)
{
	//document.getElementById(layerName).style.height="";
    document.getElementById(layerName).style.visibility="visible"; 

}

function hidelayer(layerName)
{
	document.getElementById(layerName).style.visibility="hidden";
}

/* Functions that handle preload. */
function preloadImages() { //v3.0
 var d=document; 
 	if(d.images){ 
		if(!d._p) d._p=new Array();
   		var i,j=d._p.length,a=preloadImages.arguments; 
			for(i=0; i<a.length; i++)
   				if (a[i].indexOf("#")!=0){ 
				d._p[j]=new Image; 
				d._p[j++].src=a[i];
			}
	}
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}

 
 