var newsProviders=new Array();
var newsProvidersPos = 0;
var currentListProvider = "listHepCen"; // The source we're currently on
var currentListItem = "#newsItem1"; // The news item we're currently on
var newsProviderTemp = "";
var newsItems=new Array();
var newsItemsCount = 0;
var newsItemsPos = 0;
var newsItemList = new Array();
var newsItemListH = new Array();
var newsProviderList = new Array();
var newsProviderURL = "http://www.hepatitis-central.com/rssFeedNews.php";
var titleTemp = "";
var linkTemp = "";
var descTemp = "";
var allItemsTemp = "";
var nextNewsHolder;
var newsItemHeight = 170;
var popupStatus = 0;

function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		popupStatus = 1;  
	}  
}  

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//request data for centering
	//var windowWidth = $(window).width();
	var windowWidth = myWidth;
	var windowHeight = myHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
	"position": "absolute",
	"top": (windowHeight/2-popupHeight/2) + $(window).scrollTop(),
	"left": (windowWidth/2-popupWidth/2)
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
	"height": $(document).height()
	});
}

function displayPopup() {
	var randomnumber=Math.floor(Math.random()*3);
	if ($.cookie('popNewsDay') == 1 || $.cookie('popNewsMonth') == 1 || $.cookie('newsSignedUp') == 1 || randomnumber > 0) {
	// do nothing
	} else  {
		centerPopup();
		loadPopup();
		$(window).scroll(function () { 
			centerPopup();
		});
		date = new Date();
        date.setTime(date.getTime() + (20 * 60 * 1000));
		$.cookie('popNewsDay', '1', { expires: date });
	}
}


function formProcess(event){
  event.preventDefault();
  $("#poll-container").fadeOut("slow",function(){
	$.post("/poll/ajaxPoll.php",{option: $("#pollForm input[name='option']:checked").val(), stage: $("#pollForm input[name='stage']").val(), poll: $("#pollForm input[name='poll']").val() },
	function(data) {
	  $("#poll-container").append(data).fadeIn("slow",function(){
      });
    });
	$(this).empty();
  });
}

function ajaxPollView(event) {
	hrefTemp = $(this).attr("href");
		pollTemp = hrefTemp.substring(hrefTemp.indexOf("?"),hrefTemp.indexOf("&"));
		pollTemp = pollTemp.substring(pollTemp.indexOf("=")+1);
		resultsTemp = hrefTemp.substring(hrefTemp.indexOf("&"));
		resultsTemp = resultsTemp.substring(resultsTemp.indexOf("=")+1);
		$("#poll-container").fadeOut("slow",function(){
			$.get("/poll/ajaxPoll.php",{poll: pollTemp, results:resultsTemp },
			function(data) {
				$("#poll-container").append(data).fadeIn("slow",function(){
					$("#voteOnPoll").click(ajaxPollProcess);
				});
    		});
			$(this).empty();
		});
		return false;
}

function ajaxPollProcess(event) {
	hrefTemp = $(this).attr("href");
		pollTemp = hrefTemp.substring(hrefTemp.indexOf("?"));
		pollTemp = pollTemp.substring(pollTemp.indexOf("=")+1);
		$("#poll-container").fadeOut("slow",function(){
			$.get("/poll/ajaxPoll.php",{poll: pollTemp},
			function(data) {
				$("#poll-container").append(data).fadeIn("slow",function(){
					$("#pollForm").submit(formProcess);
					$("#pollForm .viewResults").click(ajaxPollView);
				});
    		});
			$(this).empty();
		});
		return false;
}

function nextNewsItem() {
	$("#newsItem" + (newsItemsPos + 1)).removeClass("selected");
	if (newsItemsPos == (newsItemsCount-1)) {
		newsItemsPos = 0;
	} else {
		newsItemsPos++;
	}
	$("#newsItem" + (newsItemsPos + 1)).addClass("selected");
	currentListItem = "#newsItem" + (newsItemsPos + 1);
	for (var i in newsItems) {
		$(newsItems[i]).hide();
	}
	$(newsItems[newsItemsPos]).fadeIn();
	tempHeight = $(newsItems[newsItemsPos]).height();
	$("#newsInner").height(newsItemHeight);
}

