/**
 * @author jwhitcraft
 */

Ext.namespace("Ext.ux.RatingDetailManager");

Ext.ux.RatingDetailManager = function() {
    this.menus = new Array();

    this.active = false;

    this.activemenu = null;

    this.addEvents({
        "menuRegistered": true
    })
}

Ext.extend(Ext.ux.RatingDetailManager, Ext.util.Observable,{
    activate : function(active) {
        this.active = active;
    },
    register : function(obj) {
        //this.menus.add(controlkey,new Ext.ux.TalkbackMenu(controlkey));
		aId = obj.id.split('_');
        this.menus[aId[1]] = new Ext.ux.RatingDetail(obj);
        this.fireEvent("menuRegistered",aId[1]);
    },
    hide : function(e) {
        if(this.activemenu != null) {
            this.menus[this.activemenu].hide(e);
        }
    }
});


function RatingDetailHide(e) {
    if(e && e.button && e.button != 1 && e.type == "click") {
        return false;
    } else {
        ratingDetailMgr.hide(e);
    }
}

var ratingDetailMgr = new Ext.ux.RatingDetailManager();
Ext.EventManager.onWindowResize(RatingDetailHide),
Ext.get(document).on("click",RatingDetailHide);
ratingDetailMgr.activate(true);

Ext.namespace("Ext.ux.RatingDetails");

Ext.ux.RatingDetails = function(parent_el) {
	this.parent = Ext.get(parent_el);
	this.init();
}

Ext.ux.RatingDetails.prototype = {
	init : function() {
		var obj = this.parent.query('a.moreratingbtn');

		for(var o in obj) {
			if(typeof(obj[o]) != 'function') {
			    ratingDetailMgr.register(obj[o]);
			}
		}
	}
}


Ext.ux.RatingDetail = function(item){
	aId = item.id.split('_');

	this.controlobj = Ext.get(item);

	this.itemid = aId[1];
	this.typeid = aId[2];

	this.item = item;

	this.initialized = false;
	this.initializedAjax = false;

	this.initControl();

	Ext.fly(item).on('mouseover', this.onMouseover, this);
}

Ext.ux.RatingDetail.prototype = {

	onMouseover : function(){
		this.show(true);
		//Ext.fly(this.item).on('mousemove', this.onMousemove, this);
		Ext.get(document).on('mousemove', this.onMousemove, this);
	},
	onMousemove : function(e, obj){

        if(e && (e.within(this.layer) || e.getTarget().id == this.layer.id)) {
            return false;
        }

		if(e && e.within(this.item)) {
			return false;
		}

		Ext.get(document).un('mousemove', this.onMousemove);

        this.layer.hide();
        ratingDetailMgr.activemenu = null;
	},
	initControl : function() {
		if (this.initialized == false) {
			var config = {
				dh: {
					tag: "div",
					id: 'layer_' + this.itemid + '_' + this.typeid,
					cls: "x-layer x-hidden ratingdetail"
				},
				shadow: true,
				constrain: false
			};
			this.layer = new Ext.Layer(config);

			this.initialized = true;
		}
	},
	show : function(instant) {
		if(!ratingDetailMgr.active) {
            return false;
        } else if(!this.layer) {
            this.initControl();
        }

        if(!this.layer || ratingDetailMgr.activemenu == this.itemid) {
            return false;
        }

        if(ratingDetailMgr.activemenu != null && ratingDetailMgr.activemenu != this.itemid) {
            ratingDetailMgr.menus[ratingDetailMgr.activemenu].hide();
        }

		if(this.initializedAjax === true) {
			this.doShow(instant);
		} else {
			this.getAjax();
		}

	},
	doShow : function(instant)
	{
		if(typeof instant == "undefined") {
            instant = false;
        }
		this.layer.clip();

        this.setMenuPosition();

        //if(this.direction == "left") {
        //    this.layer.setBounds(this.leftpx, this.toppx, this.menuPos.width, this.menuPos.height, !instant);
        //} else {
            this.layer.setBounds(this.leftpx-this.menuPos.width+17, this.toppx+5, this.menuPos.width, this.menuPos.height, !instant);
        //}

        this.layer.unclip();

        ratingDetailMgr.activemenu = this.itemid;
	},

	getAjax : function() {
		Ext.Ajax.request({
	        url: '/rate/fetch/type/' + this.typeid + '/id/' + this.itemid,
	        callback: function(opt, is_success, o) {
                this.initializedAjax = true;
	            this.layer.update(o.responseText);
				this.doShow(true);
	        },
			scope: this
	    })
	},
	hide : function(e) {
        if(e && e.button && e.button != 1) {
            return false;
        }

        if(e && (e.within(this.layer) || e.getTarget().id == this.layer.id)) {
            return false;
        }
        this.layer.hide();

		Ext.get(document).un('mousemove', this.onMousemove);

        ratingDetailMgr.activemenu = null;
	},
    setMenuPosition : function() {
        this.pos = this.controlobj.getBox();
        this.leftpx = this.pos.x;
        this.toppx = this.pos.y + this.pos.height;

        this.menuPos = this.layer.getBox();

        if((this.leftpx + this.menuPos.width) >= document.body.clientWidth && (this.leftpx + this.pos.width - this.menuPos.width) > 0) {
            this.leftpx = this.leftpx + this.pos.width;
            this.direction = "right"
        } else {
            this.direction = "left";
        }

        this.layer.setSize(0,0);
		Ext.fly(this.layer.id).show();
        //this.layer.show();
        this.layer.setLocation(this.leftpx,this.toppx)
    }
}

