var Application		= com_pagesociety_ux_Application;
var Container 		= com_pagesociety_ux_component_Container;
var Component 		= com_pagesociety_ux_component_Component;
var FlowLayout 		= com_pagesociety_ux_layout_FlowLayout;
var ComponentEvent 	= com_pagesociety_ux_event_ComponentEvent;
var ResourceEvent	= com_pagesociety_ux_event_ResourceEvent;
var Label			= com_pagesociety_ux_component_text_Label;
var ImageResource	= com_pagesociety_ux_component_ImageResource;
var ImageDecorator	= com_pagesociety_ux_decorator_ImageDecorator;
var MaskedDecorator	= com_pagesociety_ux_decorator_MaskedDecorator;
var PageContainer	= com_pagesociety_ux_component_container_PageContainer;
var SlideShow		= com_pagesociety_ux_component_media_SlideShow;
var MovingValue		= com_pagesociety_ux_MovingValue;
var Random			= com_pagesociety_util_Random;
var UserModule		= com_pagesociety_web_module_User;
var ResourceModule	= com_pagesociety_web_module_Resource;
var Guide			= com_pagesociety_ux_component_dim_Guide;

var PAGE_WIDTH	 	= 948;

var COLOR_GREEN 	= 0x4B7C75;
var COLOR_WHITE		= 0xffffff;

function init_menu()
{
	var at_home = document.location.pathname == "/";
	var in_about = pathfind(["/about-us","/location/","/principal/", "/joblisting/"]);
	var at_project = pathfind("/project/");
	var at_projects = pathfind("/projects");
	var in_project = at_project || at_projects;
	var in_advocacy = pathfind(["/advocacy","/summerprogram/","/student/","/fellowship/"]);
	var in_news = pathfind(["/news"]);
	var at_search = pathfind("/search");

	if (at_home || in_about)
		$("top_bar_site_search_input").addClassName("on_white");

	if (in_about)
		init_header("about");
	else if (in_advocacy)
		init_header("advocacy");
	else if (in_news)
		init_header("news");
	else if (!in_project && !at_search)
		init_header("other");

	if (!in_about && $("secondary_navigation"))
	{
		var sel = $("secondary_navigation").getElementsByClassName("secondary_selected");
		if (sel!=null && sel.length!=0)
			sel[0].addClassName("secondary_selected_white");
	}

	var add_stuff_to_nav = function (el)
	{
		if (el==null)
			return;
		var lis = el.getElementsByTagName("li");
		for (var i=0; i<lis.length; i++)
		{
			var li = $(lis[i]);
			var a = li.getElementsByTagName("a");
			if (a==null || a.length==0)
				continue;

			a = $(a[0]);
			li.__href = a.getAttribute("href");
			li.onclick = function(){ document.location.href = this.__href; };
			li.setStyle({"cursor":"pointer"});

			if($(a).hasClassName('selected_link'))
				continue;

			if (at_home || at_search || li.parentNode.id != "primary_navigation")
				continue;

			if (at_project && i != 1)
				li.addClassName( "project" );
			else if (at_projects)
				li.addClassName( "projects" );
			else if (in_about)
				li.addClassName("darker" );
			else if (in_advocacy)
				li.addClassName( "advocacy" );
			else if (in_news)
				li.addClassName("news");
//			else
//				li.addClassName( "project" );

		}
	};
	add_stuff_to_nav( $("primary_navigation_wrapper"));
	add_stuff_to_nav( $("secondary_navigation_wrapper") );

}



function init_header(type)
{
	if ($("navigation_bg")!=null)
		$("navigation_bg").addClassName(type);
	if ($("ambient_picture_wrapper")!=null)
		$("ambient_picture_wrapper").addClassName(type);
	if ($("home_link")!=null)
		$("home_link").addClassName(type);
	if ($("swa_statement_wrapper")!=null)
	{
		var s = $("swa_statement_wrapper").getElementsByClassName("swa_statement");
		if (s!=null && s.length!=0)
			s[0].addClassName(type);
	}
	if (type=="about")
	{
		$("secondary_navigation").addClassName("dark");
		if ($("navigation_backlink_wrapper")!=null)
			$("navigation_backlink_wrapper").addClassName("dark");
	}

}

function init_footer()
{
	$("footer_language").
		onclick =
			function()
			{
				$("footer_language_choices").style.visibility = "visible";
			};

	//$("footer_language").onmouseover = function() {};
	//$("footer_language").onmouseout = function() {};

}

function pathfind(a)
{
	if (!(a instanceof Array))
		a = [ a ];
	var p = document.location.pathname;
	for (var i =0; i<a.length; i++)
	{
		if (p.indexOf(a[i]) == 0)
			return true;
	}
	return false;
}





var f0 = [];
var f1 = [];
function swa_init_home()
{

	// get data from page
	var featured = get_slide_data("home_featured_projects");
	var info_items_length = $("information_items_wrapper").getElementsByClassName("homepage_info_wrapper").length;
	var info_showing = !(info_items_length == 0);

	var c = featured.length/2;
	for (var i=0; i<featured.length; i++)
	{
		if (info_showing || i<c)
			f0.push(featured[i]);
		else
			f1.push(featured[i]);
	}

	$("home_featured_projects").remove();

	var app = new Application("wrapper");
	app.setWidth(PAGE_WIDTH);

	if (IS_IOS)
		_projects0 = new ProjectSlices(app, f0);
	else
		_projects0 = new SWFComponent(app, "/static/SWASlices.swf", { dataProvider: "swa_home_p0", initFromTop: "true" });
	_projects0.setY(70);
	_projects0.setHeight(220);

	if(!info_showing)
	{
		if (IS_IOS)
			_projects1 = new ProjectSlices(app, f1);
		else
			_projects1 = new SWFComponent(app, "/static/SWASlices.swf", { dataProvider: "swa_home_p1" });
		_projects1.setY(510);
		_projects1.setHeight(220);
	}
	var header = new DOMComponent(app, "header");
	header.setY(39);

	var language_selector = new DOMComponent(app, "language_selector");
	language_selector.setY(43);
	language_selector.setX(0);
	language_selector.setWidth(500);

	var content = new DOMComponent(app, "content");
	content.setY(291);
	//content._decorator.zIndex = 999;

	var footer = new DOMComponent(app, "footer");
	footer.setY(730);


	var items = new DOMComponent(app, "information_items_wrapper");
	if(info_showing != 0)
	{
		items.setVisible(true);
		items.setY(510);
		footer.setY(540+info_items_length*200);
	}
	else
	{
		items.setVisible(false);
	}
	app.render();

}

function swa_home_p0()
{
	return f0;
}

function swa_home_p1()
{
	return f1;
}

var ProjectSlices = Class.create(Container,
{
	initialize: function($super, parent, images)
	{
		$super(parent);

		this.iconsNode = new Container(this);
		this.iconsNode.setLayout(new FlowLayout(FlowLayout.TOP_TO_BOTTOM));
		this.iconsNode._layout._margin.bottom = 7;
//		this.iconsNode.setBackgroundVisible(true);
//		this.iconsNode.setBackgroundColor(0x0000ff);

		for (var i = 0; i < images.length; i++)
		{
			var p = new ProjectSlide(this.iconsNode);
			p.setValue(images[i]);
			p.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

		}
	},

	on_load: function(e)
	{
		this.render();
	},


	render: function($super)
	{
		var ph = this.getHeight();
		var h = this.iconsNode.getLayout().calculateHeight();
		this.iconsNode.setHeight(h);
		this.iconsNode.setY((ph-h)*.5);

		$super();
	}


});


