var tooltip=function() {
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 20;
    var endalpha = 100;
    var alpha = 0;
    var tt,t,c,b,h;
    var ie = document.all ? true : false;
    return {
        show:function(v,w) {
        if(tt == null){
            tt = document.createElement('div');
            tt.setAttribute('id',id);
            t = document.createElement('div');
            t.setAttribute('id',id + 'top');
            c = document.createElement('div');
            c.setAttribute('id',id + 'cont');
            b = document.createElement('div');
            b.setAttribute('id',id + 'bot');
            tt.appendChild(t);
            tt.appendChild(c);
            tt.appendChild(b);
            document.body.appendChild(tt);
            tt.style.opacity = 0;
            tt.style.filter = 'alpha(opacity=0)';
            document.onmousemove = this.pos;
        }
        tt.style.display = 'block';
        c.innerHTML = v;
        tt.style.width = w ? w + 'px' : 'auto';
        if(!w && ie){
            t.style.display = 'none';
            b.style.display = 'none';
            tt.style.width = tt.offsetWidth;
            t.style.display = 'block';
            b.style.display = 'block';
        }
        if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function(){tooltip.fade(1)},timer);
        },
        pos:function(e) {
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
        },
        fade:function(d) {
            var a = alpha;
            if((a != endalpha && d == 1) || (a != 0 && d == -1)){
                var i = speed;
                if(endalpha - a < speed && d == 1){
                    i = endalpha - a;
                }else if(alpha < speed && d == -1){
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            }else{
                clearInterval(tt.timer);
                if(d == -1){tt.style.display = 'none'}
            }
        },
        hide:function() {
            clearInterval(tt.timer);
            tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
        }
    };
}();

function toggle(elementId, togglerId) {

	var toggleElement = document.getElementById(elementId);
	var toggler = document.getElementById(togglerId);

	if (toggleElement.style.display == "none") {
		var browserName = navigator.appName;

		if (browserName == "Microsoft Internet Explorer") {
			toggleElement.style.display = "block";
		}

		else {

			toggleElement.style.display = "table-row";
		}

		toggler.className = "toggleExpanded";
	}

	else {
		toggleElement.style.display = "none";
		toggler.className = "toggleCollapsed";
	}
}

function setVisibility(elementId, value) {

  if (document.getElementById(elementId) != null) {
    document.getElementById(elementId).style.visibility = value;
  }
}

function toggleVisibility(elementId) {

  var element = document.getElementById(elementId);

  if (element.style.visibility == "hidden") {
    element.style.visibility = "visible";
  }
  else {
    element.style.visibility = "hidden";
  }
}

function setDisplay(elementId, value) {

  var element = document.getElementById(elementId);

  if (element != null) {
    if (element.style.display != value) {
      element.style.display = value;
    }
  }
}

function toggleDisplay(elementId) {

  var element = document.getElementById(elementId);

  if (element.style.display == "none") {
    element.style.display = "inline";
  }
  else {
    element.style.display = "none";
  }
}

function getSelectedAdID() {

  var len = document.forms["adEditSelectForm"].adEditSelect.length;

  var id = "";
  for (var i = 0; i < len; i++) {
    if (document.forms["adEditSelectForm"].adEditSelect[i].checked == true) {
      id = document.forms["adEditSelectForm"].adEditSelect[i].value;
      break;
    }
  }

  return id;
}

function setEditDelete(obj) {

  document.forms['adEditSelectForm'].selectedRowIndex.value = obj.parentNode.parentNode.rowIndex;
  document.forms['advancedForm'].immo_ads_id.value = getSelectedAdID();

  if (document.getElementById('editAdButton') != null) {
    document.getElementById('editAdButton').style.visibility = "visible";
  }

  if (document.getElementById('deleteAdButton') != null) {
    document.getElementById('deleteAdButton').style.visibility = "visible";
  }
}

function clickOnAddButton() {
  toggleDisplay('advancedFormContainer');
  toggleDisplay('idUserAds');

  document.forms['advancedForm'].add.value = "true";
  document.forms['advancedForm'].edit.value = "false";
  
  if (document.getElementById('addImmoButton') != null) {
    document.getElementById('addImmoButton').style.visibility = "hidden";
  }

  if (document.getElementById('editAdButton') != null) {
    document.getElementById('editAdButton').style.visibility = "hidden";
  }

  if (document.getElementById('deleteAdButton') != null) {
    document.getElementById('deleteAdButton').style.visibility = "hidden";
  }
}

function submitEditForm(ln) {

  if (getSelectedAdID() != '') {
    //document.forms['advancedForm'].action = "/?action=input_contact&ln=" + ln;
    document.forms['advancedForm'].action = "/?action=simple_ad&ln=" + ln;
    document.forms['advancedForm'].add.value = "false";
    document.forms['advancedForm'].edit.value = "true";
    document.forms['advancedForm'].submit();
  }
}

function setDeleteImage(i, pictureID) {
  document.getElementById('delete_image' + i).value = pictureID;
  
  //if (i == 1) {
  //  alert(document.forms['advancedForm'].delete_image1.value);
  //}
}

