﻿// Cufon
Cufon.replace('.featured h4, .photo .description h5, #content h1, .featured h3, .sidebar h4, .description h3, p.key');
// Cufon Hoverable
Cufon.replace('#nav li div.mn ul li h4 a, #news h3 a, #events h3 a, #bucketNav h4 a, .sidebar h4 a, #pillars li a, #share a', {hover:true});
 

//// Default Marquette Head
//var mu_header = {
//    header  : Object,
//    toggle  : Object,
//    init    : function(){
//        this.header = $('#muHeader');
//        this.toggle = $('#muHeaderToggle');
//        this.run();
//    },
//    run     : function(){
//        this.toggle.toggle(function(){
//            mu_header.header.animate({'top':'0'}, 600);
//            $(this).animate({'top':'138px'},600);
//        }, function(){
//            mu_header.header.animate({'top':'-138px'},  600);
//            $(this).animate({'top':'0'},600);
//        });
//    }
//}


// Searchbox clear
var mu_search = {
    search  : Object,
    label   : Object,
    sidebar_search : Object,
    sidebar_label  : Object,
    init    : function(){
        this.search = $('#keywords');
        this.label = $('#search label');
        this.sidebar_search = $('#sidebar_search input, #sidebar_facultySearch input');
        this.sidebar_label = $('#sidebar_search label, #sidebar_facultySearch label');
        this.run();
    },
    run     : function(){

        if(this.search.val() == ''){
            mu_search.search.addClass('on');
        }
        if(this.sidebar_search.val() == ''){
            mu_search.sidebar_search.addClass('on');
        }

        this.search.focus(function(){
            mu_search.label.animate({color:'#c9c9c9'}, 'fast');
        });
        this.search.keydown(function(){
            $(this).removeClass('on');
        });
        this.search.blur(function(){
            if($(this).val() == ''){
                $(this).addClass('on');
                mu_search.label.animate({color:'#4c4c4c'}, 'fast');
            }
        });

        this.sidebar_search.focus(function(){
            mu_search.sidebar_label.animate({color:'#c9c9c9'}, 'fast');
        });
        this.sidebar_search.keydown(function(){
            $(this).removeClass('on');
        });
        this.sidebar_search.blur(function(){
            if($(this).val() == ''){
                $(this).addClass('on');
                mu_search.sidebar_label.animate({color:'#4c4c4c'}, 'fast');
            }
        });

        $('#search input.button').click(function(){
            if($('#keywords').val() == ''){
                return false;
            }
        });
    }
}


// Navigation
var mu_nav = {
    trigger : Object,
    timer   : Object,
    hoverConfig : Object,
    init    : function(){
        this.trigger = $('#nav li');
        this.hoverConfig = {
            over    : mu_nav.over,
            out     : mu_nav.out,
            timeout : 300,
            interval : 50
        };
        this.run();
    },
    run     : function(){
        $('.noJS').removeClass('noJS');
        this.trigger.hoverIntent(mu_nav.hoverConfig);
    },
    over    : function(){
        $(this).addClass('hover');
    },
    out     : function(){
        $(this).removeClass('hover');
    }
}


