﻿// USEFUL EASING EQUATIONS -- Robert Penner 
// easeInQuad, easeOutQuad, easeInOutQuad, easeInQuint, easeOutBack
$.extend($.easing,{easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInQuint:function(e,f,a,h,g){return h*(f=f/g)*f*f*f*f+a},easeOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*((f=f/h-1)*f*((g+1)*f+g)+1)+a}});

// HoverIntent plugin -- Brian Cherne (http://cherne.net/brian/resources/jquery.hoverIntent.html)
(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);

// SIMPLE COOKIE HANDLING -- Lenon Marcel (http://cooquery.lenonmarcel.com.br/)
(function(d){d.setCookie=function(a,c,b){if(typeof a==="undefined"||typeof c==="undefined")return false;a=a+"="+encodeURIComponent(c);if(b.domain)a+="; domain="+b.domain;if(b.path)a+="; path="+b.path;if(b.duration){c=new Date;c.setTime(c.getTime()+b.duration*24*60*60*1E3);a+="; expires="+c.toGMTString()}if(b.secure)a+="; secure";return document.cookie=a};d.delCookie=function(a){return d.setCookie(a,"",{duration:-1})};d.readCookie=function(a){return(a=document.cookie.match("(?:^|;)\\s*"+a.replace(/([-.*+?^${}()|[\]\/\\])/g, "\\$1")+"=([^;]*)"))?decodeURIComponent(a[1]):null}})(jQuery);

// NSFLoopScroll -- Me (plugins.nsfdev.com/loopScroll/)
(function(b){b.fn.NSFloopScroll=function(m){var c=b.extend({},b.fn.NSFloopScroll.defaults,m);return this.each(function(e){function k(){if(i===true){if(c.rtl&&d<=-f)d=0;else if(!c.rtl&&d>=0)d=-f;a.css("marginLeft",c.rtl?(d-=a.tickRate*0.5):(d+=a.tickRate*0.5))}}var a=b(this),f=0,d=0,j=a.children(),l=j.length,g=c.tickRate,i=c.isAnim,h=e+1;for(a.anim=setInterval(k,16);l--;)f+=b(j[l]).width();a.css({width:(f*2),visibility:"visible"});j.clone().appendTo(a);c.rtl||(d=-f);a.tickRate=g.length>e?g[e]:g[g.length- 1];a.hover(function(){i=false},function(){a.hasClass("toggled")||(i=true)});if(c.toggles!=="none"){e=[];e.push(c.openToggle+'<a class="toggleTicker'+h+'" href="#" title="toggle the animation of ticker #'+h+'">toggle ticker '+h+"</a> "+c.closeToggle);b(c.toggles).append(e.join(""));b(".toggleTicker"+h).click(function(n){n.preventDefault();if(b(this).hasClass("toggled"))a.anim=setInterval(k,16);else clearInterval(a.anim);b(this).toggleClass("toggled")})}})};b.fn.NSFloopScroll.defaults={isAnim:true, tickRate:[1],rtl:true,reverse:"#rtl",toggles:"none",openToggle:"",closeToggle:""}})(jQuery);
	