var PImageResource = Class.create(Container,
{
	initialize: function($super, parent)
	{
		$super(parent);
		this.at_size = 0;
		this.preview_widths = [200,1000];
		this.preview_heights = [200,1000];
		this.img = null;
		this.resource = null;
	},

	setPreviewWidths: function(widths)
	{
		this.preview_widths = widths;
	},

	setPreviewHeights: function(heights)
	{
		this.preview_heights = heights;
	},

	setImageScalingType: function(t)
	{
		this._image_scaling_type = t;
	},

	setResource: function(res)
	{
		this.clear();
		this.at_size = 0;
		this.resource = res;
		this.add_image();
	},

	add_image: function()
	{
		this.img = new ImageResource(this);
		this.img.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));
		this.img.setPreviewWidth(this.preview_widths[this.at_size]);
		this.img.setPreviewHeight(this.preview_heights[this.at_size]);
		this.img.setResource(this.resource);
	},

	render: function($super)
	{
		if (this.img!=null)
			this.img.setImageScalingType(this._image_scaling_type);
		$super();
	},

	getImageWidth: function()
	{
		if (this.img==null)
			return 0;
		return this.img.getImageWidth();
	},

	on_load: function(e)
	{
		//Logger.log(">>>"+this.preview_widths[this.at_size]+" "+this.img.getSrc())
		if (this.at_size>0)
		{
			this.removeComponent(this._children[this.at_size-1]);
		}
		this.at_size++;
		if (this.at_size==this.preview_widths.length)
		{
			this.dispatchComponentEvent(ResourceEvent.LOAD_RESOURCE, this);
			return;
		}
		this.add_image();
		this.render();
	}
});


var ProjectSlide = Class.create(Container,
{
	initialize: function($super, parent)
	{
		$super(parent);
		this.setBackgroundVisible(false);

		this.c = new Container(this);

		this.bg = new Component(this.c);
		this.bg.setBackgroundColor(0);
		this.bg.setBackgroundVisible(true);
		this.bg.setAlpha(.3);

		this.img = new PImageResource(this.c);
		this.img.setPreviewWidths([200,800]);
		this.img.setPreviewHeights([200,800]);
		this.img.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_MASK_FULL_BLEED);
		this.img.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

		this.fg = new Component(this.c);
		this.fg.setX(-1);
		this.fg.setY(-1);
		this.fg.setHeightDelta(3);
		this.fg._decorator.mid.style.background = "-webkit-gradient(linear, left top, right top, color-stop(0%,#ffffff), color-stop(70%,rgba(255,255,255,0)))";
			//"-webkit-gradient(linear, left top, right top, from(#1a82f7), to(#2F2727));"

		this.label = new Label(this.c);
		this.label.setFontStyle("featured_project_title1");
		this.label.setWidth(800);
		this.label.setY(1);

		this.click_area = new Component(this);
		this.click_area.setBackgroundColor(0);
		this.click_area.setBackgroundVisible(true);
		this.click_area.setAlpha(0);
		this.click_area.setHeightDelta(1);

		this.setHeight(24);
	},

	setValue: function(project)
	{
		this.setUserObject(project);
		this.label.setText(project.title);
		this.img.setResource(project.attributes.resource);
		this.addEventListener(ComponentEvent.CLICK, this.on_click.bind(this));
	},

	showTitle: function()
	{
		this.fg.alphaTo(1);
	},

	hideTitle: function()
	{
		this.fg.alphaTo(0,40);
	},

	on_load: function()
	{
		this.dispatchComponentEvent(ResourceEvent.LOAD_RESOURCE, this);
		this.alphaTo(1,500);
	},

	on_click: function()
	{
		document.location.href = this.getUserObject().href;
	}

});



//////////////-----------------//////////////-------------------//////////////------------------
//-----------------//////////////-------------------//////////////------------------//////////////
//////-----------------//////////////-------------------//////////////------------------////////
//-----------------//////////////-------------------//////////////------------------//////////////




function swa_init_projects(url, lang)
{
	if(IS_IOS)
		return;
	f0 = get_project_results("project_result_wrapper");
	$("projects").innerHTML = "";
	$("projects").setStyle("height", "500px");
	swa_prep_selector_data();

	var app = new Application("projects");
	app.setWidth(PAGE_WIDTH);
	app.setHeight(500);

	if (IS_IOS)
	{
		var projects = new ProjectSlices(app,f0);
	}
	else
	{
		$("project_search_bar_wrapper").hide();
		if($("swa_pager_wrapper_wrapper"))
			$("swa_pager_wrapper_wrapper").hide();

		var projects = new SWFComponent(app, "/static/SWASlices.swf",
				{
					dataProvider: "swa_home_p0",
					selectors: "swa_get_selectors",
					restoreFromSession: "true",
					language: lang,
					moduleRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL
				});
	}
	app.render();

}

function swa_prep_selector_data()
{
	var scope_keys 			= get_string_array("project_search_scope");
	var scope_selection 	= $("project_search_scope").selectedIndex;
	var type_keys 			= get_string_array("project_search_type");
	var type_selection 		= $("project_search_type").selectedIndex;
	var location_keys 		= get_string_array("project_search_location");
	var location_selection 	= $("project_search_location").selectedIndex;
	f1 = { scopes: scope_keys, scopes_idx: scope_selection, types: type_keys, types_idx: type_selection, locations: location_keys, locations_idx: location_selection };
}
function swa_get_selectors() { return f1; }






function swa_init_search_controls(base_url,controls)
{
	var input_value; //only works for 1!
	var init = function()
	{
		for (var i=0; i<controls.length; i++)
		{
			var c = $(controls[i]);
			if (c.nodeName =="SELECT")
				c.observe("change",do_search);
			else if (c.nodeName == "INPUT")
			{
				input_value = c.value;
				c.observe("keypress",test_input);
				c.observe("focus",focus_input);
			}
		}
	};
	var do_search = function(e)
	{
		var t = e.target;
		for (var i=0; i<controls.length; i++)
		{
			var c = $(controls[i]);
			if (c == t)
				continue;
			if (c.nodeName =="SELECT")
				c.selectedIndex = 0;
			else if (c.nodeName == "INPUT")
				c.value = "";
		}
		var s = base_url+"?";
		if (t.nodeName == "SELECT")
		{
			if(e.target.selectedIndex == 0)
				;
			else
				s += e.target.options[0].value+"="+e.target.value;
		}
		else if (t.nodeName == "INPUT")
			s += "freetext="+e.target.value;
		window.location = s;
	};
	var test_input = function(e)
	{
		if (e.keyCode ==  13)
			do_search(e);
	};
	var focus_input = function(e)
	{
		if (e.target.value == input_value)
			e.target.value = "";
	};
	init();
}
/*
 *
 * var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13) {
 *
 */




//////////////-----------------//////////////-------------------//////////////------------------
//-----------------//////////////-------------------//////////////------------------//////////////
//////-----------------//////////////-------------------//////////////------------------////////
//-----------------//////////////-------------------//////////////------------------//////////////

var pp;
var slides;
function swa_get_project_slide_data()
{
	return slides;
}
function project_slide_show_change_value(page)
{
	pp.setPage(page);
}

function swa_init_project()
{

	slides = get_slide_data("project_slideshow_id");
	var s0 = [];
	for (var i=0; i<slides.length; i++)
		s0.push(slides[i].attributes.resource);
	//Logger.log(Object.toJSON(s0));
	//swa_prep_selector_data();

	var pdfa = $("project_pdf").getElementsByTagName("a");
	var pdf = null;
	if (pdfa!=null && pdfa.length!=0)
		pdf = pdfa[0].href;

	var ss_app = new Application("root");
	pp = new ProjectPage(ss_app,s0,pdf);
	$("content").style.display = "block";
	$("wrapper").innerHTML = "";
	ss_app.render();
	ss_app.render(); //DOG
}


