$(document).ready(function() {
	dropDown();
	$(function() {
	    if (window.PIE) {
	        $('ul.tabs li').each(function() {
	            PIE.attach(this);
	        });
	    }
	});
	// activate map
	mapFunctionality();
	$("a.who-are-you").fancybox();
});

$(function() {
	$(".scrollable").scrollable({circular: true}).navigator().autoscroll({interval: 4000});
});
/*
$(function() {
	$(".film").scrollable({circular: false});
});

$(".film .items img").click(function() {

	// see if same thumb is being clicked
	if ($(this).hasClass("active")) { return; }

	var url = $(this).attr("alt");

	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $("#image_wrap").fadeTo("medium", 0.5);

	var img = new Image();

	var thisTitle = $(this).attr("title");
	$(".title").html(thisTitle);
			
			
	// call this function after it's loaded
	img.onload = function() {

		// make wrapper fully visible
		wrap.fadeTo("fast", 1);

		// change the image
		wrap.find("img").attr("src", url);
		
	};

	img.src = url;
	
	// activate item
	$(".film .items img").removeClass("active");
	$(this).addClass("active");

// when page loads simulate a "click" on the first image
}).filter(":first").click();
*/

$(function() {
	$(".film").scrollable({circular: true}).scrollableAddClones();
	$(".film .items img").click(function() {
	
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }
	
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("src").replace("_t", "");
	
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap").fadeTo("medium", 1);
	
		// the large image from www.flickr.com
		var img = new Image();
	
		var thisTitle = $(this).attr("title");
		$(".title").html(thisTitle);
	
		// call this function after it's loaded
		img.onload = function() {
	
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
	
			// change the image
			wrap.find("img").attr("src", url);
	
		};
	
		// begin loading the image from www.flickr.com
		img.src = url;
	
		// activate item
		$(".film .items img").removeClass("active");
		$(this).addClass("active");
	
	// when page loads simulate a "click" on the first image
	}).filter(":first").click();
	
});
$.fn.scrollableAddClones = function(addItems) {
  // grab scrollable plugin
  var scrollable;
  if (!(scrollable = $(this).data('scrollable')) || !scrollable.getConf().circular)
    return;
  // grab scrollable elements and remember it's count
  var nodes = scrollable.getItems();
  var length = nodes.length;
  // grab class for the nodes
  var clonedClass = scrollable.getConf().clonedClass;
  // get wrap object to append the clones to
  var wrap = scrollable.getItemWrap();
  // fill as much nodes as needed for 500 pixels
  if (!addItems) addItems = Math.ceil(500 / nodes.eq(1).width());
  // create fake container to add the clones to (max 15 clones)
  var newNodesAppend = $('<span />');
  for (var i = 1; i <= (addItems < 15 ? addItems : 15); i++)
    nodes.eq(i % length).clone().addClass(clonedClass).appendTo(newNodesAppend);
  // insert HTML
  newNodesAppend.children().appendTo(wrap);
}



function dropDown(){
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}

	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 10, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#mainNav li div.sub").css({'opacity':'0'});
	$("ul#mainNav li").hoverIntent(config);
}

function mapFunctionality(){
	
	// create locations from list and plot on map
	$('ul.map-list').children('li.location').each(
		function(){
			// create new id
			var id = $('a', this).attr('id').concat('-loc')
			// collect coordinates
			var coords = $('a', this).attr('rel').split('-');
			// add location to map
			$('#map-area').append($('<a class="map-loc" href="javascript:void(0)"/>'));
			// set id, plot, and fade in each location
			$('.map-loc:last').attr('id', id)
			.css({left: coords[0] + 'px', top: coords[1] + 'px'})
			.hide()
			.fadeIn();
		}
	);
	
	// animate list hover
	$('li.location').hover(
		function(){ // hover over
			if(!$('a', this).is('.selected')){ $(this).stop().animate({width:300},{queue:false,duration:100}) };
		},
		function(){ // hover out
			if(!$('a', this).is('.selected')){ $(this).stop().animate({width:295},{queue:false,duration:50}) };
		}
	);
	
	// vars to store selected state
	var _listSelection;
	var _mapSelection;
	
	$('li.location').click( // register list location clicks
		function(){
			locationClick(this, false);
		}
	);
	
	$('a.map-loc').click( // register map location clicks
		function(){
			locationClick(this, true);
		}
	);
	
	// location click handler
	function locationClick(obj, fromMap){
		// remove class from last selected
		if(_listSelection){ 
			_listSelection.removeClass('selected');
			_listSelection.parent().stop().animate({width:295},{queue:false,duration:50}); // reset width
		};
		if(_mapSelection){ _mapSelection.removeClass('selected') };
		// determine type
		if(fromMap){
			//console.log('map:', obj);
			_listSelection = $('#' + $(obj).attr('id').replace('-loc', ''));
			_mapSelection = $(obj);
		}
		else{
			//console.log('list item:', obj);
			_listSelection = $('a', obj);
			_mapSelection = $('#' + $('a', obj).attr('id') + '-loc');
		}	
		// add selected classes
		_listSelection.addClass('selected');
		_mapSelection.addClass('selected');
		// animate width
		_listSelection.parent().stop().animate({width:300},{queue:false,duration:100});
	}
	
}

$(".jumpmenu").change(function() {
        var val = $(this).selectedValues();
        if (val != '') {
            location.href=val;
        }
    });

//CREATES NEW WINDOW FOR EXTERNAL LINKS
$(function(){
			$('a.new-window').click(function(){
			window.open(this.href);
			return false;
			});
		});
		


