/* =====================================================================
 *  JavaScript Library - africanfilmfestival.org
 *  author: Gabriel Nagmay <gabriel.nagmay@pcc.edu>
 *  compress with jsmin <http://fmarcia.info/jsmin/test.html>
 *  created: 	2009-12-24 gabriel nagmay
 *  updated: 	2010-12-20 gabriel nagmay -	updated for 2011 
 *				2011-01-17 gabriel nagmay - minor updates to background resize	
 *				2012-02-02 gabriel nagmay - don't animate slideshow if only 1 slide!
 * ===================================================================== */

// menu settings
var timeout    = 500;
var closetimer = 0;
var menuitem = 0;

$(document).ready(function(){  
	presentation();		// boo!
	dropDown();			// navigation drop down
	linkSetup();   		// update links
	slideShow(4000); 	// execute the slideshow (set duration)'
	
});


/* presentation
=============================================================================== */ 
function presentation(){
	
	// add figure image
	$("body").append('<img id="figures" />');
	
	// force resize function to set source, css
	$(window).resize();
}
	
// on window resize
$(window).resize(function(){
	 //alert(h);
        var h = $(window).height();
		var w = $(window).width();
		if(w > 1370){
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-1280.png", width:"484", height:"872"});
				var offset = Math.floor( (w-815-484)/2 );
				var m_offset = '0 '+offset+'px 0 auto';
				//alert( m_offset);
				$("#page").css({'width':850,'margin':m_offset});
				if(h > 892){
					$("#figures").css({'position':'fixed','bottom':0,'top':'auto', 'left':'auto', 'right':815+offset});
				}else{
					$("#figures").css({'position':'fixed','bottom':'auto','top':20, 'left':'auto', 'right':815+offset});	
				}

		}
		else{
			$("#page").css({'width':'auto','margin':'0 0 0 auto'});

			if(w > 1150){
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-1280.png", width:"484", height:"872"});
				if(h > 892){
					$("#figures").css({'position':'fixed','bottom':0,'top':'auto', 'left':'auto', 'right':815});
				}else{
					$("#figures").css({'position':'fixed','bottom':'auto','top':20, 'left':'auto', 'right':815});	
				}
			}else if(w > 1050){
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-1024.png", width:"300", height:"540"});
				if(h > 564){
					$("#figures").css({'position':'fixed','bottom':0,'top':'auto', 'left':'auto', 'right':825});
				}else{
					$("#figures").css({'position':'fixed','bottom':'auto','top':24, 'left':'auto', 'right':825});	
				}	
			}else if(w > 950){
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-950.png", width:"167", height:"300"});
				if(h > 423){
					$("#figures").css({'position':'fixed','bottom':0,'top':'auto', 'left':'auto', 'right':832});
				}else{
					$("#figures").css({'position':'fixed','bottom':'auto','top':124, 'left':'auto', 'right':832});	
				}	
			}else if(w > 820){
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-800.png", width:"92", height:"165"}).css({'position':'absolute','top':15, 'right':700, 'left':'auto'});;
			}else{
				$("#figures").attr({ src:"/wp-content/themes/cfaf2012/_source/style/images/figures-800.png", width:"92", height:"165"}).css({'position':'absolute','top':15, 'right':'auto', 'left':20});;
			}
		
		}
		
		
});

	


