
var sectors =
{
	dir : "/Portals/0/Skins/SSCNv2/img/sectors/",
	img : 
	[
		"academic.png",
		"clinical-0.png",
		"patients-0.png",
		"investor-0.png",
		"commercial-0.png",
		"regulatory-0.png"
	],
	
	// caches all sector images
	cache: function()
	{
		var imgs = new Array();
		
		for (i in sectors.img)
		{
			img = new Image();
			img.src = sectors.dir + sectors.img[i];			
			imgs[i] = img;
		};
		
	},
	
	init: function()
	{
		var accordion = $jq("#sectors-menu");
		var index = $jq.cookie("sectors");
		var active;
		
		$jq("#sectors-image").append("<div style=\"height:490px;width:490px;\" id=\"sectors-image-main\"><div style=\"height:490px;width:490px;\" id=\"sectors-image-inner\"></div></div>");
		$jq("#sectors-image").css("background", "url('" + sectors.dir + "shadow.png') no-repeat top left");
		
		if (index !== null) 
		{
			active = accordion.find("h3:eq(" + index + ")");
			sectors.transition(index);
		} 
		else 
		{
			active = 0;
			sectors.transition(0);
		};
		
		accordion.accordion(
		{
			header: "h3",
			event: "focus mousemove",
			active: active,
			change: function(event, ui) 
			{
				var index = $jq(this).find("h3").index(ui.newHeader[0]);
				$jq.cookie("sectors", index, {path: "/"});				
			},
			changestart: function(event, ui)
			{
				var index = $jq(this).find("h3").index(ui.newHeader[0]);
				sectors.transition(index);
			},
			autoHeight: true
		});
		
		// cache rest of images
		setTimeout("sectors.cache();", 1300);
	},
	
	// transition to next circle image
	transition: function(index)
	{
		var si = $jq("#sectors-image-main");
		var sii = si.find("#sectors-image-inner");
		
		si.stop(true, true);
		sii.stop(true, true);
		
		sii.hide();
		sii.css("background", "url('" + sectors.dir + sectors.img[index] + "') no-repeat top left");
		
		sii.fadeIn(300, function()
		{
			si.css("background", sii.css("background"));
			sii.hide();			
		});
		
	}
	
};


var theme = 
{
	setSidebars : function() 
	{
		if (($jq(".pane-sidebar-left").html().length > 0) && ($jq(".pane-sidebar-right").html().length > 0))
		{
			$jq("body").addClass("two-sidebars");
		}
		else if ($jq(".pane-sidebar-left").html().length > 0)
		{
			$jq("body").addClass("sidebar-l");
		}
		else if ($jq(".pane-sidebar-right").html().length > 0)
		{
			$jq("body").addClass("sidebar-r");
		};
	},
	
	alignSubMenu : function(s)
	{
		$jq(s + " > ul > li").each(function()
		{			
			$jq(this).find(" > ul").css("margin-left", "-" + (200 - $jq(this).width()) + "px");
		});

		
	},
	
	sectors : function()
	{
		sectors.init();
	},
	
	browser : function()
	{
		var browser = "";
		
		if($jq.browser.mozilla)
		{
			browser = "firefox";
		}
		else if($jq.browser.webkit)
		{
			browser = "webkit";
		}
		else if($jq.browser.msie)
		{
			browser = "msie";
		}
		else if($jq.browser.opera)
		{
			browser = "opera";
		}
		
		$jq("body").addClass(browser);
		
	},
	
	scrollTo : function()
	{
		
		$jq("#content-outer a").each(function()
		{
			var link = $jq(this);
			
			if (link.attr("href"))
			{
				if (link.attr("href").substring(0, 1) === "#")
				{
					link.click(function()
					{
						//alert("a[name=" + link.attr("href").replace(/#/gi, "") + "]");
						var target = $("a[id=\"" + link.attr("href").replace(/#/gi, "") + "\"]");
						
						// scroll to target
						if (target.size() > 0)
						{
							$('html, body').animate(
							{
								scrollTop: target.offset().top
							}, 500);
						}
						// scroll to top
						else
						{
							$('html, body').animate(
							{
								scrollTop: 0
							}, 500);
						};
					});
				};
			};
		});
		
		
		//~ $("a:contains('Back to top'), a:contains('request a visit form')").click(function()
		//~ {
			//~ $('html, body').animate(
			//~ {
				//~ scrollTop: 0
			//~ }, 500);
		//~ }).attr("href", "javascript:void(0);");	
	},
	
	autoScroll : function()
	{
		if (window.location.hash === "#article")
		{
			setTimeout(function()
			{
				$jq("html, body").animate({scrollTop :  ($jq(".ArticleViewer").offset().top -70) + "px"}, {duration: 1350, easing: "easeOutBack"});
			},
			200);
		};
	},
	
	fixHeight : function()
	{
		
		//~ if ($jq("#page-wrap").height() < $jq(window).height())
		//~ {
			//~ //alert("a");
			//~ //$jq("#content-outer").height($jq(window).height() - ($jq("#header").height() + $jq("#footer").height()));
		//~ };
		
		
	}
	
};



// dependancy - should be replaced with jquery cookie plugin

var cookie = {
	set : function (c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
		";path=/";
	},
	get : function (c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1; 
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			} 
	  	}
		return "";
	}
};


var members = 
{
	search : function() 
	{
		window.location = "/MembersArea/Community/Members/MemberDirectory/tabid/155/Default.aspx?msterm=" + $jq("#MemberSearchBox input.textbox").val();
		return false;
	}
};


$jq(document).ready(function()
{
	theme.alignSubMenu("#menu-member-inner");
	theme.browser();
	theme.autoScroll();
	//theme.scrollTo();
});

