$(document).ready(function() {

////// Confirm PDF download if rel != ''
	$("a[rel!=''][href$='pdf']").bind("click", function(e) {
		var state = $(this).attr('rel');

		var msg = "Press OK only if your organization is located within " + state + ".\n";

		if(confirm(msg))
		{
			return true;
		}

		alert("The application you've selected only applies to organizations within the state of " + state + ".\n\nIf you would like to receive information about Seedco Financial's programs in your organization's area, please contact us at SFSinfo@seedco.org");

		return false;
	});

////// Add nav dropdown for IE6
	$('ul#nav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});


/////// Table Striping
	$('table tbody tr:even').addClass('striped');


/////// Markets map activation
	//$("body.markets div#page div#teasers ul#doubles li img").attr("usemap", "#mapmap");
	//$("body.programs div#page div#teasers ul#doubles li img").attr("usemap", "#mapmap");
	
	var current_map_city = '';
	var map_dom_locations = 
		['body.programs div#page div#teasers ul#doubles li', 
		 'body.markets div#page div#teasers ul#doubles li',
		 'body.donate div#page div#teasers ul#doubles li'];
	var map_cities = {
		map_birmingham: { 
			text: '<p><strong>Birmingham, AL:</strong><br/>Seedco Alabama<br/>505 North 20th Street, Suite 1525<br/>Birmingham, AL 35203<br/>Tel (205) 715-2710<br/>Fax (205) 715-2711<br/>Email <a href="mailto:alabama@seedco.org">Alabama@seedco.org</a></p>',
			url:"/alabama"
		},
		map_batonrouge: { 
			text: '<p><strong>Baton Rouge, LA:</strong><br/>1 Drexel Place Box #152<br/>New Orleans, LA 70125<br/>Tel (504) 520-5727<br/>Fax (504) 482-2267<br/>Email <a href="mailto:louisiana@seedco.org">Louisiana@seedco.org</a></p>',
			url:"/louisiana"
		},
		map_bellechase: { 
			text: '<p><strong>Belle Chasse, LA:</strong><br/>Seedco Belle Chasse<br/>212 Avenue G<br/>Belle Chasse, LA 70037<br/>Tel (504) 392-2454<br/>Fax (504) 392-2456<br/>Email <a href="mailto:louisiana@seedco.org">Louisiana@seedco.org</a></p>',
			url:"/louisiana"
		},
		map_neworleans: { 
			text: '<p><strong>New Orleans, LA:</strong><br/>1 Drexel Place Box #152<br/>New Orleans, LA 70125<br/>Tel (504) 520-5727<br/>Fax (504) 482-2267<br/>Email <a href="mailto:louisiana@seedco.org">Louisiana@seedco.org</a></p>',
			url:"/louisiana"
		},
		map_newyork: { 
			text: '<p><strong>New York, NY:</strong><br/>SFS Headquarters<br/>915 Broadway, 18th Floor<br/>New York, NY 10010<br/>Tel (877) 296-8878<br/>Fax (646) 274-1165<br/>Email <a href="mailto:newyork@seedco.org">NewYork@Seedco.org</a></p>',
			url:"/new-york"
		}
	};


	for(i in map_dom_locations)
  	{
  		var map_img = $(map_dom_locations[i] + " img");
  		
  		map_img.attr("height", "241").mouseout(unhover_city);
		//.attr("src", "/images/plain_map.png")
		
		map_img.before("<span id='map_birmingham' class='map_loc'> </span>");
		map_img.before("<span id='map_batonrouge' class='map_loc'> </span>");
		map_img.before("<span id='map_bellechase' class='map_loc'> </span>");
		map_img.before("<span id='map_neworleans' class='map_loc'> </span>");
		map_img.before("<span id='map_newyork' class='map_loc'> </span>");
	
		map_img.before("<span id='map_hover_box' style='display:none;'></span>");
	
		$(map_dom_locations[i] + ".option1").css("position", "relative");
	}
	
	$("span.map_loc").mouseenter(hover_city);
	$("span.map_loc").click(click_city);
	$("#map_hover_box").click(function() { document.location.href = map_cities[current_map_city].url; } );
	
	function click_city(event_obj)
	{
		document.location.href = map_cities[$(event_obj.target).attr('id')].url;
	}
	
	function hover_city(event_obj)
	{
		if(current_map_city == $(event_obj.target).attr('id'))
		{
			return;
		}

		$('span.map_loc').css("background-position", "0 0");
		$(this).css("background-position", "-20px 0");

		current_map_city = $(event_obj.target).attr('id');

		$("#map_hover_box").css("top", $(this).position().top - 118);
		$("#map_hover_box").css("left", $(this).position().left  - 151);
		$("#map_hover_box").html(map_cities[current_map_city].text);

		if($("#map_hover_box").css("display") == 'none')
		{
			$("#map_hover_box").fadeIn();
		}
	}

	function unhover_city(event_obj)
	{
		$('span.map_loc').css("background-position", "0 0");
		$("#map_hover_box").hide();
		current_map_city = '';
	}

/////// Donate page expanding ul
	$('div.hiddenset h4').click(function() { $(this).next().slideToggle(); });


////// Banner Animation

	var slideWidth = 820;
	var slides = $('div#banner ul#slides_container li');
	var numberOfSlides = slides.length;
	var selectedPosition = 0;
	var slideTimer;
	var slideTimerRunning = false;
	var slidePause = 8000;

	var isHome = $('body').hasClass('home');

	$('ul#slides_container').css('width', slideWidth * (numberOfSlides + 1));
	
	// we duplicate the first slide at the end so we can always auto-scroll to the right
	if (numberOfSlides > 1) {
		var firstSlide = $('ul#slides_container li:first-child').clone();
		$('ul#slides_container').append("<li class='option" + (numberOfSlides + 1) + "'>" + firstSlide.html() + "</li>")
	}
	
	// go to slide animation
	function gotoSlide(thisPosition) {
		$('ul#slides_container').animate({
			'marginLeft' : getOffsetOfSlide(thisPosition)
		}, 600, function() {
			if(!slideTimerRunning) {
				slideTimerActivate();
			}
			slideAnimationComplete();
		});
		manageControls(thisPosition);
	}
	
	// this fires to check if we need to reset the margin
	function slideAnimationComplete()
	{
		if(parseInt($('ul#slides_container').css('marginLeft')) == (-slideWidth * numberOfSlides))
		{
			$('ul#slides_container').css('marginLeft', '0px');
		}
	}
	
	function getOffsetOfSlide(position)
	{
		if(position == 0 && parseInt($('ul#slides_container').css('marginLeft')) <= (-slideWidth * (numberOfSlides - 1)))
		{
			return -slideWidth * (numberOfSlides);
		}
		
		return position * -slideWidth;
	}
	
	// manage the controls to verify selected position
	function manageControls(position) {
		if(position == 0) {
			$('#prev_control').css('visibility', 'hidden')
		} else {
			$('#prev_control').css('visibility', 'visible')
		}

		if(position >= numberOfSlides - 1) {
			$('#next_control').css('visibility', 'hidden')
		} else {
			$('#next_control').css('visibility', 'visible')
		}
	}


	function manageSelectedTabs(thisTab) {

		clearTimeout(slideTimer);
		slideTimerRunning = false;

		thisTab.siblings().removeClass('selected');
		thisTab.addClass('selected');
		selectedPosition = (thisTab.index());
		gotoSlide(selectedPosition);

		var thisBullet = $('#bullet_container li').get(selectedPosition);
		$(thisBullet).siblings().removeClass('selected');
		$(thisBullet).addClass('selected');
	}


	function manageSelectedArrowControls(thisArrow) {

		clearTimeout(slideTimer);
		slideTimerRunning = false;

		selectedPosition = (thisArrow.attr('id')=='next_control') ? selectedPosition+1 : selectedPosition-1;
		
		/*
		

		$('ul#slides_container').animate({
			'marginLeft' : slideWidth*(-selectedPosition)
		});
		*/
		gotoSlide(selectedPosition);

		var thisBullet = $('#bullet_container li').get(selectedPosition);
		$(thisBullet).siblings().removeClass('selected');
		$(thisBullet).addClass('selected');

		if(isHome) {
			var selectedTab = $('ul#slides_controller li').get(selectedPosition);
			manageSelectedTabs($(selectedTab));
		}
	}


	function manageSelectedBulletControls(thisBullet) {

		clearTimeout(slideTimer);
		slideTimerRunning = false;

		thisBullet.siblings().removeClass('selected');
		thisBullet.addClass('selected');
		selectedPosition = (thisBullet.index());
		gotoSlide(selectedPosition);

		if(isHome) {
			var thisTab = $('ul#slides_controller li').get(selectedPosition);
			$(thisTab).siblings().removeClass('selected');
			$(thisTab).addClass('selected');
		}
	}




	// Home Page Banner Controls

	// create tabs for number of slides
	var div = $("#slides_controller");
	div.children().width(div.width() / div.children().size());

	// hover effect on tabs
	$('ul#slides_controller li').hover(function(){
		$(this).toggleClass('hover');
	}, function() {
		$(this).toggleClass('hover');
	});

	// add selected class to first item at start
	$('ul#slides_controller li:first-child').addClass('selected');

	$('ul#slides_controller li').click(function() {
		manageSelectedTabs($(this));
	});



	// add the controls
	var count = numberOfSlides;
	if(count > 1) {
		var miniControllerHTML = '<div id="slides_mini_controller"><span id="prev_control" class="control">Previous</span><ul id="bullet_container">';

		for(i = 0; i < count; i++) {
			miniControllerHTML += '<li class="slideshow_bullet"><a href="#">&bull;</a></li>';
		}
		miniControllerHTML += '</ul><span id="next_control" class="control">Next</span></div>';
		$('div#banner').prepend(miniControllerHTML);

		slideTimerActivate();
	}

	manageControls(selectedPosition);

	$('#bullet_container li:first-child').addClass('selected');


	// activate control arrows functionality
	$('.control').click(function(){
		manageSelectedArrowControls($(this));
	});


	// activate control bullets functionality
	$('#bullet_container li').click(function() {
		manageSelectedBulletControls($(this));
	});



	// activate auto slideshow
	function autoSlide() {

		slideTimerRunning = true;

		if (selectedPosition == numberOfSlides - 1) {
			selectedPosition = 0;
		} else {
			selectedPosition++;
		}

		gotoSlide(selectedPosition);
		manageControls(selectedPosition);

		var thisTab = $('ul#slides_controller li').get(selectedPosition);
		$(thisTab).siblings().removeClass('selected');
		$(thisTab).addClass('selected');

		var thisBullet = $('#bullet_container li').get(selectedPosition);
		$(thisBullet).siblings().removeClass('selected');
		$(thisBullet).addClass('selected');

		slideTimerActivate();
	}

	function slideTimerActivate() {
		clearTimeout(slideTimer);
		slideTimer = setTimeout(autoSlide, slidePause);
	}


	
	//open external links in new window
	$('a[href^="http://"]').attr("target", "_blank");

});








