/**
* General Ax utility lib
* @author Maciek Ruckgaber <maciek@geek-bunker.com>
* @version $Id:$
* All rights reserved geek-bunker.com 
**/
var messageObj;
var tooltpObj = {};
var dialogObj;
var panelObj  = {};
var loadmsgObj;
var binfoboxObj;

GBLib_AxResponse = function(o,parsetext){
	this.response = o;
	if(parsetext != undefined){
		this.parsetext = parsetext;
	}else{
		this.parsetext = false;
	}
	this.init();
}

GBLib_AxResponse.prototype = {
	init: function(){
		if(this.parsetext == true){
			this.json = this.parseResponse();
			this.renderNotifications();
		}else{
			//IE7 kindly leaves the \n at the end of header line
			this.contentType = this.rtrim(this.response.getResponseHeader['Content-Type']);
			if(this.isJSON()){
				this.json = this.parseResponse();
				this.renderNotifications();
			}
		}
	},
	isJSON: function(){
		if(this.contentType == 'application/jsonrequest' || this.parsetext == true){
			return true;
		}
		return false;
	},
	isAppError: function(){
		if(this.json.status == 0){
			return true;
		}
		return false;
	},
	getAppErrorHeader: function(){
		return this.json.errorHeader;
	},
	getAppErrorBody: function(){
		return this.urlDecode(this.json.errorBody);
	},
	isSetParam: function(name){
		if(this.response.argument != undefined && this.response.argument[name] != undefined){
			return true;
		}
		return false;
	},
	getParam: function(name){
		if(this.isSetParam(name)){
			return this.response.argument[name];
		}
		return null;
	},
	getResponseObj: function(){
		return this.response;
	},
	getHeader: function(){
		return this.json.header;
	},
	getBody: function(){
		if(this.isJSON()){
			if(this.json.body != undefined){
				return this.urlDecode(this.json.body);
			}else{
				return null;
			}
		}else{
			return this.response.responseText;
		}
	},
	hasFlags: function(){
		if(this.json.flags != undefined){
			return true;
		}
		return false;
	},
	isSetFlag: function(key){
		if(this.json.flags != undefined && this.json.flags[key] != undefined){
			return true;
		}
		return false;
	},
	getFlag: function(key){
		if(this.json.flags != undefined && this.json.flags[key] != undefined){
			return this.json.flags[key];
		}
		return null;
	},
	getData: function(key){
		if(this.json.data != undefined && this.json.data[key] != undefined){
			return this.json.data[key];
		}
		return null;
	},
	parseResponse: function(){
	  try{
		  //alert(this.response.responseText);
		  return YAHOO.lang.JSON.parse(this.response.responseText);
	  }catch(e){ 
		  alert('Invalid product data'); 
	  }
	},
	urlDecode: function(str){
		str = str.replace(new RegExp('\\+','g'), ' ');
		return unescape(str);
	},
	rtrim: function rtrim(str, chars){  
		chars = chars || "\\s";  
		return str.replace(new RegExp("[" + chars + "]+$", "g"), '');  
	},
	renderNotifications: function(){
		if(this.json.location != undefined){
			window.location = this.json.location;
			return;
		}
		if(this.json.notification != undefined){
			var notification = document.getElementById('notification');
			if(notification != undefined){
				notification.innerHTML = this.urlDecode(this.json.notification);
				notification.style.visibility = 'visible';
				setTimeout('document.getElementById("notification").style.visibility = "hidden"', 5000);
			}
		}
	}
}

GBLib_TabManager = function(id){
  this.id = id;
  this.active;
  this.tabs = [];
  var markup = document.getElementById(id);
  if(markup != null)
    this.container = new YAHOO.widget.TabView(id);
  else
  {
    this.container = new YAHOO.widget.TabView({id : id});
  }
}

GBLib_TabManager.prototype = {
  nameTab : function(idx,name){
    this.tabs[name] = this.container.getTab(idx);
  },
  addTab : function(name,tab,position){
    this.container.addTab(tab,position);
    this.tabs[name] = tab;
  },
  popTab : function(name){
    this.container.removeTab(this.tabs[name]);
    var tab = this.tabs[name];
    this.tabs[name] = null;
    return tab;
  },
  getActiveTab : function (){
    var active = this.container.get('activeTab');
    for(var i in this.tabs){
      if(this.tabs[i] == active)
        return i;
    }
  },
  setActiveTab : function (name){
    this.container.set('activeTab',this.tabs[name]);
  },
  tabExists : function(name){
    if(this.tabs[name] != undefined && this.tabs[name] != null)
      return true;
    return false;
  },
  getTabByName : function (name){
    return this.tabs[name];
  },
  getContainer : function(){
    return this.container;
  }
}

var GBLib_Message = {
  render : function(header,message){
    if(messageObj == null){
      messageObj = new YAHOO.widget.SimpleDialog("gbmsg",{visible:false,draggable:false,width:"300px",modal:true,fixedcenter:true});
    }

    var buttoncfg = [{text:'Ok',isDefault:true,handler:this.handleOk}];
    messageObj.cfg.queueProperty("buttons",buttoncfg);
    messageObj.setHeader(header);
    messageObj.setBody(message);
    messageObj.render(document.body);
    messageObj.show();
  },
  handleOk : function(){
    messageObj.hide();
  }
}

