/**
 * @package JLive! Chat
 * @version 4.0.6
 * @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 AutoPopupChecker = {
    websiteRoot: '',
    comUri: 'index.php?option=com_jlivechat&view=popup&tmpl=component',
    hostedModeUri: null,
    serverData: null,
    autopopupId: 'jlivechat_autopopup',
    autopopupHeight: 95,
    autopopupEffect: null,
    hiddenMarginSize: null,
    autopopupVisible: false,
    showAutoPopup: true,
    
    initialize: function () {
	setTimeout('AutoPopupChecker.run();', 1500);
    },

    run: function () {
	var livechatIframeCheck = document.getElementById(JLiveChatIFramePopup.frameid);
	var currentPageUri = String(document.location.href);
	
	if(currentPageUri.indexOf('popup_mode') > -1 || livechatIframeCheck || currentPageUri.indexOf("tmpl=component") > -1) {
	    return false;
	}

	this.loadJSLibs();
    },

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

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

	var loadRemoteXHR = false;

	if(!jlcRemoteXHR.jsLoaded && this.hostedModeUri) {
	    jlcRemoteXHR.jsLoaded = true;
	    loadRemoteXHR = true;

	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/flensed-1.0/flXHR.js');
	}

	if(typeof(window.jQuery) == 'undefined' && this.hostedModeUri && loadRemoteXHR) {
	    requiredJSLibs.push(this.hostedModeUri+'/components/com_jlivechat/js/jquery-1.4.2.min.js');
	}
	
	if(loadRemoteXHR) {
	    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();
	}

	AutoPopupChecker.performCheck();
    },

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

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

	    var uriVars = '';

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

    processServerData: function () {
	if(parseInt(this.serverData.display_html_in_seconds) > 0) {
	    var currentPageUri = String(document.location.href);
	    var displayInSeconds = (parseInt(this.serverData.display_html_in_seconds)-1)*1000; // Convert to miliseconds

	    var showPopup = true;

	    if(this.serverData.display_html_on_uris.length > 0) {
		// On show autopopup on defined urls
		showPopup = false;
		var pageUri = null;

		this.serverData.display_html_on_uris.each(function (item, index) {
		    pageUri = String(item);

		    if(currentPageUri.indexOf(pageUri) > -1) {
			// Show on this page
			showPopup = true;
		    }
		});
	    }
	    
	    if(showPopup && parseInt(this.serverData.show_popup) == 1 && this.showAutoPopup) {
		setTimeout('AutoPopupChecker.showAutoPopup();', displayInSeconds);
	    }
	}
    },

    showAutoPopup: function () {
	var body = $(document.body);

	var autopopupWrapper = new Element('div', {'id': 'jlivechat_autopopup'});

	autopopupWrapper.setHTML(this.serverData.display_html);

	autopopupWrapper.injectInside(body);

	var livechatAutoPopup = $(this.autopopupId);
	
	if(livechatAutoPopup) {
	    this.show();
	}
    },

    animateInPopup: function () {
	this.hiddenMarginSize = this.autopopupHeight*-1;
	
	this.autopopupEffect = $(this.autopopupId).effects({duration: 650, transition: Fx.Transitions.Sine.easeOut});
 	this.autopopupEffect.start({'margin-bottom': [this.hiddenMarginSize, 0]});
	this.autopopupVisible=true;
    },

    animateOutPopup: function () {
	if(!this.autopopupVisible) {
	    return false;
	}

	this.autopopupEffect.start({'margin-bottom': [0, this.hiddenMarginSize]});
	this.autopopupVisible=false;
    },

    close: function (closeDelay) {
	if(!this.hiddenMarginSize) {
	    return false;
	}

	if(!closeDelay) {
	    closeDelay = 3000;
	}

	setTimeout('AutoPopupChecker.animateOutPopup();', closeDelay);
    },

    show: function () {
	this.animateInPopup();
    }
};

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