/*
 * Superfish menu initialization 
 */
jQuery(document).ready(function(){
    // Dont throw errors on the blog -- Is this even user anywhere?
    try {
        jQuery("ul.sf-menu").superfish();
    } catch(e) { }
});  

/*
 * Dynamic external script loading on window.load
 * @param location Path to JS file
 * @param callback Optional function called once the script has finished loading
 */
 
function loadScript(location, callback) {
    jQuery(window).load(function(){
        var domscript = document.createElement("script");
        domscript.src = location;
        domscript.type = "text/javascript";
        domscript.onloadDone = false;

        if (callback) {
            domscript.onload = function() {
                domscript.onloadDone = true;                
                callback();
            };
            domscript.onreadystatechange = function() {
                if ((domscript.readyState === "complete" || domscript.readyState === "loaded") && !domscript.onloadDone) {
                    domscript.onloadDone = true;                    
                    callback();
                }
            };
        }
        document.getElementsByTagName("head")[0].appendChild(domscript);        
    });
}


/*
 * Google Analytics Event Tracking for Videos
 * @param gaVar  Google Analytics tracking object
 */

 function videoTracking(_gaq) {
     if (typeof flowplayer == 'function' && $f()) {
         var player = $f();

         // Track page load as event
         _gaq.push(['_trackEvent', 'Videos', 'Page Load', player.getClip(0).url]);

         // Track video play
         player.onCuepoint(250, function() {
             if (player.getStatus().muted == false) {
                 _gaq.push(['_trackEvent', 'Videos', 'Play', this.getClip().url]);
             }
         });

         // Track pause click
         player.onPause(function() {
             _gaq.push(['_trackEvent', 'Videos', 'Pause', this.getClip().url, Math.round(this.getTime())]);
         });

         // Track video finished
         player.onFinish(function() {
             _gaq.push(['_trackEvent', 'Videos', 'Finished', this.getClip().url]);
         });

         // Track event where buffer empties and video pauses while it refills
         player.onBufferEmpty(function() {
             _gaq.push(['_trackEvent', 'Videos', 'Buffer Empty', this.getClip().url, Math.round(this.getTime())]);
         });

         // Track action button click, if one if present
         if (typeof ab == 'object') {
             jQuery(ab).click(function() {
                 _gaq.push(['_trackEvent', 'Videos', 'Action Button', player.getClip(0).url]);
             });        
         }

         // Track 'Play Again' click 
         if (typeof pa == 'object') {
             jQuery(pa).click(function() {
                 _gaq.push(['_trackEvent', 'Videos', 'Play Again', player.getClip(0).url]);
             });        
         }       
     }
 }


// Initialize any Fancybox displays on the current page..
$(window).load(function() {
    // Fancybox is non-working in IE7 for some reason
    try {
        $('a.fancybox').fancybox();
    } catch(e) { }
});

