
$(document).ready(function(){
	
	trimReleases();
	
	// preload all featured property photos before starting the intro animation
	$("ul#property-photos li img").imgpreload(
		function()
		{			
			showFirstProperty();
			current_slide = 0;
			TOTAL_SLIDES = $("ul#property-photos li").length - 1;
		}
	);

});


function trimReleases()
{
	// check for new ticker symbol headline text and replace it to remove special characters
	var headline_text = new Array();
	
	$('div#column-three ul li').each(
		function(i)
		{
			headline_text[i] = $('div#column-three ul li:eq('+i+') a').text();
		}
	);
	
	// this is a little dirty, check the top three list items for a match and replace the text and title if necessary
	if ( headline_text[0] == "STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange – to Begin Trading as “STAG” to Highlight Name Recognition" )
	{
		$('div#column-three ul li:eq(0) a')
			.text('STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition')
			.attr('title', 'STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition');
	}
	if ( headline_text[1] == "STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange – to Begin Trading as “STAG” to Highlight Name Recognition" )
	{
		$('div#column-three ul li:eq(1) a')
			.text('STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition')
			.attr('title', 'STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition');
	}
	if ( headline_text[2] == "STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange – to Begin Trading as “STAG” to Highlight Name Recognition" )
	{
		$('div#column-three ul li:eq(2) a')
			.text('STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition')
			.attr('title', 'STAG Industrial, Inc. Announces New Ticker Symbol on New York Stock Exchange - to Begin Trading as "STAG" to Highlight Name Recognition');
	}

	$('div#column-three ul li:eq(0), div#column-three ul li:eq(1), div#column-three ul li:eq(2)').show();
	$('div#column-three ul').append('<a href="http://www.snl.com/irweblinkx/Yearlynews.aspx?iid=4263385" class="arrow-link">more news &nbsp;&nbsp;</a>');
}


function showFirstProperty()
{
	// set opacity of each featured property element to zero
	$("ul#property-photos li").css({opacity: 0}).show();
	$("ul#property-details li").css({opacity: 0}).show();
	$("ul#property-navigation").css({opacity: 0}).show();
			
	// fade in the first property detail
	$("ul#property-details li:first").animate(
		{opacity: 1},
		{
			queue: false,
			duration: 500,
			easing: 'easeInSine',
			complete: function()
			{
				// fade in the first property photo
				$("ul#property-navigation").animate(
					{opacity: 1},
					{
						queue: false,
						duration: 500,
						easing: 'easeInSine',
						complete: function()
						{
							// fade in the featured property navigation
							$("ul#property-photos li:first").animate(
								{opacity: 1},
								{
									queue: false,
									duration: 750,
									easing: 'easeInSine',
									complete: function()
									{
										fpNavigation();
										fpCycle();
									}
								}
							);							
						}
					}
				);
			}
		}
	);	
}


function fpCycle()
{
	rotating = setInterval("rotateProperties();", 5000);
}


function rotateProperties()
{	
	if ( current_slide == TOTAL_SLIDES )
	{
		current_slide = 0;
		fpAnimation();
	}
	else
	{
		current_slide++;
		fpAnimation();
	}
}


function fpNavigation()
{
	$("ul#property-navigation li").click(function(){
		if ( $(this).hasClass("in-view") )
		{
			// if the navigation item is in its selected state, disable the click
			return false;
		}
		else
		{
			var nav_position = getPosition(this.parentNode,this);
			current_slide = nav_position;
			clearInterval(rotating);
			fpAnimation();
			return false;
		}
	});
}


function fpAnimation()
{

	// reset featured property navigation
	$("ul#property-navigation li").removeClass("in-view");
	$("ul#property-navigation li:eq(" + current_slide + ")").addClass("in-view");
	
	// hide the previous featured property
	$("ul#property-details li").animate(
		{opacity: 0},
		{
			queue: false,
			duration: 500,
			easing: 'easeInSine'
		}
	);
	$("ul#property-photos li").animate(
		{opacity: 0},
		{
			queue: false,
			duration: 500,
			easing: 'easeInSine',
			complete: function()
			{
				// once previous featured property is hidden, fade in the newly selected featured property
				$("ul#property-details li:eq(" + current_slide + ")").animate(
					{opacity: 1},
					{
						queue: false,
						duration: 750,
						easing: 'easeInSine'
					}
				);
				$("ul#property-photos li:eq(" + current_slide + ")").animate(
					{opacity: 1},
					{
						queue: false,
						duration: 750,
						easing: 'easeInSine'
					}
				);
			
			}
		}
	);

}



// get position of child node
// var snoogans = getPosition(this.parentNode,this);
function getPosition(main,spec) {
	var items = main.getElementsByTagName(spec.tagName);
	var found = 0;
	for (p = 0; p < items.length; p++) {
		if (items[p] == spec) {
			found = 1;
			break;
		}
	}
	if (found) {
		return p;
	}
	else {
		return -1;
	}
}
