/* Author: Kyle Kress */
$(document).ready(function() {
	utopia(3);
	slideShow();
	navAccordian();
	modal();
});

function SearchSite(){
	document.location.href='http://www.usi.edu/search/index.asp?cx=005614860872715963267%3Aw_dluodgdqq&cof=FORID%3A10&ie=UTF-8&q=' + escape(document.getElementById('searchBox').value) + '&sa=Search';
}

function slideShow() {
	$('#gallery a').css({opacity:0});
	$('#gallery a:first').css({opacity:1});
	$('#gallery .caption').css({opacity:1});
	$('#gallery .caption').css({height: $('#gallery a').find('img').css('height')});
	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel')).animate({opacity:1}, 400);
	setInterval('gallery()',9000);
}

function gallery() {	
	//if no IMGs have show class, grab the first img
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	//Get next img, if reaches the end of slideshow, rotate back to the first img
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	//Get next image caption
	var caption = next.find('img').attr('rel');
	next.css({opacity:0}).addClass('show').animate({opacity:1}, 1000);
	current.animate({opacity:0}, 1000)
	.removeClass('show');
	$('#gallery .caption').animate({opacity:0}, { queue:false, duration:0 }).animate({opacity:0}, { queue:true, duration:500 });	
	$('#gallery .caption').animate({opacity:0},100 ).animate({opacity:1},700 );
	$('#gallery .content').html(caption);
}

function navAccordian() {
	$('#sideNav header').click(function() {
		$(this).next().slideToggle(175);
	});
}

function utopia(numOfQuotes) {
	$.getJSON('https://graph.facebook.com/comments/?ids=http%3A%2F%2Fwww.usi.edu%2Fhnh&callback=?', function(json) {
		var quotes = json['http://www.usi.edu/hnh'].data;	
		if(numOfQuotes > quotes.length) {
			numOfQuotes = quotes.length;
		}
		while(numOfQuotes--) {
			randomPick = parseInt(Math.random()*quotes.length);
			$('#utopiaQuotes').append('<blockquote><p>' + quotes[randomPick].message + '</p><cite>' + quotes[randomPick].from.name + '</cite></blockquote></li>');
			 quotes.splice(randomPick, 1);
		}
	});
}

function modal() {
	$('#tellUsButton').click(function(){
		$('#utopiaModal').show(250,function(){
			$(this).resize();
		});
	});

	$('.modalWindow>.modalClose').click(function(){
		$(this).parent().hide(250);
	});
}