// Breadcrumbs
var mu_breadcrumbs = {
    breadcrumbs : Object,
    bottom      : Object,
    rgb         : Number,
    init        : function(){
        this.breadcrumbs = $('#breadcrumbs .breadcrumbs li');
        this.rgb = 80;
        this.bottom = $('#pageControls ul.breadcrumbs li');
        this.run();
    },
    run         : function(){

        // If there are more than 5 breadcrumbson the top, this removes the first couple.
        if(this.breadcrumbs.length > 5){
            var first = this.breadcrumbs.length - 5
            $('#breadcrumbs ul.breadcrumbs li:nth-child(-n+'+ first +')').hide();
            $('#breadcrumbs ul.breadcrumbs').prepend('<li class="first"><a href="#" id="expandBreadcrumbs">[...]</a></li>');
            $('#expandBreadcrumbs').css("color","rgb(200,200,200)");
        }

        // Colors the top breadcrumbs
        for(var i = mu_breadcrumbs.breadcrumbs.length; i >= 0; i--){
            mu_breadcrumbs.breadcrumbs.find('a').eq(i).css("color","rgb(" + mu_breadcrumbs.rgb + ", " + mu_breadcrumbs.rgb + ", " + mu_breadcrumbs.rgb + ")");
            if(mu_breadcrumbs.rgb < 200){
                mu_breadcrumbs.rgb = mu_breadcrumbs.rgb + 20;
            }
        }

        // Removes all but the last three breadcrumbs on the bottom
        var last = this.bottom.length - 3
        $('#pageControls ul.breadcrumbs li:nth-child(-n+'+ last +')').remove();

        // Adds 'first' to both breadcrumbs
        $('#breadcrumbs ul.breadcrumbs li:first, #pageControls ul.breadcrumbs li:first').addClass('first');


        // Expands all breadcrumbs
        $('#expandBreadcrumbs').live('click',function(e){
            $(this).parent().remove();
            $('#breadcrumbs ul.breadcrumbs li:hidden').show();
            e.preventDefault();
        });

    }
}


// Page Controls
var mu_pageControls = {
    print       : Object,
    email       : Object,
    init        : function(){
        this.print = $('#control_print');
        this.email = $('#control_email');
        this.run();
    },
    run         : function(){
        this.print.click(function(e){
            window.print();
            e.preventDefault();
        });
        this.email.click(function(e){
            location.href = "mailto:?subject=Marquette College of Business Administration&body=" + location.href;
            e.preventDefault();
        });
    }
}


// Accordion
var mu_accordion = {
    accordion   : Object,
    init        : function(){
        this.accordion = $('.accordion');
        this.run();
    },
    run         : function(){
        for(var i = 0; i < this.accordion.length; i++){
            this.accordion.eq(i).accordion({
                active: false,
                collapsible: true,
                autoHeight: false
            });
        }
    }
}


// Attatches classes to tables
var mu_tables = {
    tables  : Object,
    init    : function(){
        this.tables = $('#col2 table');
        this.run();
    },
    run     : function(){
        this.tables.attr({
            cellpadding : '0',
            cellspacing : '0'
        });
        $('#col2 table tr:even').addClass('even');
    }
}


// Adds classes to PDF's, DOC's, XLS's, & PPT's
var mu_linkID = {
    types   : Object,
    init    : function(){
        this.types = $('a[href$=pdf],a[href$=doc],a[href$=docx],a[href$=xls],a[href$=xlsx],a[href$=ppt],a[href$=pptx]');
        this.run();
    },
    run     : function(){
        this.types.addClass(function(){
	        return $(this).attr('href').split('.').pop();
        });

        $('.pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx').live('click', function(){
            window.open(this.href);
            return false;
        });
    }
}


// adds classes to input elements
var mu_forms = {
    init    : function(){
        $('#content input').addClass(function(){
            return $(this).attr('type');
        });

        $('form input, form textarea, form select').focus(function(){
            $('.formElement.active').removeClass('active');
            $(this).parents('.formElement').addClass('active');
        });
    }
}


// Makes the main column as tall as the sidebar, if the sidebar is taller.
var mu_columns = {
    col1    : Object,
    col2    : Object,
    init    : function(){
        this.col1 = $('#col1 .o');
        this.col2 = $('#col2 .o:not(#hero .o, #home .o)');
        this.run();
    },
    run     : function(){
        if(this.col1.height() > this.col2.height()){
            if($.browser.msie && $.browser.version=="6.0"){
                this.col2.height(mu_columns.col1.height()-75);
            }else{
                this.col2.css('minHeight',mu_columns.col1.height()-75);
            }
        }
    }
}


