function GetSelectValue(elementId) {
	var Select = document.getElementById('ch'+elementId);
	if (Select.options[Select.selectedIndex].value==0)
		HideSectionAgain('chfield'+elementId);
	else
		ShowSubSection('chfield'+elementId);	
}

function HideSubSection()
{
	var slc = document.getElementsByTagName('select');
	for (var i=0;i<slc.length;i++) {
		var slcclass = slc[i].getAttribute("class");
		var slcclass = slc[i].className;
		if (slcclass == "selectShow") {		
		var opt = slc[i].getElementsByTagName('option');
		var id  = slc[i].getAttribute("id");
		var numer = id.substr(2);
		GetSelectValue(numer);
		/* tak po DOMowemu bardziej :) Nie dziala w IE :(
		for (var j=0;j<opt.length;j++) {
		   if (opt[j].getAttribute("selected")=="selected") {
			  if (opt[j].value == "0") {
				fieldOpt = "chfield"+numer;
				var subsection = document.getElementById(fieldOpt);
				subsection.style.display = "none";
			  }
		   }
		}*/
		}
	}
}

function ShowSubSection(x)
{
  var field = document.getElementById(x);
  field.style.display = "block";
  field = null;
}

function HideSectionAgain(x)
{
  var field = document.getElementById(x);
  field.style.display = "none";
  field = null;
}

function ShowHide(x) {
	var field = document.getElementById(x);
	if (field.style.display == "none")
		ShowSubSection(x);
    else
		HideSectionAgain(x);
    field = null;
}

function HideOptionLists() {
	var tables = document.getElementsByTagName("div");
	var re = new RegExp("(options)([0-9]+)$");
	for (i=0;i<tables.length;i++) {
		var id  = tables[i].getAttribute("id");		
		if (id && id.match(re)) {
			HideSectionAgain(id);
		}
	}
	tables = null;
}

function Fade() {
	if (document.getElementById("fade")) {
		var fadeElement = document.getElementById("fade");
		finalColors = [238, 238, 238];
    } else if (document.getElementById("bigfade")) {
		var fadeElement = document.getElementById("bigfade");
		finalColors = [255, 255, 255];
	}
	var color = fadeElement.style.backgroundColor;
  	color = color.split(' ');
  	color = color.join('');
	if (color.substr(0,1) != '#') {
        var color1 = parseInt(color.substr(4,3),10);
        var color2 = parseInt(color.substr(8,3),10);
        var color3 = parseInt(color.substr(12,3),10);
    } else {
        var color1 = parseInt(color.substr(1,2),16);
        var color2 = parseInt(color.substr(3,2),16);
        var color3 = parseInt(color.substr(5,2),16);
    }
    var colors = [color1, color2, color3];
    //alert(colors);
	if (!color1 || !color2 || !color3) {
		colors = [153, 255, 153];
	}
	if (colors[0] < finalColors[0] || colors[1] < finalColors[1] || colors[2] < finalColors[2]) {
		for (i=0; i<colors.length; i++) {
			if (colors[i] < finalColors[i]) {
				colors[i] += 4;
			} else {
				colors[i] = finalColors[0];
			}
		}
		fadeElement.style.backgroundColor = "rgb("+colors[0]+","+colors[1]+","+colors[2]+")";
		window.fade = window.setInterval("Fade()", 100);
	} else if (colors[0] >= finalColors[0] && colors[1] >= finalColors[1] && colors[2] >= finalColors[2])  {
		fadeElement.style.backgroundColor = "rgb("+finalColors[0]+","+finalColors[1]+","+finalColors[2]+")";
		window.clearInterval(window.fade);
	}
	fadeElement = null;
}

function checkIt(string) {
  place = detect.indexOf(string) + 1;
  thestring = string;
  return place;
}

var detect = navigator.userAgent.toLowerCase();
var browser;

sfHover = function() {
        if (checkIt('opera')) browser = "opera";
        if (checkIt('msie') && browser != 'opera') {
          var sfEls = document.getElementsByTagName("tr");
          for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function()
              {
			  	  if (this.parentNode.className == "hover")
                  	this.className+=" sfhover";
              }
            sfEls[i].onmouseout=function()
              {
                  this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
              }
          }
        }
}

/*function ShowTitle(elementId) {
	var Element = document.getElementById("ole");
	var Title = Element.getAttribute("class");
	var Text  = document.createTextNode(Title);
	var Zlew  = document.createElement("div");
	document.insertBefore(Element,Zlew);
	div.style.height = "100px";
	Div.appendChild(Text);
}*/