var GBLib_Dialog = {
  render : function(header,message,buttons,handler,config){
    var cfg = {};
    for(var i in config){
      cfg[i] = config[i];
    }
    if(cfg.width == undefined)
      cfg.width = '350px';
    if(cfg.visible == undefined)
      cfg.visible = false;
    if(cfg.fixedcenter == undefined)
      cfg.fixedcenter = false;
    if(cfg.modal == undefined)
      cfg.modal = true;
    // Avoid problems with small screens
    cfg.xy  = [document.body.clientWidth/2 - 175,100];
    cfg.constraintoviewport = false;
    
    cfg.id = cfg.id == undefined ? 'dialogBox' : cfg.id;
    dialogObj = new YAHOO.widget.Dialog(cfg.id,cfg);

    var buttoncfg = [{text:buttons.accept,isDefault:true,handler:this.handleSubmit},{text:buttons.cancel,handler:this.handleCancel}];
    dialogObj.cfg.queueProperty("buttons",buttoncfg);
    if(handler['upload'] != undefined){
      dialogObj.callback.upload = handler['upload'];
      if(cfg.submitlistener != undefined){
        dialogObj.beforeSubmitEvent.subscribe(cfg.submitlistener);
      }
    }
    else{
      dialogObj.callback.success = handler['success'];
      dialogObj.callback.failure = handler['failure'];
    }
    
    dialogObj.setHeader(header);
    dialogObj.setBody(message);
    dialogObj.render(document.body);
    dialogObj.show();
  },
  handleSubmit : function(){
    this.submit();
  },
  handleCancel : function(){
    this.cancel();
  }
}

var GBLib_InfoBox = {
  render : function(header,message,config){
    var cfg = {};
    for(var i in config){
      cfg[i] = config[i];
    }
    if(cfg.width == undefined)
      cfg.width = '180px';
    if(cfg.visible == undefined)
      cfg.visible = false;
    if(config.context == undefined && config.xy == undefined)
      cfg.fixedcenter = true;
    
    cfg.id = cfg.id == undefined ? 'infoBox' : cfg.id;
    if(tooltpObj[cfg.id] == null)
      tooltpObj[cfg.id] = new YAHOO.widget.Overlay(cfg.id,cfg);

      //tooltpObj = new YAHOO.widget.Overlay("infoBox",{context :[context.id,'tl','bl'],visible:false,width:"180px",effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}});

    tooltpObj[cfg.id].setBody(message);
    tooltpObj[cfg.id].render(document.body);
    tooltpObj[cfg.id].show();
    if(cfg.fadeTime != undefined)
      window.setTimeout("GBLib_InfoBox.hide('"+cfg.id+"')",cfg.fadeTime);
  },
  hide : function(id){
    id = id == undefined ? 'infoBox' : id;
    if(tooltpObj[id] != null)
      tooltpObj[id].hide();
  },
  destroy : function(id){
    id = id == undefined ? 'infoBox' : id;
    tooltpObj[id].destroy();
    tooltpObj[id] = null;
  }
};

var GBLib_Panel = {
  render : function(header,message,config){
    var cfg = {};
    for(var i in config){
      cfg[i] = config[i];
    }
    if(cfg.width == undefined)
      cfg.width = '180px';
    if(cfg.visible == undefined)
      cfg.visible = false;
    
    cfg.id = cfg.id == undefined ? 'panelBox' : cfg.id;
    if(panelObj[cfg.id] == null)
      panelObj[cfg.id] = new YAHOO.widget.Panel(cfg.id,cfg);

    if(header != undefined)
      panelObj[cfg.id].setHeader(header);
    panelObj[cfg.id].setBody(message);
    panelObj[cfg.id].render(document.body);
    panelObj[cfg.id].show();

    if(cfg.fadeTime != undefined)
      window.setTimeout("GBLib_Panel.hide('"+cfg.id+"')",cfg.fadeTime);

    if(cfg.destroy != undefined && cfg.destroy == true){
      panelObj[cfg.id].hideEvent.subscribe(function(){GBLib_Panel.destroy(cfg.id)}); 
    }
  },
  hide : function(id){
    id = id == undefined ? 'panelBox' : id;
    if(panelObj[id] != undefined)
      panelObj[id].hide();
  },
  destroy : function(id){
    id = id == undefined ? 'panelBox' : id;
    panelObj[id].destroy();
    panelObj[id] = null;
  }
}

var GBLib_LoadingMsg  = {
  render : function(body,cfg){
    if(loadmsgObj == null){
      var width = (cfg == undefined ? '370px' : cfg);
     loadmsgObj = new YAHOO.widget.Panel('loadmsg',{width:width,fixedcenter:true,close:false,draggable:false,modal:true,visible:false,effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}});
    }
    loadmsgObj.setBody('<img src="'+ImgDir+'loading.gif" alt=""/> ' + body);
    loadmsgObj.render(document.body);
    loadmsgObj.show();
  },
  hide : function(evt){
    loadmsgObj.hide();
    evt;
  }
}

var GBLib_TabLoadingHandler = {
  render : function(body){
    if(body.newValue == true)
      GBLib_LoadingMsg.render(this.body);
  },
  hide : function(){
    GBLib_LoadingMsg.hide();
  }
}

GBLib_QueryString = function(qs){
  this.qs = qs;
  this.params = [];
  this.init();
}

GBLib_QueryString.prototype = {
  init : function(){
    var param = this.qs.split('&');
    for(var i=0;i<param.length;i++) {
      var parts = param[i].split('=');
      this.params[parts[0]] = parts[1];
    }
  },
  getParam : function(param){
    if(this.params[param] !== undefined)
      return this.params[param];
    return null;
  }
}

GBLib_getValue = function(id){
  var inp = document.getElementById(id);
  if(inp != null){
    return inp.options[inp.selectedIndex].value;
  }
}