// Event Tracker
var mu_gaEvent = {
    track   : function(){
        // Audience Navigation
            $('#audienceNav li a').click(function(){_gaq.push(['_trackEvent','Header','Audience Navigation Click',$(this).attr('href')]);});
        // Marquette.edu nav
            $('#muHeaderToggle').click(function(){_gaq.push(['_trackEvent','Header','Marquette.edu link']);});
        // Search
            $('#searchGo').click(function(){_gaq.push(['_trackEvent','Search','Searched','',$('#keywords').text()]);});
        // Top Navigation
            $('#nav h3 a').click(function(){_gaq.push(['_trackEvent','Top Navigation','Meganav Open',$(this).text()]);});
            $('#nav h3 .mn a').click(function(){_gaq.push(['_trackEvent','Top Navigation','Meganav Link',$(this).text()]);});
            $('#nav .mn li.more a').click(function(){_gaq.push(['_trackEvent','Top Navigation','Meganav More Link',$(this).attr('href')]);});
        // Sidebar
            $('#pillars li a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Pillars',$(this).parent().attr('id')]);});
            $('#sidebar_menu li a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Menu',$(this).attr('href')]);});
            $('.sidebar_text a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Text Story',$(this).attr('href')]);});
            $('.sidebar_hybrid a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Text and Photo',$(this).attr('href')]);});
            $('.sidebar_photo a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Photo',$(this).attr('href')]);});
            $('.sidebar_photo a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Photo',$(this).attr('href')]);});
            $('.sidebar_related a').click(function(){_gaq.push(['_trackEvent','Sidebar','Clicked','Related Links',$(this).attr('href')]);});
        // Breadcrumbs
            $('#breadcrumbs li a').click(function(){_gaq.push(['_trackEvent','Top Breadcrumbs','Link Clicked',$(this).attr('href')]);});
        // In Page Link
            $('#col2 p a, #col2 li a:not(.breadcrumbs li a, #pageControls li a)').click(function(){_gaq.push(['_trackEvent','Page','Link Clicked',$(this).attr('href')]);});
        // Calendar
            $('#calendar h5 a').click(function(){_gaq.push(['_trackEvent','Calendar','Event Shown',$(this).text()]);});
            $('#searchcalendarform a').click(function(){_gaq.push(['_trackEvent','Calendar','Event Search',$('#searchcalendarform input#keywords').text()]);});
        // Faculty Page
            $('#alphabet li a').click(function(){_gaq.push(['_trackEvent','Faculty','Clicked','Alphabet',$(this).text()]);});
            $('#sidebar_facultySearch a.submit').click(function(){_gaq.push(['_trackEvent','Faculty','Search','','Name: '+$('name').val()+', Department: '+$('dept').val()]);});
        // Page Controls
            $('#pageControls .breadcrumbs li a').click(function(){_gaq.push(['_trackEvent','Page Controls','Breadcrumb Clicked',$(this).attr('href')]);});
            $('#pageControls #control_print').click(function(){_gaq.push(['_trackEvent','Page Controls','Page Printed',window.location]);});
            $('#pageControls #control_email').click(function(){_gaq.push(['_trackEvent','Page Controls','Page Emailed',window.location]);});
        // Action Bar
            $('#socials li a').click(function(){_gaq.push(['_trackEvent','Action Bar','Social Link Clicked',$(this).parent().attr('id')]);});
            $('#toolbox li a').click(function(){_gaq.push(['_trackEvent','Action Bar','Toolbox Link Clicked',$(this).text()]);});
        // Footer
            $('#footer ul li a').click(function(){_gaq.push(['_trackEvent','Footer Navigation Navigation','Link Clicked',$(this).text()]);});
    }
}


// Event Tracker
var mu_faculty = {
    trigger1: Object,
    trigger2: Object,
    init    : function(){
        this.trigger1 = $('#facultyListing dt')
        this.trigger2 = $('#facultyListing dd')
        this.run();
    },
    run     : function(){
        this.trigger1.hover(function(){
            $(this).addClass('hover');
        }, function(){
            $(this).removeClass('hover');
        });
        this.trigger2.hover(function(){
            $(this).prev('dt').addClass('hover');
        }, function(){
            $(this).prev('dt').removeClass('hover');
        });
    }
}


