/* --- //

doc: fvo-2009.js
site: Wellfire Interactive
development: Cobey Potter, Wellfire Interactive
version: q3, 2009

info: 

// --- */
var NECTAR = NECTAR || {};
NECTAR.carousel = {
	init: function(config){
		nfxc = NECTAR.carousel;
		for(var p in config){ if(!nfxc['_'+p])	nfxc['_'+p] = config[p];	}
    $(nfxc._container).children().css("display", "none");
		nfxc._getImgSet();
		if( !nfxc._transition ) { nfxc._transition = { type: 'fade', delay: '6000', speed: '3000' }; }
		nfxc._setUpTransition();
		if( !nfxc._controls ) { nfxc._controls = new Object(); }    
    nfxc._setUpControls();
		nfxc.begin();
	},
	interrupt: function(e){
    nfxc = NECTAR.carousel;
		clearTimeout(nfxc._controls.timer);
		if($(e).attr("class") == "prev"){ nfxc._prevObj(0); } else { nfxc._nextObj(0); }
    clearTimeout(nfxc._controls.timer);
    $('html').focus();
    nfxc._begin();
    return false;
  },
	_getImgSet: function(){
		nfxc = NECTAR.carousel;
		if( $("meta[name="+nfxc._meta+"]").length == 0 ) return false;
		template = $(nfxc._container).html();
		$(nfxc._container).empty();
		$("meta[name="+nfxc._meta+"]").each(function(){
			temp = $.secureEvalJSON( $(this).attr('content').replace(/'/g, "\"") );
			t = $(template).appendTo(nfxc._container);
		  for(elm in temp){
				if(temp[elm] == "img"){ nfxc.preloadImg(temp[elm].value); }
	    	if($(t).find(temp[elm])){
	        if(temp[elm].key != 'text'){ $(t).find(elm).attr(temp[elm].key, temp[elm].value); }
		    }
			}
		});
	},
	_setUpTransition: function() {
		nfxc = NECTAR.carousel;
		$(nfxc._container).children(':first').addClass('viewing').css("display", "block");
    switch(nfxc._transition.type)
    {
      case 'fade': 
        nfxc._transition.nextObj = function(){
					$(nfxc._container).children(':first').next().trigger("stateChanging");
         	$(nfxc._container).children('.viewing').fadeOut(nfxc._transition.speed).appendTo(nfxc._container).removeClass('viewing');
          $(nfxc._container).children(':first').fadeIn(nfxc._transition.speed, function(){ $(this).trigger("stateChanged"); }).addClass('viewing'); 
        }
        nfxc._transition.prevObj = function(){
					$(nfxc._container).children(':last').prev().trigger("stateChanging");
          $(nfxc._container).children('.viewing').fadeOut(nfxc._transition.speed).removeClass('viewing');
      		$(nfxc._container).children(':last').prependTo(nfxc._container).fadeIn(nfxc._transition.speed, function(){ $(this).trigger("stateChanged"); }).addClass('viewing');
        }
				nfxc._transition.setCurrent = function(){
					$(nfxc._container).children().stop(true, true);
					$(nfxc._container).children('.viewing').fadeOut(0).removeClass('viewing'); 
					stopAppend = 0;
					$(nfxc._container).children().each(function(){
						if($(this).attr("id") == $(nfxc._transition._newCurrent).attr("id") || stopAppend == 1){
							stopAppend = 1;
							return;
						}
						$(this).appendTo(nfxc._container);
					});
					$(nfxc._transition._newCurrent).trigger("stateChanging");
					$(nfxc._transition._newCurrent).fadeIn(0, function(){ $(this).trigger("stateChanged"); }).addClass('viewing');
				}
      break;
      case 'slide':
      break;
      case 'custom':
      break;
		}
  },
	_setUpControls: function() {
		nfxc = NECTAR.carousel; 
	  if( nfxc._controls.element ) { $(nfxc._container).after( nfxc._controls.element ); } 
	  switch(nfxc._controls.type) 
	    { 
	      case "overlay": 
	        /* NEEDS TO BE RECODED from Ember to NECTAR
					config.controls.overlay(); 
	        $(config.container).siblings('.overlay').children().bind('mouseenter mouseleave', function(){ $(this).toggleClass('on'); } ); 
	        $(config.container).siblings('.overlay').children().bind('click', interrupt ); */
	      break; 
	      case "custom": 
	        nfxc._controls.action.call(); 
	      break; 
	      default: break;             
	    } 
	},
	_nextObj: function(dV) { nfxc = NECTAR.carousel; if(!dV){ dV = null } nfxc._transitObj(nfxc._transition.nextObj, dV); },  
	_prevObj: function(dV) { nfxc = NECTAR.carousel; if(!dV){ dV = null } nfxc._transitObj(nfxc._transition.prevObj, dV);	},
	_transitObj: function(func, dV) {
		nfxc = NECTAR.carousel;
	  if(dV) { hS = nfxc._transition.speed; nfxc._transition.speed  = dV; }
	 	func.call();
	 	if(dV) { nfxc._transition.speed = hS; }
	 	nfxc._controls.timer = setTimeout( function(){ nfxc._nextObj() }, nfxc._transition.delay);
		return;
  },
 	begin: function() { nfxc = NECTAR.carousel; nfxc._controls.timer = setTimeout( function(){ nfxc._nextObj() }, nfxc._transition.delay); },
  preloadImg: function(source){ img = new Image(); img.src = source; return img; },
	getCurrent: function(){ return $(NECTAR.carousel._container).children('.viewing'); },
	setCurrent: function(id, dV){
		nfxc = NECTAR.carousel;
		clearTimeout(nfxc._controls.timer);
		if(!dV){ dV = null; }
		nfxc._transition._newCurrent = "#"+id;
		nfxc._transitObj(nfxc._transition.setCurrent, dV);
	}
}


$(document).ready(function()
{

	if($("html").is(".ie")){
		$("#tmpl-section .article .section:last-child, #tmpl-section .postslist li:last-child, .menu-glossary:last, .menu-glossary a:last-child, .menu-glossary span:last-child").addClass("last-child");
		$(":input").each(
			function(){
				$(this).addClass($(this).attr("type"));
			}
		);
		$("#tmpl-content .abstract > p:first").addClass("first-type");
	}

	if($("html").is(".ie7")){
		$(".pagination:last").addClass("last-child");

	}
	
	if($("html").is(".ie6")){ 
		window.ie6 = true;
		$("link").each(
			function(){
				switch( $(this).attr("href") ){
					case "/site/styles/fvo-2009.css":
					case "/site/styles/fvo-2009.ie.css":
					case "/site/styles/fvo-2009.ie7.css":		
						$(this).attr("disabled", "true");
					break;
				}
			}
		);
		$(".module .list li:first-child, .aggregator ul li:first-child, #list-sitemap .row div:first-child, .menu-glossary a:first-child, .menu-glossary span:first-child, .entries ul li:first-child").addClass("first-child");
		$("#nav-main .nav > li").hover(function(){$(this).addClass("hover");}, function(){$(this).removeClass("hover");})
		$("#nav-main .nav > li,	#nav-main .nav > li > a").addClass("first-level");
		$(".pagination:last").addClass("pagination-last");
		$(".menu-glossary:last").addClass("menu-glossary-last");
	}
	
/* --- banner rotation ---*/
		if($('#banner')){
			NECTAR.carousel.init(
				{
					container: '#banner',
					transition: { type: 'fade', delay: 10000, speed: 1000 },
					controls: { type: 'custom', ctrlClass: 'tab-bb', action: function(){ advanceCarousel(); } }
				}
			);  
		}

/* --- text resize --- */
		if($('#util-text')){ textResize(); }
		if($('.share .print')){ $('.share .print').click( function(){ print(); return false; } ); }
/* --- issues menu --- */
		if($("#ctrl-issues") && !window.ie6){
			$("#ctrl-issues h4").wrap('<div id="menu-issues"></div>');
			$("#menu-issues").click(function(){ $("#menu-issues").toggleClass("open"); });

			$("#ctrl-issues select option").not(":first").each(function(){
			    $('#menu-issues').append('<a href="'+$(this).val()+'">'+$(this).text()+'</a>');
			});

			$("#ctrl-issues form").remove();

		}

/* --- flash map inserts --- */
	if($('meta[name="flashmap"]')){ 
		var options = { mapXMLURL: $('meta[name="flashmap"]').attr("content") };
		$('#elm-map div:first').flash({swf:"/site/scripts/map.swf", width:652, height:450, flashvars: options});
	}
	
/* --- media player inserts --- */
	if($('.media')){ 
		$('.media').each(function(){
			var href= $(this).children('.preview').children('.link-more').children('a').attr('href');
			if(href){
				var hrefArr = href.split(".");
				if( $.inArray("youtube", hrefArr) != -1 ){
					hrefArr = href.split("="); 
					$(this).children(".figure").flash({swf:"http://www.youtube.com/v/" + hrefArr[1], height:285, width:285}); 
				}
				if( $.inArray("mp3", hrefArr) != -1){
					var options = { playerID: $(this).index(this), soundFile: href, bg: 0xe5e5e5,	lefticon: 0xfafafa,	lefticonhover:0xffffff,
						righticon: 0xfafafa, righticonhover:0xffffff,	leftbg:0x336699, leftbghover:0xB31212, rightbg:0x336699, rightbghover:0xB31212,
						border:0xDEEBF5, text:0x808080,	slider:0x336699, loader:0xDAEAFF,	track:0xfafafa }
					$(this).children(".figure").flash( {swf:"/site/scripts/player.swf", height:24, width:305, flashvars: options } );
				}
			}   
		});    
	}
	if($('.mediaShow')){ 
		$('.mediaShow').each(function(){
			href= $(this).attr('data-media');
			hrefArr = href.split(".");
			if( $.inArray("youtube", hrefArr) != -1 ){
				hrefArr = href.split("="); 
				$(this).flash({swf:"http://www.youtube.com/v/" + hrefArr[1], height:500, width:500}); 
			}
			if( $.inArray("mp3", hrefArr) != -1){
				options = { playerID: $(this).index(this), soundFile: href, bg: 0xe5e5e5,	lefticon: 0xfafafa,	lefticonhover:0xffffff,
					righticon: 0xfafafa, righticonhover:0xffffff,	leftbg:0x336699, leftbghover:0xB31212, rightbg:0x336699, rightbghover:0xB31212,
					border:0xDEEBF5, text:0x808080,	slider:0x336699, loader:0xDAEAFF,	track:0xfafafa }
				$(this).flash( {swf:"/site/scripts/player.swf", height:24, width:305, flashvars: options } );
			}   
		});    
	}


/* --- index tabs --- */
	$("#ctrl-tabs").prepend('<ul id="ctrl-holder"></ul>');
	$("#ctrl-tabs").prepend("<h2>"+$("#ctrl-tabs").attr("title")+"</h2>");
	$("#ctrl-tabs").attr("title", "");
	$("#ctrl-tabs > ul > li").each(function(index, domElm){
			$(domElm).attr("id", "tab-"+index);    
			$("#ctrl-holder").append('<li rel="tab-'+index+'">'+$(domElm).children('h2').text()+'</li>');
	    $(this).children('h2').remove();
			$(this).hide();
	});
	$("#ctrl-holder li").addClass("tabCtrl").click(	function(){ shiftTags($(this)); });
	$("#tab-0").show();
	$("#ctrl-tabs ul li:first").addClass("on").show();

	//$('#AdvancedSearch').hide();
	$("#ctrl-search").click(function(){ $('#AdvancedSearch').slideToggle('slow'); });	

});

function advanceCarousel(){
	
	$("#banner").after('<ul id="ctrl-billboard"><li class="support"></li></ul>');
	$("#banner > .figure")
		.each(function(index, domElm){
			$(domElm).attr("id", "bb-"+index);    
			$("#ctrl-billboard")
				.append('<li rel="bb-'+index+'" class="tab-bb">'+index+'</li>');
		});
	var $whatson = $("#ctrl-billboard li.tab-bb:first");
	$whatson.addClass("on");
	$("#ctrl-billboard li:first-child").addClass("support");
	$("#ctrl-billboard li.tab-bb")
		.click(
			function(){ 
				$li = $(this);
				$whatson = $li;
				$li.removeClass("support").removeClass("support-on").addClass("on");
				$li.siblings("li").removeClass("support").removeClass("on").removeClass("support-on");
				$li.prev("li").addClass("support");
				NECTAR.carousel.setCurrent($li.attr("rel"));
			}
		)
		.bind("on", 
			function(){
				$li = $(this);
				$whatson = $li;
				$li.removeClass("support").removeClass("support-on").addClass("on");
				$li.siblings("li").removeClass("support").removeClass("on").removeClass("support-on");
				$li.prev("li").addClass("support");
			}
		)
		.hover(
			function(){
				$li = $(this);
				if( $li.hasClass("support") ){
					$li.addClass("support-on");
				}
				if( $li.prev("li").hasClass("on") ){
					$li.prev("li").addClass("support-on");
				}else{
					$li.prev("li").addClass("support");
				}
			},
			function(){
				$li = $(this);
				if(!$li.hasClass("on")){
					$li.prev("li").removeClass("support support-on");
				}
				$whatson.addClass("on");
				$whatson.prev("li").addClass("support");
			}
		);

		$("#banner > .figure").bind("stateChanging", function(e){
			$fig = $(this);
			id = $fig.attr("id");
			$("#ctrl-billboard li[rel='"+id+"']").trigger("on");
		});
	
}

function shiftTags(currentTab){
	currentTab.addClass("on").siblings("li").removeClass("on");
	$("#" + currentTab.attr("rel")).show().siblings("li").hide();
}
function textResize(){
 
  var c = readCookie('style');
  if (c) stylizeCookie(c);
    
  $("#ctrl-textReg a").click( function(){
    stylizeCookie("txtReg");
    return false;
  });
  $("#ctrl-textLg a").click( function(){
    stylizeCookie("txtLg");
    return false;
  });
  $("#ctrl-textXLg a").click( function(){
    stylizeCookie("txtXLg");
    return false;
  });
}
function stylizeCookie(cssSize){
	$('#resizeCSS').remove();  
	if(cssSize != "txtReg"){
    $('head').append('<link rel="stylesheet" id="resizeCSS" href="/site/styles/'+cssSize+'.css" type="text/css" media="screen, projection" />');
  }
  createCookie('style', cssSize, 365);  
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions



