var makePopup_ua = navigator.userAgent.toLowerCase();
var makePopup_isLinux=(makePopup_ua.indexOf('linux')>-1);
function hideObjects() {
  var os = document.getElementsByTagName('object');
  for (var i=0; i<os.length; i++) {
    os[i].style.display="none";
  }
}
function showObjects() {
  var os = document.getElementsByTagName('object');
  for (var i=0; i<os.length; i++) {
    os[i].style.display="block";
  }
}

function AddPx(num) {
    return String(num) + "px";
}

function findParentDiv(obj) {
  while (obj) {
    if (obj.tagName.toLowerCase() == "div") {
      return obj;
    }
    if (obj.parentElement) {
      obj = obj.parentElement;
    }
    else {
      return null;
    }
  }
  return null;
}

function findParentTagById(obj, parentname) {
  while (obj) {
    if (obj.id.match(parentname)) {
      return obj;
    }

    if (obj.parentElement) {
      obj = obj.parentElement;
    }
    else {
      return null;
    }
  }
  return null;
}

//innentől mozgatás div funkciói
var topZ = 1;
var toTop;
var toLeft;

var popupHeight;
var popupWidth;
var popup;
var timer;
var h_count;
var dragObjTitle;
var arrTiming = [];

if(!h_step) {
  h_step = 80;
}
if(!timer_interval) {
  timer_interval = 40;
}


function showPopupDiv(popupId, h_reference,v_reference, x, y) {
  if (makePopup_isLinux) hideObjects();
  popup = document.getElementById(popupId);

  if(popup.parentNode.tagName !== 'BODY') {
    popup.parentNode.removeChild(popup);
    document.body.appendChild(popup);
  }

  if(arrTiming[popupId]) clearTimeout(arrTiming[popupId]);

  if(!popup) {
    arrTiming[popupId] = setTimeout("showPopupDiv('"+popupId+"',"+h_reference+","+v_reference+","+x+","+y+")",1000);
    return;
  }





  popupHeight = popup.offsetHeight;
  popupWidth = popup.offsetWidth;
  popup.className = "popupdiv";

  // IE doesn't support addEventListener, so check for its presence
  if (popup.addEventListener) {
    // firefox, etc.
    popup.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
    popup.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
    popup.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
  }
  else {
    // IE
    popup.attachEvent("onmousemove", function(e) { return mouseMove(e) });
    popup.attachEvent("onmousedown", function(e) { return mouseDown(e) });
    popup.attachEvent("onmouseup", function(e) { return mouseUp(e) });
  }

  //location.hash = popupId;
  popup.content = popup;

  if (h_reference == 1) { //1 if center, 0 if left
    toLeft = (document.documentElement.clientWidth/2)-(popup.offsetWidth/2) + x + document.documentElement.scrollLeft;
  } else {
    toLeft = x;
  }


  if(v_reference == 1) {
    if(document.documentElement.clientHeight <= popup.offsetHeight) {
      toTop = 5;
    } else {
      toTop = (document.documentElement.clientHeight/2)-(popup.offsetHeight/2) + y + document.documentElement.scrollTop;
    }
  } else {
    toTop = y;
  }
  v_step = (((document.documentElement.clientHeight/2) + popupHeight) / ((document.documentElement.clientWidth/2) +popupWidth))*h_step;

  if(animationType == 'fade') {
    popup.style.top = AddPx(toTop);
    popup.style.left = AddPx(toLeft);
    popup.style.opacity = 0;
  } else {
    popup.style.left = AddPx(-popupWidth);
    popup.style.top = AddPx(-popupHeight);
  }
  moveToPlace();
}

function closeDiv(id){
  document.getElementById(id).style.top = AddPx(-3000);
  document.getElementById(id).style.left = AddPx(-3000);

  if (makePopup_isLinux) showObjects();
}

var opacity = 0;
function moveToPlace(){
    if(animationType != 'fade') {
      if(toLeft > popup.offsetLeft){
        popup.style.left = AddPx(popup.offsetLeft + h_step);
        popup.style.top = AddPx(popup.offsetTop + v_step);
        setTimeout("moveToPlace()", timer_interval);
      }
      else
      {
        popup.style.top = AddPx(toTop);
        popup.style.left = AddPx(toLeft);
      }
    } else {
      if(opacity >= 1) {opacity=0; return;}
      incrementOpacity(popup);
      //popup.style.opacity = parseFloat(popup.style.opacity) + parseFloat(1/h_step);
      setTimeout("moveToPlace()", timer_interval);
    }

}

