/**
 * @package JLive! Chat
 * @version 4.1.1
 * @copyright (C) Copyright 2008-2010 CMS Fruit, CMSFruit.com. All rights reserved.
 * @license GNU/LGPL http://www.gnu.org/licenses/lgpl-3.0.txt

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 3 of the License, or (at your
 option) any later version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see http://www.gnu.org/licenses/.
 */

var liveChatWindow = null;
var screenWidth = 760, screenHeight = 420; // DO NOT CHANGE THESE DEFAULT VALUES

if(screen.availWidth && screen.availHeight) {
    screenWidth = screen.availWidth;
    screenHeight = screen.availHeight;
}

var livechatPopupWidth = 500;
var livechatPopupHeight = 400;

if(window.webkit) {
    // Safari popup window should be about 10px more
    livechatPopupWidth = 502;
    livechatPopupHeight = 402;
}

function requestLiveChat(popupUri, mode) {
    if (typeof(window.AutoPopupChecker) != 'undefined') {
	// Auto Popup included
	AutoPopupChecker.showAutoPopup=false;
	AutoPopupChecker.close();
    }
    
    if(mode == 'iframe') {
	JLiveChatIFramePopup.popupUri = popupUri;
	JLiveChatIFramePopup.openIFramePopup()
    } else {
	// Default, Open Popup Window
	// First close iframe popup
	JLiveChatIFramePopup.closeIFramePopup();
	
	var leftPos = (screenWidth-livechatPopupWidth)/2, topPos = (screenHeight-livechatPopupHeight)/2;

	liveChatWindow = window.open(popupUri,'LiveChatWindow','menubar=0,scrollbars=0,status=1,resizable=0,location=0,toolbar=0,height='+livechatPopupHeight+',width='+livechatPopupWidth+',left='+leftPos+',top='+topPos);

	if (window.focus && liveChatWindow) {
	    liveChatWindow.focus();
	}
    }
}

function URLEncode( str ) {
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}

RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

function hasClass(ele, cls) {
    return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele, cls) {
    if (!this.hasClass(ele, cls)) ele.className += " "+cls;
}

function removeClass(ele, cls) {
    if (hasClass(ele, cls)) {
	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
	ele.className=ele.className.replace(reg,' ');
    }
}

function removeAnchors(uri) {
    var reg = new RegExp('#[^#]+$');
    var pathOnlyRegex = new RegExp('^https?://[^/]+/');

    uri = uri.replace(reg, '');
    uri = uri.replace(pathOnlyRegex, '');

    return uri;
}


var JLiveChatUtil = {
  shouldDebug: false,

  // Note: Will fail in pathological cases (where the members contain
  // strings similar to describe() result).
  membersEqual: function(array1, array2) {
    return util.describe(array1)==util.describe(array2);
  },

  describe: function(obj) {
    if (obj==null) { return null; }
    switch(typeof(obj)) {
      case 'object': {
        var message = "";
        for (key in obj) {
          message += ", [" + key + "]: [" + obj[key] + "]";
        }
        if (message.length > 0) {
          message = message.substring(2); // chomp initial ', '
        }
        return message;
      }
      default: return "" + obj;
    }
  },

  debug: function(message) {
      if (this.shouldDebug) {
        alert("AjaxJS Message:\n\n" + message);
      }
  },

  error: function(message) {
      if (this.shouldDebug) {
        alert("AjaxJS ERROR:\n\n" + message);
      }
  },

  trim: function(str) {
    return str.replace(/(^\s+|\s+$)/g,'');
  },

  strip: function(str) {
    return str.replace(/\s+/, "");
  }
}