/* drop down
=============================================================================== */ 
function dropDown(){
	
	

	if( $('#primary-nav')){
   		$('#primary-nav > li').bind('mouseover', nav_open);
   		$('#primary-nav > li').bind('mouseout',  nav_timer);
	}
	function nav_open(){  
	   nav_canceltimer();
	   nav_close();
	   menuitem = $(this).find('ul');//;.css('visibility', 'visible');
	   menuitem.show();
	}
	
	function nav_close(){  
		if(menuitem){
			menuitem.hide();
		}
	}
	
	function nav_timer(){  
		closetimer = window.setTimeout(nav_close, timeout);
	}
	
	function nav_canceltimer(){  
		if(closetimer){  
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	// clicking anywhere else
	//document.onclick = nav_close;
	
}
	




/* update links
=============================================================================== */ 
function linkSetup() {
	// current link (link to self + account for index files)
	var locationPath = filterPath(location.pathname);
	$("a[href]").each( function () {	
		var thisPath = filterPath(this.pathname);
		if (  locationPath == thisPath && (location.hostname == this.hostname) && !this.href.match(/(\?|#)/) ) { // ? eliminates cfm pages like schedule.cfm?var=3
			$(this).addClass('current');
		}
	
	});	
}
function filterPath(string) {  // used in current link below. could be moved to var re
  return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
 }

 
/* slidechow
=============================================================================== */ 
function slideShow(speed) {

	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0}).addClass("show");
	
	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
	/* add link to first*/
	$('#slideshow-caption').click( function(){
				window.location = $('ul.slideshow a:first').attr('href');
	});
	
		
	//Display the caption
	$('#slideshow-caption').css({opacity: 0.8, bottom:0});
	
	// No animation if only 2 (1 slide, 1 caption)
	if($("ul.slideshow li").length <= 2){
		clearInterval(timer);	
		//pause the slideshow on mouse over
		$('ul.slideshow').hover(
			function () {
				$('ul.slideshow .slideshow-caption-container p').slideDown();
			}, 	
			function () {
				$('ul.slideshow .slideshow-caption-container p').slideUp();			
			}
		);
	}
	// Otherwise, call the gallery function to run the slideshow
	else{
		var timer = setInterval('gallery()',speed);
	
		//pause the slideshow on mouse over
		$('ul.slideshow').hover(
			function () {
				clearInterval(timer);
				$('ul.slideshow .slideshow-caption-container p').slideDown();
			}, 	
			function () {
				timer = setInterval('gallery()',speed);
				$('ul.slideshow .slideshow-caption-container p').slideUp();			
			}
		);
	}
	/*$('ul.slideshow').click( function(){
		window.location = $(this).find("a").attr("href"); 
	});*/
	
}

function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption') ? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');
	var href = next.find('a').attr('href');	
		
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	//Hide the caption first, and then set and display the caption
	//$('#slideshow-caption').animate({bottom:-70}, 300, function () {
			//Display the content
			$('#slideshow-caption h3').html(title);
			$('#slideshow-caption p').html(desc);		
			$('#slideshow-caption').click( function(){
				window.location = href;
			});
			//$('#slideshow-caption').animate({bottom:0}, 500);	
	//});		

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}


/* background
=============================================================================== 
var flexBackground = function(){
	
	// Define the size of our background image - used to set ratio
	var bgImageSize = { width : 1150, height : 1150 };
	// Declare and define variables
	var $window,
		$body,
		imageID = "expando",
		tallClass = 'tall',
		wideClass = 'wide',
		$bgImage, $wrapper, img, url, imgAR;
		
	//Are we dealing with ie6?
	var ie6 = ($.browser.msie && parseInt($.browser.version, 10) <= 6);
	
	//Set up the action that happens on resize
	var resizeAction = function() {
	
		var win = {
			height : $window.height(),
			width : $window.width()
		};

		// The current aspect ratio of the window
		var winAR = win.width / win.height;

		// Determine if we need to show the image and whether it needs to stretch tall or wide
		if ((winAR < imgAR)) {
			$body
				.removeClass(wideClass)
				.addClass(tallClass);
			// Center the image
			//$bgImage.css('left', Math.min(((win.width - bgImageSize.width) / 2), 0));
			$bgImage.css('right', 0);
		} else {
			$body
				.addClass(wideClass)
				.removeClass(tallClass);
			$bgImage.css('right', 0);
		}
		
		// Need to fix the height of the wrapper for IE6
		if (ie6) {
			$wrapper.css('height', win.height);
		}
	};
	
	return {
		
		//Sets up the basic functionality
		initialize : function() {
			
			// Grab elements we'll reference throughout
			$window = $(window);
			$body = $('body');
			
			// Parse out the URL of the background image and drop out if we don't have one
			url = $body.css('background-image').replace(/^url\(("|')?|("|')?\);?$/g, '') || false;	
			if (!url || url === "none" || url === "") {
				return;
			}
			
			// Get the aspect ratio of the image
			imgAR = bgImageSize.width / bgImageSize.height;

			// Create a new image element
			$bgImage = $('<img />')
						.attr('src', url)
						.attr('id', imageID);
						
			// Create a wrapper and append the image to it.
			// The wrapper ensures we don't get scrollbars.
			$wrapper = $('<div></div>')
							.css({
								'overflow' : 'hidden',
								'width' : '100%',
								'height' : '100%',
								'z-index' : '-1'
							})
							.append($bgImage)
							.appendTo($body);
							
			// IE6 Doesn't do position: fixed -apply a class which gets used in the CSS to emulate position: fixed
			if (ie6) {
				$wrapper.addClass('ie6fixed');
			} else {
				$wrapper.css({
					'position' : 'fixed',
					'top' : 0,
					'left' : 0
				});
			}
			
			// Set up a resize listener to add/remove classes from the body 
			$window.bind('resize', resizeAction);

			// Set it up by triggering a resize
			$window.trigger('resize');
		}
	};
}();

$(document).ready(flexBackground.initialize);

*/ 
