/**
 * file Communicator.js with Communicator class
 * @author Janusz Rygał
 * @date 26.03.2009
 */

/**
 * @class Communicator
 * Communicator class
 */

 var Communicator = function(){
    
    this.animating = false;
    this.alertShow = false;

    this.checkNewMessage = function(){
       var doc = document.getElementById('communicatorWindow-iframe').contentDocument;

      if(doc.applets && doc.applets.communicatorApplet){
           if(doc.applets.communicatorApplet.isNewMessage()){
               if(!context.communicator.animating){
                   //context.communicator.animating =  true;
                   var id = 'communicatorWindow';
				   var win = Ext.getCmp(id);

                   if(win.isVisible()){
                       doc.applets.communicatorApplet.setNewMessageInfo(false);
                   }else{
                       if(!context.communicator.alertShow){
                            alert(context.translator.translate("new_comm_message"));
                            context.communicator.alertShow = true;
                        }
                       elem = Ext.get('starter_0');
                       elem.puff({
                                    easing: 'easeOut',
                                    duration: 2,
                                    remove: false,
                                    useDisplay: false
                          });
                       setTimeout("Ext.get('starter_0').setVisible(true)", 2200);
                   }
               }
           }else{
               context.communicator.animating =  false;
               elem = document.getElementById('starter_0');
               elem.style.border= '0px;';
           }
       }
       setTimeout("context.communicator.checkNewMessage()", 5000);
    }

	this.createCommunictorWindow = function(){ 
		
		var win = Ext.getCmp('communicatorWindow');
        if(win){
             win.show();
             
             var doc = document.getElementById('communicatorWindow-iframe').contentDocument;
             doc.applets.communicatorApplet.setNewMessageInfo(false);
             context.communicator.alertShow = false;
             return;
		}
        
		var pass	   = context.user.userLoader.getAt(0).get('cookie_pass');
		var login      = context.user.userLoader.getAt(0).get('jabber_login');
		
		var maxHeight = Ext.getBody().getHeight() - 44;
        
		var item = new Object();
			item.url 	 = "communicator/Communicator_0.1/build/comm.php?user_login="+login+"&user_pass="+pass;
			item.winText = "Pressiton Communicator";

        /*
          var html  = "<div style=\"width: 100%; height: 100%; \">"
            html += "<APPLET codebase=\"http://www.polconstruct.com/communicator/Communicator_0.1/build/classes\" code=\"communicator01/appletForm.class\" width=\"100%\" height=\"100%\">";
            html += "<param name=\"archive\" value=\"Communicator_0.1.jar, smack.jar, smackx.jar\" />";
            html += "<param name=\user_login\" value=\""+login+"\"/>";
            html += "<param name=\"user_pass\" value=\""+pass+"\"/>";
            html += "</APPLET>";
            html += "</div>";
         */


		var fullUrl = '';
			if(item.url.indexOf('http://') >= 0)
			      fullUrl = item.url;
			else
			      fullUrl = "http://www.polconstruct.com/"+item.url;
			        
			        
			        var pane = new Ext.Panel({
			        							autoWidth: true,	
			        							id: 'mainWindowPanel_-_communicator',	
			        							html: "<div style=' width: 100%; height: 100%;'><iframe id='communicatorWindow-iframe' style='width: 100%; height: 100%; overflow: auto;' src='"+fullUrl+"'></iframe></div>"
			        						});
			        
			        if(!win){
			            win = new Ext.Window({
			                id: 'communicatorWindow',
			                windowId: 'communicatorWindow',
			                height: maxHeight,
                            minimizable: false,
                            maximizable: true,
                            y: 5,
			                title:item.winText,
			                shadow: context.user.preferences.windowShadow,
			                animCollapse: context.user.preferences.animateCollapseWindow,
			                resizable: true,
			                width:     1000,
			                iconCls: 'bogus',
			                items: [pane],
			                shim:true,
			                constrainHeader:true
			            });
			            win.onceLoaded = false;
			        }
			        
			        win.on("resize", function(win_, w_, h_){
			        		var id = 'mainWindowPanel_-__communicator';
				        	var panel = Ext.getCmp(id);
                            if(panel)
				        	panel.setHeight(win_.getInnerHeight());
			        });

                    win.on("beforeclose", function(win_){
                        win_.hide();
                        return false;
                    })

			        
			        win.show();
                    context.communicator.checkNewMessage();
	}
}

