/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var Linker = function(url_){

    this.url = Ext.urlDecode(url_);

    this.linkStore = null;

    this.newLinkStore = null;

    this.isNewLink = function(){
        if(this.url.id && this.url.module){
            return true;
        }
    }

    this.init = function(){
        this.linkStore = new Ext.data.Store({
						                    proxy: new Ext.data.HttpProxy({
						                                      url: project_url + 'GetLinkData',
						                                      method: 'GET',
						                                      waitMsg:'Loading ...'
						                                     }),
						                   reader : new Ext.data.JsonReader({
									                            id: 'link_reader',
									                            root: 'link',
									                            fields: [
									                            		 {name: 'id',           type: 'int'},
									                            		 {name: 'sub_url',      type: 'string'},
									                            		 {name: 'module',       type: 'int'},
                                                                         {name: 'win_title',    type: 'string'},
                                                                         {name: 'win_id',       type: 'string'},
                                                                         {name: 'added_by',     type: 'int'}
									                            		]
							                         		 })
						});

        this.newLinkStore = new Ext.data.Store({
						                    proxy: new Ext.data.HttpProxy({
						                                      url: project_url + 'AddLink',
						                                      method: 'GET',
						                                      waitMsg:'Loading ...'
						                                     }),
						                   reader : new Ext.data.JsonReader({
									                            id: 'new_link_reader',
									                            root: 'link',
									                            fields: [
									                            		 {name: 'link_val',      type: 'string'}
									                            		]
							                         		 })
						});
    }
    
    this.createLink = function(data_, win_){

        this.init();
        
        this.newLinkStore.load({
                params: {
                            mod:        data_['id'],
                            subUrl:     data_['url'],
                            winId:      data_['windowId'],
                            winTitle:   data_['winText']
                        }
        });

        this.newLinkStore.on('load', function(store_, rec_){
          
                var infoWin = new Ext.Window({
   			   				 	title:'Module Url - version 2.0',
   			   				 	modal: true,
   			   				 	width: 500,
                                renderTo: win_.getEl(),
   			   				 	height: 80,
   			   				 	autoScroll: true,
                                items: [
                                            new Ext.form.TextField({
                                                width: '100%',
                                                readOnly: true,
                                                id: 'linkField',
                                                value: context.linker.newLinkStore.getAt(0).get('link_val')
                                            })
                                       ]
   			   				 });

   			   	infoWin.show();

                //infoWin.toFront();

                Ext.getCmp('linkField').selectText();
        }, this.newLinkStore);

    }

    this.openModuleByUrl = function(){

        this.init();

        this.linkStore.load({
            params:{
                  id: this.url.id
                }
        });

        this.linkStore.on('load', function(store_, rec_){
            if(context.linker.linkStore.getAt(0).get('win_id') < 0){
                Ext.Msg.alert('Error', "Object to that url does not exist!");
                return;
            }else{
                var item = new Object();
                item.windowId 	= context.linker.linkStore.getAt(0).get('win_id');
                item.id 		= context.linker.linkStore.getAt(0).get('module');
                item.url 		= context.linker.linkStore.getAt(0).get('sub_url');
                item.winText 	= context.linker.linkStore.getAt(0).get('win_title');
                context.menu.programs.openProgramWindow(item);
            }
        }, this.linkStore);
    }

}
