YUI.add('print', function(Y) {

	function Print(config) {
		this._initPrintListener(config);
	}

	Print.prototype = {
		_initPrintListener : function(selector){
			Y.on("click",this._print, selector);
		},
		
		_print : function(e){
			e.preventDefault();
			var resetLightbox = function(){};

			var lb = e.currentTarget.ancestor(".pot_lightbox");
			if (lb){
				var lightbox_container = lb.ancestor("div");
				var lightbox_container_current_styles = {
					position : lightbox_container.getComputedStyle("position"),
					top: lightbox_container.getComputedStyle("top"),
					left : lightbox_container.getComputedStyle("left")
				};
				
				lightbox_container.setStyles({
					position:"absolute",
					top:0,
					left:0
				});

				var lb_body = lb.one(".yui-widget-bd");
				var lb_body_current_styles = {
					height: lb_body.getStyle("height")
				};
				lb_body.setStyle('height', '100%');

				if(lb.hasClass("iframe")){
					var iframe = Y.Node.getDOMNode(lb.one('iframe'));

					lb.one("iframe").set("scrolling","no");
					var iframe_current_styles = {
						height: lb.one("iframe").getComputedStyle("height")
					};
					lb.one("iframe").setStyle("height", iframe.contentWindow.document.body.scrollHeight  + "px");
					lb.one("iframe").addClass("full_width");
				}
				
				resetLightbox = function(){
					Y.later(1000, this, function(){
						lightbox_container.setStyles(lightbox_container_current_styles);
						lb.removeClass("full_width");
						lb_body.setStyles(lb_body_current_styles);
						if (iframe){
							lb.one('iframe').setStyles(iframe_current_styles);
							lb.one('iframe').removeClass("full_width");
							lb.one("iframe").set("scrolling","");
						}
					}, {}, false);
				}

			}

			window.print();
			resetLightbox();
		}
	}

	Y.Print = Print;
}, '@VERSION@' ,{
	requires:['node','event']
});

YUI().use('node', function(Y) {

	// add hover to file_upload inputs
	var hover = function(e) {
		if (e.type === 'mouseout') {
			Y.one('.fake_file .fake_button a').setStyle('color', '#543c9e');
		}
		else {
			Y.one('.fake_file .fake_button a').setStyle('color', '#000');
		}
	};
	var flu = Y.one('#file_upload');
	if (flu) {
		flu.on('mouseover', hover);
		flu.on('mouseout', hover);
	}

	Y.all('.content .key_links a[href="/datatool/"]').addClass('popup').addClass('data_tool');
	Y.on('click', function(e) {
		var target = e.target;
		if (target.get('nodeName') != 'A' && target.get('parentNode').get('nodeName') != 'A') {
			return;
		}
		else if (target.get('parentNode')) {
			if (target.get('parentNode').get('nodeName') == 'A') {
				target = target.get('parentNode');
			}
		}
		if (target.hasClass('popup')) {
			e.preventDefault();
			var i, popup_settings = '',
			class_name = target.get('className').split(' '),
			my_target = target.get('href');
			if (class_name.length > 1) {
				for (i in class_name) {
					if (class_name[i] != 'popup') {
						target.addClass(class_name[i]);
					}
				}
			}
			if (target.hasClass("microsite")){
				popup_settings = "status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,height=800,width=1024";
			}
			else if (target.hasClass("old_microsite")){
				popup_settings = "status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,height=480,width=640";
			}
			else if (target.hasClass("career_app")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=620,width=650";
			}
			else if (target.hasClass("map")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=740,width=880";
			}
			else if (target.hasClass("analyst_meeting")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=740,width=892";
			}
			else if (target.hasClass("video")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=580,width=924";
			}
			else if (target.hasClass("media_player")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=605,width=924";
			}
			else if (target.hasClass("audio")){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=603,width=925";
			}
			else if (target.hasClass("slideshow") ){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=605,width=923";
			}
			else if (target.hasClass("transcript") ){
				popup_settings = "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=600,width=600";
			}
			else if (target.hasClass("market_image") ){
				popup_settings = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=580,width=967";
			}
			else if (target.hasClass("note")) {
				popup_settings = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=600,width=550";
			}
			else if (target.hasClass("note_wide")) {
				popup_settings = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=600,width=920";
			}			
			else if (target.hasClass('data_tool')) {
				my_target = my_target.replace('datatool', 'data-tool');
				popup_settings = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=680,width=967";
			}
			else {
				popup_settings = "status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,height=768,width=1024";
			}
			window.open(my_target, '_blank', popup_settings);
		}
		else if (target.hasClass('download') && target.get("nodeName").toUpperCase() == "A" && target.get("href").lastIndexOf(".pdf") == (target.get("href").length - 4)) {
			e.preventDefault();
			popup_settings = "status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,height=768,width=1024";
			window.open(target.get('href'), '_blank', popup_settings);
		}
	}, 'body', this);
});