//Homepage Banner
var banner = {
	banner		: Object,
	slides		: Object,
	timer		: Object,
	locked		: Boolean,
    clicked     : false,
	init		: function(){
		this.banner = $('#slider');
		this.slides = $('.slide')
		this.locked = false;
		this.navInit();
		this.change($('.slide:first-child'),$('#bannerNav a:first-child'));
		this.startTimer();
	},
	clearTimer	: function(){
		clearInterval(banner.timer)
	},
	startTimer	: function(){
		this.timer = setInterval(function(){
            if(!banner.clicked){
			    banner.next();
            }
		}, 5000)
	},
	resetTimer	: function(){
		this.clearTimer();
		this.startTimer();
	},
	next		: function(){
		var newSlide = (!$('.slide.on').is(':last-child')) ? $('.slide.on').next() : $('.slide:first-child')
		var newNav = (!$('#bannerNav a.on').is(':last-child')) ? $('#bannerNav a.on').next() : $('#bannerNav a:first-child')
		this.change(newSlide,newNav)
	},
	prev		: function(){
		var newSlide = (!$('.slide.on').is(':first-child')) ? $('.slide.on').prev() : $('.slide:last-child')
		var newNav = (!$('#bannerNav a.on').is(':first-child')) ? $('#bannerNav a.on').prev() : $('#bannerNav a:last-child')
		this.change(newSlide,newNav)
	},
	change		: function(newSlide, newNav){
		if(!banner.locked){
			banner.locked = true;
			if($('.slide.on')){
				$('.slide.on').fadeOut(1000, function(){
					$(this).removeClass('on');
				})
			}
			newSlide.addClass('on').fadeIn(1000, function(){
				banner.locked = false;
			})
		    $('#bannerNav a.on').removeClass('on')
		    $(newNav).addClass('on');	
		}
	},
	navInit		: function(){
		this.banner.prepend('<div id="bannerNav"></div><a href="#" id="bannerPrev">Prev</a><a href="#" id="bannerNext">Next</a>')
		for(var i = 0; i < banner.slides.length; i++){
			$('#bannerNav').append('<a href="#" rel="'+i+'">'+(i+1)+'</a>');
			if(i == 0){
				$('#bannerNav a').addClass('on')
			}
		}
					
		$('#bannerNav a').live('click', function(e){
			if(!banner.locked){
                banner.clicked = true;
				var newSlide = banner.slides.eq($(this).attr('rel'));
				banner.change(newSlide,$(this));
			}
			e.preventDefault();
		});
					
		$('#bannerNext').live('click', function(e){
			if(!banner.locked){
                banner.clicked = true;
				banner.next();
			}
			e.preventDefault();
		});
					
		$('#bannerPrev').live('click', function(e){
			if(!banner.locked){
                banner.clicked = true;
				banner.prev();
			}
			e.preventDefault();
		});

        $('#bannerPrev').hover(function(){
            $(this).stop(true, true).animate({
                marginLeft:0
            }, 'fast', 'easeOutCirc');
        }, function(){
            $(this).stop(true, true).animate({
                marginLeft:'-20px'
            }, 'fast', 'easeOutCirc');    
        });

        $('#bannerNext').hover(function(){
            $(this).stop(true, true).animate({
                marginRight:0
            }, 'fast', 'easeOutCirc');
        }, function(){
            $(this).stop(true, true).animate({
                marginRight:'-20px'
            }, 'fast', 'easeOutCirc');
        });
					
		this.banner.hover(function(){
            $('#bannerNav').stop(true, true).animate({
                marginTop:0
            }, 'fast', 'easeOutCirc');
			this.clearTimer();
		}, function(){
            $('#bannerNav').stop(true, true).animate({
                marginTop:'-21px'
            }, 'fast', 'easeOutCirc');
			this.startTimer();
		})
	}
}


function initialize_global(){
//    mu_header.init();
    mu_search.init();
    mu_nav.init();
    mu_breadcrumbs.init();
    mu_pageControls.init();
    mu_accordion.init();
    mu_tables.init();
    mu_linkID.init();
    mu_forms.init();
    mu_columns.init();
    mu_gaEvent.track();
    mu_faculty.init();
    banner.init();
};


$(document).ready(function(){
    initialize_global();
});


/**
* 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);
