/* ---------------------------- 
[Global Javascript]

Project: 		Marco Macon
Last change:	10/18/11 (Added video events)
Created by:		Third Wave Digital (www.thirdwavedigital.com)
------------------------------- */

$(document).ready(function() {
	
	/* ---------------------------- */
	/* Slideshow & Image Sizing
	/* ---------------------------- */
	
	if ($("#homepage").length > 0){
		// It's the homepage
		$("#slideshow img").each (function() {
			$("#slideshow-nav").append('<li><a href="">&bull;</a></li>');
		});
		$("#slideshow-nav li").click(function(event) {
			$('#slideshow').cycle('pause');
			resizeImage();
		});
		function onBefore(curr,next,opts) {

		};
		function onAfter(curr, next, opts) {
			$("#slideshow-nav li a").removeClass("active")
			$("#slideshow-nav li:eq("+opts.currSlide+") a").addClass("active")
		};
		$('#slideshow').cycle({
			fx:    'fade', 
			speed:  1000,			
			timeout: 6000,
			before: onBefore,
			after: onAfter,
			containerResize: false,
			slideResize: false,
			fit: 1,
			pager: '#slideshow-nav',
			pagerAnchorBuilder: function(idx, slide) { 
				return '#slideshow-nav li:eq(' + idx + ') a'; 
			}
		});
	} else {
		// It's an interior page
		$('#slideshow').cycle({
			timeout:  0,
			random: 1			
		});
	};
	
	$(window).resize(function() {
		resizeImage();
	});
	
	function resizeImage() {
		$('#slideshow img').each(function(){
			$(this).css('width','100%').css('height','auto');
		});
	};
	
	/* ---------------------------- */
	/* Dropdowns
	/* ---------------------------- */

	var config = {    
         sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)    
         interval: 30,  // number = milliseconds for onMouseOver polling interval    
         over: doOpen,   // function = onMouseOver callback (REQUIRED)    
         timeout: 30,   // number = milliseconds delay before onMouseOut    
         out: doClose    // function = onMouseOut callback (REQUIRED)    
    };

    function doOpen() {
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    }
 
    function doClose() {
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    }

    $("nav ul li").hoverIntent(config);
	
	/* ---------------------------- */
	/* Image Rollovers & Preload
	/* ---------------------------- */

	$(".hover").each(function() {
        if ($(this).attr("src").match(/-on\.(.+)$/i)) {
            $(this).removeClass("hover");
        }
    });
	
   	$(".hover").not(".active").hover(function() {
			s = $(this).attr("src").replace(/\.(.+)$/i, "-on.$1");
        	$(this).attr("src", s);
    }, function() {
			s = $(this).attr("src").replace(/-on\.(.+)$/i, ".$1");
        	$(this).attr("src", s);
	});
	
	var preload = new Array();
	
    $(".hover").each(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "-on.$1");
        preload.push(s)
    });
	
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
	
	/* ---------------------------- */
	/* Twitter Feed
	/* ---------------------------- */
	
	$('#twitter').jTweetsAnywhere({
    	username: 'MarcoMacon',
    	count: 1
	});

	  	
});