function openWindow(href) {
	window.open(href, '_blank', 'status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,height=768,width=1024');
}

/**
* Adds modal support
*/
YUI.add('modal', function(Y) {

	var UA   = Y.UA,
	Lang = Y.Lang;

	function Modal(config){
		this._initModality();
	}

	Modal.ATTRS = {
		modal : {
			value : false,
			setter: function(val) {
				this._setModality(val);
			}
		}
	};

	Modal.prototype = {

		_initModality : function()
		{
			this._setModality(this.get("modal"));
		},

		_setModality : function(modal)
		{
			if (modal) {
				this._buildMask();

				if ( !this._modalityListeners ) {
					this._modalityListeners = [];
					this._modalityListeners.push(Y.before(function(){
						this.showMask();
					},this,"show"));

					this._modalityListeners.push(Y.on("click",function(){
						Y.lightbox.hide();
					}, Y.one("#lightbox_mask")));

					this._modalityListeners.push(Y.after(function(){
						this.hideMask();
					},this,"hide"));
				}

			} else {
				if ( this._modalityListeners ) {
					for(var i = 0, len = this._modalityListeners.length; i < len; i++ ) {
						Y.detach(this._modalityListeners[i]);
					}
					delete this._modalityListeners;

					this._removeMask();
				}
			}
		},

		_removeMask : function()
		{
			var oMask = this._mask,
			oParentNode;

			if (oMask) {
				oMask = Y.Node.getDOMNode(oMask);
				this.hideMask();
				oParentNode = oMask.parentNode;
				if (oParentNode) {
					oParentNode.removeChild(oMask);
				}

				this._mask = null;
			}

		},

		_buildMask : function()
		{
			var oMask = this._mask;
			if (!oMask) {
				var m_oMaskTemplate = document.createElement("div");
				m_oMaskTemplate.className = "mask";
				m_oMaskTemplate.innerHTML = "&#160;";

				oMask = m_oMaskTemplate.cloneNode(true);
				oMask.id = this.get("contentBox").get("id") + "_mask";

				document.body.insertBefore(oMask, document.body.firstChild);

				this._mask = Y.one( oMask );

				if ( UA.gecko && UA.os == "mac") {
					this._mask.addClass("block-scrollbars");
				}

				// Stack mask based on the element zindex
				this._stackMask();
			}
		},

		_stackMask: function()
		{
			if (this._mask) {
				var panelZ = this.get("zIndex");
				if (!Lang.isUndefined(panelZ) && Lang.isNumber(panelZ)) {
					this._mask.setStyle("zIndex",panelZ - 1);
				}
			}
		},

		showMask : function ()
		{
			if (this.get("modal") && this._mask) {
				Y.one("body").addClass("masked");
				this.sizeMask();
				this._mask.setStyle("display","block");
			}
		},

		sizeMask : function()
		{
			if (this._mask)
			{
				var oMask = this._mask,
				viewWidth = this._mask.get("docWidth"),
				viewHeight = this._mask.get("docHeight");

				oMask.setStyle("height",viewHeight + "px");
				oMask.setStyle("width",viewWidth + "px");
			}
		},

		hideMask : function ()
		{
			/* hide lightbox sharethis (if open) when lightbox closes */
			var share = Y.one('#stwrapper');
			if(share != undefined && share.getStyle("visibility") === "visible")
			{
				var share_link = Y.one('.share');
				share_link.simulate('click');
			}
			if (this.get("modal") && this._mask) {
				this._mask.setStyle("display","none");
				Y.one("body").removeClass("masked");
			}
		}
	};

	Y.Modal = Modal;
}, '@VERSION@', {
	requires:['node-event-simulate']
});