var ProjectPage = Class.create(Container,
{
	initialize: function($super, parent, s0, pdf)
	{
		$super(parent);

		this.pdf = pdf;

		var full_bleed_images = this.getApplication().getSharedObject("full_bleed_images");
		if (full_bleed_images == null)
		{
			this.getApplication().setSharedObject("full_bleed_images", "true");
			full_bleed_images = "true";
		}


		if (IS_IOS)
		{
			var S = this.slideshow = new ProjectSlideShow(this);
			S.swf = function() { return S; };
			if (full_bleed_images!="true")
			{
				$("top_bar_site_search_input").addClassName("on_white");
				$("footer").removeClassName("shadowed");
				this.slideshow.setUncropped();
			}
		}
		else
		{
			if (full_bleed_images!="true")
			{
				$("top_bar_site_search_input").addClassName("on_white");
				$("footer").removeClassName("shadowed");
			}

			this.slideshow = new SWFComponent(this, "/static/SWAProject.swf",
				{
					dataProvider: "swa_get_project_slide_data",
					fullBleedImages: full_bleed_images,
					jpegQuality: 60,
					moduleRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL,
					resourceRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL+"/"
				});
		}

		this.header = new DOMComponent(this, "navigation");
		//this.header.setY(39);
		this.header.setWidth(PAGE_WIDTH);
		this.header.setHeight(54);
		this.header.alignX(Guide.CENTER);
//		this.header.setBackgroundVisible(true);
//		this.header.setBackgroundColor(0xffffff);

		this.menu = new DOMComponent(this, "primary_navigation");
		this.menu.setWidth(PAGE_WIDTH);
		this.menu.setHeight(54);
		this.menu.alignX(Guide.CENTER);
		this.menu.setY(291);
		this.menu.setAlpha(0);

		this.content_wrap = new Container(this);
		this.content_wrap.setBackgroundVisible(true);
		this.content_wrap.setBackgroundColor(0xffffff);
		this.content_wrap.setBackgroundAlpha(0);
		this.content_wrap.setWidth(PAGE_WIDTH);
		this.content_wrap.alignX(Guide.CENTER);


		this.content = new DOMComponent(this.content_wrap, "content");
		this.content_title = this.content._decorator.element.getElementsByClassName("top")[0].innerHTML;
		this.content_text = this.content._decorator.element.getElementsByClassName("right")[0];
		this.content.setAlpha(0);

		//TODO, add events and move all the controller stuff to this class, instead of passing everything to nav...
		this.ssnav = new ProjectNav(this, this.slideshow, s0, this.content_wrap, this.menu, this.pdf, full_bleed_images);
		this.ssnav.setWidth(PAGE_WIDTH);
		this.ssnav.setHeight(40);
		this.ssnav.alignX(Guide.CENTER);
		this.ssnav.alignY(Guide.BOTTOM,-50);
		this.ssnav.addEventListener("download", this.on_download.bind(this))
		this.ssnav.addEventListener("related", this.on_related.bind(this))

		this.footer = new DOMComponent(this, "footer");
		this.footer.setY(10);
		this.footer.setWidth(PAGE_WIDTH);
		this.footer.setHeight(54);
		this.footer.alignX(Guide.CENTER);
		this.footer.alignY(Guide.BOTTOM);

		if (IS_IOS)
		{
			this.slideshow.setValue(s0);
			this.slideshow.addEventListener(ComponentEvent.CHANGE_VALUE, this.update.bind(this));
		}

	},
	fadeInInfo: function(time)
	{
		this.menu.alphaTo(1,500);
		this.content_wrap.backgroundAlphaTo(0.9,850);
		this.content.alphaTo(1,850);
	},
	setPage: function(p)
	{
		//only called from flash slideshow
		this.ssnav.update();
		this.render();
	},

	update: function()
	{
		this.ssnav.update();
	},

	on_download: function()
	{
		document.location.href = this.pdf;
		return false;
	},

	on_related: function()
	{

		//if someone is coming from a page outside the domain this doesnt work...
		//could set a cookie (last search page) and read it
		//{
			if (history.length==0)
				document.location.href = "/projects";
			else
				history.back();
			return;
		//}
		//we don't do this cool overlay anymore... at client's request
		//too bad!
		if (this.rc == null)
		{
			this.rc = new Container(this);

			this.upper = new Container(this.rc);
			this.upper.setBackgroundVisible(true);
			this.upper.setBackgroundColor(0x55736A);
			this.upper.setBackgroundAlpha(.7);
			this.upper.setHeight(291);

			this.lower = new Container(this.rc);
			this.lower.setBackgroundVisible(true);
			this.lower.setBackgroundColor(0xffffff);
			this.lower.setBackgroundAlpha(.9);
			this.lower.setY(291);
			this.lower.setHeightDelta(-291);

			this.relatedc = new Container(this.rc);
			this.relatedc.setWidth(PAGE_WIDTH);
			this.relatedc.setHeight(100);
			this.relatedc.alignX(Guide.CENTER);
			this.relatedc.setY(100);
			this.relatedc.setBackgroundVisible(true);
			this.relatedc.setBackgroundColor(0xF4EDCE);
			this.relatedc.setBackgroundAlpha(.7);
			this.relatedc.addEventListener(ComponentEvent.CLICK, this.on_project.bind(this))

			this.relatedl = new Label(this.relatedc);
			//this.relatedl.setText("RELATED");
			//this.relatedl.setFontStyle("swa_link")

			this.relatedt = new Label(this.relatedc);
			this.relatedt.setText(this.content_title);
			this.relatedt.setX(15);
			this.relatedt.setWidthDelta(-100);

			this.relatedr = new Label(this.relatedc);
			this.relatedr.setText("RETURN TO PROJECT");
			this.relatedr.setFontStyle("swa_link blue");
			this.relatedr.setWidth(170);
			this.relatedr.setHeight(20);
			this.relatedr.alignX(Guide.RIGHT,-10);
			this.relatedr.alignY(Guide.BOTTOM,-5);

			this.related = new SWFComponent(this.rc, "/static/SWASlices.swf",
			{
				selectors: "swa_get_selectors",
				restoreFromSession: "true",
				moduleRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL
			});
			this.related.setWidth(PAGE_WIDTH);
			this.related.alignX(Guide.CENTER);
			this.related.setY(330);
			this.related.setHeightDelta(-375);

			this.header.bringToFront();
			this.menu.bringToFront();
			this.footer.bringToFront();
		}
		this.ssnav.setVisible(false);
		this.content_wrap.alphaTo(0,100);
		this.rc.setVisible(true);
		this.render();
	},

	on_project: function()
	{
		this.ssnav.setVisible(true);
		this.ssnav.update_info();
		this.rc.setVisible(false);
		this.render();
	},

	render: function($super)
	{
		var h = this.getHeight();
		var m = Math.min(h-this.menu.getY()-210, 322);
		if (m<100)
		{
			this.content_wrap.alphaTo(0,200);
//			this.content_wrap.setVisible(false);

		}
		else
		{
			this.content_wrap.alphaTo(1,200);
//			this.content_wrap.setVisible(true);
			this.content_wrap.setHeight(m);
			this.content_wrap.setY(h-m-130);
		}
		this.content_text.style.height = ((m-70) > 0?(m-70):0)+"px";

		$super();
	}
});




