
/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/tearsheets/ExchangeList.js
*/
var ExchangeList = function() {
	this.hasTitle(false);
	this.hasCloseLink(false);
	
	var preload = new Image(); 
		preload.src = this.SRC_ACTIVE_ICON;
}

ExchangeList.Extend(Popup);

ExchangeList.prototype.CSS_MAIN = "popup customDropDown";
ExchangeList.prototype.ICON_SIZE = 24;
ExchangeList.prototype.ICON_HORIZONTAL_OFFSET = 10;
ExchangeList.prototype.POPUP_SIZE = 325;

ExchangeList.prototype.CSS_DROPDOWN = "exchangeListDropDown";
ExchangeList.prototype.CSS_DROPDOWN_TEXT = "exchangeText";
ExchangeList.prototype.CSS_FLAG = "wsod-flag";

ExchangeList.prototype.URL_EXCHANGE_LIST = "/ft/resources/buffer/getExchangeList.asp";
ExchangeList.prototype.URL_LINKS = "/ft/tearsheets/performance.asp";
ExchangeList.prototype.SRC_ACTIVE_ICON = "/ft/resources/image/dropdown-active.gif";

ExchangeList.prototype.init = function() {
	Events.add(Element.get("exchangeList"), "click", this.draw, this);
	
	this.hasIcon = Element.get("exchangeList") ? Element.get("exchangeList").tagName == "IMG" : false;
}

ExchangeList.prototype.getAnchorElement = function() { return null; }
ExchangeList.prototype.setAnchorElement = function(el) {
	el = Element.get(el);

	this.getAnchorElement = function() {
		return el;
	}
}

ExchangeList.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';
};

ExchangeList.prototype.getExchangeListContent = function(args) {
	this.getBuffer().abortRequests();
	this.getBuffer().load({
		url: this.URL_EXCHANGE_LIST,
		contentType: "text/javascript",
		data: {
			symbol: args.symbol,
			company: args.company
		},
		preventEval: true,
		onload: this.buildDropDown,
		context: this
	});
}

ExchangeList.prototype.buildDropDown = function(cb) {
	var results = this.getSerializer().deserialize(cb.getResult());
	
	Element.removeChildNodes(this.getContent());

	var dropDownContent = Element.create("div"),
		table,
		tbody,
		close,
		exchangeMap = {},
		countryMap = {},
		exchangeCount = 0,
		countryCount = 0,
		sCountryCode = '',
		sCountryCodeLowerCase = '';
	
	table = Element.create("table", {}, [
		tbody = Element.create("tbody")
	])
	
	for (var i = 0; i < results.length; i++) {
		sCountryCode = results[i].countryCode || '';
		sCountryCodeLowerCase = (sCountryCode.length) ? sCountryCode.toLowerCase() : '';

		Element.create("tr", {"class":0==i&&results[i].isPrimary?"primary":""}, [
			Element.create("td", {"class":"flag"}, [
				Element.create("div", {"class":this.CSS_FLAG+" "+"flag-"+sCountryCodeLowerCase})
			]),
			Element.create("td", {}, [
				Element.create("a", {"href":this.URL_LINKS+"?s="+results[i].symbol.replace("&", "%26")}, results[i].symbol)
			]),
			Element.create("td", {}, results[i].exchange)
		], tbody);
		
		if (!exchangeMap[results[i].exchange]) {
			exchangeMap[results[i].exchange] = true;
			exchangeCount++;
		}
	
		if (!countryMap[results[i].countryCode]) {
			countryMap[results[i].countryCode] = true;
			countryCount++;
		}
	}
	
	if (results.length) {
		if (results[0].isPrimary) {
			var text = "The primary listing for "+results[0].name+" is on the "+results[0].exchange;
			
			if (exchangeCount > 1 || countryCount > 1) {
				text += ", but it is also traded";
				
				if (exchangeCount > 1) {
					text += " on "+(exchangeCount-1)+" other exchange"+(exchangeCount-1 > 1 ? "s" : "");
				}
				
				if (countryCount > 1) {
					text += " in "+(countryCount-1)+" countr"+(countryCount-1 > 1 ? "ies" : "y");
				}
			}
		} else {
			var text = "";
				text += "This security is traded on "+exchangeCount+" exchange"+(exchangeCount > 1 ? "s" : "");
				text += " in "+countryCount+" countr"+(countryCount > 1 ? "ies" : "y");
		}
		
		text += ".";
		
		Element.create("div", {"class":this.CSS_DROPDOWN_TEXT}, text, dropDownContent);
	}
	
	Element.addChild(dropDownContent, table);
	
	Element.addChild(this.getContent(), dropDownContent);
	
	this.sizeShim();
	
	this.getExchangeListContent = function() { return; }
	this.buildDropDown = function() { return; }
}

