/**
 * @version 0.1
 * @author Anna Fijol
 * @copyright Nionex GmbH
 */

/** changing Image dynamically */
Ext.override(Ext.Button, {
	setIconNow: function(url){
		if (this.rendered){
			var btnEl = this.getEl().child(this.buttonSelector);
			btnEl.setStyle('background-image', 'url(' +url+')');
		}
	}
});
Ext.namespace('FLA');
FLA.app = function(){
    // public space     **********************************************************

	
    return { 
        // public methods **********************************************************
        run: function(flashWidth, flashHeight, autoLoadUrl){
			//alert('flashpoup run ' +flashWidth + ', ' + flashHeight);			
			
			var btn_closePopup = {
			        xtype: 'button'
			        ,text: ''
			        ,cls: "x-btn-icononly"
			        ,id:'btn-close'
			        ,icon: FLA.iconUrlPrefix + '/lib/ext3/images/popup-button-close.gif'
			        ,overicon: FLA.iconUrlPrefix + '/lib/ext3/images/popup-button-close-over.gif'
			        ,iconCls:'icon-inside-btn'
			        ,disabled: false
			        ,listeners:{
						'click': function(){
							win.close();
						}
						,'mouseover' : function(){
							this.setIconNow(this.overicon);
						}
						,'mouseout' : function(){
							this.setIconNow(this.icon);
						}
			        }  
			    };

		    var win = new Ext.ux.FadeInWindow({
		    	id:'flashPopupWin'
		    	,modal:true
		        ,width:flashWidth + 22
		        ,height: flashHeight + 67
		        ,layout:'fit'
		        ,border:false
		        ,closable:false
		        ,draggable:false
		        ,resizable:false
				,iconCls:'icon-upload'
				,headerAsText:true
				,bodyStyle: 'padding: 4px 10px 0px 10px'
				,animateTarget: 'logo'
				,fadeInDuration: 1.5//0.3
				,items:[{
		    		id:'mypanel'
		    		,xtype:'panel'
		    		,width: flashWidth
		    		,height: flashHeight
		    		,border:false
		    		,autoLoad:{url:autoLoadUrl,scripts:true}
					,bodyStyle:'border: 0px solid red;'
					,bbar: new Ext.Toolbar({
		    	           items: ['->', btn_closePopup]	    	                     
		    	        })   
		    	}]       
		    });
		   win.show.defer(100, win);
		}// end FLA.app.run()

    	,worldmapPreselected: function(winWidth, winHeight, autoLoadUrl, initItemMc){
    		autoLoadUrl = autoLoadUrl + '&initItemMc=' + initItemMc;
    		//alert('Fla.app.worldmapPreselected: '+autoLoadUrl+'&initItemMc='+initItemMc);
    		FLA.app.worldmap(winWidth, winHeight, autoLoadUrl);
    	}
    	,worldmap:function(winWidth, winHeight, autoLoadUrl){
    		//alert('FLA.app.worldmap: ' +autoLoadUrl);

    		   		
    		var positionX = Ext.get('page_margins').getLeft(false);
    		if (Ext.isIE){positionX = positionX -3}
    		
    		// stage? sonst main
    		var yElem = Ext.get('stage')? Ext.get('stage'): Ext.get('main');
    		//alert(yElem.getTop(false));
    		var positionY = yElem.getTop(false);
    		if (Ext.isIE){positionY = positionY -3}else{positionY = positionY-1}
    		positionY = positionY - 8;

    		//69;
    		
    		if(FLA.app.worldmapWin == undefined){
    			FLA.app.worldmapPanel = new Ext.Panel({
	       			 border: false
	       			,width:1004
	       			,height:400
	       			,autoShow:true
	       			//,unstyled:true
	       			,autoLoad:{url:autoLoadUrl, scripts:true}
	       		});
    		
    		
    		
	    		FLA.app.worldmapWin = new Ext.ux.FadeInWindow({
			    	id:'worldmapPopupWin'
			    	,modal:true
			        ,width:winWidth
			        ,height: winHeight+16
			        ,layout:'fit'
			        ,border:false
			        ,closable:false
			        ,draggable:false
			        ,resizable: false
			        ,shadow: false
			        ,y: positionY
			        ,x: positionX
					,iconCls:'icon-upload'
					,headerAsText:true
					,bodyStyle: 'padding: 0px 0px 0px 0px'
					,fadeInDuration: 0.1
					,autoLoadUrl: autoLoadUrl
					//,plain:true	
					,closeAction:'hide'
					,items:[FLA.app.worldmapPanel]  
			    });
	    		
    		}else{
    			FLA.app.worldmapPanel.load(autoLoadUrl)
    		}
    		FLA.app.worldmapWin.show();
    	}


    	,worldmapClose: function(){
    		FLA.app.worldmapWin.hide(); 
    	}

    	,login: function(flashWidth, flashHeight, autoLoadUrl){
			FLA.app.loginWin = new Ext.ux.FadeInWindow({
		    	id:'loginWin'
		    	,modal:true
		        ,width:flashWidth + 22
		        ,height: flashHeight
		        ,layout:'fit'
		        ,border:false
		        ,closable:false
		        ,draggable:false
		        ,resizable:false
				,iconCls:'icon-upload'
				,headerAsText:true
				,bodyStyle: 'padding: 0px 10px 0px 10px'
				,fadeInDuration: 0.5
				,autoLoad:{url:autoLoadUrl, scripts:true, scope:this}        
		    });
			FLA.app.loginWin.show.defer(100, FLA.app.loginWin);
		}
    	,loginClose:function(){
    		FLA.app.loginWin.close();
    	}
		,loginSubmit: function(loginUrl, genError, formId, errorOutputId){
            if(formId == undefined || formId == null || formId.length <= 0){
                formId = 'loginform';
            }
            if(errorOutputId == undefined || errorOutputId == null || errorOutputId.length <= 0){
                errorOutputId = 'errorOutput';
            }
			Ext.Ajax.request({
				   url: loginUrl,
				   form: formId,
				   method: 'POST',
				   success: function(response, opts) {
				      var obj = Ext.decode(response.responseText);
				      //console.dir(obj);
				      if(obj.success == true){
                          var theWindowHref = window.location.href;
                          if(theWindowHref.substring((theWindowHref.length -1), theWindowHref.length) == "#"){
                              theWindowHref = theWindowHref.substring(0, (theWindowHref.length -1));
                          }
                          if(theWindowHref.indexOf('logout=true') != -1){
                            window.location.href = theWindowHref.replace(/logout=true/, "");
                          } else if(theWindowHref.indexOf('weblogin=weblogin') != -1) {
                              if(theWindowHref.indexOf('weblogin1') != -1){
                                window.location.href = theWindowHref.replace(/weblogin1/, "weblogin2");
                              } else {
                                window.location.href = theWindowHref.replace(/weblogin2/, "weblogin1");
                              }
                          } else {
                              window.location.href = Ext.urlAppend(theWindowHref, 'weblogin=weblogin1');
                          }
				      }else {
				    	  Ext.fly(errorOutputId).update(obj.error);				    	  
				      }
				   },
				   failure: function(response, opts) {
					  Ext.fly(errorOutputId).update(genError);
				   }
				});
			
		},
        logOut: function(logOutUrl) {
            Ext.Ajax.request({
               url: logOutUrl,
               success: function(response, opts) {
                  window.location.reload(true);
               },
               failure: function(response, opts) {
                  window.location.reload(true);
               }
            });
        }

    };// end return
}(); // end FLA.app
// end of file           ********************************************************** //
