
$(document).ready(function(){
	
	body_ID = $("body").attr("id");
	
	$('#hynes-slider li img').imgpreload(
		function()
		{
			hynesSlideShow();
			if ( body_ID == "home" ){showTweets();}
		}
	);
	
});

function showTweets()
{
	// fade first set of tweets into view
	$(".tweets").css({opacity: 0}).show();
	$(".tweets:eq(0)").animate({opacity: 1},{queue: false,duration: 1500,easing: 'easeInSine'});
}


function hynesSlideShow()
{
	// remove loader graphic
	$("#hynes-slider").addClass("remove-loader");
	
	// set opacity of each featured property element to zero
	$("#hynes-slider li").css({opacity: 0});
		
	// fade in the first property detail
	$("#hynes-slider li:eq(0)").show().animate({opacity: 1},{queue: false,duration: 1000,easing: 'easeInSine'});
	
	current_slide = 0;
	current_tweet = 0;
	TOTAL_SLIDES = $("#hynes-slider li").length - 1;
	
	// create slideshow navigation
	createSliderNavigation();
	
	// cycle through slides
	cycleSlides();

}

function cycleSlides()
{
	rotate_slides = setInterval("nextSlide();", 7000);
	if ( body_ID == "home" ){rotate_tweets = setInterval("nextTweet();", 7000);}
}

function nextSlide()
{
	if ( current_slide == TOTAL_SLIDES )
	{
		$("#hynes-slider li:eq(" + TOTAL_SLIDES + ")").animate(
			{opacity: 0},
			{
				queue: false,
				duration: 500,
				easing: "easeInSine",
				complete: function()
				{
					$("#hynes-slider li").hide().css({opacity: 0});
					$("#hynes-slider li:eq(0)").show().animate({opacity: 1}, {queue: false, duration: 500, easing: "easeInSine"});
					$("#hynes-slider-nav li").removeClass("in-view");
					$("#hynes-slider-nav li:first").addClass("in-view");
				}
			}
		);
		current_slide = 0;
	}
	else
	{
		$("#hynes-slider li:eq(" + current_slide + ")").animate(
			{opacity: 0},
			{
				queue: false,
				duration: 500,
				easing: "easeInSine",
				complete: function()
				{
					$("#hynes-slider li").hide().css({opacity: 0});
					$("#hynes-slider li:eq(" + (current_slide + 1) + ")").show().animate({opacity: 1}, {queue: false, duration: 500, easing: "easeInSine"});
					current_slide++;
					$("#hynes-slider-nav li").removeClass("in-view");
					$("#hynes-slider-nav li:eq(" + current_slide + ")").addClass("in-view");
				}
			}
		
		);
	}

}


function nextTweet()
{
	if ( current_tweet == TOTAL_SLIDES )
	{
		$(".tweets").animate(
			{opacity: 0},
			{
				queue: false,
				duration: 500,
				easing: 'easeInSine',
				complete: function()
				{
					$(".tweets:eq(0)").show().animate({opacity: 1},{queue: false,duration: 400,easing: 'easeInSine'});
				}
			}
		);
		current_tweet = 0;
	}
	else
	{
		$(".tweets").animate(
			{opacity: 0},
			{
				queue: false,
				duration: 500,
				easing: 'easeInSine',
				complete: function()
				{
					$(".tweets:eq(" + current_slide + ")").show().animate({opacity: 1},{queue: false,duration: 400,easing: 'easeInSine'});
				}
			}
		);
		current_tweet++;
	}
}



function createSliderNavigation()
{
	// create the dot navigation based on total number of slides
	var nav_count = $("#hynes-slider li").length;
	for ( nav_items=0; nav_items < nav_count; nav_items++ )
	{
		$("#hynes-slider-nav").append("<li><a href=\"#\">" + nav_items + "</a></li>");
	}
	// set width of navigation for accurate positioning
	var NAV_ITEM_WIDTH = 20;
	var total_width = nav_count * NAV_ITEM_WIDTH;
	var set_position_left = (960 - total_width)/2;
	
	$("#hynes-slider-nav").css({left: "" + set_position_left + "px"});
	$("#hynes-slider-nav li:first").addClass("in-view");
	
	// if user clicks on navigation, stop the cycle, turn off all slides and then display the matching slide
	$("#hynes-slider-nav li").click(function()
	{
		if ( $(this).hasClass('in-view') )
		{
			// do nothing
			return false;
		}
		else
		{
			var nav_position = getPosition(this.parentNode,this);
			clearInterval(rotate_slides);
			
			$("#hynes-slider li").hide().css({opacity: 0});
			$("#hynes-slider li:eq(" + nav_position + ")").show().animate({opacity: 1},{queue: false,duration: 500,easing: "easeInSine"});
			$("#hynes-slider-nav li").removeClass("in-view");
			$("#hynes-slider-nav li:eq(" + nav_position + ")").addClass("in-view");
			
			// if user is on the home page, stop the tweets from cycling and display the tweets that match up with the current slide
			if ( body_ID == "home" )
			{
				clearInterval(rotate_tweets);
				$(".tweets").hide().css({opacity: 0});
				$(".tweets:eq(" + nav_position + ")").show().animate({opacity: 1},{queue: false,duration: 400,easing: 'easeInSine'});
			}
			return false;
		}
	});
}


// 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;
	}
}
