﻿/**
* 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);

// round main menu submenus
DD_roundies.addRule("#menu li.active span.outer", "6px 6px 0 0", true);

$(document).ready(function() {
	
	$("body").addClass("js");
    
    // add print page link
    $("#print-page").html('<a href="#" id="print-page-link">Print Page</a>')
    $("#print-page-link").click(function() {
        window.print();
        return false;
    });
    
    // slide down menus
    var activeMenu;
    
    var menuOver = function() {
        d = $(this).find("div");
        if(d.length) {
            d.slideDown("fast");
            $(this).addClass("open");
        }
    }
    var menuOut = function() {
        d = $(this).find("div");
        if(d.length) {
            d.slideUp("fast");
            $(this).removeClass("open");
        }
    }
    var menuConfig = {    
        sensitivity: 3,  
        interval: 100, 
        timeout: 300, 
        over: menuOver,   
        out: menuOut
    }
    $("#menu > ul > li").hoverIntent(menuConfig);

    // left-opening menu on some menu items
    $("#menu > ul > li").each(function() {
	index = $(this).prevAll().length;
	if(index == 7 || index == 8) {
	    margin = $(this).width() - 286;
	    $(this).find("div").css("margin-left", margin + "px");
	}
    });
    
    // case study slider
    $('#case-study-slider')
        .after('<a id="case-study-slider-prev" href="#">Previous Image</a><a id="case-study-slider-next" href="#">Next Image</a>')
        .cycle({ 
            fx:     'fade', 
            speed:  '500', 
            timeout: 0, 
            next:   '#case-study-slider-next', 
            prev:   '#case-study-slider-prev',
            before: function() {
                $("#case-study-caption p").fadeOut(150);
            },
            after: function() {
                $("#case-study-caption p").html(this.alt).fadeIn(150);
            }
    });
     
    // show top 6 case studies and add "more" link
    if ($("#case-studies").find("div.case-study-list-row").size() > 3){
        $("#case-studies").find("div.case-study-list-row:gt(2)").hide();
        $("#case-studies").after('<p class="right-link"><a href="#" id="case-studies-more">See more case studies</a></p>');
        // show all if "more" link is clicked
        $("#case-studies-more").click(function() {
            $("#case-studies").find("div.case-study-list-row").show();
            $("#case-studies-more").remove();
            return false;
        });
    }
    
    // news filter
    $("#news-filter-region").change(function() {
        if($(this).find(":selected").hasClass("trust")) {
            $("#news-filter-trust-field").css("display", "inline");
        } else {
            $("#news-filter-trust-field").css("display", "none");
        }
    });
    
    // two column list - even up heights of headline for neatness
    $(".two-column-list").find("li").each(function() {
        if($(this).hasClass("row")) {
            first = $(this).find("h3 a");
            second = $(this).next().find("h3 a");
            max = Math.max(first.height(), second.height());
            first.css("height", max + "px");
            second.css("height", max + "px");
        }
    });
    
});