var oldLen = 0;
var lingonOwoc = new Array('images/fruit/lingon/owoce/', 'jpg', 1, 7);
var lingonTeren = new Array('images/fruit/lingon/teren/', 'jpg', 1, 3);
var borowkaOwoc = new Array('images/fruit/borowka/owoce/', 'jpg', 1, 4);
var borowkaTool = new Array('images/fruit/borowka/zbieraczki/', 'jpg', 1, 6);
var jagodaOwoc = new Array('images/fruit/moroszka/owoce/', 'jpg', 1, 7);
var jagodaTeren = new Array('images/fruit/moroszka/teren/', 'jpg', 1, 4);
var duzyDomek = new Array('images/kamping/duzy/','jpg', 1, 6);
var malyDomek = new Array('images/kamping/maly/','jpg', 1, 6);
var namiot = new Array('images/kamping/namiot/','jpg', 1, 5);
var camping = new Array('images/kamping/kamping/','jpg', 1, 9);
var uBerriesShop = new  Array('images/kamping/skup/','jpg', 1, 7);
var norsjoA = new  Array('images/norsjo/','jpg', 1, 5);
var uPlace = new Array('images/kamping/uzytki/', 'jpg', 1, 5);

function switchImage(dst, dstArr, next){
    if (next == 1){
        dstArr[2]+=1;
        if (dstArr[2] > dstArr[3]){
            dstArr[2] = 1;
        }
    } else {
        dstArr[2] -= 1;
        if (dstArr[2] < 1){
            dstArr[2] = dstArr[3];
        }
    }      
    
    var dstObj = getElementReference(dst);
    dstObj.src = dstArr[0]+dstArr[2]+"." + dstArr[1];
     return;       
}

function checkGeustBookLimit(obj, limit){
  var object = getElementReference(obj);  
  object.className = "guestBookEntryTextField";
  
  getElementReference('gbLen').value = object.value.length;  
  
  if (window.event && object.value.length >= limit){        
    object.value = object.value.substring(0, limit);
    object.className = 'guestBookEntryTextFieldError';
    window.event.returnValue = false;    
  }
  
}


// don't remember where did I get it from, so if author of this function reads it
// please contact me at: mariusz.skwarczynski@gmail.com
function getElementReference(elemName){   
   isNS4 = (document.layers) ? true : false;
   isIE4 = (document.all && !document.getElementById) ? true : false;
   isIE5 = (document.all && document.getElementById) ? true : false;
   isNS6 = (!document.all && document.getElementById) ? true : false;
   
   if(isIE4){
      return document.all[elemName];
   }
   else if (isNS6 || isIE5){
      return document.getElementById(elemName);
   }
   else{
      return document.layers[elemName];
   }   
   return 0;
}

function createHTMLElement(objTag, objName, objId, objClass){
  var oReturn = document.createElement(objTag);
  oReturn.className = objClass;
  oReturn.name = objName;
  oReturn.id = objId;
  return oReturn;
}

function removeAddPerson(main, objName){ 
  var oToRemove = getElementReference(objName);
  getElementReference(main).removeChild(oToRemove);
}

function createAdditionalPerson(dst){

  var dstObj = getElementReference(dst);
  var elemNo = dstObj.childNodes.length;

  var dstSubContainer = createHTMLElement("div","addContainer_"+elemNo,"addContainer_"+elemNo, "addPersonContainer","oneLineDiv");
  
  var nameContainer = createHTMLElement("div","","",'oneLineDiv');
  var nameLabelContainer = createHTMLElement("div","","",'fiftyLeft');
  var nameInputContainer = createHTMLElement("div","","",'fiftyRight');  
  var nameLabel = createHTMLElement("span","","", 'title');  
  var nameInput = createHTMLElement("input","addUsrName[]","addUsrName_"+elemNo,'inputText');
  
  nameLabelContainer.appendChild(nameLabel);
  nameInputContainer.appendChild(nameInput);
  nameContainer.appendChild(nameLabelContainer);
  nameContainer.appendChild(nameInputContainer);
  
  var surrNameContainer = createHTMLElement("div","","",'oneLineDiv');
  var surrNameLabelContainer = createHTMLElement("div","","",'fiftyLeft');
  var surrNameInputContainer = createHTMLElement("div","","",'fiftyRight');   
  var surrNameLabel = createHTMLElement("span","","","title");
  var surrNameInput = createHTMLElement("input","addUsrSurr[]","addUsrSurr_"+elemNo,"inputText");
  
  surrNameLabelContainer.appendChild(surrNameLabel);
  surrNameInputContainer.appendChild(surrNameInput);
  surrNameContainer.appendChild(surrNameLabelContainer);
  surrNameContainer.appendChild(surrNameInputContainer);
  
  var peselContainer = createHTMLElement("div","","",'oneLineDiv');
  var peselLabelContainer = createHTMLElement("div","","",'fiftyLeft');
  var peselInputContainer = createHTMLElement("div","","",'fiftyRight');
  var peselLabel = createHTMLElement("span","","","title");
  var peselInput = createHTMLElement("input","addUsrPesel[]","addUsrPesel_"+elemNo,"inputText");
  
  peselLabelContainer.appendChild(peselLabel);
  peselInputContainer.appendChild(peselInput);
  peselContainer.appendChild(peselLabelContainer);
  peselContainer.appendChild(peselInputContainer);
  
  nameLabel.appendChild(document.createTextNode("Imie: "));
  surrNameLabel.appendChild(document.createTextNode("Nazwisko: "));
  peselLabel.appendChild(document.createTextNode("PESEL: "));  
    
  var btnsContainer = createHTMLElement("div","","","oneLineDiv");
  var btnAddAnother = createHTMLElement("button","","","mainButton");
  btnAddAnother.setAttribute("type", "button", 0);
  btnAddAnother.appendChild(document.createTextNode("Dodaj kolejną osobę"));
  btnAddAnother.onclick = function() {
    createAdditionalPerson("addPerson");
  };
  
  var btnRemoveCurrent = createHTMLElement("button","","","mainButton");
  btnRemoveCurrent.setAttribute("type", "button", 0);
  btnRemoveCurrent.appendChild(document.createTextNode("Usuń"));
  btnRemoveCurrent.onclick = function() {
    removeAddPerson(dst, "addContainer_"+elemNo);
  };
  
  btnsContainer.appendChild(btnAddAnother);
  btnsContainer.appendChild(btnRemoveCurrent);
  
  var hrObj = createHTMLElement("div","","","oneLineDiv");
  hrObj.style.borderTop = '1px solid black';
    
  dstSubContainer.appendChild(nameContainer);
  dstSubContainer.appendChild(surrNameContainer);
  dstSubContainer.appendChild(peselContainer);
  dstSubContainer.appendChild(btnsContainer);
  dstSubContainer.appendChild(hrObj);
  dstObj.appendChild(dstSubContainer);
}