function showLinks() {
	var linkMsg = document.createElement('div');
	linkMsg.setAttribute('id','linkMsg');
	linkMsg.style.display    = "none";
	linkMsg.style.visibility = 'hidden';
	
	var body = document.getElementsByTagName("body").item(0);
	body.appendChild(linkMsg);
	var lnks = body.getElementsByTagName('a');
	var msg  = new Array();
	//var j    = 0;
	for (i=0;i<lnks.length;i++) {
		if (lnks[i].getAttribute("title") && lnks[i].hasChildNodes) {
			lnks[i].onmouseover = function(e) {
				if (!e) var e = window.event;
				x = mouseX(e);
				y = mouseY(e);
				return ShowMsg(this, x, y);
			}
			lnks[i].onmouseout = function() {
				//pausecomp(250);
				document.getElementById('linkMsg').style.display = 'none';
			}
			//j++;
		}
	}
	lnks    = null;
	linkMsg = null;
	body    = null;
}

function pausecomp(millis) {
	date = new Date();
	var curDate = null;	
	do {
		var curDate = new Date();
	} while(curDate-date < millis);
}

function ShowMsg(link, x, y) {
	if (document.getElementById && x && y) {
		var linkMsg = document.getElementById('linkMsg');
		var msg   = link.getAttribute('title');
		var title = document.createTextNode(msg);
		if (linkMsg.firstChild) {
			var old = linkMsg.firstChild;
			linkMsg.replaceChild(title, old);
		}
		else {
			linkMsg.appendChild(title);
		}
		linkMsg.style.display    = "block";
		linkMsg.style.visibility = 'visible';
		linkMsg.style.position   = 'absolute';
		linkMsg.style.top        = y+10+"px";
		linkMsg.style.left       = x+3+"px";
		linkMsg = null;
	}
}

function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
   		return evt.clientX + (document.documentElement.scrollLeft ?
   			document.documentElement.scrollLeft :
   			document.body.scrollLeft);
	else return null;
}

function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
   		return evt.clientY + (document.documentElement.scrollTop ?
   		document.documentElement.scrollTop :
   		document.body.scrollTop);
	else return null;
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

function SectionsTree() {
	var sectionsTable = document.getElementById('sections');
	var shLinks = sectionsTable.getElementsByTagName('a');
	var hides   = new Array(shLinks.length);
	for (var i = 0; i < shLinks.length; i++) {
		if (shLinks[i].getAttribute('id') && shLinks[i].getAttribute('id').substring(0, 6) == 'shLink') {
			var poddzialy = document.getElementById('poddzialy' + shLinks[i].getAttribute('id').substring(6));			          
			if (poddzialy.className != 'moved') {
                poddzialy.style.display = 'none';
            } else {
                poddzialy.style.display = 'block';
                shLinks[i].childNodes.item(0).setAttribute('src','http://www.cms.magneticpoint.com/admin/images/minus.gif');
            }
			shLinks[i].onclick = function() {
				//var id = shLinks[i].getAttribute('id').substring(6);
				var hide = document.getElementById('poddzialy' + this.getAttribute('id').substring(6));
				if (hide.style.display == 'block') {
					hide.style.display = 'none';
					this.childNodes.item(0).setAttribute('src','http://www.cms.magneticpoint.com/admin/images/plus.gif');
				} else {
					hide.style.display = 'block';
					this.childNodes.item(0).setAttribute('src','http://www.cms.magneticpoint.com/admin/images/minus.gif');
                }
                hide = null;
			}
			poddzialy = null;
		}
	}
	hides         = null;
	shLinks       = null;
	sectionsTable = null;
}

function createAjaxObject() {
    var browser = navigator.appName;
    if (window.ActiveXObject){
        AjaxObject = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        AjaxObject = new XMLHttpRequest();
    }
    return AjaxObject;
}

function refreshSession() {
	var http = createAjaxObject();
	http.open('get', 'refresh.php');
    http.onreadystatechange = function() {
			if (http.readyState == 4) {
				var response = http.responseText;
			}
		}
    http.send(null);
}

window.onload = function() {
  showLinks();
  HideSubSection();
  HideOptionLists();
  if (document.getElementById('fade') || document.getElementById('bigfade')) {
  	Fade();
  }
  if (document.getElementById('sections')) {
  	SectionsTree();
  }
  if (window.ActiveXObject || window.XMLHttpRequest) {
  	var refreshSession  = window.setInterval("refreshSession()", 60000);
  }
}