/**
* Adds fixed positioning support
*/
YUI.add('widget-position-fixed', function(Y){
	function WidgetPositionFixed(config){
		this._initFixedCenter()
	}
	
	WidgetPositionFixed.ATTRS = {
		fixedCenter: {
			value: false,
			setter: function(val) {
				this._setFixedCenter(val);
			}
		}
	};

	WidgetPositionFixed.prototype = {
		_initFixedCenter: function(config){
			this._setFixedCenter(this.get('fixedCenter'));
		},
		
		_setFixedCenter: function(fixedCenter){
			if (fixedCenter) {
				if (!this._positionFixedListeners) {
					this._positionFixedListeners = [];
					this._positionFixedListeners.push(Y.after(function(){
						this.center();
					}, this, 'show'));
					
					this._positionFixedListeners.push(Y.after('resize', function(){
						if (this.get('visible')) {
							this.center();
						}
					}, window, this));

					this._positionFixedListeners.push(Y.after(function(){
						this.unsetFixPosition();
					}, this, 'hide'));
				}
			}
			else {
				if (this._positionFixedListeners) {
					for (var i = 0, len = this._positionFixedListeners.length; i < len; i++) {
						Y.detach(this._positionFixedListeners[i]);
					}
					delete this._positionFixedListeners;
				}
			}
		},
		
		center: function(){
			var fix_position = true;

			var region = this.get('boundingBox').get('region');
			if ((region.right - region.left) > this.get('boundingBox').get('winWidth')) {
				fix_position = false;
			}
			if ((region.bottom - region.top) > this.get('boundingBox').get('winHeight')) {
				fix_position = false;
			}
			if (fix_position && (Y.UA.ie < 6 || Y.UA.ie >= 7)) {
				this.setFixPosition();
			}

			this._center();
		},
			
		_center: function(){
			var nViewportOffset =10,
			elementWidth = this.get('boundingBox').get('offsetWidth'),
			elementHeight = this.get('boundingBox').get('offsetHeight'),
			viewPortHeight = this.get('boundingBox').get('winHeight'),
			viewPortWidth = this.get('boundingBox').get('winWidth'),
			x,
			y;

			if (elementWidth < viewPortWidth) {
				x = (viewPortWidth / 2) - (elementWidth / 2) + this.get('boundingBox').get('docScrollX');
			}
			else {
				x = nViewportOffset + this.get('boundingBox').get('docScrollX');
			}

			if (elementHeight < viewPortHeight) {
				y = (viewPortHeight / 2) - (elementHeight / 2) + this.get('boundingBox').get('docScrollY');
			}
			else {
				y = nViewportOffset + this.get('boundingBox').get('docScrollY');
			}
			this._setX(x);
			this._setY(y);
		},
		
		setFixPosition: function(){
			this.get('boundingBox').setStyle('position', 'fixed');
		},
		
		unsetFixPosition: function(){
			this.get('boundingBox').setStyle('position', 'absolute');
		}
	};
	
	Y.WidgetPositionFixed = WidgetPositionFixed;
});

