/* jQuery compatibility fixes, by Mazdak Rezvani */
if(typeof jQuery !== 'undefined') {
	(function($){
	  $.emptyFunction = function(){};
	 } 
	)(jQuery);
}

if(typeof Prototype === 'undefined') {
  var Class = {
  	create: function() {
  		return function() {
  			this.initialize.apply(this, arguments);
  		}
  	}
  }

  Object.extend = function(destination, source) {
  	for (property in source) destination[property] = source[property];
  	return destination;
  }

  Function.prototype.bind = function(object) {
  	var __method = this;
  	return function() {
  		return __method.apply(object, arguments);
  	}
  }

  Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  	return function(event) {
  		__method.call(object, event || window.event);
  	}
  }

	var $ = function(element){
	  if (arguments.length > 1) {
	    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
	      elements.push($(arguments[i]));
	    return elements;
	  }
	  if (typeof element == 'string')
	    element = document.getElementById(element);
	  return element;
	}
}

if(typeof Ajax === 'undefined') { Ajax = {}; }
if(typeof Ajax.Request === 'undefined') {
	Ajax.Request = function(url, params) {
	  var method = params['method'].toUpperCase() || 'POST';

	  jQuery.ajax({
	    url: url,
	    type: method,
	    dataType: params['evalResponse'] ? 'script' : 'html',
	    data: params['postBody'] || params['parameters'] || params,
	    beforeSend: function(xhr){ 
	      (params['onLoading'] || jQuery.emptyFunction)(xhr)
      },
	    success: function(data, textStatus) {
  	    (params['onSuccess'] || jQuery.emptyFunction)(this);
  	    if(params['evalScripts'] || params['onSuccessAfterEvalResponse'])
  	      eval(data);
  	    if(typeof params['onSuccessAfterEvalResponse'] == 'function')
  	      params['onSuccessAfterEvalResponse'](data);
  	  },
	    complete: function(request, textStatus) { 
	      (params['onComplete'] || jQuery.emptyFunction)(request, textStatus); 
	    },
	    error: function(request, textStatus, errorThrown) { 
	      (params['onFailure'] || jQuery.emptyFunction)(request, textStatus, errorThrown);
	    } 
	  });
	}
}

function swapColor(id, bgcolor, color) {
  if (bgcolor != '') { document.getElementById(id).style.backgroundColor = bgcolor; }
  if (color != '') { document.getElementById(id).style.color = color; }
}

function swapDecoration(id, decoration) {
  document.getElementById(id).style.textDecoration = decoration;
}

function gotoUrl(url) {
  window.location.href = url;
}

function setCheckedTrue(id, bool) {
  if (bool != 1) {
    document.getElementById(id).checked = true;
  }
}

function setCheckedFalse(id, bool) {
  if (bool != 1) {
    document.getElementById(id).checked = false;
  }
}

function hideMenu(menuId) {
	document.getElementById(menuId).style.top = '-10000px';
}

function clearCallIn(menuId) {
	var codeString = 'window.clearTimeout(timeoutId' + menuId + ')';
	eval(codeString);  
}
function showMenu(menuId,topValue) {
	document.getElementById(menuId).style.top = topValue;
	timeOutName = eval('window.timeoutId'+menuId);
	if (timeOutName) {
		clearCallIn(menuId);
	}
}
function callIn(menuId) {
	var codeString = 'window.timeoutId' +menuId + ' = setTimeout("hideMenu(\''+menuId+'\')",800)';
	eval(codeString);
}

function toggleDiv(showDiv,hideDiv) {
		document.getElementById(showDiv).style.display = "block";
		if (hideDiv && hideDiv != '') {
		document.getElementById(hideDiv).style.display = "none";
		}
}
function assignClass(el,cl) {
		document.getElementById(el).className = cl;
}
var chat = '';

function popup(url, width, height) {
  window.open(url,"_blank","width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,resizable=1,status=0,menubar=0,scrollbars=0");
}

function popup2(url, width, height, scrollbars) {
  window.open(url,"_blank","width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,resizable=1,status=0,menubar=0,scrollbars=" + scrollbars);
}

function popupChat(url, width, height) {
  if (!chat.closed && chat.location) {
    chat.focus();
  } else {
    chat = window.open(url,"_honchat","width=" + width + ",height=" + height + ",toolbar=0,location=1,directories=0,resizable=1,status=0,menubar=0,scrollbars=0");
    chat.focus();
  }
}

function popupLarge(url, target) {
  window.open(url,target);
}

function favoriteByButton(userEnc,favSec, minRateVal,WEBROOT, userMeetEnc, prevRatee) {

	var url = WEBROOT + '/ajax/favorites/';

	new Ajax.Request (
		url, 
		{
			method: 'post', 
			postBody : 'favAns=Y&fevId=' + userEnc + '&section='+ favSec +'&minR=' + minRateVal + '&curRt=10&fevMId=' + userMeetEnc,
			onComplete: function(res) {
				runPostCompleteAjax(res.responseText,1,WEBROOT, prevRatee); 
			}
		}
	);

}

