$(document).ready(function() 
{	
	//FFS: Show The Plan
	$("#ShowThePlan > SELECT").change(function() {
		getDropDownValue($(this+":selected"));
	});
	
	
	//HEADER MENU
	$('ul#navigationMenu > li').mouseover(function() {
		$(this).find(".sub").show();
	}).mouseout(function() {
		$(this).find(".sub").hide();
	});
	$('ul#navigationMenu > li#liInternational').mouseover(function() {
		$(this).find("IMG").attr("src", template_url + "/images/International2.png");
	}).mouseout(function() {
		$(this).find("IMG").attr("src", template_url + "/images/International1.png");
	});
	$('ul#navigationMenu > li#liEnrollNow').mouseover(function() {
		$(this).find("IMG").attr("src", template_url + "/images/EnrollNow2.png");
	}).mouseout(function() {
		$(this).find("IMG").attr("src", template_url + "/images/EnrollNow1.png");
	});

	
	//Footer Rollover Images for More Project + New Life Network
	$('div#footerAffiliates > a > img.newlife').mouseover(function() {
		$(this).attr("src", template_url + "/images/newlife_logo-rollover.png");
	}).mouseout(function() {
		$(this).attr("src", template_url + "/images/newlife_logo.png");
	});
	

	$('div#footerAffiliates > a > img.moreproject').mouseover(function() {
		$(this).attr("src", template_url + "/images/moreproject_logo-rollover.png");
	}).mouseout(function() {
		$(this).attr("src", template_url + "/images/moreproject_logo.png");
	});
	
	
	if ( jQuery( '#diamondRunEvents' ).length )
	{
	    jQuery.getFeed({
	        url: template_url + "/js/proxy.php?url=http://feeds.r3global.com/diamond",
	        success: function(feed)
	        {
				var html = feed_li_list( feed );
	            jQuery( '#diamondRunEvents' ).html( "<ul>"+ html +"<li><a href=\"/events/\">View Events +</a></li></ul>" );
	        }
	    });
	}
	
	if ( jQuery( '#featuredEvents' ).length )
	{
	    jQuery.getFeed({
	        url: template_url + "/js/proxy.php?url=http://feeds.r3global.com/featured",
	        success: function(feed)
	        {
				var html = feed_li_list( feed );
	            jQuery( '#featuredEvents' ).html( "<ul>"+ html +"<li><a href=\"/events/\">View Events +</a></li></ul>" );
	            if ($("#shrinkingText").length) ;//shrinkText();
	        }
	    });
	}
	
	if ( jQuery( '#upcomingWebcasts' ).length )
	{
	    jQuery.getFeed({
	        url: template_url + "/js/proxy.php?url=http://feeds.r3global.com/webcasts",
	        success: function(feed)
	        {
				var html = feed_li_list( feed );
	            jQuery( '#upcomingWebcasts' ).html( "<ul>"+ html +"</ul>" );
	        }
	    });
	}
	function feed_li_list ( feed ) 
	{
		var html = '';
		var descriptions = "";
		
		for( var i = 0; i < feed.items.length && i < 5; i++ ) 
		{
			var item = feed.items[i];
			
			descriptions += item.link.replace(/^\s*/, "").replace(/\s*$/, "") + "<br/>";
			descriptions += item.description;
									
			var eventDate = item.category;
			eventDate = item.category.substring(0, 10);
			eventDate = Date.parse( eventDate ).toString( "dddd, MMM d, yyyy" );
			
			html += '<li>' +
						'<span><a href="'+ item.link + '">' + eventDate  + '</a></span>' + 
						item.title +
					'</li>';
		}
		
		return html;
	}
	
	//Populates the Live Webcast page title, date, and link <h1>
	if ( jQuery( '#wbTitle' ).length )
	{
	    jQuery.getFeed({
	        url: template_url + "/js/proxy.php?url=http://feeds.r3global.com/webcasts",
	        success: function(feed)
	        {
	    		//get first webcast item
	    		var item = feed.items[0];
	    		
	    		//variable to hold all values
	    		var values = "";
	    		
	    		//trim hyperlink
	    		values += item.link.replace(/^\s*/, "").replace(/\s*$/, "") + "<br/>";
	    		
	    		//append atrocious XML description of item
	    		values += item.description;
	    		
	    		//make all HTML linebreaks uniform so we can...
	    		values = values.replace(/<br \/>/g, "<br/>");
	    		//...split atrocious XML into array of values
				values = values.split("<br/>");
	    		
				//variables to hold desired values
	    		var calendarLink = "";
				var location = "";
				var webcastDate = "";
				var speaker = "";
				
				//get current year (used to get webcastDate)
				var c=new Date();
				c=c.getFullYear();
				var f=c+1;
				
				//for each element in array, look for our desired values
				for (var a=0; a < values.length; a++) 
				{
					//Will always check if the variable is empty first.
					//This will prevent overriding of values if the
					//"notes" section of the atrocious XML
					//meets any of the if-conditions.
					
					//get calendar URL
					//testing for existence of substring "trumba"
					if (calendarLink == "" && 
						values[a].indexOf("trumba") > 0) 
					{
						calendarLink = values[a];
					}
					
					//get "Irvine, CA" or "Jacksonville, FL"
					//testing for a comma followed by "CA" or "FL"
					else if (location == "" && (
							 values[a].indexOf(", CA ") > 0 || 
							 values[a].indexOf(", FL ") > 0))
					{
						//drop zip code off the end
						location = values[a].substr(0, values[a].length-7);
					}
					
					//get date (e.g. - Tuesday, October 12, 2010, 7:30&nbsp;–&nbsp;9pm)
					//testing for comma, then the year, then another comma
					//check one year ahead, too - in case it's Dec. to Jan.
					else if (webcastDate == "" && (
							 values[a].indexOf(", " + c + ", ") > 0 || 
							 values[a].indexOf(", " + f + ", ") > 0))
					{
						webcastDate = values[a];
					}
					
					//get speaker
					//testing for existence of substring "Speaker Name"
					else if (speaker == "" &&
							 values[a].indexOf("Speaker Name") > 0)
					{
						//start after "<b>Speaker Name</b>&nbsp;"
						speaker = values[a].substr(26);
					}
				}
				
				//place desired values on page
				document.getElementById("wbTitle").innerHTML = "TEAM R3Global LIVE Webcast (" + location + ")";
				document.getElementById("wbSpkr").innerHTML = "Featuring: " + speaker;
				document.getElementById("wbDate").innerHTML = webcastDate;
				document.getElementById("wbCalndrLink").innerHTML = "<a href='" + calendarLink + "'>Click here for full event details &raquo;</a>";
				document.getElementById("wbLocation").innerHTML = location.slice(-2);
				
		    }
	    });
	}
	
	
	// Homepage context header box buttons
    $(".topContextHeaderBox > DIV > STRONG > SPAN").hide();
	
    $(".topContextHeaderBox").corner();
	
    $(".topContextHeaderBox").hover( function () {
			$(this).find(" > DIV > STRONG").css("color", "#4b0f86");
			$(this).find(" > DIV > STRONG > SPAN").show();
	}, function () {
			$(this).find(" > DIV > STRONG").css("color", "#000000");
			$(this).find(" > DIV > STRONG > SPAN").hide();
	});
	
    $(".topContextHeaderBox").click( function () {
	    var name = $(this).find(" > DIV > STRONG").attr("class");

	    if ( name == "formula-for-success" ) {
			window.location = server_url + "/the-system/formula-for-success/formula-for-success-1/";
	    } else if ( name == "r3global-store" ) {
	    	window.location = "http://www.r3globalstore.com/";
		} else if ( name == "calendar-of-events" ) {
			window.location = server_url + "/events/r3global-calendar/";
		}
    });
	
    // Make the Search <input> focus/blur logic.
	$("#searchForm #s").attr("value", "Search R3Global.com");
	
	$("#searchForm #s").focus(function(e){  
		$(this).attr("value", "");
	}).blur(function(e){  
		$(this).attr("value", "Search R3Global.com");
	});  

	 // Make the Newsletter Signup Banner <input> focus/blur logic.
	$("#email_address").attr("value", "Enter email address");
	
	$("#email_address").focus(function(e){  
		$(this).attr("value", "");
	}).blur(function(e){
		var input = $(this).attr("value");
		if (input == "") {
			$(this).attr("value", "Enter email address");
		}
	});  
	
	//Homepage Header Image Rotator
	$('#r3gBannerRotator').innerfade({
		speed: 1000,
		timeout: 7000,
		type: 'sequence',
		containerheight: '257px'
	});
		
    $("#ddFlyerDownload").hover( function () {
		$(this).attr("src", template_url + "/microsites/dream-days-2010/images/click_printableflyer-rollover.png");
	}, function () {
		$(this).attr("src", template_url + "/microsites/dream-days-2010/images/click_printableflyer.png");
	});
    
    if($("#attntn").length)
    {
    	getAttention(1, 1);
	}
});

function getAttention(i, j) {
	
	var color = 'red';
	
	if (i%2 == 0)
	{
		color = 'black';
	}
	
	if(j % 20 == 0)
	{
		if (i % 7 > 0)
		{
			document.getElementById('attntn').style.color = color;
			setTimeout('getAttention(' + ++i + ', ' + j + ')', 250);
		}
		else
			setTimeout('getAttention(1, 1)', 250);
	}
	else
		setTimeout('getAttention(' + i + ', ' + ++j + ')', 250);
}

function shrinkText() {
	
	center($("#shrinkingText"));
	$("#shrinkingText").fadeIn(1000);
	setTimeout("shrink()", 1500);
}

function center(element) {
	element.css("top", ( $(window).height() - element.outerHeight() ) / 3 + $(window).scrollTop() + "px");
	element.css("left", ( $(window).width() - element.outerWidth() ) / 2 + $(window).scrollLeft() + "px");
};

function shrink()
{
	var goalTop = $("#attntn").offset().top,
		goalLeft = $("#attntn").offset().left;

	$("#shrinkingText").animate({
		left: goalLeft,
		top: goalTop - 120,
		fontSize: '12px',
		opacity: 0}, 1000, function () {$(this).hide();});
}