/* add close functionality */
YUI.add('widget-close', function(Y) {
	function WidgetClose(config) {
		this._initClose()
	}

	WidgetClose.ATTRS = {
		close : {
			value : false,
			setter: function(val) {
				this._setClose(val);
			}
		}
	};

	WidgetClose.prototype = {
		
		_initClose : function(config)
		{
			Y.before(function(){
				this._setClose(this.get("close"));
			},this,"show")
		},

		_setClose : function(close)
		{
			if (close) {
				Y.one('#'+this.get("contentBox").get("id")+' .close').removeClass("hide");

				if ( !this._closeListeners ) {
					this._closeListeners = [];
					this._closeListeners.push(Y.one('#'+this.get("contentBox").get("id")+' .close').on("click", function(e){
						e.preventDefault();
						var share = Y.one('#stwrapper');
						if(share != undefined && share.getStyle("visibility") === "visible")
						{
							var share_link = Y.one('.share');
							share_link.simulate('click');
						}
						this.hide()
					}, this));
				}

			} else {
				Y.one('#'+this.get("contentBox").get("id")+' .close').addClass("hide");
				if ( this._closeListeners ) {
					for(var i = 0, len = this._closeListeners.length; i < len; i++ ) {
						Y.detach(this._closeListeners[i]);
					}
					delete this._closeListeners;
				}
			}
		}
	};

	Y.Close = WidgetClose;
});

/* add height adjust functionality */
YUI.add('height-adjust', function(Y) {
	function WidgetHeightAdjust(config) {
		this._initHeightAdjust()
	}

	WidgetHeightAdjust.ATTRS = {
		height_adjust : {
			value : true,
			setter: function(val) {
				this._setHeightAdjust(val);
			}
		}
	};

	WidgetHeightAdjust.prototype = {
		_initHeightAdjust : function(config)
		{
			this._setHeightAdjust(this.get("height_adjust"));
		},

		_setHeightAdjust : function(height_adjust)
		{
			if (height_adjust) {
				if ( !this._heightAdjustListeners ) {
					this._heightAdjustListeners = [];
					this._heightAdjustListeners.push(Y.before(function(){
						if (this.get("boundingBox").get("winHeight") < this.get("boundingBox").get("offsetHeight") ){
							this.get("contentBox").one(".yui-widget-bd").setStyle("height", ((this.get("boundingBox").get("winHeight"))*.7) + "px");
							if(!this.get("contentBox").hasClass("iframe")) {
								this.get("contentBox").one(".yui-widget-bd").addClass("height_adjust");
							}
							else {
								this.get("contentBox").one("iframe").setStyle("height", ((this.get("boundingBox").get("winHeight"))*.7) + "px");
							}
						}
					},this,"show"));
					
					this._heightAdjustListeners.push(Y.on(function(){
						this.get("contentBox").one(".yui-widget-bd").setStyle("height", "auto").removeClass("height_adjust");//.setStyle("overflow", "visible");
					},this,"hide"));
				}
				
			} else {
				if ( this._heightAdjustListeners ) {
					for(var i = 0, len = this._heightAdjustListeners.length; i < len; i++ ) {
						Y.detach(this._heightAdjustListeners[i]);
					}
					delete this._heightAdjustListeners;
				}
			}
		}
	};

	Y.HeightAdjust = WidgetHeightAdjust;
});

YUI.add('lightbox-shareable', function(Y) {

	function WidgetLightboxShareable(config) {
		this._initLightboxShareable()
	}

	WidgetLightboxShareable.ATTRS = {
		shareable : {
			value : true,
			setter: function(val) {
				this._setLightboxShareable(val);
			}
		},
		
		shareable_href : {
			value : 'http://www.potashcorp.com',
			setter: function(val) {
				this._setLightboxShareableHref(val);
			}
		}

	};

	WidgetLightboxShareable.prototype = {
		_initLightboxShareable : function(config)
		{
			this._setLightboxShareable(this.get("shareable"));

		},

		_setLightboxShareable : function(shareable)
		{
			if (shareable) {
				if (this.get("contentBox").one("#share_lightbox")){
					this.get("contentBox").one("#share_lightbox").removeClass("hide");
				}

				Y.initShare(this.get("shareable_href"));
			} else {
				if (this.get("contentBox").one("#share_lightbox")){
					this.get("contentBox").one("#share_lightbox").addClass("hide");
				}

			}
		},

		_setLightboxShareableHref : function(href)
		{

		}

	};

	Y.LightBoxShareable = WidgetLightboxShareable;
});

