/********************************************************\
                         D I V S
\********************************************************/

function reloadImage(img) {
  var src = img.src;
  if (src.indexOf('?') == -1) {
    src += '?r=' + new Date().getTime();
  } else if (src.indexOf('?r=') == -1) {
    src = src.replace(/&r=[^&]*/, '') + '&r=' + new Date().getTime();
  } else {
    src = src.replace(/\?r=[^&]*/, '?r=' + new Date().getTime());
  }
  var newimg = new Image();
  newimg.onload = newimg.onerror = function() {img.src = src;}
  newimg.src = src;
}

function w(id, txt) {
  if (typeof(txt) == 'undefined') {
    return 1;
  }
  if (!document.getElementById) {
    return 0;
  }
  var div = document.getElementById(id);
  try {
    if (div.innerHTML != txt) {
      div.innerHTML = txt;
    }
  } catch(e) {
    return 0;
  }
  return 1;
}

function h(id) {
  if (!document.getElementById) {
    return 0;
  }
  div = document.getElementById(id);
  try {
    if (div.style.display != 'none') {
      div.style.display = 'none';
    }
  } catch(e) {
    return 0;
  }
  return 'h';
}

function hh(id) {
  for (var i=1; h(id + i); i++) {}
}

function s(id, display) {
  if (!document.getElementById) {
    return 0;
  }
  div = document.getElementById(id);
  if (typeof(display) == 'undefined') {
    display = '';
  }
  try {
    if (div.style.display != display) {
      div.style.display = display;
    }
  } catch(e) {
    return 0;
  }
  return 's';
}

function ss(id) {
  for (var i=1; s(id + i); i++) {}
}

function t(id, display) {
  if (!document.getElementById) {
    return 0;
  }
  div = document.getElementById(id);
  if (div.style.display == 'none') {
    return s(id, display);
  }
  return h(id);
}

function m(id, pid, left, top) {
  var o = document.getElementById(id);
  var div = document.getElementById(pid);
  var pos = getElementPosition(div);
  var l = pos.x + div.offsetWidth + left;
  var t = pos.y + top;
  var pageSize = getPageSize();
  var pageScroll = getPageScroll();
  if (l < pageScroll[0]) {
    l = pageScroll[0];
  }
  if (t < pageScroll[1]) {
    t = pageScroll[1];
  }
  if (l + o.offsetWidth - pageScroll[0] > pageSize[2]) {
    l = pageSize[2] - o.offsetWidth + pageScroll[0];
  }
  if (t + o.offsetHeight - pageScroll[1] > pageSize[3]) {
    t = pageSize[3] - o.offsetHeight + pageScroll[1];
  }
  pos = getElementPosition(o);
  o.style.left = (l - (pos.x - o.offsetLeft)) + 'px';
  o.style.top = (t - (pos.y - o.offsetTop)) + 'px';
}