var ProjectSlideShow = Class.create(Container,
{
	initialize: function($super, parent)
	{
		$super(parent);
		this.setBackgroundVisible(true);

		this.ss = new PSlideShow(this);
		this.ss.addEventListener("FIRST_SLIDE_LOADED",function(){project_slide_show_show_info()});
		this.setFullBleed();

		this.arrows = new Container(this);

		this.arrow_right = new ImageResource(this.arrows);
		this.arrow_right.setSrc("/static/images/arrow_right_large.png");
		this.arrow_right.alignX(Guide.RIGHT,-10);
		this.arrow_right.alignY(Guide.CENTER);
		this.arrow_right.setAlpha(0);
		this.arrow_right.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

		this.arrow_right_over = new Component(this.arrows);
		this.arrow_right_over.setBackgroundVisible(true);
		this.arrow_right_over.setBackgroundColor(0xff00ff);
		this.arrow_right_over.setAlpha(0);
		this.arrow_right_over.alignX(Guide.RIGHT);
		this.arrow_right_over.setWidth(200);
		this.arrow_right_over.addEventListener(ComponentEvent.CLICK, this.do_next.bind(this));
		this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_next.bind(this));
		this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_next.bind(this));

		this.arrow_left = new ImageResource(this.arrows);
		this.arrow_left.setSrc("/static/images/arrow_left_large.png");
		this.arrow_left.setX(10);
		this.arrow_left.alignY(Guide.CENTER);
		this.arrow_left.setAlpha(0);
		this.arrow_left.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

		this.arrow_left_over = new Component(this.arrows);
		this.arrow_left_over.setBackgroundVisible(true);
		this.arrow_left_over.setBackgroundColor(0xff00ff);
		this.arrow_left_over.setAlpha(0);
		this.arrow_left_over.setWidth(200);
		this.arrow_left_over.addEventListener(ComponentEvent.CLICK, this.do_prev.bind(this));
		this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_prev.bind(this));
		this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_prev.bind(this));

		this.ss._decorator.zIndex = -1;
		this.arrows._decorator.zIndex = 999999;

		if (IS_IOS)
		{
			var SS = this.ss;
			add_touch_listeners(this._decorator.element,
					function(x,y,dx,dy)
					{
						var p = SS.getPage();
						p += dx/(SS.getApplication().getWidth());
						if (p>SS._slides._children.length-1) p = SS._slides._children.length-1;
						if (p<0) p = 0;
						SS.setPage(p);
					});
		}

	},

	setValue: function(slides)
	{
		this.ss.setValue(slides);
	},

	setFullBleed: function()
	{
		this.ss.setPage(0);
		this.ss.render();
		this.ss.setHeightPercent(100);
		this.ss.setY(0);
		this.ss.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_MASK_FULL_BLEED);
		//this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_SIMPLE_FADE);
		this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_HORIZONTAL);
		this.setBackgroundColor(0x666666);
	},

	setUncropped: function()
	{
		this.ss.setPage(0);
		this.ss.render();
		this.ss.setHeight(525);
		this.ss.setY(139);
		this.ss.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_FIT_HEIGHT);
		this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_HORIZONTAL);
		this.setBackgroundColor(0xffffff);
	},

	render: function($super)
	{
		if (this.ss.getImageScalingType()==ImageDecorator.IMAGE_SCALING_VALUE_FIT_HEIGHT)
		{
			this.ss._slides.setXoffset((this.getWidth()-PAGE_WIDTH)*.5);
		}
		else
		{
			this.ss._slides.setXoffset(0);
		}

		$super();
	},

	slides: function()
	{
		return this.ss.getSlides();
	},

	at: function()
	{
		return this.ss.getCurrentIndex();
	},


	on_load: function()
	{
		this.render();
	},

	over_next: function()
	{
		//if (this.at()!=this.slides().length-1)
			this.arrow_right.alphaTo(1);
	},

	out_next: function()
	{
		this.arrow_right.alphaTo(0);
	},

	over_prev: function()
	{
		if (this.at()!=0)
			this.arrow_left.alphaTo(1);
	},

	out_prev: function()
	{
		this.arrow_left.alphaTo(0);
	},

	do_next: function()
	{
		this.next();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	next: function()
	{
		if (this.at()==this.slides().length-1)
			this.ss.setPage(0);
		else
			this.ss.setPage(this.ss.getPage()+1);
	},

	do_prev: function()
	{
		this.prev();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	prev: function()
	{
		this.ss.setPage(this.ss.getPage()-1);
		if (this.at()==0)
			this.arrow_left.alphaTo(0);
	}
});


var ProjectNav = Class.create(Container,
{
	initialize: function($super, parent, project_slideshow, data, project_info, main_nav, pdf, fullbleed)
	{
		$super(parent);

		this.data = data;
		this.slideshow = project_slideshow;
		this.info = project_info;
		this.main_nav = main_nav;

		this.bg = new Component(this);
		this.bg.setBackgroundVisible(true);
		this.bg.setBackgroundColor(0x0);
		this.bg.setAlpha(.5);

		this.c = new Container(this);
		this.c.setLayout(new FlowLayout(FlowLayout.LEFT_TO_RIGHT));
		this.c._layout._margin.right = 20;
		this.c.setX(-18);
		this.c.setY(9);
		this.c.setWidthDelta(-60);

		this.pager = new PPager(this.c, data.length);
		this.pager.addEventListener("next", this.do_next.bind(this));
		this.pager.addEventListener("prev", this.do_prev.bind(this));

		this.cropped = new TButton(this.c, "View Images Uncropped", "View Images Full Bleed");
		this.cropped.label.setY(8);
		this.cropped.setWidth(170);
		this.cropped.addEventListener(ComponentEvent.CLICK, this.do_cropped.bind(this));
		if (fullbleed!="true")
			this.cropped.toggle();

		this.show_info = new TButton(this.c,  "Hide Info",  "Show Info");
		this.show_info.label.setY(8);
		this.show_info.setWidth(88);
		this.show_info.addEventListener(ComponentEvent.CLICK, this.do_info.bind(this));

		if (pdf != null)
		{
			this.download = new TButton(this.c, "<a href='"+pdf+"' target='_blank'>Download Project PDF</a>");
			this.download.label.setY(8);
			this.download.setWidth(170);
			//this.download.addEventListener(ComponentEvent.CLICK, this.do_download.bind(this));//handled by the <a>...
		}

		this.related = new TButton(this.c, "Return to Search");
		this.related.label.setY(8);
		this.related.setWidth(140);
		this.related.addEventListener(ComponentEvent.CLICK, this.do_related.bind(this));

	},

	slides: function()
	{
		return this.data;
	},

	at: function()
	{
		return this.slideshow.swf().at();
	},

	do_next: function()
	{
		this.slideshow.swf().next();
		this.slideshow.render();
		this.update();
		this.render();

	},

	do_prev: function()
	{
		this.slideshow.swf().prev();
		this.slideshow.render();
		this.update();
		this.render();
	},

	do_cropped: function()
	{
		this.cropped.toggle();
		this.cropped.render();
		if (!this.cropped.getValue())
		{
			$("top_bar_site_search_input").addClassName("on_white");
			$("footer").removeClassName("shadowed");
			this.slideshow.swf().setUncropped();
		}
		else
		{
			$("top_bar_site_search_input").removeClassName("on_white");
			$("footer").addClassName("shadowed");
			this.slideshow.swf().setFullBleed();
		}
		this.slideshow.render();


		this.getApplication().setSharedObject("full_bleed_images",this.cropped.getValue());
	},

	do_info: function()
	{
		this.show_info.toggle();
		this.show_info.render();
		this.update_info();
		this.render();
	},

	do_download: function()
	{
		this.dispatchComponentEvent("download", this);
	},

	do_related: function()
	{
		this.dispatchComponentEvent("related", this);
	},

	update: function()
	{
		this.pager.setValue( this.at()+1 , this.slides().length );
		this.pager.render();

		if (this.at()==0)
		{
			if (!this.show_info.getValue())
				this.show_info.toggle();
		}
		else
		{
			if (this.show_info.getValue())
				this.show_info.toggle();
		}
		this.show_info.render();
		this.update_info();
	},

	update_info: function()
	{
		var b = this.show_info.getValue();
//		this.main_nav.alphaTo(b ? 1 : 0, 500);
//		this.info.alphaTo(b ? 1 : 0, 500);
		Logger.log("SHOW INFOA?="+b);
		this.main_nav.setVisible(b);
		this.main_nav.render();
		this.info.setVisible(b);
		this.info.render();

	}

});

