// when the DOM is ready...
var player;
var playerInitialized = false;
var currentPlayer;
function playerReady(obj) {
	var id = obj['id'];
	if( id == 'n0' )
	{
		playerInitialized = true;
		player = document.getElementById(obj['id']);
		player.sendEvent("PLAY","true");
		currentPlayer = player;
	}
};

jQuery(document).ready(function($) {
	
	
	
	// store mouse position
	var $mouseX = 0;
	var $mouseY = 0;
	$().mousemove( function(e) {
	   $mouseX = e.pageX; 
	   $mouseY = e.pageY;
	 });
	
	// add tooltips
	$(".view-work").each(function(i) {
	    setToolTip( $(this) );
	});
	
	// initialize the tooltip
	function setToolTip( element ) {
		element.mouseenter(function() {
			// on hovering over, find the element we want to fade *up*
			    var fade = $("#tooltip");

			    // if the element is currently being animated (to a fadeOut)...
			    if (fade.is(':animated')) {
			      // ...take it's current opacity back up to 1
			      fade.stop().fadeTo(250, 1);
			    } else {
			      // fade in quickly
			      fade.fadeIn(250);
			    }
	    }).mouseleave(function() {
			var fade = $("#tooltip");
			
		  	if (fade.is(':animated')) {
			      fade.stop().fadeTo(500, 0);
			    } else {
			      // fade away slowly
			      fade.fadeOut(500);
			    }
	    }).mousemove(function(){
			$("#tooltip").css({'left' : $mouseX + 5, 'top' : $mouseY - 35 });			
	    });;
	}
	
	function hideToolTip() {
		$("#tooltip").addClass("tool-tip-out");
	}
	
	
	
	/* stories video player functionality */
	function playVideo( element ) {
		if( playerInitialized )
		{
			var player = document.getElementById( 'n'+ element.attr("id") );
			if( player != currentPlayer )
			{
				player.sendEvent("PLAY","true");
				player.sendEvent("SEEK",0);
				currentPlayer.sendEvent("PLAY","false");
				currentPlayer = player;
			}
		}
	}

    var $panels = $('#slider .scroll-container > div');
	if( $panels.length > 0 )
	{
		
    	var $container = $('#slider .scroll-container');
		
	    // if false, we'll float all the panels left and fix the width 
	    // of the container
	    var horizontal = true;

	    // float the panels left if we're going horizontal
	    if (horizontal) {
	        $panels.css({
	            'float' : 'left',
	            'position' : 'relative' // IE fix to ensure overflow is hidden
	        });

	        // calculate a new width for the container (so it holds all panels)
	        $container.css('width', $panels[0].offsetWidth * $panels.length);
	    }

	    // collect the scroll object, at the same time apply the hidden overflow
	    // to remove the default scrollbars that will appear
	    var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	    // apply our left + right buttons
	    if( $panels.length > 1 )
		{
			$scroll
				.after('<div id="scroll-button-container"><div class="scroll-buttons left"></div><div class="scroll-buttons right"></div></div>');
		}
	
	    // handle nav selection
	    function selectNav() {
	        $(this)
	            .parents('ul:first')
	                .find('a')
	                    .removeClass('selected')
	                .end()
	            .end()
	            .addClass('selected');
			playVideo( $(this) );
	    }

	    $('.sub-nav').find('a').click(selectNav);

	    // go find the navigation link that has this target and select the nav
	    function trigger(data) {
	        var el = $('.sub-nav').find('a[href$="' + data.id + '"]').get(0);
	        selectNav.call(el);
	    }

	    if (window.location.hash) {
   	        trigger({ id : window.location.hash.substr(1) });
   	    } else {
   	        $('ul.sub-nav a:first').click();
   	    }


	    var scrollOptions = {
	        target: $scroll, // the element that has the overflow

	        // can be a selector which will be relative to the target
	        items: $panels,

	        navigation: '.sub-nav a',

	        // selectors are NOT relative to document, i.e. make sure they're unique
	        prev: 'div.left', 
	        next: 'div.right',

	        // allow the scroll effect to run both directions
	        axis: 'xy',

	        onAfter: trigger, // our final callback


	        // duration of the sliding effect
	        duration: 500,

	        // easing - can be used with the easing plugin: 
	        // http://gsgd.co.uk/sandbox/jquery/easing/
	        easing: 'swing'
	    };

	    // apply serialScroll to the slider - we chose this plugin because it 
	    // supports// the indexed next and previous scroll along with hooking 
	    // in to our navigation.
	    $('#slider').serialScroll(scrollOptions);

	    // now apply localScroll to hook any other arbitrary links to trigger 
	    // the effect
	    $.localScroll(scrollOptions);
		
	    // finally, if the URL has a hash, move the slider in to position, 
	    // setting the duration to 1 because I don't want it to scroll in the
	    // very first page load.  We don't always need this, but it ensures
	    // the positioning is absolutely spot on when the pages loads.
	    scrollOptions.duration = 1;
	    $.localScroll.hash(scrollOptions);
	
		
	
	}
	
	function setNavRollOver( element ) {
		$("#" + element + "-area").mouseenter(function(){
	 	  $("#nav-" + element).removeClass("nav-out");
	    }).mouseleave(function(){
		  $("#nav-" + element).addClass("nav-out");
	      // $("#nav-gallery").removeClass("nav-over");
	    });
	}
	
	setNavRollOver( 'stories' );
	setNavRollOver( 'gallery' );
	setNavRollOver( 'about' );
	setNavRollOver( 'blog' );
	
	
	
	// $('h1 a').simpletip({ fixed: false, content: 'Bloah' }); 
	
});