

var cookieName = "hmb_datastore"; 
var cookieData = null; 

function getEl(name,popup) {
   if(getEl.arguments.length == 1) {
     return document.getElementById(name); 
   } else {
     return window[popup].document.getElementById(name);    	
   }
}

function addToSticky(houseId) {
   var curSticky = getSavedInput("sticky", ""); 
   var ids = null; 
   if(curSticky != "") {
      ids = curSticky.split("@@"); 
   } else {
      ids = new Array(); 
   }
   ids[ids.length] = houseId; 
   updateStickyInfo(ids); 
   blinkStickyLink(5);   
}

function removeFromSticky(houseId) {
   var curSticky = getSavedInput("sticky", ""); 
   var ids = curSticky.split("@@"); 
   var remove = 0; 
   var i = j = 0; 
   for(; i < ids.length; i++) {
      if(i != j) {
         ids[j] = ids[i]; 
      } else {
        if(ids[i] != houseId) {
           ++j; 
        } else {
          ++remove; 
        }
      }
   }
   if(remove != 0) {
      ids.length = ids.length - remove; 
   }
   updateStickyInfo(ids); 
}

function updateStickyInfo(theArray) {
   var count = 0; 
   if(theArray) {
      saveInput("sticky", arrayToCommaList(theArray)); 
      count = theArray.length; 
   } else {
      var stickyStr = getSavedInput("sticky", ""); 
      if(stickyStr != "") {
         var theArray = stickyStr.split("@@"); 
         count = theArray.length; 
      }
   }
   var obj = getEl("mz-anzahl"); 
   if(obj) {
      obj.innerHTML = "" + count; 
   }
}
   
   
function arrayToCommaList(theArray) {
   var str = null; 
   for(i = 0; i < theArray.length; i++) {
      if(str == null) {
         str = theArray[i]; 
      } else {
         str += "@@" + theArray[i]; 
      }
   }
   if(str == null) {
      str = ""; 
   }
   return str; 
}

function shiftStickyStatus(houseId,popup) {
   var curSticky = getSavedInput("sticky", ""); 
   var stickyIds = curSticky.split("@@"); 
   var found = false; 
   for(j = 0; j < stickyIds.length && j < 20; j++) {
      if(stickyIds [j] == houseId) {
         found = true; 
         break; 
      }
   }
   if(found) {
      removeFromSticky(houseId); 
   } else {
     if(stickyIds.length >= 20) {
        alert("Sie können maximal 20 H\xE4user auf Ihrem Merkzettel haben"); 
        return; 
     }
     addToSticky(houseId)
   }
   if(shiftStickyStatus.arguments.length==1){
   	initSticky(new String(houseId)); 
   } else {
   	initSticky(new String(houseId),popup); 
   }
}

function initSticky(houses,popup) {
   if(houses == "") {
      return; 
   }
   var curSticky = getSavedInput("sticky", ""); 
   var stickyIds = curSticky.split("@@"); 
   var houseIds = houses.split(","); 
   for(i = 0; i < houseIds.length; i++) {
      houseId = houseIds[i]; 
      var found = false; 
      for(j = 0; j < stickyIds.length && j < 20; j++) {
         if(stickyIds [j] == houseId) {
            found = true; 
            break; 
         }
      }
      if(initSticky.arguments.length==1) {
      	var link = getEl("mzL" + houseId); 
      	var image = getEl("mzI" + houseId);
      } else {
      	var link = getEl("mzL" + houseId,popup); 
      	var image = getEl("mzI" + houseId,popup);
      }

      if(link && image) {
         if(found) {
            link.innerHTML = "Von Merkliste entfernen"; 
            image.src = "layout/merk-entfernen.gif"; 
         } else {
            link.innerHTML = "Auf Merkliste setzen"; 
            image.src = "layout/merk-setzen.gif"; 
         }
      }
   }

}
   
   
function showCurrentSticky() {
   alert(getSavedInput("sticky", "")); 
}

function blinkStickyLink(num) {
   obj = getEl("mz-perma"); 
   if(obj != null) {
      if(num % 2 == 0) {
         obj.style.color = "#00a337"; 
         if(num != 0) {
            setTimeout("blinkStickyLink(" + (num - 1) + ")", 400); 
         }
      } else {
         obj.style.color = "#ffffff"; 
         setTimeout("blinkStickyLink(" + (num - 1) + ")", 400); 
      }
   }
}
   
   
function setCookie(name, value) {
   var argv = setCookie.arguments; 
   var argc = setCookie.arguments.length; 
   //var expires = new Date(2010, 0, 1); 

   var expires = new Date();
   var tmpDate = expires.getTime() + (1 * 2 * 60 * 60 * 1000) //Tage,Stunden,Minuten,Sekunden,Millisekunden;
   expires.setTime(tmpDate);
   
   var path = "/"; 
   var domain = (argc > 4) ? argv[4] : null; 
   var secure = (argc > 5) ? argv[5] : false; 
   document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); 
}

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) {
         var endstr = document.cookie.indexOf(";", j); 
         if(endstr ==- 1)endstr = document.cookie.length; 
         return unescape(document.cookie.substring(j, endstr)); 
         }
      i = document.cookie.indexOf(" ", i) + 1; 
      if(i == 0)break; 
      }
   return null; 
}

function getSavedInput(name, defaultValue) {
   if(cookieData == null) {
      cookieData = getCookie(cookieName); 
   }
   var tmpData = cookieData; 
   if(tmpData == null) {
      return defaultValue; 
   }
   var index = tmpData.indexOf("#"); 
   while (index !=- 1) {
      var curData = tmpData.substring(0, index); 
      var index2 = curData.indexOf("="); 
      if(index2 !=- 1) {
         var foundName = curData.substring(0, index2); 
         if(foundName == name) {
            return curData.substring(index2 + 1); 
         } else {
            tmpData = tmpData.substring(index + 1); 
            index = tmpData.indexOf("#"); 
         }
      } else {
         index =- 1; 
      }
   }
   return defaultValue; 
}

function saveInput(name, value) {
   if(cookieData == null) {
      cookieData = getCookie(cookieName); 
      if(cookieData == null) {
         cookieData = ""; 
      }
   }
   var tmpData = cookieData; 
   if(tmpData == null) {
      tmpData = ""; 
   }
   var found = 0; 
   var newData = ""; 
   var index = tmpData.indexOf("#"); 
   while (index !=- 1) {
      var curData = tmpData.substring(0, index); 
      var index2 = curData.indexOf("="); 
      if(index2 !=- 1) {
         var foundName = curData.substring(0, index2); 
         if(foundName == name) {
            newData += name + "=" + value + "#"; 
            found = 1; 
         } else {
            newData += curData + "#"; 
         }
         tmpData = tmpData.substring(index + 1); 
         index = tmpData.indexOf("#"); 
      } else {
         index =- 1; 
      }
   }
   if(newData == "" || found == 0) {
      newData += name + "=" + value + "#"; 
   }
   cookieData = newData; 
   setCookie(cookieName, cookieData); 
}

