// JavaScript Document

// Define current_edition_num in 'global' scope so that it's persistent
var current_edition_num = 1;
var intervalID;

function nextEdition(){
	// default to the first edition
	var next_num = 1;
	
	// get child count
	var children_count = $("#coinslot").children().size(); 
	
	// if current_edition_num is less than to the number of children
	if (current_edition_num < children_count){
		next_num = $("#coinslot" + current_edition_num).next().attr("id").replace(/coinslot/,"");		
	}
	
	clickAction(next_num);
}

function clickAction(num)
{
	$("#coinslot" + current_edition_num + " img").removeClass("current");
	$("#edition" + current_edition_num).fadeOut("slow");

	current_edition_num = num;

	$("#coinslot" + current_edition_num + " img").addClass("current");
	$("#edition" + current_edition_num).fadeIn("slow");		
}

function sanitizeTubePressString(str) {
	var removeFront = 'function onclick(event) {\n    tubePress_popup("';
	var removeBack = '", 355, 425);\n}';
	var sanitizedString = str.replace(removeFront,"").replace(removeBack,"");
	return sanitizedString;
}



$(document).ready(function() {

	/* Display Flash Lantern Intro for visitors every 2 weeks */
	$("#intro").each(function(){
		$("#wrapper").css("display","none");
		swfobject.embedSWF("intro.swf", "intro_flash", "100%", "100%", "9.0.0", "", {}, {wmode: "window", menu: "false", quality: "high", bgcolor: "#000000"}, {});
		$("#intro_enter").removeAttr("href");
	});
	$("a#intro_enter").click(function() {
		$("#intro").remove();
		$("#wrapper").css("display","block");
	});

	/* Display Flash Lantern Logo */
	swfobject.embedSWF("/wp-content/themes/NFS_2009/swf/lantern_logo.swf", "flash_logo", "940", "160", "9.0.0", "", {}, {wmode: "transparent", menu: "false", quality: "high", bgcolor: "#3d3a31"}, {});

	/* Spam harvester protection - CM's plugin */
	$("span.mailme").each(function(){
		   var spt = $(this);
		   var addr = $(spt).text().replace(" hailing from ","@").replace(" from the TLD of ",".");
		   $(spt).after('<a href="mailto:'+addr+'">'+ addr +'</a>')
		   $(spt).remove();
	});

	$(".mailimage img").each(function(){
		var spt = $(this);
		var addr = $(spt).attr("alt").replace(" at ","@").replace(" dot ",".");
		$(spt).wrap('<a href="mailto:'+ addr +'"></a>');
	});
	
	/* Image border & drop shadow */
	$(function(){
		//alert("inside function");
		$(".post img.alignleft").wrap('<div class="image_dropshadow post_img_left"></div>');
		$(".post img.alignright").wrap('<div class="image_dropshadow post_img_right"></div>');
		$("#rd_list img").wrap('<div class="image_dropshadow post_img_left"></div>');
		
		var img_width = $(".image_dropshadow img").attr("width") + 30;
		var img_height = $(".image_dropshodow img").attr("height") + 65;

		$('.image_dropshadow').each(function() {
			// 'this' is a DOM element
			this.width = img_width;
			this.height = img_height;
		});
	});
	
	//$(".image_dropshadow").dropShadow();


	/* Suckerfish dropdown menus by Myles Angell http://be.twixt.us/jquery/suckerFish.php */
	$("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );
    if (document.all) {
        $("#nav-one li").hoverClass ("sfHover");
    }
	
	/* Homepage News Article */
	/*$('.newspaper:not(:first)').hide();
	$("#coinslot a").click(function() {
		$('.newspaper').fadeOut("slow");
		$($(this).attr("id")).fadeIn("slow");
		//$($(this + " img").addClass("current"));
	});*/
	
	
	// editions container was hidden, so make visible
	$('#newstand').css("visibility","visible");

	// hide all editions
	$('.newspaper').hide();

	// Prepare coinslot functionality
	$("#coinslot a").click(function() {
		// stop rotation
		clearInterval(intervalID);

		// Do the click action
		var num = $(this).attr("id").replace(/coinslot/,"");
		clickAction(num);
	});
	
	// 'Click' the Current Edition
	clickAction(current_edition_num);
	
	// set rotation script
	intervalID = window.setInterval('nextEdition()', 5000);
	
	
	//calendar fix: keep links in same window
	//$('#wp-calendarLarge a').removeAttr("target");

	//tubepress bug fix - make it open in thickbox
	
	$(".tubepress_thumb_widget .tubepress_video_thumb_inner a").each(
	 
		function() {
			var onclickObject = $(this).attr("onclick").toString();
			var urlTmp = onclickObject.split("tubePress_popup(");
			urlTmp = urlTmp[urlTmp.length-1];
			urlTmp = urlTmp.split(",");
			var urlString = urlTmp[0].substr(1,(urlTmp[0].length-2))+"?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=350&width=400";
			$(this).removeAttr("onclick");
			$(this).removeAttr("href");
			$(this).attr("href",urlString);
			$(this).attr("title","");
			$(this).addClass("thickbox");
		}
	 
	);
	
	// donate button hover
	$("#donate_button img").hover(
		function() {
			$(this).attr("src","/wp-content/themes/NFS_2009/images/button_donate_on.png");
		},
		function() {
			$(this).attr("src","/wp-content/themes/NFS_2009/images/button_donate_off.png");		
		}
	);
	
	
	$("ul.sf-menu").superfish({
		delay:       800,                            // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       'fast',                          // faster animation speed 
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false                            // disable drop shadows 
	}); 
	
	$('#platform_list_wrapper').mousemove(function(e) {
	
		var position = $(this).position();
		var offset = $(this).offset();
		var x = e.pageX - (offset.left);
		var y = e.pageY - (offset.top);
		var scroll_padding = 94;
		var container_height = $(this).height();
		var list_height = $("#platform_list").height();
		if (y < scroll_padding) {
			// don't scroll
			var y_percent = 0;
		} else {
			if (y > container_height-scroll_padding) {
				var y_percent = 1;
			} else {
				var y_percent = (y-scroll_padding)/(container_height-(scroll_padding*2));
			}
		}
		var move_to = Math.round(-((list_height-container_height)*y_percent));
		
		//$("#platform_list").css("top",move_to+"px");
		$("#platform_list").animate({
				top: move_to
			}, 50
		);
		
	});



	$("#sps_wrapper_header h1 a").hover(
		function() {
			$("#sps_wrapper_header").css("background-position","3px 0");
		},
		function() {
			$("#sps_wrapper_header").css("background-position","5px 5px");
		}
	);

});


/* Suckerfish dropdown menus by Myles Angell http://be.twixt.us/jquery/suckerFish.php */
$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};    