YUI.add('lightbox-printable', function(Y) {

	function WidgetLightboxPrintable(config) {
		this._initLightboxPrintable()
	}

	WidgetLightboxPrintable.ATTRS = {
		printable : {
			value : true,
			setter: function(val) {
				this._setLightboxPrintable(val);
			}
		}

	};

	WidgetLightboxPrintable.prototype = {
		_initLightboxPrintable : function(config)
		{
			this._setLightboxPrintable(this.get("printable"));
		},

		_setLightboxPrintable : function(printable)
		{
			if (printable) {				
				if (this.get("contentBox").one(".print")){
					this.get("contentBox").one(".print").removeClass("hide");
				}

				if ( !this._lightboxPrintableListeners ) {
					this._lightboxPrintableListeners = [];
					this._lightboxPrintableListeners.push(Y.before(function(){

						Y.one('body').addClass(this.get("contentBox").get("id"));

						this.set('printable', true);
						if (this.get('contentBox').hasClass("no_print") || (this.get('contentBox').hasClass("iframe") && !this.get('contentBox').hasClass("force_print"))) {
							this.set('printable', false);
						}
					},this,"show"));
					
					this._lightboxPrintableListeners.push(Y.after(function(){
						Y.one('body').removeClass(this.get("contentBox").get("id"));
					},this,"hide"));

				}

			} else {
				if (this.get("contentBox").one(".print")){
					this.get("contentBox").one(".print").addClass("hide");
				}

				if ( this._lightboxPrintableListeners ) {
					for(var i = 0, len = this._lightboxPrintableListeners.length; i < len; i++ ) {
						Y.detach(this._lightboxPrintableListeners[i]);
					}
					delete this._lightboxPrintableListeners;

				}
			}
		}
	};

	Y.LightBoxPrintable = WidgetLightboxPrintable;
});

/* add close functionality */
YUI.add('lightbox-loading', function(Y) {

	function LightboxLoading(config) {
		this._initLoading()
	}

	LightboxLoading.ATTRS = {
		loading : {
			value : false,
			setter: function(val) {
				this._setLoading(val);
			}
		}
	};

	LightboxLoading.prototype = {
		_initLoading : function(config)
		{
			this._setLoading(this.get("loading"));
		},

		_setLoading : function(loading)
		{
			return;
		},
		
		showLoading: function() {
			Y.loading_lightbox.render();
			Y.loading_lightbox.show();
		}
	};

	Y.LightboxLoading = LightboxLoading;
});

