
/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/modules/glossary/DragAndDrop.js
*/
var DragDrop = function(){}

DragDrop.prototype.init = function(){

	Events.draggable({
		selector:".draggable", 
		parent: ".adminCatHolder",
		context: this, 
		over: this.mouseOver, 
		down: this.mouseDown, 
		up: this.drop, 
		move : this.drag,
		dynamicEvent:{
			type:"mouseout"
			,handler: this.mouseOut
			,context: window
		}
	})

}

DragDrop.prototype.mouseOver = function(e, el){
	e.cancel();
	if(Element.hasClass(el, "draggable")){
		Element.addClass(el, 'hover');
	}
}

DragDrop.prototype.mouseOut = function(e, el){
	e.cancel();
	Element.removeClass(el, 'hover');
}

DragDrop.prototype.mouseDown = function(e, el){
	e.cancel();
	this.target = el;
	if(Element.hasClass(this.target, "draggable")){
		this.width = Element.getSize(this.target).width;				
		this.height = Element.getSize(this.target).height;	
		this.startX = Element.getXY(this.target).x;
		this.startY = Element.getXY(this.target).y;
	
		var ev = e.nativeEvent,
		   y = ev.clientY,
		   x = ev.clientX;
				
		Element.removeClass(this.target, "draggable");				
		Element.addClass(this.target, "selected");
		Element.setWidth(this.target, this.width);
	
		this.container = Element.getParentBySelector(this.target, "div.adminCatHolder");
		this.scrollY = Element.getViewport().top;
		this.offsetX = 20;
		this.offsetY = 108.5 - this.scrollY;
		var containerSize = Element.getSize(this.container)
		var containerXY = Element.getXY(this.container)
		this.containerPos = {
			left: containerXY.x
			,right: containerXY.x + containerSize.width
			,top: containerXY.y
			,bottom: containerXY.y + containerSize.height
		}

		this.x = this.startX-this.offsetX
		this.y = (y-(this.height/2)) - this.offsetY
		
		Element.setXY(this.target, this.x, this.y);
	}
}

DragDrop.prototype.drag = function(e, el){
	e.cancel();
	this.children = Element.parseSelector("div.draggable", this.container)
	this.aPositions = this.getPositions();
	
	var ev = e.nativeEvent,
		   y = ev.clientY,
		   x = ev.clientX;
		
	this.x = this.startX-this.offsetX
	this.y = (y-(this.height/2))-this.offsetY
	
	Element.setXY(this.target, this.x, this.y);

	(y < this.containerPos.top || y > this.containerPos.bottom) ? Element.addClass(this.target, "noDrop") : Element.removeClass(this.target, "noDrop");
}

DragDrop.prototype.drop = function(e, el){
	e.cancel();
	var currEl = this.target;
	if(Element.hasClass(currEl, "selected")){
	
		var ev = e.nativeEvent,
			   x = ev.clientX,
			   y = ev.clientY+ this.scrollY

		for (var i=0; i<this.aPositions.length; i++){
		
			if(0 == i && y < this.aPositions[i] && y > this.containerPos.top){
				Element.insertBefore(currEl, this.children[i]);
				
			}else if (y > this.aPositions[i] && y < this.aPositions[i+1]){
				Element.insertAfter(currEl, this.children[i]);
				
			}else if (y > this.aPositions[i] && y < this.containerPos.bottom){
				Element.insertAfter(currEl, this.children[i]);
				
			}else if( y > this.containerPos.bottom){
				Element.insertAfter(currEl, this.children.length-1)
				
			}else if(y < this.containerPos.top){
				Element.insertBefore(currEl, this.children[0])
				
			}
		}
		
		Element.removeClass(currEl, "selected");
		Element.removeClass(currEl, "hover");
		Element.removeClass(currEl, "noDrop");
		Element.addClass(currEl, "draggable");
	}

}

DragDrop.prototype.getPositions = function(){
	var aPositions = [];
	
	for (i=0; i<this.children.length; i++){
		var location = Element.getXY(this.children[i]).y + (Element.getSize(this.children[i]).height/2)
		aPositions.push(location);
	}
	
	return aPositions;
}

DragDrop.prototype.getOrder = function(){
	var aList = Element.parseSelector("div.draggable", this.container)
	var aOrder = [];
	
	for (var i=0; i<aList.length; i++){
		aOrder.push(aList[i].getAttribute("order"))
	}
	
	return aOrder;
}

var dragDrop = new DragDrop();
/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/modules/markets/sectorsAndIndustries/industryDropDown.js
*/
var IndustryDropDown = function() {
    this.hasTitle(false);
    this.hasCloseLink(false);

    var preload = new Image();
        preload.src = this.SRC_ACTIVE_ICON;
}

IndustryDropDown.Extend(Popup);

IndustryDropDown.prototype.ICON_SIZE = 24;
IndustryDropDown.prototype.ICON_HORIZONTAL_OFFSET = 10;
IndustryDropDown.prototype.POPUP_SIZE = 325;

IndustryDropDown.prototype.CSS_DROPDOWN = "industryListDropDown";
IndustryDropDown.prototype.CSS_DROPDOWN_TEXT = "industryDropDownText";
IndustryDropDown.prototype.BUFFER_URL = '/ft/resources/buffer/sectorsAndIndustries/getListOfIndustries.asp'
IndustryDropDown.prototype.URL_LINKS = "/ft/markets/sectorsAndIndustries/industries.asp";
IndustryDropDown.prototype.SRC_ACTIVE_ICON = "/ft/resources/image/dropdown-active.gif";

