var template_url = "/wp-content/themes/nfs2011";
var social_dropdown = "closed";
var intervalID;
var scrambleIntervalID;
var letterIntervalID;
var tabGlowIntervalID;

var home_video_desc = '';
// Tell Vimeo what function to call
var oEmbedCallback = 'embedVideo';
// Set up the URL
var oEmbedUrl = 'http://vimeo.com/api/oembed.json';
// Load the first one in automatically?
var loadFirst = true;
// This function puts the video on the page
function embedVideo(video) {
	var videoEmbedCode = video.html;
	document.getElementById('embed').innerHTML = unescape(videoEmbedCode);
}
// This function loads the data from Vimeo
function loadScript(url) {
	var js = document.createElement('script');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}

$(function() {
	$('.scroll-pane').jScrollPane({showArrows: true});
});


$(document).ready(function(){

    // Creating hoverscroll on news feed
	$('#news_feed').hoverscroll({
		vertical:		true,	// Display the list vertically or horizontally
		width:			500,	// Width of the list container
		height:			370,		// Height of the list container
		arrows:			true,	// Display direction indicator arrows or not
		arrowsOpacity:	1,	// Max possible opacity of the arrows
		fixedArrows:	false,	// Fixed arrows on the sides of the list (disables arrowsOpacity)
		rtl:			false	// Print images from right to left
		//debug:			true	// Debug output in the firebug console
    });
	// Starting the movement automatically at loading
	// @param direction: right/bottom = 1, left/top = -1
	// @param speed: Speed of the animation (scrollPosition += direction * speed)
	var hs_direction = -1,
		hs_speed = 3;
	//$("#news_feed")[0].startMoving(hs_direction, hs_speed);
	$("#news_feed").each(function(){
		$(this)[0].startMoving(hs_direction, hs_speed);
	});
    // Creating hoverscroll on news feed
	$('#staff_scroller').hoverscroll({
		vertical:		false,	// Display the list vertically or horizontally
		width:			882,	// Width of the list container
		height:			215,		// Height of the list container
		arrows:			false,	// Display direction indicator arrows or not
		arrowsOpacity:	1,	// Max possible opacity of the arrows
		fixedArrows:	false,	// Fixed arrows on the sides of the list (disables arrowsOpacity)
		rtl:			false	// Print images from right to left
		//debug:			true	// Debug output in the firebug console
    });
	$("#staff_scroller").each(function(){
		$(this)[0].startMoving(hs_direction, hs_speed);
	});

	home_video_desc = $("#home_video_desc").html();
	
	// resize nav container to accommodate for different nav labels
	/*
	$("#menu-main-navigation").each(function(){
		var nav_width = 0;
		var print_nav_width = '';
		$("#menu-main-navigation li").each(function(){
			if (!$(this).parent().hasClass('sub-menu')) {
				nav_width += $(this).width();
				print_nav_width += $(this).width() + ', ';
			}
		});
		$("#access, #access .menu-header, div.menu").css("width",(nav_width+20)+"px");
		$(".wf-active #access, .wf-active #access .menu-header, .wf-active div.menu").css("width",(nav_width+20)+"px");
		if ($('html').hasClass('wf-active')) {
			var tk = 'loaded';
		} else {
			var tk = 'not loaded';
		}
		$('body').append('<!-- '+print_nav_width+' '+tk+' -->');
	});
	*/

	// show signup landing page 
	var screen_width = screen.width;
	var screen_height = screen.height;
	if (screen_width <= 700 || screen_height <= 500 || window.location.pathname == '/broadcast' || window.location.pathname == '/broadcast/') {
		// skip mobile browsers
	} else {
		var COOKIE_NAME = 'nfs_gf2011';
		var cookie_options = { path:'/', expires: 1 };
	    /*if (!$.cookie(COOKIE_NAME)) {
	    	$.cookie(COOKIE_NAME, 'landing_relaunch', cookie_options);
	    	// Cookie just set. Now display landing page
	    	$('body').append(
				// Relaunch splash:
				'<div id="landing_container" class="landing_relaunch">'+
	    		'	<div id="landing_content">'+
	    		'		<div id="landing_links">'+
	    		'			<p class="landing_close_text"><a class="landing_close">Proceed &rarr;</a></p>'+
	    		'		</div>'+
	    		'		<a id="landing_close" class="landing_close"></a>'+
	    		'	</div>'+
	    		'</div>'
	    		// global forum splash:
	    		'<div id="landing_container" class="landing_gf2011">'+
	    		'	<div id="landing_content">'+
	    		'		<div id="landing_links">'+
	    		'			<p><a href="/events/global-forum-on-human-trafficking/" id="landing_gf_button"><img src="'+template_url+'/media/images/landing_gf2011_button.png" alt="Details &amp; Discounted Registration &gt; Here" /></a></p>'+
	    		'			<p class="landing_close_text"><a class="landing_close">Skip and go to site</a></p>'+
	    		'		</div>'+
	    		'		<a id="landing_close" class="landing_close"></a>'+
	    		'	</div>'+
	    		'</div>'
	    	);
	    	$("a.landing_close").click(function(){
	    		$("#landing_container").remove();
	    	});
	    } else {
	    	// Cookie already set, skip landing page
	    }*/
	    
	}
	// social media header tabs
	function open_social_tabs(section) {
		$("#tab_wrapper").animate({
			top:0
			}, 300, 
			function(){
				$("#tab_content_"+section).fadeIn('fast');
			}
		);
	}
	function close_social_tabs() {
		$("a.tab_button").removeClass("active");
		social_dropdown = "closed";
		$(".tab_content_section").fadeOut('fast',function(){
			$("#tab_wrapper").animate({
				top:-400
				}, 300
			);
		});
	}
	$("#tab_close_button").click(function(){
		close_social_tabs();
		return false;
	});
	$("a.tab_button").click(function(){
		var tab_id = $(this).attr("id").replace(/tab_button_/,'');
		if (tab_id == 'facebook') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'facebook';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'facebook') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open facebook
					social_dropdown = 'facebook';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_facebook").fadeIn('fast');
				}
			}
		} else if (tab_id == 'rss') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'rss';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'rss') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open rss
					social_dropdown = 'rss';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_rss").fadeIn('fast');
				}
			}
		} else if (tab_id == 'twitter') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'twitter';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'twitter') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open twitter
					social_dropdown = 'twitter';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_twitter").fadeIn('fast');
				}
			}
		} else if (tab_id == 'email') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'email';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'email') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open email
					social_dropdown = 'email';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_email").fadeIn('fast');
				}
			}
		}
		$("#tab_wrapper #tab_promo").fadeOut();
		$('body').css("margin-top","0");
		var cookie_options = { path:'/', expires: 14 };
		$.cookie('flashy_tabs','disable',cookie_options);
		clearInterval(tabGlowIntervalID);
		return false;
	});

	// Add target=_blank
	$('a[rel^="blank"]').each(function(){
		$(this).attr("target","_blank");
	});

	// Nav menu mods 
	$(".menu-item-type-custom").each(function(){
		$(this).attr("target","_blank");
	});
	$(".menu-item li").each(function(){
		$(this).has("ul").addClass('menu-has-submenu');
	});

	function big_scroller_transition(direction) {
		var source_l3 = $(".big_scroller_slide_l3").html();
		var source_l2 = $(".big_scroller_slide_l2").html();
		var source_l1 = $(".big_scroller_slide_l1").html();
		var source_0 = $(".big_scroller_slide_0").html();
		var source_r1 = $(".big_scroller_slide_r1").html();
		var source_r2 = $(".big_scroller_slide_r2").html();
		var source_r3 = $(".big_scroller_slide_r3").html();
		$("#big_scroller_nav a, #big_scroller_slider .big_scroller_slide a, #big_scroller_arrow_left, #big_scroller_arrow_right").addClass("no-click");
		if (direction == 'left') {
			var move_left = '+=942';
		} else if (direction == 'right') {
			var move_left = '-=942';
		}
		$("#big_scroller_slider, #big_scroller_active_slider").animate({
			left: move_left
		}, 600, function(){
			// reorder slides
			if (direction=='left') {
				$(".big_scroller_slide_l3").html(source_r3);
				$(".big_scroller_slide_l2").html(source_l3);
				$(".big_scroller_slide_l1").html(source_l2);
				$(".big_scroller_slide_0").html(source_l1);
				$(".big_scroller_slide_r1").html(source_0);
				$(".big_scroller_slide_r2").html(source_r1);
				$(".big_scroller_slide_r3").html(source_r2);
			} else if (direction=='right') {
				$(".big_scroller_slide_l3").html(source_l2);
				$(".big_scroller_slide_l2").html(source_l1);
				$(".big_scroller_slide_l1").html(source_0);
				$(".big_scroller_slide_0").html(source_r1);
				$(".big_scroller_slide_r1").html(source_r2);
				$(".big_scroller_slide_r2").html(source_r3);
				$(".big_scroller_slide_r3").html(source_l3);
			}
			// move #big_scroller_active_slider to left:-942px and #big_scroller_slider to left:-1884px;
			$("#big_scroller_active_slider").css("left","-942px");
			$("#big_scroller_slider").css("left","-1884px");
			$("#big_scroller_nav a, #big_scroller_slider .big_scroller_slide a, #big_scroller_arrow_left, #big_scroller_arrow_right").removeClass("no-click");
		});
	}
	
	// big scroller nav buttons
	$("#big_scroller_nav a, #big_scroller_slider .big_scroller_slide, #big_scroller_arrow_left, #big_scroller_arrow_right").click(function(){
		var direction;
		if ($(this).hasClass("no-click")) {
			return false;
		} else {
			
			if ($(this).hasClass("big_scroller_slide_r1") || $(this).attr("id") == "big_scroller_arrow_right") {
				direction = 'right';
			} else if ($(this).hasClass("big_scroller_slide_l1") || $(this).attr("id") == "big_scroller_arrow_left") {
				direction = 'left';
			} else {
				direction = $(this).attr("id").replace(/big_scroller_nav_/,'');
			}
			big_scroller_transition(direction);
			clearInterval(intervalID);
			return false;
		}
	});
	
	$("#big_scroller_active_slider .big_scroller_slide").hover(
		function() {
			$("span",this).fadeIn("fast");
		},
		function() {
			$("span",this).fadeOut("fast");
		}
	);

	// Word scramble on homepage
	var scramble_phrases = Array();
	var count_phrases = 0;
	var current_phrase = 1;
	$("#home_scramble #fb-root").hide();
	$("#home_scramble p.scramble_hidden").each(function(){
		count_phrases++;
		scramble_phrases[count_phrases] = $("#scramble_"+count_phrases).text();
	});
	var rand_alpha = Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	var count_scramble_cycles = 0;
	var max_scramble_cycles = 5;
	function scramble_letter(path,letter) {
		var random = Math.floor(Math.random() * (26 - 0 + 1) + 0);
		setTimeout(function() {
			count_scramble_cycles++;
			if(count_scramble_cycles >= 20) {
				$(path).html(letter);
				count_scramble_cycles = 0;
			} else {
				$(path).html(rand_alpha[random]);
				scramble_letter(path,letter);
			}
		}, 20);
	}
	function scramble_interval() {
		scrambleIntervalID = window.setInterval(function(){ scramble_phrase(); }, 11000);
	}
	function scramble_phrase() {
		clearInterval(scrambleIntervalID);
		if (current_phrase < count_phrases) {
			current_phrase++;
		} else {
			current_phrase = 1;
		}
		var phrase_char_count = scramble_phrases[current_phrase].length;
		var row = 1;
		var col = 1;
		for(i=0;i<=phrase_char_count;i++) {
			var letter = scramble_phrases[current_phrase].charAt(i);
			if (letter == "|") {
				for(j=col;j<=22;j++) {
					$(".line_"+row+" .col_"+j).html("&nbsp;");
				}
				row++;
				col = 1;
			} else {
				if (letter == " ") {
					letter = "&nbsp;";
				}
				var path = ".line_"+row+" .col_"+col;
				scramble_letter(path,letter);
				col++;
			}
		}
		if(col < 22 || row < 4) {
			for(i=row;i<=4;i++) {
				for(j=col;j<=22;j++) {
					$(".line_"+i+" .col_"+j).html("&nbsp;");
				}
			}
		}
		scramble_interval();
		//alert("row = " + row + " col = "+col)
		//var line_bits = scramble_phrases[current_phrase].split()
	}
	
	var cookie_options = { path:'/', expires: 1 };
    if (!$.cookie('flashy_tabs')) {
    	$.cookie('flashy_tabs', 'enable', cookie_options);
    	// Cookie just set.
    } else {
    	// Cookie already set, skip
    }

	// start big scroller animation and word scramble
	if ($("body").hasClass("home")) {
		scramble_phrase();
		intervalID = window.setInterval(function(){ big_scroller_transition("right"); }, 10000);
	}
	if ($.cookie('flashy_tabs') == 'enable') {
		tab_glow();
		$("body").css("margin-top","0px");
		$("#tab_wrapper").append('<div style="background:#5A5A5A; position:absolute; width:100%; height:30px; bottom:0; left:0;" id="tab_promo"><div style="width:940px; margin:0 auto; text-align:right; font-size:1.4em; color:#ea7231; padding-top:5px;">Access our social media accounts using these new tabs</div></div>');
	}

	// social tab glow effect
	function tab_glow_interval() {
		tabGlowIntervalID = window.setInterval(function(){ tab_glow(); }, 10000);
	}
	function tab_glow() {
		clearInterval(tabGlowIntervalID);
		var delay = 300;
		$("#tabs a.tab_button img").each(function(){
			var this_img = $(this);
			delay += 100;
			this_img.animate({
					opacity: 0
				}, 
				delay, 
				function() {
					// Animation complete.
					this_img.animate({
						opacity: 1
					}, 
					delay);
				}
			);
		});
		tab_glow_interval();
	}


	// GCE list
	$(".gce-list-desc a").each(function(){
		var href = $(this).attr("href");
		var parent_li = $(this).parent().parent();
		$(".gce-list-event",parent_li).wrapInner('<a href="'+href+'">');
		//$(this).parent().parent().wrapInner('<a href="'+href+'">');
		$(this).parent().remove();
	});

	// Vimeo Videos on homepage
	$("#home_video_tns a").each(function(){
		var img_src = $("img",this).attr("src");
		$(this).css("background","url("+img_src+") 50% 50% no-repeat");
		$(this).animate({
				opacity:0.5
			},100
		);
	});
	$("#home_video_tns a").hover(
		function(){
			$(this).animate({
					opacity:1
				},300
			);
			$("#home_video_desc").html($(this).attr("title"));
		},
		function(){
			$(this).animate({
					opacity:0.5
				},300
			);
			$("#home_video_desc").html(home_video_desc);
		}
	);
	$("#home_video_tns a").click(function(){
			home_video_desc = "<span>Now Playing: </span>"+$(this).attr("title");
			$("#home_video_desc").html(home_video_desc);
			loadScript(oEmbedUrl + '?url=' + $(this).attr("href") + '&title=0&byline=0&portrait=0&color=ff9933&width=370&height=208&autoplay=true&callback=' + oEmbedCallback);
			return false;
	});	
	$("#home_videos #embed").each(function(){
		loadScript(oEmbedUrl + '?url=' + $("#home_video_tns a:first").attr("href") + '&title=0&byline=0&portrait=0&color=ff9933&height=208&width=370&callback=' + oEmbedCallback);
	});
	
	// World map on homepage 
	$("#home_world_map a").hover(
		function(){
			var id = $(this).attr("id");
			var title = $("img",this).attr("alt");
			$("img",this).fadeIn('fast');
			$("#home_world_map h2 span").html(title);
		},
		function(){
			var id = $(this).attr("id");
			$("img",this).fadeOut('fast');
			$("#home_world_map h2 span").html("");
		}
	);

	// Platform slider on homepage
	$("#program_scroller_slider").each(function(){
		var tn_count = $(this).children().length;
		var tn_width = 185;
		var tn_stage_width = 912;
		var tn_scroller_width = (tn_count*tn_width);
		var tn_position = 0;
		$(this).css("width",tn_scroller_width+"px");
		if (tn_count > 5) {
			// center badeges in scroller
			var centered_position = Math.floor((tn_count-5)/2);
			tn_position = centered_position;
			var target_position = -(tn_position * tn_width);
			$(this).css("left",target_position+"px");
			// show arrows
			$("#program_scroller_arrow_left").css("display","block");
			$("#program_scroller_arrow_right").css("display","block");
			$("#program_scroller_arrow_right").click(function(){
				tn_position += 1;
				if(tn_position > (tn_count - 5)) {
					tn_position = tn_count - 5;
				} else {
					var target_position = -(tn_position * tn_width);
					$("#program_scroller_slider").animate({
						left: target_position 
						}, 200
					);
				}
				return false;
			});
			$("#program_scroller_arrow_left").click(function(){
				tn_position -= 1;
				if (tn_position < 0) {
					tn_position = 0;
				} else {
					var target_position = -(tn_position * tn_width);
					$("#program_scroller_slider").animate({
						left: target_position
						}, 200
					);
				}
				return false;
			});
		}
	});
	
	// Platform links on homepage
	$("#program_scroller_slider a span em").each(function(){
		var text_height = $(this).height();
		var block_height = $("#home_program_scroller").height();
		var top_padding = (block_height - text_height) / 2;
		$(this).css("padding-top",top_padding+"px");
	});
	$("#program_scroller_slider a span").hide();
	$("#program_scroller_slider a").hover(
		function(){
			$("span",this).fadeIn("fast");
		},
		function(){
			$("span",this).fadeOut("fast");
		}
	);
	
	// Staff Bios
	$(".staff_member_container a").click(function(){
		var id = $(this).attr("href");
		$('.staff_full_bio').fadeOut('fast', function() {
		});
		$(".staff_member_container").removeClass("staff_member_container_active");
		$(this).parent().addClass("staff_member_container_active");
		$(id).fadeIn('fast');
		return false;
	});
	
	var action_content_height = 0;
	$(".action-content").each(function(){
		var element_height = $(this).height();
		if (element_height > action_content_height) {
			action_content_height = element_height;
		}
		if (!$(this).hasClass("active")) {
			$(this).hide();
		}
	});
	$("#action-content-wrapper").css("height",action_content_height+"px");
	
	function swap_action_content(target) {
		$("#action-content-wrapper .active").fadeOut("fast",function(){
			$(this).removeClass("active");
			$("#"+target).fadeIn("fast");
			$("#"+target).addClass("active");
		});
	}
	$("#action-heading-links a").click(function(){
			$("#action-heading-links li.active").removeClass("active");
			$(this).parent().addClass("active");
			var target = $(this).attr("href").replace(/#/,"");
			swap_action_content(target);
			return false;
	});

	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_square' /*, // light_rounded / dark_rounded / light_square / dark_square / facebook 
		overlay_gallery: false ,
		showTitle: false,
		opacity: 0.5,
		animationSpeed: 'fast',
		default_width: 700*/
	});

	$("span.mailme").each(function(){
		   var spt = $(this);
		   var addr = $(spt).text().replace(" (at) ","@").replace(" dot ",".");
		   $(spt).after('<a href="mailto:'+addr+'">'+ addr +'</a>')
		   $(spt).remove();
	});

	// position big scrolling images on homepage and page dropshadow
	function reposition_scroller() {
		var content_width = 942;
		var container_width = 2826;
		var window_width = $(window).width();
		var window_height = $(window).height();
		var body_height = $("body").height();
		var left_position = (window_width - container_width)/2;
		$("#big_scroller_container").css("left",left_position+"px");
		// enable banner images
		$(".big_scroller_slide img").each(function(){
			var real_img = $(this).attr("id");
			$(this).attr("src",real_img);
		});
		$("#big_scroller_active_slider").css("display","block");
		$("#big_scroller_slider").css("display","block");
		left_position = (window_width - content_width)/2;
		$("#page_dropshadow").css("left",left_position+"px");
		$("#page_dropshadow").css("min-height",window_height+"px");
		if (window_height > body_height) {
			var clip_height = window_height;
		} else {
			var clip_height = body_height;
		}
		$("#page_dropshadow").css("clip","rect(0px, 952px, "+clip_height+"px, -10px)");
		$("#page_dropshadow").css("display","block");
		var arrow_width = (window_width-content_width)/2;
		$("#big_scroller_arrows a").css("width",arrow_width+"px");
	}
	reposition_scroller();
	$(window).resize(function(){
		reposition_scroller();
	});
	

});

(function() {
	try {
		Typekit.load({
			active: function() {
				var nav_width = 0;
				$("#menu-main-navigation li").each(function(){
					if (!$(this).parent().hasClass('sub-menu')) {
						nav_width += $(this).width();
					}
				});
				$("#access, #access .menu-header, div.menu").css("width",(nav_width+15)+"px");
				var col_3_height = 0;
				$(".column-3").each(function(){
					var this_height = $(this).height();
					$(this).attr("rel","height-"+this_height);
					if (this_height > col_3_height) {
						col_3_height = this_height;
					}
				});
				$(".column-3").css("height",col_3_height+"px");
			},
			inactive: function() {
				$("#access, #access .menu-header, div.menu").css("width",(nav_width+15)+"px");
			}
		})
	} catch(e) {}
})();