var PSlideShow = Class.create(SlideShow,
{
	initialize: function($super, parent, widths, heights)
	{
		$super(parent);
		this.setLinearLoad(true);
		this.widths = widths ? widths : [32,1500];
		this.heights = heights ? heights : [32,1000];
	},

	setValue: function($super, a)
	{
		$super(a);
	},

	cell_renderer: function($super, p, data)
	{
		var p = new ImageResource(p);
		p.setUserObject(data);
		p.setPreviewWidth(this.widths[1]);
		p.setPreviewHeight(this.heights[1]);
		p.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load_slide.bind(this));
		p.setImageScalingType(this._image_scaling_type);
	},

	load_cell_handler: function($super, cell, data)
	{
		if (cell==null)
			throw new Error("!")
		cell.setResource(data);
		this.render();
	},

	render: function($super)
	{
		for (var i=0; i<this._slides._children.length; i++)
		{
			var p = this._slides._children[i];
			p.setImageScalingType(this._image_scaling_type);
		}
		$super();
	}
});

var PPager = Class.create(Container,
{
	initialize: function($super,parent,max)
	{
		$super(parent);

		this.arrow_left = new ImageResource(this);
		this.arrow_left.setSrc("/static/images/arrow_left_small.png");
		this.arrow_left.addEventListener(ComponentEvent.CLICK, this.translateEvent.bind(this,"prev"));

		this.pager_bg = new ImageResource(this);
		this.pager_bg.setSrc("/static/images/project_pager_bg.png");
		this.pager_bg.setX(18);
		this.pager_bg.addEventListener(ComponentEvent.CLICK, this.translateEvent.bind(this,"next"));

		this.arrow_right = new ImageResource(this);
		this.arrow_right.setSrc("/static/images/arrow_right_small.png");
		this.arrow_right.setX(105);
		this.arrow_right.addEventListener(ComponentEvent.CLICK, this.translateEvent.bind(this,"next"));

		this.pager_text = new Label(this);
		this.pager_text.setX(26);
		this.pager_text.setY(4);
		this.pager_text.setWidth(50);
		this.pager_text.setFontStyle("project_page_of_total");
		this.pager_text.addEventListener(ComponentEvent.CLICK, this.translateEvent.bind(this,"next"));
		this.setValue(1,max);
		//this.pager_text.addEventListener(ComponentEvent.CLICK, this.do_next.bind(this));
	},

	setValue: function(a,b)
	{
		this.pager_text.setText( "<b>"+a+"</b> / "+b );
		this.arrow_left.setAlpha( (a==1) ? 0 : 1 );

	}
});



var TButton = Class.create(Container,
{
	initialize: function($super, parent, t0, t1)
	{
		$super(parent);
		this.t0 = t0;
		this.t1 = t1;
		this.label = new Label(this);
		this.label.setText(t0);
		this.label.setWidthPercent(100);
		this.label.setFontStyle("swa_link");
		this.value = true;
	},

	getValue: function()
	{
		return this.value;
	},

	toggle: function()
	{
		this.value = !this.value;
		if (this.value)
			this.label.setText(this.t0);
		else
			this.label.setText(this.t1);
	}
});


var DOMComponent = Class.create(Component,
{
	initialize: function($super, parent, target)
	{
		$super(parent);
		var t = $(target);
		this._decorator.mid.insert(t);
		this.setHeight( this._decorator.mid.getHeight() );
	}
});

var SWFINC = 1;
var SWFComponent = Class.create(Component,
{
	initialize: function($super, parent, swf, props)
	{
		$super(parent);
		var id = "___r"+(SWFINC++);
		this._decorator.mid.id = id;
		this._decorator.bg.remove();
		do_swf(id, swf, props, this.swfok.bind(this));//?
	},

	swfok: function(e)
	{
		Logger.log(e);
		this.swf = e.ref;
	},

	swf: function()
	{
		return get_swf(this._decorator.mid.id);
	}
});

//////////////-----------------//////////////-------------------//////////////------------------
//-----------------//////////////-------------------//////////////------------------//////////////
//////-----------------//////////////-------------------//////////////------------------////////
//-----------------//////////////-------------------//////////////------------------//////////////



function swa_init_advocacy()
{
	var ar = get_advocacy_results("advocacy_topic_summary");
	ar.sort(function(a,b){return Math.random()<.5 ? -1 : 1});

	$("planning_show").innerHTML = "";

	var ss_app = new Application("planning_show");
	ss_app.setWidth(PAGE_WIDTH);
	ss_app.setHeight(314);
	var ss = new AdvocacySlideShow(ss_app);
	ss.setValue(ar);
	ss_app.render();

}






var AdvocacySlideShow = Class.create(Container,
{
	initialize: function($super, parent)
	{
		$super(parent);
		this.setDecorator(new com_pagesociety_ux_decorator_MaskedDecorator());

		this.setBackgroundVisible(true);
		this.setBackgroundColor(0x666666);

		this.ss = new ASlideShow(this);
		this.ss.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_MASK_FULL_BLEED);
		//this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_SIMPLE_FADE);
		this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_HORIZONTAL);
		this.ss.addEventListener(ComponentEvent.CLICK, this.do_read.bind(this));

//		this.arrows = new Container(this);
//
//		this.arrow_right = new ImageResource(this.arrows);
//		this.arrow_right.setSrc("/static/images/arrow_right_medium.png");
//		this.arrow_right.alignX(Guide.RIGHT);
//		this.arrow_right.alignY(Guide.CENTER);
//		this.arrow_right.setAlpha(0);
//		this.arrow_right.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));
//
//		this.arrow_right_over = new Component(this.arrows);
//		this.arrow_right_over.setBackgroundVisible(true);
//		this.arrow_right_over.setBackgroundColor(0xff00ff);
//		this.arrow_right_over.setAlpha(0);
//		this.arrow_right_over.alignX(Guide.RIGHT);
//		this.arrow_right_over.setWidth(200);
//		this.arrow_right_over.addEventListener(ComponentEvent.CLICK, this.do_next.bind(this));
//		this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_next.bind(this));
//		this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_next.bind(this));
//
//		this.arrow_left = new ImageResource(this.arrows);
//		this.arrow_left.setSrc("/static/images/arrow_left_medium.png");
//		this.arrow_left.alignY(Guide.CENTER);
//		this.arrow_left.setAlpha(0);
//		this.arrow_left.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));
//
//		this.arrow_left_over = new Component(this.arrows);
//		this.arrow_left_over.setBackgroundVisible(true);
//		this.arrow_left_over.setBackgroundColor(0xff00ff);
//		this.arrow_left_over.setAlpha(0);
//		this.arrow_left_over.setWidth(200);
//		this.arrow_left_over.addEventListener(ComponentEvent.CLICK, this.do_prev.bind(this));
//		this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_prev.bind(this));
//		this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_prev.bind(this));

		this.read = new Label(this);
		this.read.setText("READ ARTICLES")
		this.read.setFontStyle("blue");
		this.read.setWidth(120);
		this.read.setX(20);
		this.read.setY(240);
		this.read.addEventListener(ComponentEvent.CLICK, this.do_read.bind(this));

		this.more = new Label(this);
		this.more.setText("MORE ADVOCACY TOPICS")
		this.more.setFontStyle("blue");
		this.more.setWidth(200);
		this.more.setX(160);
		this.more.setY(240);
		this.more.addEventListener(ComponentEvent.CLICK, this.do_next.bind(this));

	},

	setValue: function(slides)
	{
		this.value = slides;
		this.ss.setValue(slides);
	},

	do_read: function()
	{
		location.href = (this.slides()[this.at()].data.href);
	},

	slides: function()
	{
		return this.ss.getSlides();
	},

	at: function()
	{
		return this.ss.getCurrentIndex();
	},


	on_load: function()
	{
		this.render();
	},

	over_next: function()
	{
		//if (this.at()!=this.slides().length-1)
			this.arrow_right.alphaTo(1);
	},

	out_next: function()
	{
		this.arrow_right.alphaTo(0);
	},

	over_prev: function()
	{
		if (this.at()!=0)
			this.arrow_left.alphaTo(1);
	},

	out_prev: function()
	{
		this.arrow_left.alphaTo(0);
	},

	do_next: function()
	{
		this.next();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	next: function()
	{
		if (this.at()==this.slides().length-1)
			this.ss.setPage(0);
		else
			this.ss.setPage(this.ss.getPage()+1);
	},

	do_prev: function()
	{
		this.prev();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	prev: function()
	{
		this.ss.setPage(this.ss.getPage()-1);
		if (this.at()==0)
			this.arrow_left.alphaTo(0);
	}
});


