offer.GroupsView = function(){
	this.GroupsView()
}
offer.GroupsView.prototype=	{
		GroupsView:function(){
		this.tree = new jslt.TreeView("offerGroupsDiv", this.evGroupClick)
		this.locationUtils=new jslt.element.Location()
		$.HistoryManager.register(this)
		var btn=document.getElementById("offerBtn")
		btn.onclick=function(){
			offerInstance.groupsView.evMainLabelClick()
		}
		btn.innerHtml=jslt.rb.DRB.getString('offer','ourOfferLabel')
		$(document).bind('evOfferGroupRequired',this.evOfferGroupRequired)
		$(document).bind('evFindProductPathResult',this.evFindProductPathResult)
		$(document).bind('siteMenuClick',this.evMenuClick)
	},
	evMenuClick:function(e,txt){
		if(txt!='offer'){
			offerInstance.groupsView.tree.unsign()
		}
	},
	/**
	 * Listen for requirement
	 */
	evOfferGroupRequired:function(e,params){
		offerInstance.groupsView.clickTreeElement(params)
	},
	evMainLabelClick:function(){
		offerInstance.informWorld()
		offerInstance.getMainGroups()
		$(document).trigger('EVENT_NAVIGATION_PATH_CHANGE',[[{label:jslt.rb.DRB.getString('offer','ourOfferLabel')}]])
			this.selectedId=0
			$.HistoryManager.save(this)
	},
	/**
	 * 
	 * @param {Object} params id_offer_group
	 */
	clickTreeElement:function(params){
		offerInstance.informWorld()
		var liElms=document.getElementsByTagName("li")
			for(var i=0;i<liElms.length;i++){
				if(liElms[i].firstChild.nodeName.toLowerCase()=="a"){
				var k2=this.locationUtils.keys2(liElms[i].firstChild.href)
				if(k2.id_offer_group==params.id_offer_group){
					this.tree.evNodeClick(liElms[i])
					break;
				}
				}
			}
	},
	myModuleStateChangeHandler:function(state){
		//console.log(state)
	},
	selectedId:null,
    evGroupClick :function(ref){
		offerInstance.informWorld()
        //console.log(ref)
        var pNode = ref
        var labels = []
        while (true) {
            var nn = pNode.nodeName.toLowerCase()
            if (nn == "ul" || nn == "li") {
                if (nn == "li") {
                    labels.push(pNode.firstChild.innerHTML)
                }
            }
            else {
                break;
            }
            pNode = pNode.parentNode
        }
		labels=labels.reverse();
		$(document).trigger('EVENT_NAVIGATION_PATH_CHANGE',[labels])
        var a = ref.firstChild
        var ah = a.href
        var ar = ah.split("/")
		var id=ar.pop()
        offerInstance.evGroupChange(id)
		offerInstance.groupsView.selectedId=id
		offerInstance.groupsView.pageNum=0
		$.HistoryManager.save(offerInstance.groupsView)	
    }
	,saveState:function(){
		var p={id_offer_group:this.selectedId}
		if(this.pageNum!=0){
			p.pageNum=this.pageNum
		}
		return {hmgrp:'GroupsView',params:p}
	}
	,loadState:function(par){
		if(par.hmgrp=='GroupsView'){
			if(par.id_offer_group!='0'){
				this.clickTreeElement(par)
			}
			else{
				this.evMainLabelClick()
			}
		}
	}
	,evProductPageChange:function(num){
		this.pageNum=num
		offerInstance.evProductPageChange(num)
		$.HistoryManager.save(this)
	}
	,evFindProductPathResult:function(e,arr){
		offerInstance.groupsView.clickTreeElement({id_offer_group:arr[0]})
	}
}