function showHideSpam() {

  if (document.getElementById('spamText').style.visibility == "visible") {
     document.getElementById('spamText').style.visibility = "hidden";
  }
  else {
    document.getElementById('spamText').style.visibility = "visible";
  }
}

function showHideLogin(e, where) {
  
  toggleDisplay('loginDiv');

  var element = document.getElementById('loginDiv');

  if (where == 'header') {
    element.style.backgroundImage = "url('images/login_bg1.png')";
    
    if (document.all) {
      element.style.pixelTop = e.y - 20;
      element.style.pixelLeft = e.x - 280;

      element.style.width = 250;
    }
    else {
      element.style.top = (e.pageY - 20) + 'px';
      element.style.left = (e.pageX - 280) + 'px';

      element.style.width = '250px';
    }
  }
  else if (where == 'menu_my_ads') {
    element.style.backgroundImage = "url('images/login_bg2.png')";

    if (document.all) {
      element.style.pixelTop = e.y + 160;
      element.style.pixelLeft = 10;

      element.style.width = 200;
    }
    else {
      element.style.top = (e.pageY + 160) + 'px';
      element.style.left = '10px';

      element.style.width = '200px';
    }
  }
  else if (where == 'menu_login') {
    element.style.backgroundImage = "url('images/login_bg2.png')";
    
    if (document.all) {
      element.style.pixelTop = e.y + 110;
      element.style.pixelLeft = 10;

      element.style.width = 200;
    }
    else {
      element.style.top = (e.pageY + 110) + 'px';
      element.style.left = '10px';

      element.style.width = '200px';
    }
  }
  if (where == 'footer') {
    element.style.backgroundImage = "url('images/login_bg1.png')";

    if (document.all) {
      element.style.pixelTop = e.y - 220;
      element.style.pixelLeft = e.x - 170;

      element.style.width = 250;
    }
    else {
      element.style.top = (e.pageY - 220) + 'px';
      element.style.left = (e.pageX - 170) + 'px';

      element.style.width = '250px';
    }
  }
}

function emailMsgWindow() {
  var obj = document.getElementById("emailDiv");

  obj.style.display = "inline";


  var IpopTop = (document.body.clientHeight - obj.offsetHeight) / 2;
  var IpopLeft = (document.body.clientWidth - obj.offsetWidth) / 2;

  obj.style.left = IpopLeft + document.body.scrollLeft + 'px';
  obj.style.top = "100px"; //IpopTop + document.body.scrollTop - 100 + 'px';

  obj.style.zIndex = "1";
}

function resetCitiesSelectBox() {
  var obj = document.getElementById('city');
  if (obj != null) {
    obj.innerHTML = "<option value=''>-</option>";
  }
}

function resizePopular() {
  var obj = document.getElementById('popularDiv');
  if (obj != null) {
    var htmlheight = document.body.parentNode.scrollHeight;
    var windowheight = window.innerHeight;
    var top = findPosY(obj);
    //alert("windowheight = " + windowheight + ", htmlheight = " + htmlheight + ", top = " + top);
    if (htmlheight < windowheight) {
      obj.style.height = (windowheight - top - 40) + "px";
    }
    else {
      obj.style.height = (htmlheight - top - 40) + "px";
    }
  }
}

function findPosX(obj) {
  var curleft = 0;
  if(obj.offsetParent)
      while(1)
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;

  return curtop;
}

function blockField(element_id) {
    element = document.getElementById(element_id);
    if (element.disabled == false) {
        element.disabled = true;
    } else {
        element.disabled = false;
    }
}

function zoomImage(img_id, data_id) {

  element = document.getElementById(img_id);
  data = document.getElementById(data_id);
  if (element.getAttribute("src") == "images/inputLogoTrans.png") {
      data.setAttribute("class", "commonDataZoom");
      element.setAttribute("src", "images/inputLogoZoom.png");
  } else {
      data.setAttribute("class", "commonData");
      element.setAttribute("src", "images/inputLogoTrans.png");
  }
}

function changeSelectByValue(id, value) {

  var select = document.getElementById(id);
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i].value == value) {
      if (select.selectedIndex != i) {
        select.selectedIndex = i;
      }

      break;
    }
  }
}

function countCharacters(input_id, input_text_id, max) {
  
  counter = parseInt(document.getElementById(input_id).value);
  inputLength = document.getElementById(input_text_id).value.length;
  //alert(inputLength);
  if (inputLength == 0) {
      document.getElementById(input_id).value = max;
  }

  if (counter <= max && counter >=0 && inputLength != 0) {
      document.getElementById(input_id).value = max - inputLength;
      counter = parseInt(document.getElementById(input_id).value);
  }

  if (counter < 0) {
      document.getElementById(input_id).value = 0;
      //alert("Uneli ste svih " + max + " karaktera");
      document.getElementById(input_text_id).value = document.getElementById(input_text_id).value.toString().substr(0, max);
  }
}