function getElementPosition(elem) {
  var left = 0;
  var top = 0;
  var current_elem = elem;
  while (current_elem) {
    left += current_elem.offsetLeft;
    top += current_elem.offsetTop;
    current_elem = current_elem.offsetParent;
  }
  return {'x' : left, 'y' : top};
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// Code taken from http://www.dynamicdrive.com/dynamicindex4/lightbox2/js/lightbox.js
//
function getPageScroll(){

  if (typeof(window.pageXOffset) != 'undefined') {
    return [window.pageXOffset, window.pageYOffset];
  }
  if (document.body && typeof(document.body.scrollLeft) != 'undefined') {
    return [document.body.scrollLeft, document.body.scrollTop];
  }
  if (document.documentElement && typeof(document.documentElement.scrollLeft) != 'undefined') {
    return [document.documentElement.scrollLeft, document.documentElement.scrollTop];
  }
  return [0, 0];
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Code taken from http://www.dynamicdrive.com/dynamicindex4/lightbox2/js/lightbox.js
// Edit for Firefox by pHaez
//
function getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {        
    xScroll = document.body.scrollWidth;
    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
    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 = windowWidth;
  } else {
    pageWidth = xScroll;
  }


  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}

/***************************************************\
                 R E S O U R C E S
\***************************************************/

var cachedResources = new Object();

function fetchResource(id, url) {
  s('wpdc_global_loader');
  AjaxRequest.get({
    'async' : false,
    'url' : url,
    'onSuccess' : function (req) {cachedResources[id] = req.responseText; try{pageTracker._trackPageview(url);} catch(e){}},
    'onError' : function() {}
  });
  h('wpdc_global_loader');
  if (typeof(cachedResources[id]) != 'undefined') {
    return true;
  }
  return false;
}

function fetchCachedResource(id, url) {
  if (typeof(cachedResources[id]) != 'undefined') {
    return true;
  }
  return fetchResource(id, url);
}

/********************************************************\
                       P O P U P S
\********************************************************/

var popupSuffix = 0;

function showPopupLoader() {
  s('wpdc_popup_loader' + popupSuffix);
}

function hidePopupLoader() {
  h('wpdc_popup_loader' + popupSuffix);
}

function setPopupImage(url) {
  document.images['wpdc_popup_image' + popupSuffix].src = url;
}

function reloadPopupImage() {
  reloadImage(document.images['wpdc_popup_image' + popupSuffix]);
}

function showPopup() {
  if (popupSuffix > 1) {
    h('wpdc_popup_window' + (popupSuffix-1));
  } else {
    try {
      document.getElementById('wpdc_gallery_container').style.visibility = 'hidden';
      document.getElementById('wpdc_profile_info').style.visibility = 'hidden';
    } catch(e) {}
  }
  var pageSize = getPageSize();
  var pageScroll = getPageScroll();
  try {
    document.getElementById('wpdc_popup_background1').style.height = pageSize[1];
    document.getElementById('wpdc_popup_background2').style.height = pageSize[1];
    document.getElementById('wpdc_popup_window' + popupSuffix).style.top = pageScroll[1] + 'px';
  } catch(e) {}
  ss('wpdc_popup_background');
  s('wpdc_popup_window' + popupSuffix);
  return false;
}

function hidePopup() {
  if (popupSuffix == 0) {
    return true;
  }
  h('wpdc_popup_window' + popupSuffix);
  if (popupSuffix > 1) {
    s('wpdc_popup_window' + (popupSuffix-1));
  } else {
    hh('wpdc_popup_background');
    try {
      document.getElementById('wpdc_gallery_container').style.visibility = 'visible';
      document.getElementById('wpdc_profile_info').style.visibility = 'visible';
    } catch(e) {}
  }
  popupSuffix--;
  return false;
}

function prefillPopup(content) {
  return w('wpdc_popup_content' + popupSuffix, content) ? false : true;
}

function showModal(type, msg, title, handlerOnClose) {
  if (typeof(handlerOnClose) != 'function') {
    handlerOnClose = hidePopup;
  }
  popupSuffix++;
  if (fetchCachedResource(type + 'Popup' + popupSuffix, '/ajax/resources/modal.xml?type=' + type + '&suffix=' + popupSuffix)) {
    if (prefillPopup(cachedResources[type + 'Popup' + popupSuffix])) {
      return true;
    }
    w('wpdc_popup_title' + popupSuffix, title);
    w('wpdc_popup_message' + popupSuffix, msg);
    document.getElementById('popup_handler' + popupSuffix).onclick = handlerOnClose;
  } else {
    if (prefillPopup(
      '<div class="widget_pop_mess">Widgee!</div>' + 
      '<div class="widget_pop_mess_det">' + msg + '</div>' +
      '<br><br><input id="fb_popup_handler' + popupSuffix + '" type="button" value="OK" onClick="hidePopup();">'
    )) {
      return true;
    }
    try {
      document.getElementById('fb_popup_handler' + popupSuffix).onclick = handlerOnClose;
    } catch (e) {}
  }
  showPopup();
  return false;
}

function showSuccess(msg, title, handlerOnClose) {
  return showModal('success', msg, title, handlerOnClose);
}

function showInfo(msg, title, handlerOnClose) {
  return showModal('info', msg, title, handlerOnClose);
}

function showError(msg, title, handlerOnClose) {
  return showModal('error', msg, title, handlerOnClose);
}

function showYesNo(msg, title, handlerOnYes, handlerOnNo) {
  if (showModal('yesno', msg, title, handlerOnNo)) {
    return true;
  }
  if (typeof(handlerOnYes) != 'function') {
    handlerOnYes = hidePopup;
  }
  document.getElementById('yes_handler' + popupSuffix).onclick = handlerOnYes;
}

function prefillAndShowPopup(msg) {
  popupSuffix++;
  if (prefillPopup(msg)) {
    return true;
  }
  showPopup();
  return false;
}

function loadAndShowPopup(id, url, cached) {
  if (typeof(cached) != 'undefined' && cached == 1 ? fetchCachedResource(id, url) : fetchResource(id, url)) {
    return prefillAndShowPopup(cachedResources[id]);
  }
  return true;
}

function showFatalError(msg, title, handlerOnCLose) {
  if (typeof(msg) == 'undefined') {
    msg = 'A fatal error has occurred while trying to process your request. Please try again.';
  }
  if (typeof(handlerOnClose) != 'function') {
    handlerOnClose = function() {location.reload(); return false;};
  }
  showError(msg, title, handlerOnClose);
}

function handlePopupKeyPress(event) {
  if (popupSuffix == 0) {
    return true;
  }
  try {
    if (typeof(event) == 'undefined') {
      event = window.event;
    }
    if (event && (event.keyCode === 27)) {
      hidePopup();
      return false;
    }
  } catch(e) {}
}

document.onkeypress = handlePopupKeyPress;
