var _pageTitle = document.title;// = "Your site name here";

/* METHODS used by other overlay code */
var activeVideoPanel = 0; 
function removeActiveVideo() {
	videoCheck();
};
function reembedActiveVideo() {
	//do not do if chatter overlay is open
	var _chatterOpen = $('#chatter-flash').length !== 0;
	if(activeVideoPanel && !_chatterOpen) {
		embedVideo(activeVideoPanel);
	}
};

/* video methods */
function videoCheck() {
	//find active panel
	if(window.location.hash) {
		var _active = window.location.hash.replace('/','');
		removeVideo(_active);
	}
};
function removeVideo(panel_id) {	
	var $activePanel = $(panel_id);
	if($activePanel.hasClass('video')) {
		//remove video
		$(panel_id + '-video').remove();
		//show link again (timeout to prevent flash of image)
		setTimeout(function() {
			$activePanel.find('a[type=video/m4v]').show();
		},500);
	}
}
function embedVideo(panel_id) {
	var $panel = $(panel_id);
	if($panel.hasClass('video')) {
		activeVideoPanel = panel_id;
		//get video link
		var $vlink = $panel.find('a[type=video/m4v]');
		$vlink.hide();
		//create video container
		var _vcont_id = $panel.attr('id') + '-video';
		$('<div id="' + _vcont_id + '" />').insertAfter($vlink);
		//get video url
		var _vurl = ($vlink.attr('href')).replace('mv4','flv');
		//embed video
		var flashvars = {};
		flashvars.flvPath = _vurl;
		var params = {};
			params.wmode = 'opaque';
		var attributes = {};
		swfobject.embedSWF("_files/swf/eco_video_player_small.swf", _vcont_id, "380", "215", "9.0.28", null, flashvars, params, attributes);	
		//stupid IE
		if($.browser.msie && parseInt($.browser.version) < 8) {
			document.title = _pageTitle;			
		}		
	} else {
		activeVideoPanel = false;
	}	
}	


$(document).ready(function() {
	
	$('#home-page-articles .nav a').click(function() {
		$('#home-page-articles .nav a').removeClass('current');
		$(this).addClass('current');
		var targetId = $(this).attr('href').substring(1);
		var $target = $('#' + targetId);
		videoCheck();
		if(window.location.hash.indexOf(targetId) === -1) {
			if (window.location.hash.charAt(2) !== '#') {
				window.location.hash = '/' + targetId;
			}
		}
		$('#slider').scrollTo($target, 500, {
			onAfter: function() { 
				embedVideo('#' + targetId);
			}
		});
		return false;
	});
	
	//set beginning state
	if(window.location.hash) {
		var _id = window.location.hash.substr(2);
		var $to = $('#' + _id);
		$('#home-page-articles .nav').find('a[href$="#' + _id + '"]').addClass('current').click();
	} else {
		$('#home-page-articles .nav a:eq(0)').addClass('current');
	}	

});