$(document).ready(function() {
	var tempLoc = window.location.toString();
	if (tempLoc.indexOf("www") == -1) {
		newsProviderURL = "http://hepatitis-central.com/rssFeedNews.php";
	}
	$("#pollForm").submit(formProcess); // setup the submit handler
	$("#pollForm .viewResults").click(ajaxPollView);
	//$("#voteOnPoll").click(ajaxPollProcess);
	
	// start newsBox code
	// Hide all the providers and news items besides the first one
	$("#newsInner .newsProvider").each(function(i){
		newsProviders[i] = this; // Create a provider reference list
		if (i != 0) { $(this).hide(); } else {
			$(this).children(".newsItem").each(function(i){
				newsItemsCount++;
				newsItems[i] = this; // Create a provider news item list for the first provider
				if (i != 0) { $(this).hide(); }
			});
		}
	});
	
	$("#newsItemList li a").each(function(i) {
		newsItemListH[i] = $(this).text();
	});
	
	// Get references to each provider link
	$("#newsProviderList a").each(function(i){
		newsProviderList[i] = this;
	});
	
	$("#newsProviderList a").click(function () { 
		clearInterval(nextNewsHolder);
		if ($(this).attr("id") != currentListProvider) {
			$("#newsProviderList a").each(function () {
				$(this).removeClass("selected");
			});
			$(this).addClass("selected");
			$(currentListItem).removeClass("selected");
			currentListItem = "#newsItem1";
			$(currentListItem).addClass("selected");
			currentListProvider = $(this).attr("id");
			newsProviderTemp = "#news" + currentListProvider.substring(4);
			if (currentListProvider != "listHepCen") {
				$("#" + currentListProvider).text("Loading...");
				$.ajax({
					type: "GET",
					url: newsProviderURL + "?url=" + currentListProvider,
					dataType: "xml",
					success: function(xml) {
						allItemsTemp = "<h3>" + $(xml).find('title:first').text() + "</h3>";
						$(xml).find('item').each(function(i){
							titleTemp = $(this).find('title').text();
							linkTemp = $(this).find('link').text();
							descTemp = $(this).find('description').text();
							allItemsTemp += "<div class='newsItem'><h4><a href='" + linkTemp + "' target='_blank'>" + titleTemp + "</a></h4><p>" + descTemp + "</p></div>";
							newsItemList[i] = titleTemp;
						}); //close each
						$("#newsItemList li a").each(function(i) {
							if (newsItemList[i].length > 60) {
								tempString = newsItemList[i].substring(0,67);
								$(this).html(tempString + "...");
							} else {
								$(this).html(newsItemList[i]);
							}
						});
						newsItems = new Array();
						newsItemsCount = 0;
						newsItemsPos = 0;
						$(newsProviderTemp).html(allItemsTemp);
						$(newsProviderTemp).children(".newsItem").each(function(i){
							newsItemsCount++;
							newsItems[i] = this;
							$(this).hide();
						});
						$(newsItems[0]).show();
						
						for (var j in newsProviders) {
							$(newsProviders[j]).hide();
						}
						tempText = currentListProvider.substring(4);
						$("#" + currentListProvider).text(tempText);
						$(newsProviderTemp).fadeIn();
						tempHeight = $(newsItems[0]).height();
						$("#newsInner").height(tempHeight+55);
					} // close success
				}); //close $.ajax(
			} else { // Just make HepCen list active
				
				newsItems = new Array();
				newsItemsCount = 0;
				newsItemsPos = 0;
				$("#newsItemList li a").each(function(i) {
					$(this).html(newsItemListH[i]);
				});
				$(newsProviderTemp).children(".newsItem").each(function(i){
					newsItemsCount++;
					newsItems[i] = this;
					$(this).hide();
				});
				$(newsItems[0]).show();	
				for (var j in newsProviders) {
					$(newsProviders[j]).hide();
				}
				$(newsProviderTemp).fadeIn();
				tempHeight = $(newsItems[0]).height();
				$("#newsInner").height(tempHeight+55);
			}
		} else {

		}
		return false;
	}); // end newsProviderList function
	
	$("#newsItemList #newsItem1").click(function () {
		clearInterval(nextNewsHolder);
		if ($(this).attr("id") != currentListItem) {
			$(currentListItem).removeClass("selected");
			currentListItem = "#" + $(this).attr("id");
			$(currentListItem).addClass("selected");
			$(newsItems[1]).hide();
			$(newsItems[2]).hide();
			$(newsItems[3]).hide();
			$(newsItems[0]).removeAttr("filter");
			$(newsItems[0]).fadeIn();
			tempHeight = $(newsItems[0]).height();
			$("#newsInner").height(tempHeight+55);
		}
		return false;
	});
	$("#newsItemList #newsItem2").click(function () {
		clearInterval(nextNewsHolder);
		if ($(this).attr("id") != currentListItem) {
			$(currentListItem).removeClass("selected");
			currentListItem = "#" + $(this).attr("id");
			$(currentListItem).addClass("selected");
			$(newsItems[0]).hide();
			$(newsItems[2]).hide();
			$(newsItems[3]).hide();
			$(newsItems[1]).removeAttr("filter"); 
			$(newsItems[1]).fadeIn();
			tempHeight = $(newsItems[1]).height();
			$("#newsInner").height(tempHeight+55);
		}
		return false;
	});
	$("#newsItemList #newsItem3").click(function () {
		clearInterval(nextNewsHolder);
		if ($(this).attr("id") != currentListItem) {
			$(currentListItem).removeClass("selected");
			currentListItem = "#" + $(this).attr("id");
			$(currentListItem).addClass("selected");
		}
		$(newsItems[0]).hide();
		$(newsItems[1]).hide();
		$(newsItems[3]).hide();
		$(newsItems[2]).fadeIn();
		$(newsItems[2]).removeAttr("filter");  
		tempHeight = $(newsItems[2]).height();
		$("#newsInner").height(tempHeight+55);
		return false;
	});
	$("#newsItemList #newsItem4").click(function () {
		clearInterval(nextNewsHolder);
		if ($(this).attr("id") != currentListItem) {
			$(currentListItem).removeClass("selected");
			currentListItem = "#" + $(this).attr("id");
			$(currentListItem).addClass("selected");
		}
		$(newsItems[0]).hide();
		$(newsItems[1]).hide();
		$(newsItems[2]).hide();
		$(newsItems[3]).fadeIn();
		$(newsItems[3]).removeAttr("filter"); 
		tempHeight = $(newsItems[3]).height();
		$("#newsInner").height(tempHeight+55);
		return false;
	});
	// End newsbox code
	
	$("#siteTopics2 > ul > li").hover(function () {
		$(this).css("background-color","#FFFFBB");
		$(this).css("cursor","pointer");
		$(this).find("p").css("display","block");
	},function () {
		$(this).css("background-color","#FFFFFF");
		$(this).find("p").css("display","none");
	});
	$("#siteTopics2 > ul > li").click(function () {
		if ($(this).find("h4 a").attr("target") == "_blank") {
			window.open($(this).find("h4 a").attr("href"));
		} else {
			window.location = $(this).find("h4 a").attr("href");
		}
		return false;
	});
	$("#mainNav li").mouseenter(function(){
		$(this).addClass("sfhover");
	});
	$("#mainNav li").mouseleave(function(){
		$(this).removeClass("sfhover");
	});
	nextNewsHolder = setInterval("nextNewsItem()",7000);
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$("#popupLater").click(function(){
		$.cookie('popNewsMonth', '1', { expires: 30 });
		disablePopup();
	});
	
	var t = window.setTimeout(displayPopup,15000);
});