var ASlideShow = Class.create(SlideShow,
		{
			initialize: function($super, parent)
			{
				$super(parent);
				this.setLinearLoad(true);
			},

			setValue: function($super, a)
			{
				$super(a);
				this.setPlaying(true);
			},

			cell_renderer: function($super, p, data)
			{
				var c = new Container(p);
				c.data = data;

				c.img = new PImageResource(c);
				c.img.setPreviewWidths([1500]);
				c.img.setPreviewHeights([1000]);
				c.img.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load_slide.bind(this));

				c.fgwash = new Container(c);
				c.fgwash.setBackgroundVisible(true);
				c.fgwash.setBackgroundColor(0xffffff);
				c.fgwash.setAlpha(.7);
				c.fgwash.setHeight(129);
				c.fgwash.setY(129);

				c.fg = new Container(c);
				c.fg.setY(129);

				c.title = new Label(c.fg);
				c.title.setX(20);
				c.title.setY(4);
				c.title.setWidthDelta(-150);

			},

			load_cell_handler: function($super, cell, data)
			{
				cell.img.setResource(data.attributes.resource);

				var t = "<span class='advocacy_title'>"+data.title+"</span><br/>";
				if (data.intro!=null && data.intro!="")
					t += "<span class='advocacy_subtitle'>"+data.intro+"</span><br/>";
				if (data.author!=null && data.author!="")
					t += "<span class='advocacy_author'>by "+data.author+"</span><br/>";
				cell.title.setText(t);
				this.render();
			},

			render: function($super)
			{
				for (var i=0; i<this._slides._children.length; i++)
				{
					var p = this._slides._children[i];
					p.img.setImageScalingType(this._image_scaling_type);
				}
				$super();
			}
		});







function swa_init_advocacy_topic(url)
{
	swa_init_search_controls(url,
		 	["advocacy_topic_search_category","advocacy_topic_search_tag","advocacy_topic_search_month_year","advocacy_topic_search_freetext"]);

	if (IS_IOS)
		return;

	swa_prep_adv_selector_data();
	$("advocacy_topic_search_bar_wrapper").innerHTML = "";
	app = new Application("advocacy_topic_search_bar_wrapper");
	app.setWidth(PAGE_WIDTH);
	app.setHeight(40);

	projects = new SWFComponent(app, "/static/AdvocacySelector.swf",
			{
				selectors: "swa_get_adv_selectors",
				page_url: url,
				moduleRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL
			});
	projects.setHeight(30);
	projects._decorator.element.style.zIndex = 10;
	projects._decorator.mid.style.zIndex = 10;

//	var d = new DOMComponent(app,"col_left");
//	d.setHeight(800);
//	d.setY(30);

	app.render();

}

function swa_prep_adv_selector_data()
{
	var cat_keys = get_string_array("advocacy_topic_search_category");
	var cat_selection = $("advocacy_topic_search_category").selectedIndex;
	var tag_keys = get_string_array("advocacy_topic_search_tag");
	var tag_selection = $("advocacy_topic_search_tag").selectedIndex;
	var date_keys = get_string_array("advocacy_topic_search_month_year");
	var date_selection = $("advocacy_topic_search_month_year").selectedIndex;
	f1 = { categories: cat_keys, categories_idx: cat_selection, tags: tag_keys, tags_idx: tag_selection, dates: date_keys, dates_idx: date_selection };
}
function swa_get_adv_selectors() { return f1; }

function swa_adv_topic_menu_display(b)
{
  projects.setHeight(b?500:40);
  projects.render();
}

//////////////-----------------//////////////-------------------//////////////------------------
//-----------------//////////////-------------------//////////////------------------//////////////
//////-----------------//////////////-------------------//////////////------------------////////
//-----------------//////////////-------------------//////////////------------------//////////////
function init_full_bleed_image(id)
{
	var d = $(id).getElementsByTagName("IMG");
	if (d.length==0)
		return;
	var img = d[0];
	$(id).innerHTML = "";
	var app = new Application(id);
	app.setWidth(220);
	app.setHeight(160);
	var ir = new ImageResource(app);
	ir.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_MASK_FULL_BLEED);
	//img.addEventListener(ResourceEvent.LOAD_RESOURCE, var on_load.bind(this));
	//img.setPreviewWidth(500);
	//img.setPreviewHeight(500);
	//img.setResource(resource);
	ir.setSrc(img.src);
	app.render();
	return ir;
}



function make_slide_show(id,width,height,show_info,preview_widths,preview_heights,autoplay,img_scale_type)
{
	var slides = get_slide_data(id);
	if (slides.length==0)
		return;
	var dataFuncName = "get_slide_data_"+id;
	window[dataFuncName] = function() { return slides; };
	$(id).innerHTML = "";
	$(id).setStyle({position:'relative',width:width,height:height});
	var ss_app = new Application(id);
	ss_app.setWidth(width);
	ss_app.setHeight(height);
	var ss = new SWASlideShow(ss_app,show_info,preview_widths,preview_heights,dataFuncName,autoplay,img_scale_type);
	ss.setValue(slides);
	ss_app.render();
	return ss;
}

