/**
 * @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 JLiveChatIFramePopup = {
    hostedUri: null,
    popupUri: 'index.php?option=com_jlivechat&view=popup&tmpl=component&popup_mode=iframe',
    iframeOpen: false,
    websiteRoot: '',
    iframeObject: null,
    closedPermanently: false,
    frameid: 'livechat_iframe',

    openIFramePopup: function () {
	if(JLiveChatIFramePopup.iframeOpen) {
	    return false;
	}
	
	var popW = 504;
	var popH = 404;

	if(window.webkit) {
	    // Safari popup window should be about 10px more
	    popW = 514;
	    popH = 414;
	}

	if(!this.iframeObject) {
	    var body = document.getElementsByTagName('body').item(0);
	    
	    this.iframeObject = document.createElement('iframe');
	    this.iframeObject.src = this.popupUri;
	    this.iframeObject.id = this.frameid;
	    this.iframeObject.name = this.frameid;
	    this.iframeObject.width=popW;
	    this.iframeObject.height=popH;
	    this.iframeObject.frameborder=0;
	    this.iframeObject.scrolling='no';
	    this.iframeObject.allowautotransparency=true;
	    this.iframeObject.style.overflow='hidden';
	    this.iframeObject.style.display='block';
	    this.iframeObject.style.zIndex=10000;
	    this.iframeObject.style.bottom=0;
	    this.iframeObject.style.right=0;
	    this.iframeObject.style.border='none';

	    body.appendChild(this.iframeObject);
	} else {
	    removeClass(this.iframeObject, 'jlc-hide');
	}
	
	this.iframeOpen=true;
	this.closedPermanently=false;

	setTimeout('JLiveChatIFramePopup.monitorIFramePopup();', 300);
    },

    monitorIFramePopup: function () {
	if(!JLiveChatIFramePopup.iframeOpen || JLiveChatIFramePopup.closedPermanently) {
	    return false;
	}
	
	var recheck = true;
	
	if(window.location.hash == '#close_window') {
	    // The iframe window is attempting to close, close it
	    window.location.hash="#";
	    JLiveChatIFramePopup.closeIFramePopup();
	} else if(window.location.hash == '#minimize_window') {
	    // The iframe window is attempting to minimize, minimize it
	    window.location.hash="#";
	    addClass(JLiveChatIFramePopup.iframeObject, 'jlc-popup-minimized');
	} else if(window.location.hash == '#restore_window') {
	    // The iframe window is attempting to restore, restore it
	    window.location.hash="#";
	    removeClass(JLiveChatIFramePopup.iframeObject, 'jlc-popup-minimized');
	}
	
	if(recheck) {
	    setTimeout('JLiveChatIFramePopup.monitorIFramePopup();', 300);
	}
    },

    closeIFramePopup: function () {
	if(JLiveChatIFramePopup.iframeObject) {
	    if(JLiveChatIFramePopup.iframeOpen || !JLiveChatIFramePopup.closedPermanently) {
		JLiveChatIFramePopup.iframeOpen=false;
		JLiveChatIFramePopup.closedPermanently=true;

		addClass(JLiveChatIFramePopup.iframeObject, 'jlc-hide');
	    }
	}
    }
};

