$(function(){
	initSlideShow();
	initState();
	
	//Landing Page Form
	if($('.landingTB').length){
		if($('.landingTB').val().length > 0) {
			$('#email').hide();
		}
		
		if($('.landingZip').val().length > 0) {
			$('#zip').hide();
		}
		
		$('.fieldContainer').click(function() {
			$(this)
				.child('label').hide()
				.siblings('input').focus();
		});
	}else{
		$('#publishForm').ajaxForm({
			dataType: 'json',
			success: function(data) {
				if (data.success) {
					$('#publishForm').hide();
					$('#signupSuccess').show();
				} else {
						//alert('Failed with the following errors: '+data.errors.join(', '));
				}
			}
		});
	}
	
	//Twitter Feed
	
	$(".thehermancain").tweet({
		join_text: "auto",
		username: "THEHermanCain",
		avatar_size: 48,
		count: 7,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	$(".connection").tweet({
		join_text: "auto",
		username: "CainConnections",
		avatar_size: 48,
		count: 7,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
});

function initState() {
	var state = geoip_region();
	var stateName = geoip_region_name();
	$('#logo').css({
		'background-image': 'url(/images/states/' + state.toLowerCase() + '.png)',
		'background-repeat': 'no-repeat',
		'background-position': 'top'
	});
	
	var stateMenu = $('.stateMenu');
	if(stateMenu.length){
		stateMenu
			.find('#stateName').html(stateName).end()
			.find('#stateRed').attr('src', '/images/states/red/' + state.toLowerCase() + '.png');	
	}	
}

var curIndex = 0;
var imageCount;
var slideShowTimer;
var slideShowTimerInterval = 5000;
function initSlideShow() {
	var slideShow = $('#hero');
	if(!slideShow)
		return;
		
	var hero = slideShow.find('figure').first();
	imageCount = hero.children('span').length;
	
	// Click event handler for slideshow arrows
	slideShow.find('.heroButton').click(function(){
		var button = $(this);
		// Abort if button is disabled
		if(button.hasClass('disabled'))
			return;
			
		toggleTimer(false);
		
		if(button.attr('id') == 'heroButtonRight') {
			curIndex++;	
		}else{
			curIndex--;
		}
		
		// Get margin-left of hero image container and strip 'px'
		var heroMarginLeft = hero.css('marginLeft');
		heroMarginLeft = Number(heroMarginLeft.substring(0, heroMarginLeft.indexOf('p')));
		
		// Set index to first image if past last image
		if(curIndex+1 > imageCount)
			curIndex = 0;
		
		// Fire slideshow transition
		changeSlideShowImage();
		toggleTimer(true);
	});
	
	// Click event handler for menu items
	slideShow.find('#heroMenu').find('li').click(function() {
		toggleTimer(false);
		var menuItem = $(this);
		
		// Abort if menu item is already active
		if(menuItem.hasClass('active'))
			return;
			
		// Get index of clicked menu item
		curIndex = menuItem.siblings().andSelf().index(menuItem);
		// Fire slideshow transition
		changeSlideShowImage(curIndex);
		toggleTimer(true);
	});
	
	toggleTimer(true);
}

function toggleTimer(start) {
	if(start) {
		slideShowTimer = setTimeout("timerComplete()", slideShowTimerInterval);
	}else{
		clearTimeout(slideShowTimer);	
	}
}

function timerComplete() {
	curIndex++;
	// Set index to first image if past last image
	if(curIndex+1 > imageCount)
		curIndex = 0;
		
	changeSlideShowImage();
	
	slideShowTimer = setTimeout("timerComplete()", slideShowTimerInterval);
}

function changeSlideShowImage() {
	// Animate to next image in slideshow	
	$('#heroImageContainer').animate({ marginLeft: '-' + (curIndex) * 999 + 'px' }, 1200, 'easeOutExpo');
	
	// Select menu item for the current image index
	setSelectedSlideShowMenuItem(curIndex);
	
	// Clear disabled class from left button if needed
	if(curIndex == 1) {
		$('#heroButtonLeft').removeClass('disabled');
	}else if(curIndex == imageCount){
		$('#heroButtonLeft').addClass('disabled');
	}else if(curIndex == 0) {
		$('#heroButtonLeft').addClass('disabled');
	}
}

function setSelectedSlideShowMenuItem(menuItemIndex) {
	$('#heroMenu')
		.find('li').removeClass('active')
		.eq(menuItemIndex).addClass('active');
}

// Clear Text Feild
function clearText(thefield){
	if (thefield.defaultValue==thefield.value){
		thefield.value = ""
	}
}
  
//Facebook Feed
(function(d, s, id) {
  	var js, fjs = d.getElementsByTagName(s)[0];
  	if (d.getElementById(id)) {return;}
  		js = d.createElement(s); js.id = id;
  		js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  		fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
	
//Hide and Show
function show(div){
	$('.hide').hide();
	$('#' + div).show();
}


