//
// Cookie Functions
//
// Written by: Bill Dortch, hIdaho Design <bdortch@hidaho.com>
// released to the public domain
//
//******************************************************************
//

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//******************************************************************
//

function explodeArray(ciag,separator) 
{
  tempArray = new Array(1);
  var i = 0;
  var tempString = new String(ciag);

  while (tempString.indexOf(separator) > 0) 
  {
    tempArray[i]=tempString.substr(0,tempString.indexOf(separator));
    tempString=tempString.substr(tempString.indexOf(separator)+1,tempString.length-tempString.indexOf(separator)+1); 
    i=i+1
  }

  tempArray[i]=tempString;
  return tempArray;
}

function implodeArray(tablica,separator) 
{
	return tablica.join(separator);
}

function showHide(id)
{     	
	var tablica = explodeArray(GetCookie("menu"),'|');
		
	for(var i=0; i<tablica.length; i++)
	{			
		if(tablica[i].substr(0, tablica[i].length-2) == id)
		{
			obj = document.getElementById(id);
			if(obj.style.display == "block")
			{
				tablica[i] = tablica[i].substr(0, tablica[i].length-2)+'-0';
				obj.style.display = "none"; 

				id_bg = id+'bg';		
				objbg = document.getElementById(id_bg);
				objbg.style.background = "url(bck_menu1_plus.gif)";
				//alert('zamykam(showHide): '+id);
				
			} else {
				tablica[i] = tablica[i].substr(0, tablica[i].length-2)+'-1';
				obj.style.display = "block"; 
					
				id_bg = id+'bg';		
				objbg = document.getElementById(id_bg);
				objbg.style.background = "url(bck_menu1_minus.gif)";
				//alert('otwieram(showHide): '+id)
				
				if(GetCookie("last_id") == null)
				{				
					DeleteCookie("last_id");
					var czas = new Date (); 
        			czas.setTime(czas.getTime() + (24 * 60 * 60 * 1000)); 
					SetCookie("last_id",id,czas,'/');
					//alert('ustawiam default last_id: '+id);				
				} else {
					
					var last_id = GetCookie("last_id");
					
					if(last_id != id)
					{
						for(var j=0; j<tablica.length; j++)
						{			
							if(tablica[j].substr(0, tablica[j].length-2) == last_id)
								tablica[j] = tablica[j].substr(0, tablica[j].length-2)+'-0';
						}
												
						objlast = document.getElementById(last_id);
						objlast.style.display = "none";
						id_bg = last_id+'bg';
						objlastbg = document.getElementById(id_bg);
						objlastbg.style.background = "url(bck_menu1_plus.gif)";
						//alert('zamykam(showHide) z last_id: '+last_id);
						
						DeleteCookie("last_id");
						var czas = new Date (); 
        				czas.setTime(czas.getTime() + (24 * 60 * 60 * 1000)); 
						SetCookie("last_id",tablica[i].substr(0, tablica[i].length-2),czas,'/');
						//alert('ustawiam last_id(showHide): '+tablica[i].substr(0, tablica[i].length-2));
					}
				}							
			}						
		}
	}
	
	DeleteCookie ("menu");
	
	var ciag = implodeArray(tablica,'|');

	var czas = new Date (); 
    czas.setTime(czas.getTime() + (24 * 60 * 60 * 1000));  
	SetCookie("menu",ciag,czas,'/'); 
	//alert('Uwzgledniono zmiany w ciasteczku');
}

function dzialaj()
{
	var tablica = explodeArray(GetCookie("menu"),'|');
	 
	for(var i=0; i<tablica.length; i++)
	{	
		if(tablica[i].substr(tablica[i].length - 1) == 1) // otwieramy
		{
			var id_ = tablica[i].substr(0, tablica[i].length-2);
			
			obj = document.getElementById(id_);
			obj.style.display = "block";
			
			var id_bg = id_+'bg';		

			objbg = document.getElementById(id_bg);
			objbg.style.background = "url(bck_menu1_minus.gif)";	
			//alert('otwieram(dzialaj): '+id_);
										
		} else { // zamykamy
			var id_ = tablica[i].substr(0, tablica[i].length-2);

			obj = document.getElementById(id_);
			obj.style.display = "none";

			var id_bg = id_+'bg';	

			objbg = document.getElementById(id_bg);
			objbg.style.background = "url(bck_menu1_plus.gif)";	
			//alert('zamykam(dzialaj): '+id_);		
		}
	}
}

function start_menu(dane_from_php)
{
	//alert('menu start');
	
	if(GetCookie("menu") == null)
	{	
		var ciag = implodeArray(dane_from_php,'|');

		var czas = new Date (); 
        czas.setTime(czas.getTime() + (24 * 60 * 60 * 1000)); 
		SetCookie("menu",ciag,czas,'/'); 
		//alert('Ustawiono defaultowe ciasteczko');
		
		dzialaj();
	}
	else
	{
		var tablica = explodeArray(GetCookie("menu"),'|');
			
		if(tablica.length != dane_from_php.length)
		{
			var ciag = implodeArray(dane_from_php,'|');

			var czas = new Date (); 
        	czas.setTime(czas.getTime() + (24 * 60 * 60 * 1000)); 	 
			SetCookie("menu",ciag,czas,'/'); 
			//alert('Zmiany w strukturze menu - nowe ciasteczko');
		
			dzialaj();			
		} else {
			dzialaj();
		}

	} 	
}


function msize()
{
	var czas = new Date (); 
   czas.setTime(czas.getTime() + + (24 * 60 * 60 * 1000)); 	
	
	var string;
	string = screen.width+"x"+screen.height;
	SetCookie('dstat_size',string,czas,'/');
}