var SWASlideShow = Class.create(Container,
{
	initialize: function($super, parent, show_info,preview_widths,preview_heights,dataFuncName,autoplay,img_scale_type)
	{
		$super(parent);

		this.show_info = show_info != null ? show_info : true;
		this.preview_widths = preview_widths ? preview_widths : [32,500];
		this.preview_heights = preview_heights ? preview_heights : [32,500];
		if (img_scale_type==null)
			img_scale_type = -1;


		if (IS_IOS)
		{
			this.ss = new PSlideShow(this, this.preview_widths, this.preview_heights);
			this.ss.setImageScalingType(ImageDecorator.IMAGE_SCALING_VALUE_MASK_FULL_BLEED);
			this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_SIMPLE_FADE);
			//this.ss.setPageContainerType(com_pagesociety_ux_component_container_PageContainer.TYPE_HORIZONTAL);
			this.ss.setBackgroundVisible(true);
			this.ss.setBackgroundColor(0x666666);
		}
		else
		{
			var onChangeFuncName = "get_slide_data_"+this._parent.getId();
			var T = this;
			window[onChangeFuncName] = function()
			{
				var a = T.at();
				if(T.timer)
					T.timer.stop();
				T.setPage(a);
				T.render();
			};
			this.ss = new SWFComponent(this, "/static/SWAProject.swf",
				{
					dataProvider: dataFuncName,
					onChange: onChangeFuncName,
					imgLeft: "/static/images/arrow_left_medium.png",
					imgRight: "/static/images/arrow_right_medium.png",
					imgOffset: 0,
					fullBleedImages: true,
					imgScaleType: img_scale_type,
					pageType: 1,
					moduleRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL,
					resourceRootUrl: com_pagesociety_web_ModuleRequest.SERVICE_URL+"/"
				});
		}

		if (this.show_info)
		{
			this.captionc = new Container(this);
			this.captionc.setHeight(14);
			this.captionc.setBackgroundVisible(true);
			this.captionc.setBackgroundColor(0xE5E2DF);
			this.captionc.alignY(Guide.BOTTOM);

			this.slide_label = new Label(this.captionc);
			this.slide_label.setText(".");
			this.slide_label.setWidthPercent(90);
			this.slide_label.setY(-2);

			this.pager = new SSSPager(this.captionc);
			this.pager.alignX(Guide.RIGHT);
			this.pager.addEventListener("goto_page", this.on_goto_page.bind(this));
		}

		if (IS_IOS)
		{
			this.arrows = new Container(this);

			this.arrow_right = new ImageResource(this.arrows);
			this.arrow_right.setSrc("/static/images/arrow_right_medium.png");
			this.arrow_right.alignX(Guide.RIGHT, 1);
			this.arrow_right.alignY(Guide.CENTER);
			this.arrow_right.setAlpha(0);
			this.arrow_right.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

			this.arrow_right_over = new Component(this.arrows);
			this.arrow_right_over.setBackgroundVisible(true);
			this.arrow_right_over.setBackgroundColor(0xff00ff);
			this.arrow_right_over.setAlpha(0);
			this.arrow_right_over.alignX(Guide.RIGHT);
			this.arrow_right_over.setWidth(100);
			this.arrow_right_over.addEventListener(ComponentEvent.CLICK, this.do_next.bind(this));
			this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_next.bind(this));
			this.arrow_right_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_next.bind(this));
			this.arrow_right_over._decorator.element.style.cursor = "pointer";

			this.arrow_left = new ImageResource(this.arrows);
			this.arrow_left.setSrc("/static/images/arrow_left_medium.png");
			this.arrow_left.setX(-1);
			this.arrow_left.alignY(Guide.CENTER);
			this.arrow_left.setAlpha(0);
			this.arrow_left.addEventListener(ResourceEvent.LOAD_RESOURCE, this.on_load.bind(this));

			this.arrow_left_over = new Component(this.arrows);
			this.arrow_left_over.setBackgroundVisible(true);
			this.arrow_left_over.setBackgroundColor(0xff00ff);
			this.arrow_left_over.setAlpha(0);
			this.arrow_left_over.setVisible(false);
			this.arrow_left_over.setWidth(100);
			this.arrow_left_over.addEventListener(ComponentEvent.CLICK, this.do_prev.bind(this));
			this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OVER, this.over_prev.bind(this));
			this.arrow_left_over.addEventListener(ComponentEvent.MOUSE_OUT, this.out_prev.bind(this));
			this.arrow_left_over._decorator.element.style.cursor = "pointer";
		}

		if (autoplay==null || autoplay)
		{
		this.timer = new Timer(Math.random()*3000+4500);
		this.timer.addEventListener(TimerEvent.TIMER, this.on_tick.bind(this));
		this.timer.start();
		}

	},

	on_tick: function()
	{
		this.next();
		this.render();
	},

	setValue: function(slides)
	{
		this.slides = slides;
		var s0 = [];
		for (var i=0; i<slides.length; i++)
			s0.push(slides[i].attributes.resource);
		if (IS_IOS)
			this.ss.setValue(s0);
		this.setPage(0);
	},

	setPage: function(p)
	{
		if (p==null) p =0;


		if (IS_IOS)
			this.ss.setPage(p);
		if (this.show_info)
		{
			this.pager.setValue(p,this.slides.length);
			if (IS_IOS && this.at()==0)
				this.arrow_left.alphaTo(0);
			var c = "&nbsp;";
			if (this.slides[p].title!=null)
				c+="<span class=\"slideshow_caption\">"+this.slides[p].title+"</span>";
			else
				c+="<span class=\"slideshow_caption\">&nbsp;</span>";
			var cap = this.slides[p].caption;
			if (cap!=null)
			{
				//if (cap.length<35)
				//	c+="<span class=\"slideshow_caption\"> - "+cap+"</span>";
				//else
					c+="<br/><div class='slideshow_small_caption'>"+cap+"</div>";
			}
			this.slide_label.setText(c);
		}
		if (IS_IOS)
			this.arrow_left_over.setVisible(p!=0);
	},


	render: function($super)
	{
		if (this.show_info)
		{
			var h = this.getHeight()-25;
			if (IS_IOS)
				this.arrows.setHeight( h )
			this.ss.setHeight( h );
		}
		$super();
	},

	rslides: function()
	{
		return this.ss.getSlides();
	},

	at: function()
	{
		if (IS_IOS)
			return this.ss.getCurrentIndex();
		else
			return this.ss.swf().at();
	},


	on_load: function()
	{
		this.render();
	},

	over_next: function()
	{
		this.arrow_right.alphaTo(1);
	},

	out_next: function()
	{
		this.arrow_right.alphaTo(0);
	},

	over_prev: function()
	{
		if (this.at()!=0)
		{
			this.arrow_left.alphaTo(1);
			this.arrow_left_over.setVisible(true);
		}
	},

	out_prev: function()
	{
		this.arrow_left.alphaTo(0);
		this.arrow_left_over.setVisible(false);
	},


	on_goto_page: function(e)
	{
		if(this.timer)
			this.timer.stop();
		if (!IS_IOS)
			this.ss.swf().setPage(e.data);
		this.setPage(e.data);
		this.render();
	},

	do_next: function()
	{
		if(this.timer)
			this.timer.stop();
		this.next();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	next: function()
	{
		var a = this.at()+1;
		if (this.at()==this.slides.length-1)
			a = 0;
		this.setPage(a);
		if (!IS_IOS)
			this.ss.swf().setPage(a);
	},

	do_prev: function()
	{
		this.timer.stop();
		this.prev();
		this.render();
		this.dispatchComponentEvent(ComponentEvent.CHANGE_VALUE,this);
	},

	prev: function()
	{
		this.setPage(this.at()-1);
	}
});



var SSSPager = Class.create(Container,
		{
			initialize: function($super,parent)
			{
				$super(parent);

				this.pager_text = new Label(this);
				this.pager_text.setWidth(40);
				this.pager_text.setFontStyle("slideshow_caption");
				this.pager_text.setY(-2);

				this.pages = new Container(this);
				this.pages.setLayout(new FlowLayout(FlowLayout.LEFT_TO_RIGHT));
				this.pages.getLayout().getMargin().left = 3;
				this.pages.setX(40);
			},

			setValue: function(a,b)
			{
				this.pager_text.setText((a+1)+" / "+b );
				this.pages.clear();
				for (var i=0; i<b; i++)
				{
					var c = new Component(this.pages);
					c.setBackgroundVisible(true);
					c.setBackgroundColor(i != a ? 0xA3ACAA : 0x63B9AB);
					c.setWidth(5);
					c.setUserObject(i);
					c._decorator.element.style.cursor = "pointer";
					c.addEventListener(ComponentEvent.CLICK, this.on_click_page.bind(this));
				}

			},

			on_click_page: function(e)
			{
				var page = e.component.getUserObject();
				this.dispatchComponentEvent("goto_page",this,page);
			},

			getWidth: function($super)
			{
				return this.pages.getX()+this.pages.getLayout().calculateWidth();
			}
		});