IndustryDropDown.prototype.init = function() {
    var dropDownLink = WSDOM.Element.get('industryDropDown');
    WSDOM.Events.add(dropDownLink, 'click', this.draw, this)
}

IndustryDropDown.prototype.getAnchorElement = function() { return null; }
IndustryDropDown.prototype.setAnchorElement = function(el) {
    el = Element.get(el);

    this.getAnchorElement = function() {
        return el;
    }
}

IndustryDropDown.prototype.position = function() {
    var contentWell = Element.getXY(Element.get("wsod"));

    var view = Element.getViewport();
    var frame = this.getFrame();

    var frameSize = Element.getSize(frame);

    var frameTop, frameLeft;
    if (this.getAnchorElement()) {
        var anchorXY = Element.getXY(this.getAnchorElement());
        var anchorSize = Element.getSize(this.getAnchorElement());

        frameTop = anchorXY.y + this.ICON_SIZE;

        if (anchorXY.x - this.POPUP_SIZE < contentWell.x) {
            frameLeft = anchorXY.x - this.ICON_HORIZONTAL_OFFSET;
        } else {
            frameLeft = anchorXY.x + anchorSize.width - frameSize.width + this.ICON_HORIZONTAL_OFFSET;
        }
    } else {
        frameTop = Math.max(view.top + (view.height / 2) - (frameSize.height / 2), 0) - contentWell.y;
        frameLeft = Math.max((view.width / 2) - (frameSize.width / 2), 0) - contentWell.x;
    }

    Element.setXY(frame, frameLeft, frameTop);
    this.sizeShim();
    // add a class instead
    frame.style.visibility = 'visible';
};

IndustryDropDown.prototype.getDropDownListContent = function(args) {
    
    var cb = this.getContentBuffer();

    var conn = cb.load({
        debug: true,
        url: this.BUFFER_URL,
        method: "post",
        contentType: "text/javascript",
        preventEval: true,
        context: this,
        data: {
            wsodIssue: args.symbol
            ,pageType: args.pageType
        },
        onload: this.buildDropDown
    });
}

IndustryDropDown.prototype.close = function(e, el) {
    IndustryDropDown.Super(this, ["close"]);
};

IndustryDropDown.prototype.draw = function(e, el) {
    this.setAnchorElement(el);

    if (!Element.hasClass(this.getFrame(), this.CSS_DROPDOWN)) {
        Element.addClass(this.getFrame(), this.CSS_DROPDOWN);

        Element.addChild(this.getContent(),
			    Element.create("div", { "class": "placeholder" })
		    );

        var closeSide = "right";
        if (Element.getXY(this.getAnchorElement()).x - this.POPUP_SIZE < Element.getXY("wsod").x) {
            closeSide = "left";
        }

        Element.addChild(this.getFrame(),
			    Element.create("img", { "class": "closeDropDown", "src": this.SRC_ACTIVE_ICON, "style": "top:" + ((this.ICON_SIZE - 1) * -1) + "px;" + closeSide + ":" + this.ICON_HORIZONTAL_OFFSET + "px;", "Events":
				    { "type": "click", "handler": this.close, "context": this }
			    })
		    );
    }

    var frame = this.getFrame();
    Element.removeClass(frame, this.CSS_HIDDEN);

    this.position();
    
    this.SECTOR_NAME = el.getAttribute('sectorName');
    this.IS_INDUSTRY = el.getAttribute('isIndustry');

    this.getDropDownListContent({
        symbol: el.getAttribute('symbol')
	    , pageType: el.getAttribute('pageType')
    });

    //IndustryDropDown.Super(this, ["draw"]);    
}

IndustryDropDown.prototype.buildDropDown = function(cb) {
    var results = new Serializer().deserialize(cb.getResult());

    Element.removeChildNodes(this.getContent());

    var dropDownContent = Element.create("div"),
        table,
        tbody;

    table = Element.create("table", {}, [
		tbody = Element.create("tbody")
	])

    for (var i = 0; i < results.length; i++) {
        if (results[i].wsodIssue != -32768) {
            Element.create("tr", {}, [
			    Element.create("td", { "class": "textLeft" }, [
				    Element.create("a", { "href": this.URL_LINKS + "?s=" + results[i].wsodIssue }, results[i].fullName)
			    ]),
		    ], tbody);
        }
    }

    if (results.length) {
        if (this.IS_INDUSTRY == 'true') {
            var text = "There are " + results.length + " other industries in the " + this.SECTOR_NAME + " sector.";
        } else {
            var text = "There are " + results.length + " industries in the " + this.SECTOR_NAME + " sector.";
        }
        Element.create("div", { "class": this.CSS_DROPDOWN_TEXT }, text, dropDownContent)
    }

    Element.addChild(dropDownContent, table);

    Element.addChild(this.getContent(), dropDownContent);

    this.sizeShim();

    this.getDropDownListContent = function() { return; }
    this.buildDropDown = function() { return; }
}

IndustryDropDown.prototype.getContentBuffer = function() {
    var cb = new ContentBuffer();

    this.getContentBuffer = function() {
        return cb;
    }

    return this.getContentBuffer();
}