/* **************
THIS IS IN SERIOUS NEED OF CLEANUP
The sad thing is, this is the much reduced, simplified, less extravagant version
************** */
//  the only way I've found to detect chrome on windows so far is pretty weak
//  if (this.navigator.userAgent.indexOf('Windows') != -1 && this.navigator.userAgent.indexOf('Chrome/') != -1 ) { alert('chrome on windows') }
if (this.navigator.userAgent.indexOf('Firefox/3.0') != -1 ) { $('html').addClass('FF3')}
	var isWhen = $('#id > li > ul'),
		navLinks = $('#nav > li'),
		toReveal = $('#nav li, h1, h2'),
		toCufon = $('#nav > li > a, h1, h2'), // in Firefox 3, the submenus will also be cufonified if cufon is applied to it
		hasCufon = $.readCookie('cvf01'),
		atFontCounter = 0,
		menu = $('.hasChildren > ul a'),
		spd = 250,
		onChrome;
	$('.hasChildren > ul').hide();

	if (this.navigator.userAgent.indexOf('Windows') == -1 && this.navigator.userAgent.indexOf('Chrome/') != -1 ) {
		onChrome = 1;
	}

	if ($.browser === 'safari' || onChrome == 1) { // sorry
		root.className=(!root.className)?"isJS":root.className+" wk";
	}
	// tweak letter spacing in logo schedule
	if ($('#id > ul > li:nth-child(odd)').css('letterSpacing') != ('normal' || '1px')) {
		$('#id > ul > li:nth-child(odd)').css('letterSpacing', 0).children('span').css('letterSpacing', '1px');
	}
	// Figure out whether we need cufon or not
	if (hasCufon==='0') { // are numbers in cookies always strings or just with this plugin?
		isWhen.slideDown(200, 'easeInQuad');
		doRender();
	} else if (hasCufon==='1') {
		$('html').addClass('hasCufon');
		cufonify();
	} else if (hasCufon===null) {
		$.setCookie('cvf01', 0, {
			duration: 2 // leave cookie for 2 days
		});
		
		// setup test
		$('body').append('<span id="font-test" style="position: absolute; left: -1000%; font-family: verdana, sans-serif; font-size: 10px">---TEST---</span>');
		var isAtFont = $('#font-test').width();
		setTimeout(atFontCheck, 20);
		}

// these functions should only run if no cookie is set
	function atFontCheck() { 
		$('#font-test').css({fontFamily: '"Folksolid", verdana, sans-serif'});
		setTimeout(atFontRun, 40);
	}
	function atFontRun() { // if, after 300ms, the font hasn't loaded, assume it won't. Note that this will sometimes load Cufon unnecessarily e.g. Chrome when zoomed in
		if (isAtFont === $('#font-test').width() && atFontCounter < 30) {
			atFontCounter++;
			setTimeout(atFontRun, 10); 
		} else if (atFontCounter === 30) {
			noAtFont();
		} else {
			atFontCleanUp();
		}
	}

	function noAtFont() {
		if (isAtFont === $('#font-test').width()) {
			$.setCookie('cvf01', 1, {
				duration: 2 // leave cookie for 2 days
			});
			cufonify();
		}
	}

	function cufonify() {
		$.getScript('http://cvf2010.nsfdev.com/assets/templates/cvf/js/cufon.js');
		$.getScript('http://cvf2010.nsfdev.com/assets/templates/cvf/js/Folksolid_400.font.js', function(){
			if (typeof Cufon == 'undefined') {
				$('#nav').addClass('firefox3');
				setTimeout(atFontCleanUp, 50);
			} else {
				Cufon.replace(toCufon, {
					hover: true,
					hoverables: { a:true }
				});
				
				Cufon.now();
				setTimeout(atFontCleanUp, 50);
				}
		});
	}



	function atFontCleanUp() {
		$('#font-test').remove();
		isWhen.slideDown(300, 'easeInQuad');
		doRender();
	}