function favoriteRemoveByButton(userEnc,WEBROOT,rmId) {

	var url = WEBROOT + '/ajax/favorites/';

	new Ajax.Request(url, { 
			method: 'post', 
			postBody : 'fr=' + rmId + '&fevId=' + userEnc,
			onComplete: function(res) {
				runPostCompleteAjax(res.responseText,2,WEBROOT);
			}
		}
	);

}
function runPostCompleteAjax(res,sectionVal,WEBROOT, prevRatee){

	var msgDetail="";

	if (sectionVal == 1) {
		msgDetail=" Saved to your favorites! ";
		if (!prevRatee && (prevRatee != '')) {
		document.getElementById("addFavText").className = "iconLink hide";
		document.getElementById("removeFavText").className = "iconLink";
		}
		else {
			document.getElementById("prevAddFavText").className = "iconLink hide";
		}
	} else if (sectionVal == 2) {
		msgDetail=" Your favorite has been deleted! ";
		document.getElementById("addFavText").className = "iconLink";
		document.getElementById("removeFavText").className = "iconLink hide";
	}
	if (!prevRatee && (prevRatee != '')) {
		document.getElementById("favoriteMsg").style.display="inline";
		document.getElementById("msgContainer").style.display="inline";
		document.getElementById("msgContainer").style.borderColor="red";
		document.getElementById("msgContainer").innerHTML=msgDetail;
	}
	else {
		document.getElementById("prevFavoriteMsg").style.display="inline";
		document.getElementById("prevFavoriteMsg").innerHTML=msgDetail;
	}
	// loadFavoriteListAjax(WEBROOT);
}

function loadFavoriteListAjax(WEBROOT) {

	var url = WEBROOT + '/ajax/favorites/';

	new Ajax.Request(url, { 
		method: 'post',
		postBody : 'refFav=1',
		onComplete: function(res) {
			loadFavoriteRes(res.responseText); 
		}
	});
}

function loadFavoriteRes(res){
		document.getElementById("favoriteListRecent").innerHTML=res;
}

function getPics(userid, match, linkid, webroot) {

	var url = webroot;

	$(linkid).innerHTML = 'Loading..';

	new Ajax.Request(url + '/jsond/', {

		method: 'get',
		parameters: { action: 'getPics', user: userid, match: match },
		evalJSON: true,

		onSuccess: function (transport) {
			var json = transport.responseJSON;
			$(linkid).innerHTML = 'More Photos';
			$('multiPics').innerHTML = json.message;
			$('multiPics').style.display = 'block';
		},

		onFailure: function() {
			alert('failure connecting');
		}
	});

}

var dropdownDisplayChange = null;

function setDisplay(e, event, targetElement, option, timeout) {
  if (dropdownDisplayChange) {
    clearTimeout(dropdownDisplayChange);
  }

  if(navigator.appName=="Microsoft Internet Explorer") {
      if(timeout != null) {
        dropdownDisplayChange = setTimeout(function() {
          if(targetElement == 'dd') {document.getElementById("ie_iframe_shim").style.display = option;}
          document.getElementById(targetElement).style.display = option
        }, timeout);
      } else {
        if(targetElement == 'dd') {document.getElementById("ie_iframe_shim").style.display = option;}
        document.getElementById(targetElement).style.display = option;
      }
  } else {
      if(timeout != null) {
        dropdownDisplayChange = setTimeout("document.getElementById(\"" + targetElement + "\").style.display = \"" + option + "\"", timeout);
      } else {
        document.getElementById(targetElement).style.display = option;
      }
  }
}

function removeMouseEvents() {
  if(navigator.appName=="Microsoft Internet Explorer") {
    document.getElementById('signInDropDown').onmouseout=function(){};
    document.getElementById('disappear').onmouseover=function(){};
    document.getElementById('formContainer').onmouseover=function(){};
  } else {
    document.getElementById('signInDropDown').setAttribute('onMouseOut', '');
    document.getElementById('disappear').setAttribute('onMouseOver', '');
    document.getElementById('formContainer').setAttribute('onMouseOver', '');
  }
}
function replaceMouseEvents() {
  if(navigator.appName=="Microsoft Internet Explorer") {
    document.getElementById('signInDropDown').onmouseout=function(){setDisplay(this,event,'signInDropDown','none',300)};
    document.getElementById('disappear').onmouseover=function(){setDisplay(this,event,'signInDropDown','none',0)};
    document.getElementById('formContainer').onmouseover=function(){setDisplay(this,event,'signInDropDown','block')};
  } else {
    document.getElementById('signInDropDown').setAttribute('onMouseOut', 'setDisplay(this,event,\'signInDropDown\',\'none\',300)');
    document.getElementById('disappear').setAttribute('onMouseOver', 'setDisplay(this,event,\'signInDropDown\',\'none\',0)');
    document.getElementById('formContainer').setAttribute('onMouseOver', 'setDisplay(this,event,\'signInDropDown\',\'block\')');
  }
}

function checkEnter(e) { // e is event object passed from function call
  var charCode;
  
  if(e && e.which) {
    charCode = e.which;
  } else {
    charCode = e.keyCode;
  }
  
  if(charCode==13) { // if character code is equal to ASCII 13 (the enter key)...
    document.logMeIn.submit();
    return false;
  } else {
    return true;
  }
}

function setUsernameField() {
  document.getElementById("usernameFieldClicked").value = "1";
  return false;
}