/**
/ THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function getPageSize() {
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
  }
  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
        if(document.documentElement.clientWidth){
          windowWidth = document.documentElement.clientWidth;
        } else {
          windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
  }
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
        pageHeight = windowHeight;
  } else {
        pageHeight = yScroll;
  }
  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){
        pageWidth = xScroll;
  } else {
        pageWidth = windowWidth;
  }
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
  return arrayPageSize;
};
/**
/ THIRD FUNCTION
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
function getPageScroll() {
  var xScroll, yScroll;
  if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {   // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
  }
  arrayPageScroll = new Array(xScroll,yScroll);
  return arrayPageScroll;
};
function popup(id) {
  if (id!=null) {
    $('#dialog-message').load(id, function() {
      $('#dialog-box').removeClass('newsletter-form');
      resizeDialogBox(false,true);
      $('#dialog-overlay').fadeTo(10, 0.8);
      $('#dialog-box').fadeIn(40);
      $('#dbox').show();
      $('#lbox').hide();
    });
  }
}
function popupNewsLetter(id) {
  if (id!=null) {
    $('#dialog-message').load(id, function() {
      $('#dialog-box').addClass('newsletter-form');
      resizeDialogBox(false,true);
      $('#dialog-overlay').fadeTo(10, 0.8);
      $('#dialog-box').fadeIn(40);
      $('#dbox, #lbox').hide();
    });
  }
}
function lightbox(id) {
  if (id!=null) {
    $('#dialog-message').load(id, function() {
      $('#dialog-box').removeClass('newsletter-form');
      resizeDialogBox(false,true);
      $('#dialog-overlay').fadeTo(10, 0.8);
      $('#dialog-box').fadeIn(40);
      $('#dbox').hide();
      $('#lbox').show();
    });
  }
}
function ipopup(id) {
  if (id!=null) {
    var icontent = $('#'+id).html();
    $('#dialog-message').html(icontent);
    resizeDialogBox();
    $('#dialog-overlay').fadeTo(10, 0.8);
    $('#dialog-box').fadeIn(40);
  }
}
function resizeDialogBox(animate,first,settop) {
  var animTime,
      arrPageScroll = getPageScroll(),
      arrPageSizes = getPageSize(),
      browsWidth = $(window).width();
      browsHeight = $(window).height();
      dialogTop =  ((arrPageSizes[3]/2)+arrPageScroll[1]) - ($('#dialog-box').height()/2),
      dialogLeft = (arrPageSizes[0]/2) - ($('#dialog-box').width()/2);
  if (dialogTop==0 || dialogTop<60) {dialogTop = 60;}
  if (browsWidth >= arrPageSizes[1]) $('#dialog-overlay').css({height:arrPageSizes[1], width:arrPageSizes[0]});
  if (first===true) $('#dialog-overlay').css({height:arrPageSizes[1], width:arrPageSizes[0]});
  if (dialogLeft<0) dialogLeft = 0;
  if (dialogTop<0) dialogTop = 0;
  if(animate===true) {
    if (animTime) clearTimeout(animTime);
    animTime = setTimeout(function() {
      $('#dialog-box').animate({left:dialogLeft,top:dialogTop}, {queue:false, duration: 40});
    }, 2);
  } else {
    $('#dialog-box').css({left:dialogLeft,top:dialogTop});
  }
}
function initLoadLink() {
  $('a.load').click(function () {
    var thisurl = $(this).attr('href');
    $('#dialog-message').load(thisurl, function() {
      resizeDialogBox(true,false);
    });
    return false;
  });
}
function initDialogBox() {
  $(window).bind('resize', function () {
    if (!$('#dialog-box').is(':hidden')) {
      resizeDialogBox(true,false);
    }
  });
//  $(window).bind('scroll', function() {
//    resizeDialogBox(true,false);
//  });
  $('a.popup').click(function () {
    var thisurl = $(this).attr('href');
    popup(thisurl);
    return false;
  });
  $('a.ipopup').click(function () {
    var thisid = $(this).attr('rel');
    ipopup(thisid);
    return false;
  });
  $('a.button, #dialog-overlay').click(function () {
    $('#dialog-overlay, #dialog-box').fadeOut(40);
    return false;
  });
}

jQuery(document).ready(function(){
  initDialogBox();
});