/////positioning util
function get_yscroll()
{
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	//var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	return dsoctop;
}


/////////////////////////////////////////////////////////////////
//utilities for converting html into objects

function get_project_results(id)
{
	var projects = document.getElementsByClassName(id);
	var slides = [];
	for (var i=0; i<projects.length; i++)
	{
		var c 					= projects[i];
		var slide_data			= {};
		slide_data.title 		= get_class_inner_text(c, "project_result_title");
		slide_data.subtitle 	= get_class_inner_text(c, "project_result_subtitle");
		slide_data.description 	= get_class_inner_text(c, "project_result_description");
		slide_data.attributes	= { resource: get_resource_data(c) };
		var a = c.getElementsByTagName("a")[0];
		slide_data.href = a.getAttribute("href");
		slides.push(slide_data);
	}
	return slides;
}


function get_advocacy_results(id)
{
	var topics = document.getElementsByClassName(id);
	var slides = [];
	for (var i=0; i<topics.length; i++)
	{
		var c 					= topics[i];
		var slide_data			= {};
		slide_data.title 		= get_class_inner_text(c, "title");
		slide_data.author 		= get_class_inner_text(c, "author");
		slide_data.intro 		= get_class_inner_text(c, "intro");
		slide_data.attributes	= { resource: get_resource_data(c) };
		var a = c.getElementsByTagName("a")[0];
		slide_data.href = a.getAttribute("href");
		slides.push(slide_data);
	}
	return slides;
}

function get_slide_data(id)
{
	var slides = [];
	var lis = $(id).getElementsByTagName("li");
	for (var i=0; i<lis.length; i++)
	{
		var c 					= lis[i];
		var slide_data			= {};
		slide_data.title 		= get_class_inner_text(c, "swa_slide_title");
		slide_data.caption 		= get_class_inner_text(c, "swa_slide_caption");
		slide_data.credit 		= get_class_inner_text(c, "swa_slide_credit");
		slide_data.description 	= get_class_inner_text(c, "swa_slide_description");
		slide_data.attributes	= { resource: get_resource_data(c) };
		var wrapper = c.parentNode.parentNode.parentNode;
		if (wrapper.className == "featured_project_wrapper")
		{
			var a = wrapper.getElementsByTagName("a")[0];
			slide_data.href = a.getAttribute("href");
			slide_data.title = get_class_inner_text(wrapper, "swa_slide_title");
		}
		slides.push(slide_data);
	}
	return slides;
}

function get_class_inner_text(div,class_name)
{
	var d = $(div);
	if (d==null)
		return null;
	var c = d.getElementsByClassName(class_name);
	if (c.length==0)
		return null;
	var v = com_pagesociety_util_StringUtil.trim( $(c[0]).getInnerText().stripTags().strip() );
	return v;
}

var IMG_TAG = "PSIMG";

function get_resource_data(div)
{
	var res;
	if (div==null)
		return;
	if (div.className==IMG_TAG)
		res = div;
	else
		res = div.getElementsByTagName(IMG_TAG)[0];

	if (res==null)
	{
		return;//project might not have any images//
		alert("VIDEO NOT YET SUPPORTED");
	}
	var o ={
			id: 			Number(res.getAttribute("ps:id")),
			type:			res.getAttribute("ps:type"),
			attributes:
			{
				'simple-type':	res.getAttribute("ps:simple-type"),
				'path-token': 	res.getAttribute("ps:path-token")
			},
			src:			res.getAttribute("src")
		};
	return new com_pagesociety_persistence_Entity(o);
}

function get_string_array(el)
{
	el = $(el);

	var e = [];
	for (var i=0; i<el.childNodes.length; i++)
	{
		var c = el.childNodes[i];

		if(c.nodeType != 1 )
			continue;

		e.push( com_pagesociety_util_StringUtil.trim( $(c).getInnerText() ));
	}
	return e;
}

//function entity(el)
//{
//	var e = { type: el.className, id: el.id };
//	for (var i=0; i<el.childNodes.length; i++)
//	{
//		var c = el.childNodes[i];
//		if (c.className!=null)
//			e[c.className] = get_value(c);
//	}
//	return e;
//}

function get_value(el)
{
	if (el.nodeName==IMG_TAG)
		return el.src;
	else
		return com_pagesociety_util_StringUtil.trim( el.textContent.stripTags().strip() );
}


function swa_navigate(url)
{
  document.location.href = url;
}


// swf communication

function do_swf(id, swf, props, callback)
{
	if (callback==null)
		callback = function(e){ Logger.log(e); }
	var u = com_pagesociety_web_ModuleRequest.SERVICE_URL+"/";
	var props = props ? props : {};
	props.swf = swf;
	props.moduleRootUrl = u;
	props.resourceRootUrl = u;
	write_swf(
		id, "100%", "100%",
		props,
		{ allowScriptAccess: "always", allowFullScreen: "true", wmode: "transparent" },
		{ id: id },
		callback
	);
}
function write_swf(id, width, height, swf_vars, swf_params, swf_attributes, callback)
{
	swfobject.embedSWF("/static/Bootstrap.swf", id, width, height, "9.0.0",
			"/static/js/swfobject/expressInstall.swf", swf_vars, swf_params, swf_attributes, callback);
}

function get_swf(id)
{
	return swfobject.getObjectById(id);
}


// input elements util

function init_input_element(id)
{
	var s_input = $(id);
	s_input.onfocus = function()
	{
		var v = com_pagesociety_util_StringUtil.trim(this.value);
		if(v == this.defaultValue)
			this.value = '';
	}

	s_input.onblur = function()
	{
		var v = com_pagesociety_util_StringUtil.trim(this.value);
		if(v == '')
			this.value = this.defaultValue;
	}
}






var last_touch_x;
var last_touch_y;
function add_touch_listeners(el,touch_callback,gesture_callback)
{
	el.ontouchstart = function(e)
	{
		last_touch_x = e.touches[0].pageX;
		last_touch_y = e.touches[0].pageY;
	}
	el.ontouchmove = function(e)
	{
		if(e.touches.length == 1)
		{
			var touchx = e.touches[0].pageX;
			var dx = last_touch_x - touchx;
			var touchy = e.touches[0].pageY;
			var dy = last_touch_y - touchy;
			touch_callback(touchx,touchy, dx, dy);
			last_touch_x = touchx;
			last_touch_y = touchy;
			e.preventDefault();
		}
	}

	el.ontouchend = function(e)
	{

	}
	el.ongesturechange = function(e)
	{
		if (gesture_callback==null)
			e.preventDefault();
		else
			gesture_callback(e.scale,e.rotation);
	}

}



//youtube video stuff
var _swa_video_callback;
function init_video(callback)
{
  //Load player api asynchronously.
  _swa_video_callback = callback;
  var tag = document.createElement('script');
  tag.src = "http://www.youtube.com/player_api";
   var firstScriptTag = document.getElementsByTagName('script')[0];
   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}

function onYouTubePlayerAPIReady() {
	_swa_video_callback()
}



function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function IS_IE()
{
	return (getInternetExplorerVersion() > 0);

}

