function returnObjById( id ){ 
	if (document.getElementById){
		var returnVar = document.getElementById(id);
	}
	else if (document.all){
		var returnVar = document.all[id];
	}
	else if (document.layers){
		var returnVar = document.layers[id];
	}
	return returnVar; 
}

function changeLogo(title){
	var logoObj = returnObjById("TVPSA");
	
	if(logoObj == null ) return 1;
	var theChildren  = logoObj.childNodes;
	var imgObj = null;
	var imgObjSrc = null;
	if(theChildren[0].nodeName=="A" && theChildren[0].firstChild.nodeName=="IMG"){
		imgObjSrc = theChildren[0].firstChild.src;
		imgObj = theChildren[0].firstChild;
	}else if(theChildren[0].nodeName=="IMG") {
		imgObjSrc = theChildren[0].src;	
		imgObj = theChildren[0];
	}
	if (imgObjSrc!=null){
		
		if(title=="kontrastowa" && imgObjSrc.indexOf('/gfx/dostepnosc')==-1 ){
			imgObj.src = imgObjSrc.replace('/gfx','/gfx/dostepnosc');
		} else if(title!="kontrastowa" && imgObjSrc.indexOf('/gfx/dostepnosc')!=-1){
			imgObj.src = imgObjSrc.replace('/gfx/dostepnosc','/gfx');	
		}
	}
}

function setActiveStyleSheet(title,noCookie) {
  var noCookie = typeof(noCookie) != 'undefined' ? noCookie : false;

  var i, a, main;
  if(title=="tekstowa"){
	  disableStyleSheets();
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		  if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title") == title) {
	  		a.disabled = false;
		  }
	  }	  
  } else {
	  enableStyleSheets();
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	      if(!a.getAttribute("title") || (a.getAttribute("title") && a.getAttribute("title") == title)){
		  	 a.disabled = false;
		  }else {
 		     a.disabled = true;
		  }
	  }
  }
  
  if(noCookie == false){
	  createCookie("style", title, 365);
  }	  
  changeLogo(title);

  
}

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, strTEst;
  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;
}




function disableStyleSheets() {
	for(i=0;i<document.styleSheets.length;i++) {
		void(document.styleSheets.item(i).disabled=true);
	}
  	el = document.getElementsByTagName('*');
	var ssStyles ='';
	var strTemp ='';
	for ( i=0; i<el.length; i++) {
		//fix dla style="height:35px" w gornej belce
		if(el[i].style.cssText && el[i].style.cssText.indexOf('height: 35px')!=-1 && el[i].style.cssText.indexOf('height: auto')==-1){
			strTemp = el[i].style.cssText; 
			strTemp = strTemp+'height: auto';
			setStyle(el[i],strTemp);
			ssStyles += el[i].style.disabled +' ' + strTemp + '#';
		}	
	}
}
function enableStyleSheets() {
	for(i=0;i<document.styleSheets.length;i++) {
		void(document.styleSheets.item(i).disabled=false);
	}
}

function setStyle( object, styleText ) { 
	if( object.style.setAttribute ) { 
		object.style.setAttribute("cssText", styleText ); 
	} else { 
		object.setAttribute("style", styleText ); 
	} 
}


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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  //Nie ustawiamy niepotrzebnie cookie
  setActiveStyleSheet(title, true);
  //changeLogo(title);
  //Podmiana loga gdy zmiana na kontrastowa wersje i z powrotem
  //content załadowany
  
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

//To musi zostać - gdy style są podmieniane zanim załaduje się content
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title, true);