// quick fade in function that doRender() will call
	function reveal(obj, callback) {
		obj.css({ opacity: 0, visibility: 'visible'}).animate({opacity: 1}, 300, 'easeInQuint', callback);
	}	
	function doRender() {
		misc(); // any other OCD tweaks get called now
		if($.support.opacity) { // for good browsers
			reveal(isWhen.children()/*, posMenu*/ );
			toReveal.each(function(){
				reveal($(this));
			});
		} else { // for internet explorer
			toReveal.each(function(){
				reveal($(this), function() {
					this.style.removeAttribute('filter');
				});
			});
			isWhen.children().each(function(i) {
				reveal($(this), function() {
					this.style.removeAttribute('filter');
					toReveal.css('visibility', 'visible');
				});
			});
		}

		setTimeout(startCrawl, 300); // Chrome 4, maybe webkit generally, behaves unpredictably when measuring images while other animations are happening. This should help get around that
		if(hasCufon) {
		// don't need anything? 
		}
	}


	function misc() {
		if($('.schedule').length > 0) {
			$('.schedule td:first-child').each(function() {
				var s = $(this), sh = s.html().replace(/-/g,'&#8210;'); // figure dash
				sh = sh.replace(/&#8210;&#8210;/g,'<span style="letter-spacing: -2px; padding: 2px">&#8210;&#8210;</span>'); // smoosh double figure dash together to look like a sold dash
				s.html(sh);
			});
		}
	}

	function startCrawl() { // scroll the sponsors
		$('#featured-sponsors-scroll')
		.wrap('<div id="ticker"></div>')
		.NSFloopScroll({tickRate: [2]})
		.after('<div id="ticker-left"></div><div id="ticker-right"></div>')
		.delay(25)
		.find('img')
		.css({ visibility: 'visible'});
	}

//	function posMenu() {
//		$('#nav > li').each(function(){ // step one: store the relative position of each element
//			var l = $(this).position();
//			$(this).data('pos',{l: l.left, t: l.top});
//			// console.log(l);
//		});
//		// needs to happen after all measurements are taken, hence two loops
//		$('#nav > li').each(function() { // step two: set top and left coords  to stored values and set position to absolute
//			$(this).css({ left: $(this).data('pos').l, top: /* $(this).data('pos').t */  0,  position: 'absolute'});
//		});
//		/*@cc_on
//		$('.hasChildren').each(function() { // step three: only needed for IE 7, ensure submenu is the same width as parent item
//			var l = $(this).width(), c = $(this).children('ul').width();
//			if ( c !== l) {
//				$(this).children('ul').css('minWidth',l);
//			}
//		}); @*/
//	}	
	

	function menuToggle(e) {
		spd = spd === 250 ? 100 : 250;
		var that = $(this).is('a') ? $(this).closest('li').get(0) : this, // tab focusable?
			fadeToggle = function() {
				spd === 100 ? fadeIn(50) : fadeOut(50);
			};

		$('ul', that).toggle(spd).children('li').fadeToggle;
	}
		
var mToggle = {    
	sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)    
	interval: 100, // number = milliseconds for onMouseOver polling interval    
	over: menuToggle, // function = onMouseOver callback (REQUIRED)    
	timeout: 400, // number = milliseconds delay before onMouseOut    
	out: menuToggle // function = onMouseOut callback (REQUIRED)    
}	
	$('.hasChildren').hoverIntent(mToggle);

	$('#sponsor-list li:nth-child(6), #sponsor-list li:nth-child(11)').addClass('start');
/* google analytics */
if (typeof pageTracker === "object" && pageTracker._trackPageview) {

	// track outbound clicks
	$('[rel=external]').click(function(e) {
		var thisOut = $(this).attr('href');
		pageTracker._trackPageview('OUT/'+thisOut);
	});
	
	// track PDF download
	$('a[href$=pdf]').attr('target', '_blank').click(function(){
		var filename = 	$(this).attr('href');
		pageTracker._trackEvent("Downloads", "PDF", filename);
	});

}

var phcrd = '<a rel="external" href="http://www.flickr.com/photos/dnkb/2550216261/" id="phcrd" style="font-size: 10px; font-family: sans-serif; text-transform: uppercase; text-decoration: none; position: absolute; bottom: 75px; right: 2px; padding: 9px 9px; display: block; visibility: hidden; max-width: 120px; color: #777; background-color:#ddd; border: 1px solid #eee" title="photo by DNKB on flickr">photo by dknb</a>'
$('img[src$=banner03.jpg]').after(phcrd);
$('#phcrd').animate({opacity: .5}, 1).css('visibility', 'visible').hover(function() { $(this).stop().animate({ opacity: .9}, 500)},function() { $(this).animate({ opacity: .3}, 2000, 'easeInOutQuad')});