function incrementOpacity(obj) {
     opacity = parseFloat( opacity );
     // IE/Win
     obj.style.filter = "alpha(opacity:"+parseInt(opacity*100)+")";
     // Safari<1.2, Konqueror
     obj.style.KHTMLOpacity = opacity;
     // Older Mozilla and Firefox
     obj.style.MozOpacity = opacity;
     // Safari 1.2, newer Firefox and Mozilla, CSS3
     obj.style.opacity = opacity;

    opacity = parseFloat(1/h_step)+ parseFloat(opacity);
}


function mouseDown(e) {
  var curElem = e.srcElement || e.target;
  var dragTitle;

  if(ie){
    dragTitle = document.getElementById(findParentDiv(curElem).parentNode.id);
  } else {
    dragTitle = e.currentTarget || findParentDiv(curElem);
  }

  if (curElem.id != 'popup_title') {
    return false;
  }
  if (dragTitle) {
    if (dragTitle.className != 'popupdiv') {
      return;
    }
  }
  // Start the drag, but first make sure neither is null
  if (curElem && dragTitle) {
    // Attach the document handlers. We don't want these running all the time.
    addDocumentHandlers(true);

    // Move this window to the front.
    dragTitle.style.zIndex = topZ;
    dragTitle.content.style.zIndex = topZ;
    topZ++;

    // Check if it's the button. If so, don't drag.
    if (curElem.className != "divTitleButton") {

      // Save away the two objects
      dragObjTitle = dragTitle;

      // Calculate the offset
      dragOffsetX = e.clientX - dragTitle.offsetLeft;
      dragOffsetY = e.clientY - dragTitle.offsetTop;

      // Don't let the default actions take place
      if (e.preventDefault) {
        e.preventDefault();
      } else {
        document.onselectstart = function () { return false; };
        e.cancelBubble = true;
        return false;
      }
    }
  }
}

function mouseMove(e) {
  if (dragObjTitle) {
    if (!e.preventDefault) {
      // This is the IE version for handling a strange
      // problem when you quickly move the mouse
      // out of the window and let go of the button.
      if (e.button == 0) {
        finishDrag(e);
        return;
      }
    }

    dragObjTitle.style.left = AddPx(e.clientX - dragOffsetX);
    dragObjTitle.style.top = AddPx(e.clientY - dragOffsetY);
    dragObjTitle.content.style.left = AddPx(e.clientX - dragOffsetX);
    dragObjTitle.content.style.top = AddPx(e.clientY - dragOffsetY);
    if (e.preventDefault) {
      e.preventDefault();
    }
    else {
      e.cancelBubble = true;
      return false;
    }
  }
}

function mouseUp(e) {
  if (dragObjTitle) {
    finishDrag(e);
  }
}

function finishDrag(e) {
  var finalX = e.clientX - dragOffsetX;
  var finalY = e.clientY - dragOffsetY;
  if (finalX < 0) { finalX = 0 };
  if (finalY < 0) { finalY = 0 };

  dragObjTitle.style.left = AddPx(finalX);
  dragObjTitle.style.top = AddPx(finalY);
  dragObjTitle.content.style.left = AddPx(finalX);
  dragObjTitle.content.style.top = AddPx(finalY);

  // Done, so reset to null
  dragObjTitle = null;
  addDocumentHandlers(false);
  if (e.preventDefault) {
    e.preventDefault();
  }
  else {
    document.onselectstart = null;
    e.cancelBubble = true;
    return false;
  }
}

function addDocumentHandlers(addOrRemove) {
  if (addOrRemove) {
    if (document.body.addEventListener) {
      // firefox, etc.
      document.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
      document.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
      document.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
    }
    else {
      // IE
      document.onmousedown = function() { mouseDown(window.event) } ;
      document.onmousemove = function() { mouseMove(window.event) } ;
      document.onmouseup = function() { mouseUp(window.event) } ;
    }
  } else {
    if (document.body.addEventListener) {
      // firefox, etc.
      removeEventListener("mousedown", function(e) { return mouseDown(e) }, true);
      removeEventListener("mousemove", function(e) { return mouseMove(e) }, true);
      removeEventListener("mouseup", function(e) { return mouseUp(e) }, true);
    } else {
      // IE
      // Be careful here. If you have other code that sets these events,
      // you'll want this code here to restore the values to your other handlers,
      // rather than just clear them out.
      document.onmousedown = null;
      document.onmousemove = null;
      document.onmouseup = null;
    }
  }
}