YUI.add('widget-io', function(Y) {

	function WidgetIO(config) {
		this._initIO()
	}

	WidgetIO.ATTRS = {
		io : {
			value : true,
			setter: function(val) {
			},
			uri : {
				value : ""
			}
		}

	};

	WidgetIO.prototype = {
		io_cfg : {

		},
		
		_initIO : function(config)
		{
			this.io_cfg = {
				method: "",
				timeout:20000,
				on: {
					start: this._start,
					complete: this._complete,
					success: this._success,
					failure: this._failure,
					end: this._end
				},
				context: this ,
				headers: {
					'X-Transaction': 'POST Request',
					'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
				},
				
				arguments: {
					start: 'test',
					complete: '',
					success: '',
					failure: '',
					end: ''
				},
				
				data : '',

				configurable: Y.Array(["method", "arguments", "data", "form"]),
				set: function(param,value){
					if (Y.Array.indexOf(this.configurable, param) > -1) {
						this[param] = value;
					}
				},
				
				reset: function(){
					Y.Array.each(this.configurable, function(v, k){
						this[v] = '';
					}, this);
				}
			}
		},

		call: function(uri){
			if (!uri.match('external_gateway')){
				uri = uri + 'index.json';
			}
			var d = new Date();
			this.set('uri', uri);
			uri += (uri.indexOf('?') == -1) ? '?' : '&';
			uri += Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
			Y.io(uri, this.io_cfg);
		},
		
		_reset: function(){
			this.io_cfg.reset();
		},
		
		_start: function(){
			if (this.get('loading')) {
				this.showLoading();
			}
		},
		
		_complete: function(){},
		
		_success: function(io_id, o, args){
			if (o.responseText !== undefined) {
				try {
					var response = Y.JSON.parse(o.responseText);
					this._beforeSuccess();
					if (this._tryRedirect(response, args)) {
						return;
					}
					this.updateContent(response);
					if (Y.one('#form_wrapper input')) {
						Y.one('#form_wrapper input').focus();
					}
					this._afterSuccess(response); 
					this._formSuccess(response, args);
					this._tableHighlightSuccess(response);
					this._clearBeforeAndAfterSuccess();
					this._reset();
					if(window.addGlossaryListeners) {
						addGlossaryListeners();
					}
				}
				catch(e) {
					this._failure(null, {
						statusText:e
					} );
				}
			}
			else {
				this._failure(e);
			}

		},
		
		_failure: function(id, o, args){
			var fail_message = {
				header:"<span class=\"error\">"+o.statusText+"</span>",
				body:"<p>There was an error retrieving the data. Please try again.</p>",
				footer: "<p>If the problem persists please contact us</p>"
			};
			this.updateContent(fail_message);
		},
		
		_end : function(){

		},
		
		showIframe: function(url, class_name){
			var separator = '?';
			if (url.indexOf('?') != -1) {
				separator = '&';
			}
			if (class_name != '') {
				class_name = 'class="'+class_name+'"';
			}

			var iframe = Y.Node.create('<iframe src="'+url+ separator + 'iframe=1" id="iframe" '+class_name+' frameborder="0"></iframe>');
			this.updateContent({
				body: iframe
			});
			this.show();
		},
		
		updateContent:function(o) {
			this._updateHeader(o.header);
			this._updateBody(o.body);
			this._updateFooter(o.footer);
			if (Y.loading_lightbox){
				Y.loading_lightbox.hide();
			}
			this.show();
		},
		
		_updateHeader:function(header){
			if (!header || header == "")
				this.set("headerContent", " ");
			else
				this.set("headerContent", header);
		},
		
		_updateBody:function(body){
			if (!body || body == "")
				this.set("bodyContent", " ");
			else
				this.set("bodyContent", body);
		},
		
		_updateFooter:function(footer){

			if (!footer || footer == "")
				this.set("footerContent", " ");
			else
				this.set("footerContent", footer);
		},
		
		resetContent: function(){
			this.updateContent({
				header:"",
				body:"",
				footer:""
			});
		},

		_beforeSuccess : function(){}, 
		_afterSuccess : function(){}, 

		_formSuccess : function (response, args) {
			if (this.get("contentBox").one('form')){
				this._submitForm.call(this, response);
			}
		},
		
		_tableHighlightSuccess : function (response) {
			if (this.get("contentBox").all('.highlightTable') && Y.Lang.isFunction(highlightTables)){
				highlightTables();
			}
		},
		
		_submitForm : function(e, o){
			var forms = this.get('contentBox').all("form.ajax_form");
			forms.each(function(form, k){
				form.on('submit', function(e, o){
					e.preventDefault();
					o.io_cfg.set("method", this.get("method") );
					o.io_cfg.set("form", {
						id:this
					});
					var redirect = this.get("action").match(/redirect=[^&]*&?/);

					if (redirect){
						o.io_cfg.set("arguments", {
							success:{
								redirect:redirect[0].split("=")[1]
							}
						});
					}
					o.call(this.get("action"));

				}, null, this);
			}, this);
		},
		
		_tryRedirect: function(o, args){
			if (o.body){
				try {
					var redirect = Y.JSON.parse(o.body);
					if (redirect){
						window.location = args.success.redirect;
						return true;
					}
				}
				catch(e) {
					return false;
				}
			}
		},
		
		_clearBeforeAndAfterSuccess:  function(){ 
			this._beforeSuccess = function(){}; 
			this._afterSuccess = function(){}; 
		} 		

	};

	Y.WidgetIO = WidgetIO;
},"@VERSION@", {
	requires:['json', 'io-form']
});

