if(typeof console === "undefined") {
    console = { log: function() { } };
}

$(function() {
    
	var $ = jQuery;
	
	var NPF	=	{
	
		overlays : function() {
			$(document).keyup(function(event){
				if (event.keyCode == 27) {
					$("#overlay").fadeOut("slow", function(){
						$(this).remove();
					});
					$("#overlay-content").fadeOut("slow", function(){
						$(this).remove();
					});
				}
			});

			$("#overlay").live("click",function(){
				$("#overlay").fadeOut("slow", function(){
					$(this).remove();
				});
				$("#overlay-content").fadeOut("slow", function(){
					$(this).remove();
				});
				return false;
			});
		
			$(".close").live("click",function(){
				$("#overlay").fadeOut("slow", function(){
					$(this).remove();
				});
				$("#overlay-content").fadeOut("slow", function(){
					$(this).remove();
				});
				return false;
			});		
		},
	   	   
		features : function() {
			homeFeatures = $('#home-features');

			featuresContainer = $('#home-features-content');
					
			featureLinks = $('#home-features a.prev, #home-features a.next');

			$(homeFeatures).css('background', '#193477 url(/assets/images/loading.gif) center center no-repeat');
			
			//get the next and prev links
			$(featureLinks).live('click', function(event){
				var url = $(this).attr('href');
						
				event.preventDefault();
				$.get(url, function(data){		
					$(featuresContainer).fadeOut('slow', function(){
						$(featuresContainer).html(data);
						$(featuresContainer).fadeIn('slow');
					});
				});	
			});		
		},
		
		media : function() {
			$("a.media-video").click(function(){
				var data = $(this).closest(".video").html();
			
				data = data.toString().replace(/139/g, '469').replace(/201/g, '720');
				
				$(".media-main").html(data);

				if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				}else{
					$.scrollTo(".main h1", 400);
				}
			});
		},
	
		hovers : function() {
		
			$('.heading .select').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});	

			$('.stories .search').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});	
			
			$('#entryform .search').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});

		},
		
		paginate : function() {
			
			$("#storyWrapper .paginate a").live("click", function(e){
				//	e.preventDefault();
					
				var url = $(this).attr("href").toString().replace("outoftown","outoftown/stories") + NPF.buildSearchQueryString();

				try{
					$.get(url, function(data){
						$("#storyWrapper").empty().append(data);
						NpfMap.setupMarkers();
					});	
				}catch(e){}
					
				return false;
			});
			
			
			$("div.stories div.upcoming-programs .paginate a").live("click", function(e){
				e.preventDefault();			
				var url = $(this).attr("href"); //.replace("j2j","j2j/upcoming-programs");				
				$(".stories .upcoming-programs").load(url, function(){ 
					//NpfMap.setupMarkers(); 
				});
			});
			
			
			$("div.stories div.past-programs .paginate a").live("click", function(e){
				e.preventDefault();			
				var url = $(this).attr("href");//.replace("j2j","j2j/past-programs");				
				$(".stories .past-programs").load(url, function(){ 
						//NpfMap.setupMarkers(); 
				});
			});
			
		},
		
		loadStories: function(event)
		{
			var url = $("#stories-search").attr('action') + NPF.buildSearchQueryString();

			$("#storyWrapper").load(url , function(){  
				NpfMap.setupMarkers(); 
				$('div.stories a.view-all-stories').show(); 
			});	
			
			//needed for the first after dom is ready
			if (event != undefined)
			{
				return false;
			}
		},
		
		buildSearchQueryString: function()
		{
			searchQueryString = '';
			
			//get the q if there is one
			var q = $('input[name=q]').val();
			
			//console.log(countryLocation);
			if (q != '' && q != 'Search')
			{
				searchQueryString = '?q=' + encodeURI(q);
			}
			
			return searchQueryString;		
		},

		setupSearch: function()
		{
			$("#stories-search").live("submit", function(e){
				e.preventDefault();
				NPF.loadStories();
			});
		},
		
		init : function () {
			NPF.hovers();
			NPF.paginate();
			NPF.setupSearch();
			NPF.media();
			NPF.overlays();
			
			if ($("#home-features").length > 0)
			{
				NPF.features();
			}
		}
	
	};
	
	NPF.init();
        
    // AWARDS DINNER CAROUSEL
    if ($('div.gallery-widget'))
    {
        $("#gallery-thumbs").jCarouselLite({
           btnNext: "a.next",
           btnPrev: "a.previous"
        });

	       $('.gallery-widget div.gallery-main').first().show();

	       $('.thumbs').click(function(event) {
	           event.preventDefault();

	           var  link = $(event.target).parent('a'),
	                photoID = link.attr('href'),
	                galleryMain = $('div#gallery-image-' + photoID),
	                galleryMains = $('div.gallery-main');

	            galleryMains.hide();
	            galleryMain.show();
	       }); 
    }
    
	// LIGHTBOXES
	$('.awards .photos a').lightBox();
	$('.programs .photos a.lb').lightBox();
	$('.galleries ul.gallery a.lb').lightBox();
	$('.galleries .gallery-detail a.lb').lightBox();

	// CLEAR SEARCH BOXES
	$('.default').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
			this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
			this.value = default_value;
			}
		});
	});

			// LIGHTBOXES
			$('.guidelines a').click(function(){
				$.ajax({
					type: "POST",
					url: $(this).attr("href"),
					data: "",
					success: function(data){
						var winW = $(window).width();
						var winH = $(window).height();
						var ph = $(this).parent().height();
						$("body").append(data);
						var w = $("#overlay-content").width();
						var h = $("#overlay-content").height();
						$("#overlay-content").css('left', winW/2-w/2).css('top', winH/2-h/2).fadeIn("slow");
						$("#overlay").fadeIn("slow");
						$.scrollTo(0, 400);
					}
				});
				return false;
			});

			$('a.psbio').click(function(){
				$.ajax({
					type: "POST",
					url: $(this).attr("href"),
					data: "",
					success: function(data){
						var winW = $(window).width();
						var winH = $(window).height();
						
						var ph = $(this).parent().height();
						
						$("body").append(data);
						
						var w = $("#overlay-content").width();
						var h = $("#overlay-content").height();
						var o = $("#overlay-content").offset();
						
						$("#overlay-content").css('left', winW/2-w/2).css('top', o.top + (winH/2-h/2)).fadeIn("slow");
						$("#overlay").fadeIn("slow");
					}
				});
				return false;
			});




	// BLOG NAV
    // $('#header ul.navigation li.blogs').hover(function() {
    //  $(this).addClass('over');
    // }, function() {
    //  $(this).removeClass('over');
    // });  
    // 
    // $('.main .select').hover(function() {
    //  $(this).addClass('over');
    // }, function() {
    //  $(this).removeClass('over');
    // });  
	
	$(".accordion h2").each(function(){
		$(this).next("div").hide();
	});
	
	$(".accordion h2").click(function(){
		$(this).next("div").slideToggle("slow");
		
		return false;
	});
	
	
	//accordion();

	
	if ($('.home .beatbook ul li a').length > 0)
	{
	    $('.home .beatbook ul li a').each(
	        function()
	        {
	            var text = $(this).text();
	            var text = text.replace(/^\s+|\s+$/g, '');    
            
                var html = '';
                var counter = 0;
                
                for (var i=0; i < text.length; i++) {
                    
                    if (counter === 0)
                    {
                        html += '<span>';
                    }
                    
                    html += text.charAt(i);
                    
                    counter++;

                    if (counter > 20 && text.charAt(i) == ' ')
                    {
                        html += '</span>';
                        counter = 0;
                    }
                };
                
                html += '</span>'
                
                $(this).text('');
                $(this).html(html)
	        }
	    );
	}
	
	$("#past-programs div.pager a").live('click', function() {
		var link = $(this),
		href = link.attr('href').toString().replace('programs-and-resources', 'programs-and-resources/past-programs')
		$("#past-programs").load(href);
		return false; 
   });

   		$(document).ready(function() {
		$('#topheadline').css({background: '#C7C7C7', opacity:'0'});
			$('#topheadline').animate({opacity:'1', backgroundColor: '#BC1907'}, 1500);
			$('#close_it').click(function() {
				$('#topheadline').animate({height: 'toggle', opacity:'0'}, 'slow');
			});
		});
   
});
