/**
 * Add default "temporary" text to inputs
 * Courtesy of http://dabrook.org/blog/articles/input-field-hints-plugin-for-jquery/
 */
jQuery.fn.inputFieldText = function(string, hintClass) {
  this.each(function() {
      $(this).addClass(hintClass).filter(function(){
      	return ($(this).val() == '' || $(this).val() == string);
      }).val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass(hintClass).val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == ''){
          $(this).addClass(hintClass).val(string);
        }
      });
  });
};

$.fn.nextUntil = function(expr) {
	var match = [];
	this.each(function(){
		for (var i = this.nextSibling; i; i = i.nextSibling) {
			if (i.nodeType != 1) continue;
			if ($(i).is(expr)) break;
			match.push(i);
		}
	});
	return this.pushStack(match, arguments);
};

$(function(){
	$('.expandable-content H3').wrap('<div class="h3-wrapper"></div>').parent().click(function(){
		$(this).removeClass('expanded').removeClass('contracted').addClass(($(this).next().toggle().is(':visible') ? 'expanded' : 'contracted'));
	}).each(function(){
		$(this).nextUntil('.h3-wrapper').wrapAll('<div class="content-wrapper"></div>');
	}).addClass('contracted').next().hide();
	$('.h3-wrapper').each(function(){
		$(this).prepend($('<img src="/img/expander_bg.png" />').width($(this).width()).height($(this).height()));
	});
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

$(function(){	
	var active = $('#menu LI.active');
	active.addClass('over');
	$('#menu LI').hover(
		function(){
			active.removeClass('over');
			$(this).addClass('isover');
		},
		function(){
			$(this).removeClass('isover');
			active.addClass('over');
		}
	);
	$.preLoadImages("/img/menu-active.jpg", "/img/menu-rollover.jpg", "/img/menu.jpg", "/img/previousdisabledarrow.gif", "/img/previousarrowover.gif", "/img/nextdisabledarrow.gif", "/img/nextarrowover.gif");
});

$(function(){
	$("div.newsCarousel").carousel({
		dispItems: 3,
		showEmptyItems:false,
		slideEasing: 'easeOutQuart',
		combinedClasses: true,
		animSpeed: 700
	}).find('LI').show();
	// bit of hack we need to get it right
	$('#right-separator').css({marginLeft:'16px'});
});

$(function(){
	$('A[href^=http://www.surveymonkey.com]').addClass('survey iframe');
});

$(function(){
	$("div.newsViewCarousel").carousel({
		dispItems: 1,
		slideEasing: 'easeOutQuart',
		animSpeed: 700
	}).find('LI').show();
});

$(function(){
	$("div.mainNewsCarousel").carousel({
		dispItems: 1,
		slideEasing: 'easeOutQuart',
		animSpeed: 700,
		loop: true,
		autoSlide: true,
		autoSlideInterval: 7000
	}).find('LI').show();
});

$(function() {
	$("#MemberPassword").hide();
	var new_input = document.createElement("input");
	new_input.setAttribute("id", 'Password_hidden');
	new_input.setAttribute("type","text");
	new_input.value = 'Password';
	$("#MemberPassword").after(new_input);
	$('#Password_hidden').focus(function(){

		$(this).hide().prev().show().focus();
	});
	$("#MemberPassword").blur(function(){
		if ($(this).val() == '') {
			$(this).hide().next().show();
		}
	});
});

$(function() {
	if (typeof $().tabs == 'function') {
		$("#news-tabs").tabs();
	}
});

$(function(){
	
	if (!$.fn.fancybox) {
		return;
	}
	
	// do images
	$('a.newsitem-image').fancybox({
		zoomSpeedIn:400,
		zoomSpeedOut:400,
		overlayShow: true,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		hideOnContentClick:false
	});
	
	$('a.survey').fancybox({
		zoomSpeedIn:400,
		zoomSpeedOut:400,
		frameWidth: 800,
		frameHeight: 600,
		overlayShow: true,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		hideOnContentClick:false
	});
	
	// do videos
	$('BODY').append('<div style="display:none" id="video"></div>').append('<a href="#video" id="videoLink" />');	
	$('a.newsitem-video').click(function(evt){
		
		var videoDetails = $(this).metadata({type:'attr', name:'data'});
		videoDetails.url = $(this).attr('href');
		videoDetails.title = $(this).attr('title');

		var flashvars = {
			autostart:'true', repeat:'false',
			width:videoDetails.width,
			height:videoDetails.height+20,
			file:videoDetails.url
		};
			
		$('#videoLink').css({position:'absolute', top:evt.pageY, left:evt.pageX}).fancybox({
			callbackOnShow:function(){
				$('#fancy_content').html('<div id="fancy_video"></div>');
				swfobject.embedSWF('/swf/player.swf', 'fancy_video', videoDetails.width, videoDetails.height+20, '9.0.115', null, flashvars, {allowfullscreen:'true'});
			},
			callbackBeforeClose:function() {
				$('#fancy_content').empty();
			},
			zoomSpeedIn:400,
			zoomSpeedOut:400,
			frameWidth:videoDetails.width,
			frameHeight:videoDetails.height+20,
			overlayShow: true,
			overlayColor: '#000',
			overlayOpacity: 0.8,
			hideOnContentClick:false
		}).trigger('click');
		
		return false;
		
	});
	
	$('a.newsitem-audio').click(function(evt){
		
		var videoDetails = [];
		videoDetails.url = $(this).attr('href');
		videoDetails.title = $(this).attr('title');

		var flashvars = {
			autostart:'true', repeat:'false',
			file:videoDetails.url,
			volume: 100
		};
			
		$('#videoLink').css({position:'absolute', top:evt.pageY, left:evt.pageX}).fancybox({
			callbackOnShow:function(){
				$('#fancy_content').html('<div id="fancy_video"></div>');
				swfobject.embedSWF('/swf/player.swf', 'fancy_video', 400, 20, '9.0.115', null, flashvars, {allowfullscreen:'true'});
			},
			callbackBeforeClose:function() {
				$('#fancy_content').empty();
			},
			zoomSpeedIn:400,
			zoomSpeedOut:400,
			frameWidth:400,
			frameHeight:20,
			overlayShow: true,
			overlayColor: '#000',
			overlayOpacity: 0.8,
			hideOnContentClick:false
		}).trigger('click');
		return false;
	});
});

$(function(){
	
	$('#header-login LABEL').each(function(){
		$('#'+$(this).attr('for')).inputFieldText($(this).text(), 'hint');
	}).hide();
	
});


$(function(){
	$('.base TBODY TR:even').addClass('even');
});


$(function(){
	$('A, AREA').filter(function(){
		var href = $(this).attr('href');
		return !this.target && ((href.indexOf(window.location.hostname) == -1 && href.match(/^https?/i)) || href.match(/\.pdf$/i));
	}).attr('target', '_blank');
});

if (swfobject.hasFlashPlayerVersion('9.0.115')) {
	swfobject.embedSWF('/swf/CampaignViewer.swf', 'CampaignViewer', '755', '340', '9.0.115', null, {urlPrefix:'', embedded:1}, {wmode:'window', allowfullscreen:'true', allowScriptAccess:'always'});
} else {
	$(function(){
		$('.noflash').each(function(){
			$(this).replaceWith($($(this).text()));
		});
	});
}