var prevX, prevY, prevId=0, clicked=false, new_click=true, sav_event, d=document;

function switchDiv(id, frame, load) {
   // Initialize menu and loading frame sources
   var menu = iniMenu(frame);

   if(load == menu.alt)
      // Switch from visible to hidden and back if we clicked on that icon before
      menu.style.display = (menu.style.display=="none")?"":"none";
   else
      initDescription(frame, load);
   menu.alt = load;
};

function initDescription(frame, load) {
   // Copy a new description and show it
   var menu = iniMenu(frame);

   if(document.getElementById && !(d.all))
      menu.innerHTML = d.getElementById('load'+load).innerHTML;
   else if(d.all)
      menu.innerHTML = d.frames['load'+load].innerHTML;

   menu.style.display = "";
};


//PICKUP
////////

function pickup(action, id) {
   //Update database
   div('send').src = './gwbbcode/pickup.php?'+action+'='+id+'&rand=' + Math.round(1000*Math.random());

   //Switch between Add and Remove links
   if (action != 'switch') {
      var opp_action = (action=='remove') ? 'add' : 'remove';
      div(action+'_'+id).style.display = 'none';
      div(opp_action+'_'+id).style.display = '';
   }
}

function pickup_set(userlist, id) {
   for (var i=0; i<d.all.length; i++)
      if ((typeof(d.all[i].id) != 'undefined') && (d.all[i].id == 'pickup_'+id))
         d.all[i].innerHTML = userlist;
}


function iniMenu(frame) {
   if(d.getElementById && !(d.all))
      return d.getElementById('show'+frame);
   else if(d.all)
      return d.all['show'+frame];
};

function div(name) {
   var d = document;
   if(d.getElementById && !(d.all))
      return d.getElementById(name);
   else if(d.all)
      return d.all[name];
};


//TEMPLATE

function switch_template(load) {
   var style = div('gws_template'+load).style;
   if (style.display == 'none') {
      //Show selected template code
      style.display = '';
      div('gws_template_input'+load).select();
      div('gws_template_input'+load).focus();
      //and hide all others
      var divs = document.getElementsByTagName('DIV');
      for (var i=0; i<divs.length; i++) {
         if (   /^gws_template[0-9]/.test(divs[i].id)
             && divs[i].id != 'gws_template'+load
             && divs[i].style.display == '') {
            switch_template(divs[i].id.match(/\d+/)[0]);
         }
      }
   }
   else {
      style.display = 'none';
   }
   return false;
}


//JavaScript Function by Shawn Olson
//Copyright 2004
//http://www.shawnolson.net
//If you copy any functions from this page into your scripts, you must provide credit to Shawn Olson & http://www.shawnolson.net
//*******************************************

function change_css(theClass,element,value) {
   //documentation for this script at http://www.shawnolson.net/a/503/
   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;
}



function getElementsByClass(searchClass) {

   if (document.all) {
        var classElements = new Array();
        var allElements = document.all;
            for (i = 0, j = 0; i < allElements.length; i++) {
                if (allElements[i].className == searchClass) {
                    classElements[j] = allElements[i];
                    j++;
                }
            }   
   } else if (document.getElementsByTagName) {
        var classElements = new Array();
        var allElements = document.getElementsByTagName("*");
            for (i = 0, j = 0; i < allElements.length; i++) {
                if (allElements[i].className == searchClass) {
                    classElements[j] = allElements[i];
                    j++;
                }
            }       
   } else {
      return;
   }

    return classElements;
}

function findPosX(obj) {
    var curleft = 0;
    var blob = obj.x;
    if (obj.offsetParent) {
        while (obj.offsetParent && obj.offsetParent.tagName != "HTML") {
            blob = blob +":"+obj.offsetParent.tagName+":"+obj.offsetLeft;
            curleft += obj.offsetLeft;
            obj = obj.offsetParent; 
        }
    }

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent && obj.offsetParent.tagName != "HTML") {
            curtop += obj.offsetTop;
            obj = obj.offsetParent; 
        }
    }

    return curtop;
}

function overlibc(obj, html, opt) {
//overlib(html);return; // disabled because of problem
    var x = findPosX(obj)+document.getElementsByTagName('BODY')[0].offsetLeft+10;
    var y = findPosY(obj)+document.getElementsByTagName('BODY')[0].offsetTop +obj.offsetHeight+2;
    var width = 456;
    var height = 100;

	if (x - document.body.scrollLeft + width  > document.body.clientWidth) {
	    x = document.body.clientWidth  - width;
	}
//	if (y - document.body.scrollTop  + height > document.body.clientHeight) {
//	    y = document.body.clientHeight - height;
//	}
	ol_fixx = x;
	ol_fixy = y;
    overlib(html);
}


var box;
var gwbb_code = Array();

function gwbb_replace(j) {
	if (box.length <= j) {
		return;
	}

	if (typeof(gwbb_code[j]) != 'undefined'){
		box[j].innerHTML = gwbb_code[j];
		box[j].style.display = 'inline';
		j = j + 1;
	}

	var func = function() {gwbb_replace(j);}
	setTimeout(func, 1);
	return;
}

function gwbb_replace2(j) {
	for (var i=0; i<box.length; i++) {
		box[i].innerHTML = gwbb_code[i];
		box[i].style.display = 'inline';
	}
}

function gwbb_call(encoding){
	box = getElementsByClass('gwbb');
	var jsElement = Array();
	for(var i=0; i<box.length; i++){
		jsElement[i] = document.createElement('script');
		jsElement[i].type='text/javascript';
		jsElement[i].src='http://omomom.celes.cc/gwbb/gwbb2.php?e='+encoding+'&n='+i+'&c='+encodeURIComponent(box[i].innerHTML);
		document.getElementsByTagName('head')[0].appendChild(jsElement[i]); 
	}
}