var gbEntryEnabled = 0;
var counterInterval = NaN;
var canEdit = true;

function turnItOn(maxChars){
  gbEntryEnabled = 1;
  canEdit = 1;
  counterInterval = setInterval("countChars("+maxChars+")", 100);
}

function updateCounter(counter, newVal){
  if ( counter.hasChildNodes() ){    
    counter.childNodes[0].nodeValue = newVal;
  } else { 
    counter.appendChild(document.createTextNode(newVal));
  }
}

function turnItOff(){
  gbEntryEnabled = 0;
  clearInterval(counterInterval);
  counterInterval = NaN;
}

function countChars(maxChars){  
  var entryObj = getElementReference('gbEntryValue');
  var counterObj = getElementReference('gbLen');
  var currValLen = entryObj.value.length;
  
  if ( currValLen >= maxChars ){
    	var currValue = entryObj.value;
    	entryObj.value = currValue.substring(0, maxChars);
    	updateCounter(counterObj, maxChars);
  } else {
    updateCounter(counterObj, currValLen);	
  }   
}
function getCursorPosition( e )
{
   var docX, docY;
   docX = 0; docY = 0;
   doAdd = true;
   if (!e)
   {
      e = window.event;           
   }
   
   if( typeof( e.pageX ) == 'number' )
   {
      docX = e.pageX;
      docY = e.pageY;
      doAdd = false;
   }
   else
   {
      docX = e.clientX;
      docY = e.clientY;
   }   
   if (doAdd == true){
    if( document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft) )
    {
       docX += document.documentElement.scrollLeft;
       docY += document.documentElement.scrollTop;
    } 
    else if( document.body && (document.body.scrollTop || document.body.scrollLeft) )
    {
       docX += document.body.scrollLeft;
       docY += document.body.scrollTop;
    }
  }
   var tmp = new Array(2);
   tmp[0] = docX;
   tmp[1] = docY;

   return tmp;
}

var currPopUpName = "";
var popupTimeOut = NaN;

function showPopUp(e, popupName){
	var popupMenu = getElementReference(popupName);
    if (currPopUpName == popupName){
        return;
    }
        
	if (currPopUpName !== "" ){
		hidePopUp();  
	} 
    
    var coords = getCursorPosition(e);  
    
    popupMenu.style.visibility = 'visible';
    popupMenu.style.display='block';
    popupMenu.style.left = coords[0]-10+'px';
    popupMenu.style.top = coords[1]+'px';    
    currPopUpName = popupName;
    popupTimeOut = setTimeout("hidePopUp()", 1000);
}

function hidePopUp(){
   if (currPopUpName !== ""){
       var popupMenu = getElementReference(currPopUpName);
       popupMenu.style.visibility = 'hidden';
       popupMenu.style.display='none';
       currPopUpName = "";
   }
   
   clearTimeout(popupTimeOut);
}

function changeImg(sImgObjName, sNewSrc){
  var imgO = getElementReference(sImgObjName);  
  imgO.src = sNewSrc; 
}


function setPopupTO(t){
    popupTimeOut = setTimeout("hidePopUp()", t);
}

function setDivPosition(obj, x, y){
 obj.style.left = x;
 obj.style.top = y;
}

function showDiv(e, objName, doHide){
 var divO = getElementReference(objName);

 if (doHide){
  divO.style.display = "none";
  divO.style.visibility = "hidden";
 } else {
   divO.style.display = "block";
   divO.style.visibility = "visible";   
   coords = getCursorPosition(e);
   setDivPosition(divO, coords[0]+20, (coords[1]/2)+10);
 }
 
}

function putObjOnCurrCursorPos(e, obj){
 coords = getCursorPosition(e);
 var getO = getElementReference(obj);
 setDivPosition(getO, coords[0]+20, (coords[1]/2)+10);
}