YUI.add('zu-lightbox', function(Y) {
	Y.Lightbox = Y.Base.build("zu-lightbox", Y.Overlay, [Y.Modal, Y.WidgetPositionFixed, Y.Close, Y.LightboxLoading, Y.WidgetIO, Y.HeightAdjust, Y.LightBoxPrintable, Y.LightBoxShareable]);
	Y.after(function(){
		Y.one('body').setStyle("overflow", "hidden");
	}, Y.Lightbox, 'hide');

}, '@VERSION@', {
	requires:['overlay','modal','widget-position-fixed','widget-close', 'lightbox-loading', 'height-adjust', 'widget-io', 'lightbox-printable', 'lightbox-shareable']
});

/***************************************************************************/

YUI().use('pot_tools', 'zu-lightbox', 'print', function(Y){
	Y.setLightboxLinks = function(e, target, hideflag){
		Y.lightbox.set('shareable_href', target.get('href'));
		if (target.hasClass('zu_lightbox') || target.hasClass('external')) {
			e.preventDefault();
			var class_name = target.get('className').split(' ');
			if (class_name.length > 1) {
				for (var i in class_name) {
					if (class_name[i] == 'small'
						|| class_name[i] == 'medium'
						|| class_name[i] == 'large'
						|| class_name[i] == 'xlarge'
						|| class_name[i] == 'full_width'
						|| class_name[i] == 'force_print'
						|| class_name[i] == 'iframe') {
						Y.one('#lightbox').addClass(class_name[i]);
					}
				}
			}
			if (target.hasClass('no_print')) {
				Y.lightbox.set('printable', false);
			}
			if (target.hasClass('iframe')) {
				if (target.hasClass('force_print')) {
					Y.lightbox.set('printable', true);
				}
				//Y.lightbox.set('shareable', false);
				Y.lightbox.showIframe(target.get('href'), target.get('id'));
			}
			else if (target.hasClass('external')) {
				Y.lightbox.set('printable', false);
				Y.lightbox.set('shareable', false);
				if (target.hasClass('follow')) {
					openWindow(target.get('href'));
					if (hideflag == true) {
						Y.lightbox.hide();
					}
				}
				else {
					Y.lightbox.call('/external_gateway/?link=' + encodeURIComponent(target.get('href')) + '&text=' + encodeURI(target.get('innerHTML')));
					if (!target.hasClass('small')) {
						Y.one('#lightbox').addClass('small');
					}
				}
			}
			else {
				Y.lightbox.set('printable', true);
				Y.lightbox.set('shareable', true);
				Y.lightbox.call(target.get('href'));
			}

			return;
		}
	}
	
	Y.initLightboxLinks = function(){
		Y.on('click', function(e){
			var target = e.currentTarget;
			Y.setLightboxLinks(e, target, true);
		}, 'body a', this);
		// link from lightbox
		Y.on('click', function(e){
			var target = e.target;
			if (target.get('nodeName') != 'A') {
				return;
			}
			Y.setLightboxLinks(e, target, true);
		}, '.pot_lightbox .yui-widget-bd', this);
		// link from lightbox from pot lightbox
		Y.on('click', function(e){
			var target = e.target;
			if (target.get('nodeName') != 'A') {
				return;
			}
			Y.setLightboxLinks(e, target, false);
		}, '#data_pot_lightbox .yui-widget-bd', this);
		if (window.LightboxClickIntercept) {
			LightboxClickIntercept.detach();
			delete LightboxClickIntercept;
		}
	}
	
	Y.initLightbox = function(){
		Y.loading_lightbox = null;
		var lightbox_container = Y.Node.create('<div id="lightbox" class="pot_lightbox"><div class="utilities"><a href="#" class="print">Print</a> </div><a href="#" class="close">X</a><div class="yui-widget-hd"></div><div class="yui-widget-bd yui-gf"></div><div class="yui-widget-ft"></div></div>');
		Y.one('body').append(lightbox_container);
		Y.lightbox = new Y.Lightbox({
			contentBox: "#lightbox",
			modal:true,
			close:true,
			loading:true,
			fixedCenter:true,
			visible:false,
			centered:true,
			zIndex:10000
		});
		var loading_lightbox_container = Y.Node.create('<div id="loading_lightbox" class="pot_lightbox"><a href="#" class="close hide">X</a><div class="yui-widget-hd"><span>Loading...</span></div><div class="yui-widget-bd yui-gf"><div style="text-align:center"><img src="/sustainability_reports/2009/common/images/interface/skins/lightbox/loading.gif" /></div></div><div class="yui-widget-ft"></div></div>');
		Y.one('body').append(loading_lightbox_container);
		Y.loading_lightbox = new Y.Lightbox({
			contentBox: "#loading_lightbox",
			modal:false,
			close:false,
			loading:false,
			fixedCenter:true,
			visible:false,
			centered:true,
			zIndex:10000
		});
		Y.before(function(){
			if (Y.UA.ie == 7) {
				document.body.setAttribute("scroll", "no");
			}
			else {
				Y.one('body').addClass("hide_overflow");
			}
		}, Y.lightbox, 'show');
		Y.before(function(){
			var class_names = Y.one('#lightbox').get('className').split(" ");
			if(class_names.length > 1 ){
				Y.one('#lightbox').set("className", "pot_lightbox");
			}
			this.resetContent();

			if (Y.UA.ie == 7){
				document.body.setAttribute("scroll", "auto");
			}
			else {
				Y.one('body').removeClass("hide_overflow");
			}

		}, Y.lightbox, 'hide');

		Y.lightbox.render();
		Y.lightbox.hide();
		Y.loading_lightbox.render();
		Y.loading_lightbox.hide();
		Y.initLightboxLinks();
	}

	Y.initPotTools = function() {
		if(Y.PotTools){
			new Y.PotTools();
		}
	}
	
	Y.initPrint =  function(){
		Y.Printer = new Y.Print(".print");
	}
	
	Y.initShare = function(href) {
		var object = SHARETHIS.addEntry({
			url: href
		},{
			button:false, 
			onmouseover:false
		});

		if (Y.one('#share_lightbox') === undefined || Y.one('#share_lightbox') === null) {
			var share = Y.Node.create('<span id="share_lightbox"><span>|</span>  <a title="Share" class="share" href="javascript:void(0);">Share<\/a><\/span>');
			var utilities = Y.one('.pot_lightbox .utilities');
			utilities.appendChild(share);
		}

		var element = Y.Node.getDOMNode(Y.one("#share_lightbox"));
		object.attachButton(element);
	}
	
	Y.initClose =  function() {
		Y.on('click', function(){
			alert("closing ...");
			window.close();
		}, '#bd .close');
	}
	
	Y.init =  function() {
		Y.initLightbox();
		Y.initPotTools();
		Y.initPrint();
		Y.initClose();
	};
	
	Y.init();
});
