// Copyright 2005-2008 Socializr, Inc.
// Author: Boris Korsunsky

// Why are you reading this?
// If you are a great programmer, email your resume to jobs@socializr.com

function szrAjaxRequest(actionParam, paramsParam, methodParam, onSuccessFnParam, onErrorFnParam, resultsElemId) {
  this.request = null;
  this.action = actionParam;
  this.params = paramsParam;
  this.method = (methodParam == "POST" ? "POST" : "GET");
  this.onSuccessFn = onSuccessFnParam;
  this.onErrorFn = onErrorFnParam;
  this.fetchingData = false;
  this.resultsElemId = resultsElemId;
  
  return this;
}

szrAjaxRequest.prototype.send = function() {
  // clear pending request
  if (this.fetchingData) {
    this.request.onreadystatechange = function() {};
    this.request.abort();
  }
  
  // initiate new request
  this.request = createHttpRequest();
  
  // In a POST we send the params of the request as a parameter in the send method.
  if (this.method == "POST") {
    this.request.open(this.method, this.action, true);  
  }
  // In a GET the request params are appended to the URL.
  else {  
    this.request.open(this.method, this.action + "?" + this.params, true);
  }
  
  this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.request.setRequestHeader("Content-length", this.params.length);
  this.request.setRequestHeader("Connection", "close"); 
  
  var szrAjax = this;
  this.request.onreadystatechange = function() {
    if (szrAjax.request.readyState == 4) {
      if (szrAjax.request.status == 200) {    
        var html = szrAjax.request.responseText;                
        szrAjax.onSuccessFn(html, szrAjax.resultsElemId);      
      }
      else {
        if (szrAjax.onErrorFn) {
          szrAjax.onErrorFn(szrAjax.resultsElemId);
        }          
      }
      szrAjax.fetchingData = false;
      szrAjax.request.onreadystatechange = function() {};
      szrAjax.request = null;
    }
  };
  
  if (this.method == "POST") {
    this.request.send(this.params);
  }
  else {
    this.request.send(null);
  }
};

/**************************************** 
 *
 * Event photos ajax loading code
 *
****************************************/

function loadAjaxEventPhotos() {
  var divs = document.getElementsByTagName("div");
    
  for (var i = 0; i < divs.length; i++) {
    if (divs[i].className == "photoalbumcontent") {
      var params = '';
      var inputs = divs[i].getElementsByTagName("input");
      var first = true;
      for(var j = 0; j < inputs.length; j++) {
        var input = inputs[j];
        if (input.value != '') {
          if (!first) {
            params += "&";            
          }
          first = false;
          
          var paramValue = input.value;
          if (input.name == "albumUrl") {
            paramValue = escape(paramValue);
          }
          
          params += input.name + "=" + paramValue;
        }
      }
      
      var div = divs[i];        
            
      var eventPhotosRequest = new szrAjaxRequest('/ajaxphotos/', params, 'GET', ajaxEventPhotosCallback, ajaxEventPhotosError, div.id);
      eventPhotosRequest.send();
      
    }
  } 
}

function ajaxEventPhotosCallback(html, resultsElemId) {
  if (html != null && html != '' && resultsElemId != null) {
    var resultsElem = document.getElementById(resultsElemId);
    resultsElem.innerHTML = html;
  }
}

function ajaxEventPhotosError(resultsElemId) {
  if (resultsElemId != null) {
    var resultsElem = document.getElementById(resultsElemId);
    resultsElem.innerHTML = "Sorry, unable to load album at this time...";
  }
}

function securePhoto(frameName, photoUrl, link, imageSize) {
  var html;
  html =  '    <div style="width:100%;height:100%;overflow:hidden;position:relative;">';
  html += '      <div align=center';
  html += '        style="background:white;position:relative;left:-' + (.5*imageSize) + 'px; width:' + (2*imageSize) + 'px; overflow:hidden;"';
  html += '            ><img style="border:0px;" height="' + imageSize + '" src="'+ photoUrl +'"';
  html += '               onclick="self.parent.location.href = \'' + link + '\'; return false;"';
  html += '               onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" />';
  html += '      </div>';
  html += '    </div>'; 
    
  window.frames[frameName].document.body.innerHTML = html;
  window.frames[frameName].document.body.style.margin = "0px";
}

function secureGalleryPhoto(frameName, photoUrl, link, imageSize) {
  var html;
  html =  '<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr valign=middle><td align=center>';
  if (document.all) {
    html += '<div style="padding: 5px; border: 1px solid #ABC;width:1%;">';
    html += '<img style="height: expression((this.height > 167 && this.height >= this.width) ? 165: true); width: expression((this.width > 167 && this.width > this.height) ? 165: true);"';
    html += '  src="' + photoUrl + '" onclick="self.parent.location.href = \'' + link + '\'; return false;"';
    html += '  onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" />';
    html += '</div>';
  }
  else {  
    html += '<img style="padding: 5px; border: 1px solid #ABC; max-height: ' + imageSize + 'px; max-width: ' + imageSize + 'px; z-index: -200; "';
    html += '  src="' + photoUrl + '" onclick="self.parent.location.href = \'' + link + '\'; return false;"';
    html += '  onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" />';
  }
  html += '</td></tr></table>';
    
  window.frames[frameName].document.body.innerHTML = html;
  window.frames[frameName].document.body.style.margin = "0px";
}