ExchangeList.prototype.close = function (e, el) {
	Element.removeClass("exchangeList", "active");

	ExchangeList.Super(this, ["close"]);
};

ExchangeList.prototype.draw = function(e, el) {
	this.setAnchorElement(el);
	
	var elExchangeList = Element.get("exchangeList");
	
	if (Element.hasClass(elExchangeList, "active")) {
		this.close();
	} else {
		Element.addClass(elExchangeList, "active");
		
		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";
			}
			
			if (this.hasIcon) {
				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}
					})
				);
			}
		}
		
		this.getExchangeListContent({
			symbol: el.getAttribute("symbol"),
			company: el.getAttribute("company")
		});
		
		ExchangeList.Super(this, ["draw"]);
	}
}

ExchangeList.prototype.getBuffer = function() {
	var cb = new ContentBuffer();
	
	this.getBuffer = function() {
		return cb;
	};
	
	return this.getBuffer();
};

ExchangeList.prototype.getSerializer = function() {
	var s = new Serializer();
	
	this.getSerializer = function() {
		return s;
		
	};
	
	return this.getSerializer();
};
/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/editorial/editorialLogin.js
*/

var EditorialToolLogin = function() {}


EditorialToolLogin.prototype.init = function() {
	this.form =               WSDOM.Element.get('signInForm');
	this.logInButton =        WSDOM.Element.get('logInButton');
	this.password =           WSDOM.Element.get('password');
	this.username =           WSDOM.Element.get('username');

	// this.forgotPasswordLink = WSDOM.Element.get('forgotPassword');
	// this.rememberMe =         WSDOM.Element.get('rememberMe');
}


EditorialToolLogin.prototype.attachEvents = function() {
	if (this.password) {
		WSDOM.Events.add(
			{
				 context: this
				,element: this.password
				,handler: this.handleKeyPress
				,type:    'keypress'
			}
		);
	}

	if (this.forgotPasswordLink) {
		WSDOM.Events.add(
			{
				 context: this
				,element: this.forgotPasswordLink
				,handler: this.forgotPassword
				,type:    'click'
			}
		);
	}

	if (this.logInButton) {
		WSDOM.Events.add(
			{
				 context: this
				,element: this.logInButton
				,handler: this.loginSubmit
				,type:    'click'
			}
		);
	}
}


EditorialToolLogin.prototype.handleKeyPress = function(ev, el) {
	// If the user presses the "Enter" key, attempt to submit the form
	if ('13' == ev.nativeEvent.keyCode) {
		/*
		var el = ev.nativeEvent.target || ev.nativeEvent.srcElement;
			el.blur();  // Otherwise, Firefox will put the focus back on the <INPUT>
			window.focus();  // IE 6 does not support el.blur()
		*/

		this.loginSubmit(ev, el);
	}
}


EditorialToolLogin.prototype.loginSubmit = function(ev, el) {
	ev.cancel();

	var username = this.username.value || false;
	var password = this.password.value || false;

	if (!username || !password) {
		alert('Enter a username and password');

		return false;
	}

	this.form.submit();
}


EditorialToolLogin.prototype.forgotPassword = function() {
}


var login = new EditorialToolLogin();
	login.init();
	login.attachEvents();

