/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);


var hoverFlag = false;
function slideSwitcher() {
	if (hoverFlag) return;
	var currentListItem = $('#featureSideBar .selected');
	var nextListItem = currentListItem.parent().next().find('a');
	if (!nextListItem.length)
		nextListItem = $('#featureSideBar a:first');

	nextListItem.click();
};

function listItemClicked() {
	var a = $(this);
	var currentIndex = a.parent().prevAll().length;
	$('#featureSideBar a').removeClass('selected');
	a.addClass('selected');
	$('#featureSlides .slide:visible').fadeOut(function() {
		$('#featureSlides .slide:eq(' + currentIndex + ')').fadeIn();
	});

	return false;
};

var numSlides;
var currentSlide = 1;

function jumpToSlide(slideNumber) {
	if (!numSlides)
		return;
	if (slideNumber > numSlides)
		return;

	if (slideNumber < currentSlide)
		slideOffsetIndex = numSlides + slideNumber - currentSlide;
	else
		slideOffsetIndex = slideNumber - currentSlide;

	$('#slideSet .slider').hide().css({
		'position': '',
		'left': ''
	});
	$('#slideSet .slider:eq(' + slideOffsetIndex + ')').show().css({
		'position': 'absolute',
		'left': '0px'
	});
	if (slideOffsetIndex)
		$('#slideSet .slider').slice(0, slideOffsetIndex).remove().appendTo('#slideSet');
	$('.slideCount').text(slideNumber + ' of ' + numSlides);
	
	currentSlide = slideNumber;
};

$(document).ready(function() {
	// SIDE NAV
	$("#issueMenu li ul").show();
	$("#issueMenu li a").click(function(){						  
		$(this).next("ul:visible").slideUp("normal");
		$(this).next("ul:hidden").slideDown("normal");
		
	});

	// FRONT PAGE ROTATOR
	$('#featureSlides .slide:not(:first)').hide();
	$('#featureSideBar a').click(listItemClicked);
	$('#featureRotator').hover(
		function() { hoverFlag = true; },
		function() { hoverFlag = false; }
	);
	setInterval(slideSwitcher, 6000);

	// IMAGE SLIDER
	numSlides = $('#slideSet .slider').length;

	$('#slideSet').css({
		'overflow': 'hidden',
		'position': 'relative'
	});
	var maxHeight = 0;
	$('#slideSet .slider').each(function(i, el) {
		maxHeight = Math.max(maxHeight, $(el).height());
	});
	$('#slideSet').height(maxHeight);

	if (location.hash && location.hash.match(/#!slide=\d+/)) {
		var hashSlide = parseInt(location.hash.match(/#!slide=\d+/)[0].substring(8), 10);
	}
	else {
		var hashSlide = currentSlide;
	}

	jumpToSlide(hashSlide);

	$(window).hashchange(function() {
		if (location.hash && location.hash.match(/#!slide=\d+/)) {
			var hashSlide = parseInt(location.hash.match(/#!slide=\d+/)[0].substring(8), 10);
			jumpToSlide(hashSlide);
		}
		else if (location.hash == '') {
			jumpToSlide(1);
		}
	});

	$('#slideshowArrowL a').click(function() {
		// Don't do anything if we're currently animating
		if ($('#slideSet :animated').length)
			return false;
		$('#slideSet .slider:last').remove().prependTo('#slideSet').css({
			'position': 'absolute',
			'left': '-685px'
		}).show();
		$('#slideSet .slider:visible').animate({'left': '+=685'}, 'slow', 'linear', function() {
			if ($(this).prev().length) {
				$(this).css({'position': '', 'left': ''});
				return;
			}

			if (currentSlide == 1)
				currentSlide = numSlides;
			else
				currentSlide -= 1;

			location.hash = '#!slide=' + currentSlide;
			$('#slideSet .slider:visible:not(:first)').hide().css({'position': '', 'left': ''});
			$('.slideCount').text(currentSlide + ' of ' + numSlides);
		});
		return false;
	});
	$('#slideshowArrowR a').click(function() {
		// Don't do anything if we're currently animating
		if ($('#slideSet :animated').length)
			return false;
		$('#slideSet .slider:eq(1)').css({'position': 'absolute', 'left': '685px'}).show();
		$('#slideSet .slider:visible').each(function(index) {
			// For the previous slide, animate off to the left, hide, and put at the end
			if (index == 0) {
				$(this).animate({'left': '-=685'}, 'slow', 'linear', function() {
					$(this).hide().remove().appendTo('#slideSet').css({'position': '', 'left': ''});
				});
			}
			// For the new current slide, animate into view and then update slide count
			else {
				$(this).animate({'left': '-=685'}, 'slow', 'linear', function() {
					if (currentSlide == numSlides)
						currentSlide = 1;
					else
						currentSlide += 1;

					location.hash = '#!slide=' + currentSlide;
					$('.slideCount').text(currentSlide + ' of ' + numSlides);
				});
			}
		});
		return false;
	});

	// SIGNUP VALIDATION
	$('#signup').validate({
		submitHandler: function(form) {
			$(".spinner").show();
			$('#article h1').eq(0).hide();
			$('#article p').eq(0).hide();
			$('#btn_submit, #error_container').hide();
			var formdata = $(form).serialize();
			$.post(
				'/handraiser/ProcessFormHandler.do',
				formdata, 
				function(data, text, xhr) {
					var xml;
					if ($.browser.msie && typeof data == "string") { 
						xml = new ActiveXObject("Microsoft.XMLDOM"); 
						xml.async = false; 
						xml.loadXML(data);                 
					} else { 
						xml = data; 
					} 

					var errorStatus = $(xml).find('error').text();
					if (errorStatus == 'true') {
						$("#error_container").html('<label>* There was an error submitting your form. Please try again later.</label>');
						$('#btn_submit').show();
						$(".spinner").hide();
					}
					else {
						$('#signup').hide();
						$(".thank_you_message").show();
						// fire success tracking tags here
					}
				}
			);
		}
	});
});


