// ====================== Everyday Explorer Site-Wide JavaScript ====================== //

// ------------- JQuery-Based ------------- //
$(document).ready(function(){

	// --- ifixpng (IE6 PNG Support Fix) --- //
	// apply to all png images and to div#logo 
	//	$('img[@src$=.png], div#logo, div#header').ifixpng();

	// --- Tabs --- //
	$(".tabbed > ul.tab-navigation").tabs();
	
	// --- Individual Video Page Collapse/Expand --- //
	$(".actions > ul").tabs({ selected: null, unselect: true, fx: { height: "toggle", opacity: "toggle" } });
	
	// --- Toggle Comments On/Off --- //
	// making sure the content is long enough
	if ($(".comments .comment").size() > 3) {
	
		// insert a wrapper div after the 3rd comment (and hide it)
		var $xwrapper = $('<div class="expand-container"></div>').insertAfter('.comments .comment:eq(2)').hide();

		// append all comments following the 3rd into that wrapper div
		$('.comments .comment:gt(2)').appendTo($xwrapper);

		// add in toggle button
		$('.comments-navigation').prepend('<li><a class="button button-show expand-toggle" href=""><span>Show All Comments</span></a></li>');

		// toggles the newly made and hidden container up and down
		$('.expand-toggle').click(function(){

			// slowly expand/collapse
			$xwrapper.slideToggle('slow');

			// toggles class and content on toggle button
			$(this).toggleClass('expand-toggle-opened')
			
			// disables the default html behavior of the <a> element
			return false;
		});
	}
	
	// --- Toggle Flag Comments On/Off --- //
	var $flagcontent = $(".comment .flag-comment").hide();
	
	// add in toggle button
	$('.comment-header').append('<a class="button button-flag flag-toggle" href=""><span>Flag This Comment</span></a>');
	
	// toggles the flag form
	$('.flag-toggle').click(function(){

		// slowly expand/collapse
		$(this).parent().parent().children('.flag-comment').slideToggle('slow');

		// toggles class on toggle button
		$(this).toggleClass('flag-toggle-opened')

		// disables the default html behavior of the <a> element
		return false;
	});
	
	// --- Video Description Collapse/Expand --- //
	// For Topic page-like places
//	$(document).ready(function() {

		// Making sure that there are at least three elements
		if ($('.individual-video .description > *').size() > 2) {

			// insert a wrapper div after the 2nd element (and hide it)
			var $xwrapper = $('<div class="expand-container"></div>').insertAfter('.expand-article > *:eq(2)').hide();

			// append all elements following the 2nd into that wrapper div
			$('.individual-video .description > *:gt(2)').appendTo($xwrapper);

			// Add in Toggle Button
			$('.individual-video .description').append('<a class="expand-toggle" href="">Expand/Collapse This</a>');

			// Toggles the newly made and hidden container up and down
			$('.expand-toggle').click(function(){

				// Slowly expand/collapse
				$xwrapper.slideToggle('slow');

				// Toggles class on Toggle Button
				$(this).toggleClass('expand-toggle-opened')

				// Disables the default HTML behavior of the <a> element
				return false;
			   });
		}	

//	});
	
	// --- Original JQuery Rating Reference --- //
	//$('form.rating').rating().show();
	
});

// ------------- Flash Embedding ------------- //


NGS.onload(function(){

	// Everyday Explorers General
	if (document.getElementById("related-national-geographic-content")){
		ngsEmbed.flash({ 	
			src: "../flash/carousel.swf", 
			width: "660",
			height: "210",			
			version:"8",
			wmode:"transparent",
			flashvars: "xmlLocation=" + encodeURI("http://www.nationalgeographic.com/ngsxmlfeed/ugcMultimediaCarousel.jsp?ci=ugcmain")
			}, "related-national-geographic-content","flash-content-replaced");
	};

	// Favorite Places 
	if (document.getElementById("favorite-places-related-national-geographic-content")){
		ngsEmbed.flash({ 	
			src: "../flash/carousel.swf", 
			width: "660",
			height: "210",			
			version:"8",
			wmode:"transparent",
			flashvars: "xmlLocation=" + encodeURI("http://www.nationalgeographic.com/ngsxmlfeed/ugcMultimediaCarousel.jsp?ci=ugcplaces")
			}, "favorite-places-related-national-geographic-content","flash-content-replaced");
	};
		
	// Green Tips
	if (document.getElementById("green-tips-related-national-geographic-content")){
		ngsEmbed.flash({ 	
			src: "../flash/carousel.swf", 
			width: "660",
			height: "210",			
			version:"8",
			wmode:"transparent",
			flashvars: "xmlLocation=" + encodeURI("http://www.nationalgeographic.com/ngsxmlfeed/ugcMultimediaCarousel.jsp?ci=ugcgreentips")
			}, "green-tips-related-national-geographic-content","flash-content-replaced");
	};
	
	// Weather
	if (document.getElementById("weather-related-national-geographic-content")){
		ngsEmbed.flash({ 	
			src: "../flash/carousel.swf", 
			width: "660",
			height: "210",			
			version:"8",
			wmode:"transparent",
			flashvars: "xmlLocation=" + encodeURI("http://www.nationalgeographic.com/ngsxmlfeed/ugcMultimediaCarousel.jsp?ci=ugcweather")
			}, "weather-related-national-geographic-content","flash-content-replaced");
	};
	
	// Wildlife and Pets 
	if (document.getElementById("wildlife-and-pets-related-national-geographic-content")){
		ngsEmbed.flash({ 	
			src: "../flash/carousel.swf", 
			width: "660",
			height: "210",			
			version:"8",
			wmode:"transparent",
			flashvars: "xmlLocation=" + encodeURI("http://www.nationalgeographic.com/ngsxmlfeed/ugcMultimediaCarousel.jsp?ci=ugcanimals")
			}, "wildlife-and-pets-related-national-geographic-content","flash-content-replaced");
	};
});