﻿/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($) { $.fn.hoverIntent = function(f, g) { var cfg = { sensitivity: 7, interval: 100, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function(ev) { cX = ev.pageX; cY = ev.pageY; }; var compare = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]); } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } }; var delay = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]); }; var handleHover = function(e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } } if (p == this) { return false; } var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } if (e.type == "mouseover") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function() { delay(ev, ob); }, cfg.timeout); } } }; return this.mouseover(handleHover).mouseout(handleHover); }; })(jQuery);
$(document).ready(function() {

    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() + 20;
                });
            };
        })(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: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };
    $('ul#topnav li .sub').css({ 'opacity': '0' });
    $('ul#topnav li').hoverIntent(config);
    // initialize scrollable
    var mediarotator = $('#mainmedia_rotator').scrollable({ vertical: true, circular: true }).navigator().autoscroll({ autopause: false, interval: 18000 });
    window.api = mediarotator.data("scrollable");
    //window.api.play();
    var scrollableconfig = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onMouseOver polling interval
        over: MediaHover, // function = onMouseOver callback (REQUIRED)    
        timeout: 1000, // number = milliseconds delay before onMouseOut
        out: MediaHoverOut // function = onMouseOut callback (REQUIRED)
    };
    var naviconfig = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: MediaNaviHover, // function = onMouseOver callback (REQUIRED)    
        timeout: 200, // number = milliseconds delay before onMouseOut
        out: MediaNaviHoverOut // function = onMouseOut callback (REQUIRED)
    };
    function MediaHover() {
        //pause/stop on mouseover mediaarea
        api.stop();

    }
    function MediaHoverOut() {
        //play on mouseout mediaarea
        //api.play();
        setTimeout(function() { api.play() }, 8000);
    }
    function MediaNaviHover() {
        $(this).click();
        $(this).delay(20000).bind('click', NaviGoToClick);
        return true;
    }
    function MediaNaviHoverOut() {
        $(this).unbind('click', NaviGoToClick);
        return true;
    }
    function NaviGoToClick() {
        var link = $(this).find("a.m-link");
        var url = (link.attr("href"));
        window.location = url;
    }

    $('#mainmedia').hoverIntent(scrollableconfig);
    $('#mainmedia_navi li').hoverIntent(naviconfig);

    $('#mainmedia_navi li:first').addClass('active');
    $('#mainmedia_navi li:last').addClass('last');
    $('p.image-text:empty').hide();
    //clickable puffs etc
    $('.puff-item').each(function() {
        $(this).addClass("clickable");
        var link = $(this).find("a.puff-link.archive");
        var url = (link.attr("href"));
        link.hide();
        if ($(this).hasClass("video") || url.indexOf("vimeo") != -1) {
            $(this).click(function() {
                InitVideo(url)
            });
        }
        else {
            $(this).click(function() {
                window.location = url;
            });
        }
    });
    $("#lowerarea .page-list li").each(function() {
        $(this).addClass("clickable");
        var link = $(this).find("a");
        $(this).click(function() {
            var url = (link.attr("href"));
            window.location = url;
        });
    });
    /*$('ul li:has(.teaser-text)').each(function() {
    //$(this).addClass("clickable");
    var link = $(this).find("a");
    $(this).click(function() {
    var url = (link.attr("href"));
    window.location = url;
    });
    });*/
    $("#video-overlay .close").click(function() { swfobject.removeSWF(); $("#videoplayer").html(""); $("#video-overlay").fadeOut("slow"); });
});

function InitVideo(url) {
    swfobject.removeSWF();
    $("#video-overlay").fadeIn("slow");//.find("#videoplayer").html("");
    //var params = { allowScriptAccess: "always" };
    //var atts = { id: "myytplayer" };
    //swfobject.embedSWF(url, "videoplayer", "940", "390", "8", null, null, params, atts);
    var video_id = url.replace("http://www.vimeo.com/", "");
	var moogaloop = false;
    // Run the javascript when the page is ready
    var swf_id = 'videoplayer';
    var flashvars = { clip_id: video_id };
    var params = { allowscriptaccess: 'always', allowfullscreen: 'true', wmode: 'transparent', base: 'http://www.vimeo.com/' };
    var attributes = {};
    // For more SWFObject documentation visit: http://code.google.com/p/swfobject/wiki/documentation    
    if (swfobject.hasFlashPlayerVersion("9.0.0")) {
        // has Flash
        swfobject.embedSWF("http://vimeo.com/moogaloop.swf", swf_id, "504", "340", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    }
    else {
        // no Flash
    }


}



