var NCDday=new Date(2011, 10, 10) //Month is 0-11 in JavaScript

var one_day=1000*60*60*24;
var one_hour=1000*60*60;
var one_minute=1000*60;
var one_second=1000;


function timeCalc() {
	var today=new Date();
	var timezoneOffset = today.getTimezoneOffset();
	
	var daysUntil = Math.ceil((NCDday.getTime()-today.getTime())/(one_day));
	var secondsInto = (Math.ceil((NCDday.getTime()-today.getTime())/(one_second)*-1))+(timezoneOffset*60)+(4*60*60);
	var minutesInto = (Math.ceil((NCDday.getTime()-today.getTime())/(one_minute)*-1))+timezoneOffset+(4*60);
	var hoursInto = (Math.ceil((NCDday.getTime()-today.getTime())/(one_hour)*-1))+(timezoneOffset/60)+4;
	
	//Calculate difference btw the two dates, and convert to days
	if (daysUntil > -1 && daysUntil < 1) {
		var percentage = 100-((secondsInto/86400)*100);
	} else if (daysUntil < -1) {
		var percentage = 0;
	} else if (daysUntil > 1) {
		var percentage = 100;
	} else {
		var percentage = 0;
		$('#nineam').remove();
	}
	if (percentage < 0) {
		window.log('cancel');
		$('#shadow').remove();
		$('#nineam').remove();
		clearInterval(timeInt);
		return false;
	}
	window.log(minutesInto);
	window.log("percentage "+percentage);
	if (daysUntil > -1 && daysUntil < 1) {
		if ($('#map #shadow').length == 0) {
			$('#map').append('<div id="shadow" />');
			$('#map').append('<img src="/images/shadow_mask.png" id="shadowMask">');
			$('#map').append('<img src="/images/label_9am.png" id="nineam">');
		}
		$('#shadow').css({
			'width': percentage+'%'
		});
		$('#nineam').css({
			'position': 'absolute',
			'left': percentage+'%',
			'bottom': '50px',
			'marginLeft':'-37px',
			'zIndex':'6'
		});
		
		var darkside = $('#shadow').width();
		
		$('#map ul li a:not(.flagHidden)').each(function(){
			var pos = $(this).position();
			if (pos.left < darkside) {
				$(this).addClass('disabled');
			} else {
				$(this).removeClass('disabled');
			}
		});
	} else {
		$('#map ul li').hide();
		$('#map').append('<div class="videoHolder"><object style="height: 280px; width: 459px"><param name="movie" value="http://www.youtube.com/v/kLQwDGDf0-o?version=3&feature=player_profilepage"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/kLQwDGDf0-o?version=3&feature=player_profilepage" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="459" height="280"></object></div>');
		clearInterval(timeInt);
	}
}


addBookmark = function() {
	if (document.all) {
		bookmarkurl=document.location;
		bookmarktitle="NET Cancer Day";
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
	} else {
		alert("Unfortunately your browser doesn't support adding bookmarks via this link.\n\nTo bookmark this page please use your browser's menu.");
	}
}

$(function(){
	
	if ( location.pathname.split("/")[1] == "learn-more" ) { 
		$('#menu-item-2144').addClass('current_page_parent');
	}
	
	
	
	if ($('body').hasClass('events')){
		
		var id = $('ul#nav li.current_page_parent').attr('id')
		$('ul#nav li.current_page_parent').removeClass('current_page_parent');
		$('#' + id).next().addClass('current_page_parent');
	
	}
	$('#bookmarkLink').click(function(e){
		e.preventDefault();
		addBookmark();
	});
	/*
	if ($('#map').length > 0 && $('body#mapdev').length < 1) {
		timeInt = setInterval("timeCalc()",2000);
		timeCalc();
	}
	*/
	if ($('#mapProgress').length > 0) {
		$('#mapProgress').append('<img src="/images/shadow_mask_bot.png" id="shadowMask">');
	}
	
	$('#map ul li a:not(.disabled)').live('click',function(e) {
		e.preventDefault();
		var w = $(this).attr('href').split('#')[1];
		if ($(this).hasClass('activated')) {
			$(this).removeClass('activated');
			$('#'+w).removeClass('activated').fadeOut();
			$('#map ul li a').removeClass('flagHidden').fadeIn();
		} else {
			$('#map ul li a.activated').removeClass('activated');
			$('.popup.activated').removeClass('activated').fadeOut();
			$(this).addClass('activated');
			$('#map ul li a:not(.activated)').addClass('flagHidden').fadeOut();
			$('#'+w).addClass('activated').fadeIn();
		}
	});
   $('.popup a.close').live('click',function(e){
   	$(this).parent().removeClass('activated').fadeOut();
		$('#map ul li a.activated').removeClass('activated');
		$('#map ul li a').removeClass('flagHidden').fadeIn();
   });
	
   $('.popup a.vidLink').live('click',function(e) {
   	e.preventDefault();
   	$.fancybox({
			'padding'		: '19px',
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 680,
			'height'		: 495,
			'overlayColor'	: '#000000',
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
   });
   $('table.directory tr td:nth-child(2)').css("width", "108px");
   $('table.directory tr td:nth-child(3)').css("width", "395px");
   $('table.directory tr td:nth-child(4)').css("width", "115px");
	// --------------------------------------------------------------
   // Share Page Slide Down
   // --------------------------------------------------------------

   $('#share-icons').hide();
   $('#share-page').toggle(function() {
		$('#share-icons').slideDown();
	}, function() {
		$('#share-icons').slideUp();
	});
	
});
