
/* MIT Engineering Homepage Effects (requires jQuery, jQuery UI Accordion, and jQuery UI effects) */
/* by White Whale Web Services */

var intro_backgrounds=[ // background images (with caption/credit) for intro quotes
	{file:'sculpture.jpg',caption:'Έργο του Γιώργου Χουλιαρά, καθηγητή γλυπτικής της Ανωτάτης Σχολής Καλών Τεχνών.',credit:'Το γλυπτό βρίσκεται στο χώρο υποδοχής του ΜΕΘΟΔΙΚΟΥ.'},
	{file:'chemical_plant.jpg',caption:'Εργοστάσιο',credit:''},
	{file:'egypt.jpg',caption:'Αρχαία Αίγυπτος',credit:''},
	{file:'leaves.jpg',caption:'Φύλλα',credit:''},
	{file:'dna.jpg',caption:'Τμήμα γονιδιώματος.',credit:''},
	{file:'circuit.jpg',caption:'Τυπωμένο κύκλωμα.',credit:''},
	{file:'bluecircles.jpg',caption:'',credit:''},
	{file:'space.jpg',caption:'Εικόνα από το διάστημα',credit:'Τηλεσκόπιο Hubble'},
	{file:'philosophers.jpg',caption:'Η Σχολή των Αθηνών.',credit:'Πίνακας του Ραφαήλ.'}
];

$(document).ready(function() { // on DOM load
	var questions_offset=0;
	
	// Append the showinfo div to the #intro .content div
	$('#intro .content').append('<div class="showinfo"><a href="#" title="Click for information about this photograph">info</a></div><div class="photo_info_container"><div class="photo_info"><p class="caption"></p><p class="credit"></p></div></div>');
	
	// Define intro styles and caption/credit
	intro_backgrounds.sort(function() { return 0.5 - Math.random();  }); // shuffle the backgrounds
	var background = intro_backgrounds.pop(); // grab one to use for the dean's quote
	$('#intro li:first-child').data('background',background).show().add('#intro h2').css('background-image','url(images/homepage/backgrounds/intro/'+background.file+')'); // and set it
	$('.photo_info_container').hide().find('.caption').html(background.caption).end().find('.credit').html(background.credit);
	var quoteList = $('#intro ul');
	// Get the homepage quotes
	var quotes = new Array();
	$.get('includes/quotes.htm',function(data) {
		var curstyle = 0;
		$(data).find('li').each(function() {
			var background = intro_backgrounds[curstyle++%intro_backgrounds.length]
			$(this).data('background',background);
			quotes.push($(this).css('background-image','url(images/homepage/backgrounds/intro/'+background.file+')').hide());
			if(!($.browser.msie&&parseFloat($.browser.version)<7)) setTimeout(function() { $('<img src="images/homepage/backgrounds/intro/'+background.file+'"/>'); },1000); // preload background images image
		});
		$.each(quotes,function(key,quote) {
			quoteList.append(quote);
		});
		$('#intro li').find('a').click(function(){ return false; }); // cancel clicks on all intro quotes
		// Intro quote navigation
		$('#intro .content').append('<div class="panel_navigation"><span class="previous">&lt; <span>προηγούμενη</span></span><span class="next"><span>επόμενη</span> &gt;</span></div>'); // append next/prev links
		$('#intro .panel_navigation .next').click(function() {
			var current = $('#intro li:visible'),
				next;
			if(current.is(':animated')) return false;
			if(current.next().length) next = current.next(); // if there's a next one, show it	
			else next = current.siblings().eq(0); // otherwise, show first
			var background = next.data('background');
			$('.photo_info_container').hide().find('.caption').html(background.caption).end().find('.credit').html(background.credit);
			$('.showinfo').find('.active').removeClass('active');
			next.fadeIn(function() {
				$('#intro h2').css('background-image','url(images/homepage/backgrounds/intro/'+background.file+')');
				current.hide();
			});
			$('#intro .panel_navigation .previous').fadeIn(400);
		});
		$('#intro .panel_navigation .previous').click(function() {
			var current = $('#intro li:visible'),
				prev;
			if(current.is(':animated')) return false;
			if(current.prev().length) prev = current.prev().fadeIn(); // if there's a previous one, show it
			else prev = current.siblings().filter(':last-child').fadeIn(); // otherwise, show last
			var background = prev.data('background');
			$('.photo_info_container').hide().find('.caption').html(background.caption).end().find('.credit').html(background.credit);
			$('.showinfo').find('.active').removeClass('active');
			prev.show();
			current.fadeOut(function() {
				$('#intro h2').css('background-image','url(images/homepage/backgrounds/intro/'+background.file+')');
			});
		});
	});	
	
	$('#content').accordion({header:'h2',clearStyle: true}); // add accordion effect
	if(location.hash) $('#content').accordion('activate',$('#content').children().index($(location.hash))); // if a specific tab was requested, show it

	$('.panel h2').click(function(){
		$('.photo_info_container').hide();
	});
	
	// News Ticker script
	$('#news .second ul').clone().prependTo('#news').wrap('<div id="newsticker"/>').children().prepend('Τελευταία Νέα:');
	$("#newsticker ul").prepend('<li>Τελευταία Νέα:'+$('#news .first h3').html()+'</li>');
	
	// Scroll newsticker
	tickerTimer = setInterval(scrollTicker,3750);		
	
	// Pause the News Ticker when it is hovered over
	$('#newsticker').hover(function(){
		clearTimeout(tickerTimer);
		clearInterval(tickerTimer);
	},function(){
		tickerTimer = setTimeout(function() {
			scrollTicker();
			tickerTimer = setInterval(scrollTicker,3750);
		},750);
	});
	
	// Opens LW-generated news links in a new window
	$('#news .lw_widget_news li a').attr("target","_top");
	
	// Behavior for the info button on the intro quotes
	$('.showinfo a').click(function(){
		$('.photo_info_container').toggle();
		$(this).toggleClass('active');
		return false;
	});
		
}); //closes document ready


var tickerTimer; 
// scrollTicker function
function scrollTicker() {
	var ticker = $('#newsticker ul'); // keeps jquery from searching for this over and over
	ticker.animate({top:'-=23px'},450,function(){
		var offset = ticker.position().top * -1; // how far has the newsticker scrolled? (positive # of pixels)
		if(offset>=(23*(ticker.find('li').length-1))) {
			ticker.css('top',-23);
		}	
	});
}

function shuffle(set) { // shuffle function to randomize photo backgrounds
    for (var j,x,i=set.length;i;j=parseInt(Math.random()*i),x=set[--i],set[i]=set[j],set[j]=x);
    return set;
};

