/*
 * Site-wide JS.
 *
 * @version SVN: $Id: init.js 193 2011-10-16 07:31:49Z jerico $
 */
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

$.fn.limitMaxlength = function(options){
	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keyup(onEdit)
	.keydown(onEdit)
	.focus(onEdit)
	.live('input paste', onEdit);
}

$.fn.mwContentSlider = function(){
	$(this).each(function(){
		var $this = $(this),
		reel = $('.reel', $this);

		$this.height($('.slide:first', $this).outerHeight());
		reel.width($('.slide', $(this)).length * $this.width());

		$('.next', $(this)).click(function(event){
			event.preventDefault();
			$this.animate({
				height: $(this).parents('.slide').next().outerHeight()
				});
			reel.animate({
				left: '-=' + $this.width()
				});
		});

		$('.prev', $(this)).click(function(event){
			event.preventDefault();
			$this.animate({
				height: $(this).parents('.slide').prev().outerHeight()
				});
			reel.animate({
				left: '+=' + $this.width()
				});
		});

	});

	return this;
};

$(document).ready(function(){

	// quick sign-in form ----------------------------------------------------------

	$('#quick-signin .trigger').click(function(event){
		event.preventDefault();
		event.stopPropagation();
		$('#quick-signin-form').toggle();
	});

	$('body').click(function(event){
		if (!$(event.target).parents('#quick-signin-form').length) {
			$('#quick-signin-form').hide();
		}
	});

	// layout-slideshow-multistate-content nav -------------------------------------

	$('.layout-slideshow-multistate-content').mwContentSlider();

	// glossary tooltip ------------------------------------------------------------

	$('.glossary-entries .photo').hover(
		function() {
			$('#glossary-photo-tooltip .image').css('background-image', 'url(' + $(this).attr('rel') + ')');

			$('#glossary-photo-tooltip').css({
				'top':             $(this).offset().top - 63,
				'left':            $(this).offset().left + $(this).outerWidth()
			}).fadeIn('fast');
		},
		function() {
			$('#glossary-photo-tooltip').hide();
		}
		);

	// lightbox
	$('.lightbox-reset').colorbox({
		iframe:true,
		innerWidth:240,
		innerHeight:200,
		opacity:.25
	});
	$('.lightbox-refer-by-email').colorbox({
		iframe:true,
		innerWidth:440,
		innerHeight:235,
		opacity:.25
	});
	$('.lightbox-subscribe-newsletter a').colorbox({
		iframe:true,
		innerWidth:440,
		innerHeight:120,
		opacity:.25
	});

	// textarea limit
	var onEditCallback = function(remaining){
		$(this).siblings('.charsRemaining').text(remaining + ' Characters Remaining');
	//		if(remaining > 0){
	//			$(this).css('background-color', 'white');
	//		}
	}
	var onLimitCallback = function(){
	//		$(this).css('background-color', 'red');
	}
	$('textarea[maxlength]').limitMaxlength({
		onEdit: onEditCallback,
		onLimit: onLimitCallback
	});

	$('#ticker ul').newsTicker();

	// #submenu
	$('#submenu li.has-submenu').hover(
		function(){
			var
			wH = $(window).height(),
			oT = $(this).offset().top,
			oH = $(this).outerHeight(),
			nH = wH - (oT + oH) - 40,
			uH = $('ul', $(this)).outerHeight();

			if (uH > nH) {
				$('ul', $(this)).height(nH);
			}

			$('ul', $(this)).slideDown('fast');

			// if no width is set yet
			if ($('ul', this).attr('data-width') == undefined) {
				var widestW = 0;
				$('li a', this).each(function(){
					if (widestW == 0) {
						widestW = $(this).outerWidth();
					} else if ($(this).outerWidth() > widestW) {
						widestW = $(this).outerWidth();
					}
				});

				$('ul', this).attr('data-width', widestW).width(widestW);
			}
		//	  if ($('ul', this).attr('data-width'))
		//	  var widest = null, widestW = 0;

		},
		function(){
			$('ul', $(this)).hide();
		}
	);

	$('.quick-nav-top .selector>ul>li').hover(
		function(){ $('ul', this).slideDown('fast'); },
		function(){ $('ul', this).hide(); }
	);

	// reposition bottom dropdowns
	$('.quick-nav-bottom .selector li ul').each(function(){
		$(this).css('top', -5);
	});

	$('.quick-nav-bottom .selector>ul>li').hover(
		function(){
			// always reset
			$('ul', this).css({ height: 'auto', top: -5 });

			var sub = $('ul', this),
				oHt = sub.outerHeight(),
				ht  = sub.height();
			sub.height(0).show().animate({
				top: oHt * -1,
				height: ht
			}, 'fast');
		},
		function(){ $('ul', this).hide(); }
	);

}); // document.ready()

$(window).load(function() {

	// nivo slider -----------------------------------------------------------------

	$('.slider-style1').nivoSlider({
		effect:    'fade',
		pauseTime: 5000
	});

});

// cufon text replacements -----------------------------------------------------

Cufon.replace('.cufon1, .quick-nav a, .quick-nav span, #submenu>ul>li>a, .paging-nav-pages a, .paging-nav-pages span, #top-menu>ul>li>a, dl.glossary-entries dt, #footer, #glossary-nav .a-z a, .heading-letter a, #glossary-nav .a-z span, #page h1, #page h2:not(.nocufon), #page h3:not(.nocufon), .prev-next-nav a, .button, .comment-reply-link', {
	fontFamily: 'Franklin Gothic Medium',
	hover: true,
	hoverables: {
		a: true,
		button: true,
		span: true
	}
});

Cufon.replace('.breadcrumb .left, .breadcrumb .right', {
	fontFamily: 'Franklin Gothic Medium',
	hover: true
});

Cufon.replace('#menu a', {
	fontFamily: 'HelveticaNeue LT 63 MdEx',
	hover: true,
	hoverables: {
		a: true,
		button: true
	}
});

