/**
 * @package JLive! Chat
 * @version 4.0.4
 * @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 ProactiveChatChecker = {
    websiteRoot: '',
    comUri: 'index.php?option=com_jlivechat&view=popup&tmpl=component',
    hostedModeUri: null,
    recheckAfterTime: 15000, // In Miliseconds, 15 seconds
    keepChecking: false,
    memberId: null,
    chatSessionId: null,
    serverResponse: null,
    
    initialize: function () {
	var currentPageUri = String(document.location.href);

	if(document.location.protocol == 'https:' || currentPageUri.indexOf("popup_mode") > -1 || currentPageUri.indexOf("tmpl=component") > -1) {
	    // Disabled
	    return false;
	}
	
	this.checkProactiveChats();
    },

    checkProactiveChats: function () {
	this.loadJSLibs();
    },

    loadJSLibs: function () {
	var requiredJSLibs = [];

	if(typeof(window.MooTools) == 'undefined') {
	    requiredJSLibs.push(this.websiteRoot+'/media/system/js/mootools.js')
	}

	if(typeof(window.jQuery) == 'undefined' && this.hostedModeUri) {
	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/flensed-1.0/flXHR.js');
	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/jquery-1.4.2.min.js');
	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/jquery.xhr.js');
	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/flensed-1.0/jquery.flXHRproxy.js');
	}

	if(requiredJSLibs.length > 0) {
	    LazyLoad.loadOnce(requiredJSLibs, this.loadJSComplete);
	} else {
	    this.loadJSComplete();
	}
    },

    loadJSComplete: function () {
	if(window.jQuery) {
	    jQuery.noConflict();
	}
	
	ProactiveChatChecker.performCheck();
    },

    performCheck: function () {
	if(this.hostedModeUri) {
	    jlcRemoteXHR.hostedModeUri = this.hostedModeUri;
	    jlcRemoteXHR.init();
	    
	    jlcRemotePost(this.comUri, { task: "recheck_proactive", do_not_log: "true", no_html: "1", t: $time() }, function(data) {
		ProactiveChatChecker.serverResponse = data;
		ProactiveChatChecker.processServerResponse();
	    });
	} else {
	    var options = {
		method: 'post',
		onSuccess: function(response) {
		    ProactiveChatChecker.serverResponse = Json.evaluate(response);
		    ProactiveChatChecker.processServerResponse();
		}
	    };

	    var handlerUri = this.comUri+'&task=recheck_proactive&do_not_log=true&no_html=1&t='+$time();

	    var uriVars = '';

	    new XHR(options).send(handlerUri, uriVars);
	}
    },

    processServerResponse: function() {
	if(ProactiveChatChecker.serverResponse.proactive) {
	    // There is a pending proactive chat for this visitor
	    ProactiveChatChecker.memberId = parseInt(ProactiveChatChecker.serverResponse.proactive.member_id);
	    ProactiveChatChecker.chatSessionId = parseInt(ProactiveChatChecker.serverResponse.proactive.chat_session_id);

	    ProactiveChatChecker.openLiveChatIframe();
	} else {
	    // No pending proactive chat requests for this visitor
	    if(ProactiveChatChecker.keepChecking) {
		setTimeout("ProactiveChatChecker.performCheck();", ProactiveChatChecker.recheckAfterTime);
	    }
	}
    },

    openLiveChatIframe: function() {
	var popupUri = this.comUri+'&popup_mode=iframe&do_not_log=true';

	requestLiveChat(popupUri, 'iframe');
    }
};

window.addEvent('domready', function() {
ProactiveChatChecker.initialize();
});
