// ------------------------------------------------------- //
// ------------------>>> Main Scripts <<<----------------- //
// ------------------------------------------------------- //
function setActiveStyleSheet(title) {
  var i, a, main;
  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;
    }
  }
}

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;
  defaultstyle = "dark";
  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 defaultstyle;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  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 correctLocalLink() {
	var url,i,a,s;
	url = "http://"+location.host+location.pathname;
	host = "http://"+location.host+"/";	
	a = document.getElementsByTagName("a");
	aLength = a.length;
	for (i = 0; i < aLength; i++){
		if (a[i].getAttribute("href").indexOf("#") != -1)
		{
			s = a[i].getAttribute("href");
			s = s. replace (host, url);
			a[i].removeAttribute("href");
			a[i].setAttribute('href', s);
		}
	}
}
function gebi(id)
{
	var x;
	try
	{
		if(document.getElementById && !document.all){x = document.getElementById(id);}else if(document.all){x = document.all[id];}	
		if (x == null) throw "err";	
		return x;
	}
	catch(er){ 
		//if(er == "err") {alert("ERROR: No Found Element['"+id+"']");}
		return;
	}
}

function mailprtect(adrr,site,dmn) {
	var add = adrr+'&#64;'+site+'.'+dmn;	
	var ml ='&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+add;
	document.write('<a href="'+ml+'" title="'+add+'" target="_blank">'+add+'</a>');
}
function init_loader() {
var arrayPageSize = getPageSize();
var objBody = document.getElementsByTagName("body").item(0);
var objLoader = document.createElement("div");
objLoader.setAttribute('id','loader');
objLoader.style.height = arrayPageSize[1] +"px";
objLoader.style.backgroundPosition = "center";
objLoader.style.backgroundRepeat = "no-repeat";
objBody.appendChild(objLoader);
}

function remove_loader() {
var loader = gebi('loader').style;
loader.display = "none";
}

function start() {
gebi('overlay').style.display = "block";
gebi('login_container').style.display='block';
gebi('login_container').style.visibility='visible';	
gebi('enter').style.display='none';
}

function end() {
gebi('overlay').style.display = "none";
gebi('login_container').style.display='none';
gebi('login_container').style.visibility='hidden';
gebi('enter').style.display='block';
}

// getPageSize() - Returns array with page width, height and window width, height
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else { 
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll) 
	return arrayPageSize;
}
function showObj(id){ gebi(id).style.display = "block";}
function hideObj(id){ gebi(id).style.display = "none";}
function init_restripe() {
	if (gebi('userlist')) {stripe('userlist');}
	if (gebi('toplist')) {stripe('toplist');}
}
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
window.onload = function(e) {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	init_restripe();
	// overlay activation
	if (!document.getElementsByTagName)
	{ 
		return; 
	} else {
		var arrayPageSize = getPageSize();
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.style.height = arrayPageSize[1] +"px";
		objBody.appendChild(objOverlay);
		correctLocalLink();
	}
}
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}