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.currentTarget).parent().attr("id") == "carousel-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 'none':
       nfxc._transition.nextObj = function(){
					$(nfxc._container).children(':first').next().trigger("stateChanging");
         	$(nfxc._container).children('.viewing').hide().appendTo(nfxc._container).removeClass('viewing');
          $(nfxc._container).children(':first').show().trigger("stateChanged").addClass('viewing'); 
        }
        nfxc._transition.prevObj = function(){
					$(nfxc._container).children(':last').prev().trigger("stateChanging");
          $(nfxc._container).children('.viewing').hide().removeClass('viewing');
      		$(nfxc._container).children(':last').prependTo(nfxc._container).show().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 '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 "clickthrough":
	       $(nfxc._container).siblings('.nui-ctrl-bb').children().bind('click', nfxc.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() { if(nfxc._transition.type == "none") return false; 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")){
		$("#nav-blog h5:first").css({background:"none"});
		$(".pagination ul li:first").addClass("first");
		$(".pagination ul li:last-child").addClass("last");
	}

	if($("html").is(".ie6")){ 
		window.ie6 = true;
		DD_belatedPNG.fix("#carousel .billboard .nav li a");
		DD_belatedPNG.fix(".user-gen li");
		DD_belatedPNG.fix(".pagination li a");
		$("#list-articles .header:first").addClass("headerMain");
		$("form .col:last").addClass("last");
	}
	
/* --- banner rotation --- */
if($('.billboard') && $('.billboard').length > 1){
  $('#carousel').addClass('nectarine');
  $('<div id="carousel-prev" class="nui-ctrl-bb"><span></span></div>').insertBefore("#carousel");
  $('<div id="carousel-next" class="nui-ctrl-bb"><span></span></div>').insertAfter("#carousel");
  NECTAR.carousel.init(
   {
    container: '#carousel',
    transition: { type: 'none' },
    controls: { type: 'clickthrough' }
   }
  );  
}

/* --- flash map inserts --- */
	if($('meta[name="flashmap"]')){ 
		var options = { mapXMLURL: $('meta[name="flashmap"]').attr("content") };
		$('#elm-map').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 } );
			}   
		});    
	}

});

