var Adviser2009Rebrand={
	// e.g. 'adviser', 'openwork', etc
	SubsiteName:'',

	ExploreAdviserDropDownVisible:false,
	AnnuityCalculatorDropDownVisible:false,

	quoteApplyDropDownVisible:false,

	/*OverrideLauncherLinks:function(){
		$('[href^="/adviser/launcher/"]').each( function(){
			this.click(function() {
				window.open(this.attr('href'));
				return false;
			});
		});
	},*/

	LoadDynamicStylesheets: function() {
		$("head").append('<link rel="stylesheet" type="text/css" href="/wcm/site/css/adviser-rebrand-2009/scriptedelements.css"></link>');
	},

	OverrideLauncherLinks:function(){
		$('[href^="/adviser/launcher/"]').each(
			function(index, item){
				var anchor = $(item);
				var href = anchor.attr("href");
				anchor.click(
					function() {
						window.open(href, 'Launcher', 'status=yes,resizable=0,toolbar=no,menubar=no,location=no,');
						return false;
					}
				);
			}
		);
	},

	PageLoad:function(height){
		// call back to server to get size required for iframe
		Adviser2009Rebrand.SetupExploreAdviserDropDown();
		Adviser2009Rebrand.SetupQuoteApplyDropDown();
		Adviser2009Rebrand.OverrideLauncherLinks();
		
		// reload page after 10 mins 
		var currentHref = window.location.href;
		//alert(currentHref);
		//var timeout = 1000 * 60 * 10;
		//setTimeout('window.location.href = "' + currentHref	+ '"', timeout);
	},

	SetupQuoteApplyDropDown: function() {
		var element = $("#quoteApply");
		if (element) {
			var sourceElementId = "quote-apply-container";
			var newHtml = "<div class=\"dropdown\" id=\"quote-apply-container\"><div onclick=\"Adviser2009Rebrand.ToggleQuoteApplyAdviserDropDown();return(false);\">Please Select...</div><ol>";
			element.children().each(
				function() {
					var option = $(this);
					var href = option.val();
					var text = option.text();
					var onclick = "" + option.attr('onclick');
					/* convert " to '  */
					onclick=onclick.replace(/\"/g, '\'');	
					var newonlick = onclick.substring(onclick.indexOf("{") + 1, onclick.indexOf("}"));
					/*alert(onclick);*/

					if(href){
						newHtml += "<li><a href=\"" + href + "\" onclick=\"" + newonlick + "\">" + text + "</a></li>";
					}
				}
			);

			$("#quote-apply-container").replaceWith (newHtml + "</ol></div>");
			
			var needsIEFix = ($.browser.msie && $.browser.version.substr(0,1)<=7);
			if(needsIEFix){
				var dropDown = $("#quote-apply-container ol");
				dropDown.css(
					{
						"height":"200px",
						"overflow-y":"scroll"
					}	
				)
			}
		}
	},

	SetupExploreAdviserDropDown:function(){
		var select = $("#exploreLV");

		if(select){
			// get current options..
			//Adviser2009Rebrand.ToggleExploreLVDropDown()
			//var newHtml = "<div id=\"exploreLVJS\"><div>Explore LV.com</div><ol>";
			var newHtml = "<div id=\"exploreLVJS\"><div onclick=\"Adviser2009Rebrand.ToggleExploreAdviserDropDown();return(false);\">Explore LV= Adviser</div><ol>";
			select.children().each(
				function(){
					var option = $(this);
					var href = option.val();
					var text = option.text();
					var onclick = "" + option.attr('onclick');
					onclick=onclick.replace(/\"/g,'\'');
					var newonlick = onclick.substring(onclick.indexOf("{")+1,onclick.indexOf("}"));

					if(href){
						newHtml += "<li><a href=\"" + href + "\" onclick=\"" + newonlick + "\">" + text + "</a></li>";
					}
				}
			);
			newHtml += "</ol></div>";

			var form = $("#exploreLVForm");
			form.replaceWith(newHtml);
		}
	},

	ToggleQuoteApplyAdviserDropDown: function() {
		var dropDown = $("#quote-apply-container ol");
		if(dropDown){
			if(Adviser2009Rebrand.quoteApplyDropDownVisible){
				Adviser2009Rebrand.HideQuoteApplyDropDown();
			} else {
				Adviser2009Rebrand.ShowQuoteApplyDropDown();
			}
		}
	},

	ToggleExploreAdviserDropDown:function(){
		var dropDown = $("#exploreLVJS ol");
		if(dropDown){
			if(Adviser2009Rebrand.ExploreLVDropDownVisible){
				Adviser2009Rebrand.HideExploreLVDropDown();
			} else {
				Adviser2009Rebrand.ShowExploreLVDropDown();
			}
		}
	},

	ShowQuoteApplyDropDown:function(){
		var dropDown = $("#quote-apply-container ol");
		if(dropDown){
			Adviser2009Rebrand.quoteApplyDropDownVisible = true;
			dropDown.fadeIn("fast");
		}
	},

	HideQuoteApplyDropDown:function(){
		var dropDown = $("#quote-apply-container ol");

		if(dropDown){
			Adviser2009Rebrand.quoteApplyDropDownVisible = false;
			dropDown.fadeOut("fast");
		}
	},

	ShowExploreLVDropDown:function(){
		var dropDown = $("#exploreLVJS ol");

		if(dropDown){
			Adviser2009Rebrand.ExploreLVDropDownVisible = true;
			dropDown.fadeIn("fast");
		}
	},

	HideExploreLVDropDown:function(){
		var dropDown = $("#exploreLVJS ol");

		if(dropDown){
			Adviser2009Rebrand.ExploreLVDropDownVisible = false;
			dropDown.fadeOut("fast");
		}
	},

	ToggleAnnuityCalculatorDropDown:function(){
		var dropDown = $("#exploreLVJS ol");
		if(dropDown){
			if(Adviser2009Rebrand.AnnuityCalculatorDropDownVisible){
				Adviser2009Rebrand.HideAnnuityCalculatorDropDown();
			} else {
				Adviser2009Rebrand.ShowAnnuityCalculatorDropDown();
			}
		}
	},
	
	IE6ResizeKick:function(){
		var isIE6 = ($.browser.msie && $.browser.version.substr(0,1)<7);
		if (isIE6){
			$("div").each(
				function(index, item){
					var htmlElement = $(item);
					htmlElement.css("height", htmlElement.css("height"));
				}
			);
		}	
	},

	ShowAnnuityCalculatorDropDown:function(){
		var dropDown = $("#annuity-calc ol");

		if(dropDown){
			Adviser2009Rebrand.AnnuityCalculatorDropDownVisible = true;
			dropDown.fadeIn("fast");
		}
	},

	HideAnnuityCalculatorDropDown:function(){
		var dropDown = $("#annuity-calc ol");

		if(dropDown){
			Adviser2009Rebrand.AnnuityCalculatorDropDownVisible = false;
			dropDown.fadeOut("fast");
		}
	},

	ShowAnnuityCalculatorResults:function(group, groupIndex){
		//alert("show annuity calc results");
		Adviser2009Rebrand.HideAnnuityCalculatorDropDown();

		var title = $("#annuity-calc-select");
		title.text(group);

		var isIE6 = ($.browser.msie && $.browser.version.substr(0,1)<7);
		if (isIE6){
			//window.location.href = "/adviser/test/annuity-calculator?annuitycalcgroup=" + groupIndex;
			var currentHref = window.location.href + "?";
			currentHref = currentHref.substr(0, currentHref.indexOf("?"));
			window.location.href = currentHref + "?annuitycalcgroup=" + groupIndex;

		} else {

			$.get("/processors/Adviser/GetAnnuityCalculatorResults?groupindex=" + groupIndex,
				function(data){
					var resultsDiv = $("#annuity-calc-results");

					resultsDiv.fadeOut("fast",
						function(){
							resultsDiv.replaceWith(data);

							resultsDiv = $("#annuity-calc-results");
							resultsDiv.fadeIn("slow");

							var uploadDateDiv = $("#annuity-upload-date");
							if(uploadDateDiv){
								uploadDateDiv.show();
							}

							$(".block75 div").each(
								function(index, item){
									var div = $(item);

									//alert(div.html() + " " + div.text());
									//alert(div + " : " + div.css("display"));
									if(div.css("display") == "none"){
										div.show();
									}
								}
							);
							// this is going to be trial and error but maybe setting a height on each div in the page will give it a kick..
							/*$("div").each(
								function(index, item){
									var htmlElement = $(item);
									var id = htmlElement.attr("id");

									if(id.substring(0, 12) != "annuity"){
										htmlElement.css("height", htmlElement.css("height"));
									}

								}
							);*/
						}
					);
				}
			);
		};
	},

	ToggleExpander:function(expanderID){
		var isIE6 = ($.browser.msie && $.browser.version.substr(0,1)<7);
		var expanderHeader = $("#" + expanderID + " > div.expanderHeader");
		var expanderContent = $("#" + expanderID + " > div.expanderContent");
		// if ie 6 we need to shove the rounded corners down a bit.. grrrr...
		var mainContent = $("#mainContent");
		var bottomDiv = $("#mainContentBottom");
		var bottomDivPosition = bottomDiv.offset({scroll:true});
		var bottomDivTop = bottomDivPosition.top;

		if(expanderContent.css("display") == "none"){
			expanderContent.css("display", "block");
			expanderHeader.removeClass("headerClosed");
			expanderHeader.addClass("headerOpen");

			// move bottomDiv down
			if(isIE6){
				//bottomDiv.css("top", bottomDivTop + expanderContent.outerHeight());
				//mainContent.css("height", mainContent.outerHeight() + expanderContent.outerHeight() + 1);
				mainContent.css("height", mainContent.outerHeight() + 1);
			}
		} else {
			var expanderContentHeight = expanderContent.outerHeight();
			expanderContent.css("display", "none");
			expanderHeader.removeClass("headerOpen");
			expanderHeader.addClass("headerClosed");

			// move bottomDiv up
			if(isIE6){
				//bottomDiv.css("top", bottomDivTop - expanderContent.outerHeight());
				mainContent.css("height", mainContent.outerHeight() - 1 - expanderContentHeight);
			}
		}
	},

	ShowMboxControls:function(){
		//alert("show mbox controls");
	},
	HomepageNewsFirstShow:true,
	HomepageNewsData:null,
	HomepageNews:null,
	HomepageNewsItemsLIs:null,

	SetupNewsListingPaging:function(){
		$(".paging a").each(
			function(index, item){
				var a = $(item);
				var id = a.attr("id");
				if(id != ""){
					var page = id.substr(12);
					a.click(
						function(){
							Adviser2009Rebrand.ShowNewsListingPage(page);
							return(false);
						}
					);
				}

			}
		);
	},
	ShowNewsListingPage:function(showPage){
		$(".paging a").each(
			function(index, item){
				var a = $(item);
				var id = a.attr("id");
				if(id != ""){
					var page = id.substr(12);
					var li = $(a.parents("li"));

					if(li.hasClass("selected")){
						li.replaceWith("<li><a href=\"\" onclick=\"Adviser2009Rebrand.ShowNewsListingPage(" + page + ");return(false);\" id=\"news-paging-" + page + "\">" + page + "</a></li>");
					}
					if(page == showPage){
						//alert("add to " + page);
						li.addClass("selected");
					} else {
						//alert("remove from " + page);
						li.removeClass("selected");
					}
				}
			}
		);

		$.getJSON("/processors/Adviser/NewsListing?subsite=" + Adviser2009Rebrand.SubsiteName + "&page=" + showPage,
			function (data){
				Adviser2009Rebrand.PopulateNewsListing(data);
			}
		);
	},
	PopulateNewsListing:function(data){
		Adviser2009Rebrand.NewsData = data.NewsItems;
		/*
		for(var index = 1;index < 6;index++){
			var li = $("#news-item-" + index);
			data.NewsItems[index-1].Height = li.outerHeight();
		}
		*/
		Adviser2009Rebrand.FadeOutNewsItem(1);
	},
	FadeOutNewsItem:function(index){
		var newsItemLi = $("#news-item-" + index);
		newsItemLi.fadeOut(200,
			function(){
				Adviser2009Rebrand.FadeInNewsItem(index);
			}
		);
	},
	FadeInNewsItem:function(index){
		var newsItemLi = $("#news-item-" + index);
		var newsItem = Adviser2009Rebrand.NewsData[index -1];
		if(newsItem){
			newsItemLi.replaceWith("<li style=\"display:none\" id=\"news-item-" + index + "\"><p class=\"listing-date\">" + newsItem.Date + "</p><h3><a>" + newsItem.Title + "</a></h3><p>" + newsItem.Summary + "<p><div class=\"pagelink right\"><span class=\"pagelinklink\"><a alt=\"" + newsItem.Summary + " \" href=\"news_detail?articleid=" + newsItem.NodeID + "\" class=\"more\" id=\"news" + newsItem.NodeID +  "\">Read more</a></span></div></li>");
			newsItemLi = $("#news-item-" + index);

			newsItemLi.fadeIn(200,
				function(){
					if(index < 6){
						Adviser2009Rebrand.FadeOutNewsItem(index+1);
					}
				}
			);
		}
	}
}


var HomePageNewsFader = function(){}
HomePageNewsFader.prototype = {
	Articles: null,
	DetailPage: null,
	LoadHomepageNews: function(clear, detailPage) {
		// reload page after 10 mins 
		var currentHref = window.location.href;
		//alert(currentHref);
		var timeout = 1000 * 60 * 10;
		setTimeout('window.location.href = "' + currentHref	+ '"', timeout);
	
		if (clear) {
			this.Articles = new Array();
		}
		
		if(detailPage && detailPage != ""){
			this.DetailPage = detailPage;
		}

		$.getJSON("/processors/Adviser/HomepageNews?subsite=" + Adviser2009Rebrand.SubsiteName + "&rqsb=" + new Date().getTime(),
			function (data){
				// Add news items into the Articles array
				for (var x=0;x<data.NewsItems.length;x++) {
					var _article = new newsItem ();
					_article.nodeid = data.NewsItems[x].NodeID;
					_article.title = data.NewsItems[x].Title;
					_article.date = data.NewsItems[x].Date;
					_article.summary = data.NewsItems[x].Summary;
					addArticle (_article);
				}

				// On the first time through, start the drawing process
				if (!Adviser2009Rebrand.HomePageNews.started) {
					Adviser2009Rebrand.HomePageNews.started = true;
					Adviser2009Rebrand.HomePageNews.removeall();
					Adviser2009Rebrand.HomePageNews.draw();
				}
			}
		);
	},
	removeall: function() {
		var homeContentUl = $("#latest-news .homeContent ul");
		homeContentUl.replaceWith("<ul></ul>");
	},
	redraw: function() {
		this.removeall();
		this.draw();
	},
	animate: function() {
		var homeContentUl = $("#latest-news .homeContent ul li");
		for (x=0; x<homeContentUl.length; x++) {
			var elementId = $(homeContentUl[x]).attr ("id");
			cmd = "Adviser2009Rebrand.HomePageNews.fade ('" + elementId + "')";
			var timeout = (x + 1) * 500;
			window.setTimeout (cmd, timeout);
		}
	},
	fade: function(elementId) {
		var element = $("#" + elementId);
		if ($.browser.msie && $.browser.version == "6.0"){
			/*var div = element.append ("<div id='overlay'>here</div>");
			div.css("border", "1px solid black");
			element.css ("z-index", "10");
			div.css ("z-index", "11");
			*/
			element.show();
		} else {
			element.fadeTo (100, .1, function(){$(this).fixClearType()});
			element.fadeTo (200, 1, function(){$(this).fixClearType()});
		}
	},
	draw: function() {
		if (this.Articles.length > 0) {

			var SubSiteUrl = "adviser";
			var temp = siteUrl.split("_");
			if (temp.length > 1) {
				SubSiteUrl = temp[1];
			}

			var homeContentUl = $("#latest-news .homeContent ul");

			var willFit = true;
			do {
				var _article = this.Articles[0];
				if(_article) {
					// Create an <li> for the article
					var url = "/" + SubSiteUrl + "/working-with-lv/news_detail?articleid=" + _article.nodeid;
					if(this.DetailPage != null){
						url = this.DetailPage + "?articleid=" + _article.nodeid;
					}
					var element = $("<li id=\"hni-" + _article.nodeid + "\">" + _article.date + "<br/><a id=\"homepageNews" + _article.nodeid + "\" onclick=\"Tracker(this,'/_adviser/HomepageNews/2120813/_/');\" href=\"" + url + "\">" + _article.title + "</a></li>");

					// Add it to the <ul>
					homeContentUl.append(element);

					// Hide it
					if ($.browser.msie && $.browser.version == "6.0") {
						element.css ("display", "none");
					} else {
						element.css ("opacity", "0");
					}
					
					// Check the list still fits in the available space
					willFit = _article.willFit();
					if(willFit) {
						// Remove the article from the array
						this.Articles.splice (0,1);
					} else {
						// Doesn't fit on screen - remove it from the <ul>
						// but keep it in the array for next time
						element.remove();
					}
				} else {
					willFit = false;
				}
			} while(willFit == true);

			// items added, now animate them
			this.animate();

			// if the articles array is running low, reload it
			if (this.Articles.length < 10) {
				this.LoadHomepageNews();
			}

			// start a timer to draw next batch of items
			window.setTimeout ("Adviser2009Rebrand.HomePageNews.redraw()", 5000)
		}
	}
}

addArticle = function(_article) {
	try {
		Adviser2009Rebrand.HomePageNews.Articles.push (_article);
	} catch (e) {
		alert (e.message);
	}
}



Adviser2009Rebrand.HomePageNews = new HomePageNewsFader();

var newsItem = function() {}
newsItem.prototype = {
	nodeid: null,
	date: null,
	title: null,
	summary: null,
	visible: false,
	animated: false,
	fadeOut: function() {
		this.element().fadeTo (200, .1, function(){$(this).fixClearType()});
		this.element().fadeTo (200, 1, function(){$(this).fixClearType()});
		this.animated = true;
	},
	element: function() {
		return $("#hni-" + this.nodeid);
	},
	willFit: function() {
		//get height of container
		var availableHeight = Number($("#latest-news .homeContent").outerHeight());

		//get height of all visible <li>'s
		var totalLiHeight = 0;
		var homeContentLIs = $("#latest-news .homeContent ul li");
		for (x=0; x<homeContentLIs.length; x++) {
			var LI = $(homeContentLIs[x]);
			totalLiHeight = totalLiHeight + (LI.outerHeight() + 22);
		}

		this.element().css("display", "block");
	
		if (totalLiHeight < availableHeight) {
			//console.log("true");
			return true;
		} else {
			//console.log("false");
			return false;
		}
	}
}


 jQuery.fn.fixClearType = function()
 {
	return this.each(function(){
		if( !!(typeof this.style.filter  && this.style.removeAttribute))
		this.style.removeAttribute("filter");
}) }

