
$(document).ready(function() {

	change_remote(0);
	div_switcher('tv_feature_switcher', 'next', '', 5000);
//	div_switcher('advanced_science_switcher', 'next', 'div#advanced_science_switcher_buttons a.AS_clickers', 3000);
	div_switcher('advanced_science_switcher', 'next', 'div#advanced_science_switcher_buttons a.AS_clickers', false);
	adjust_body_height();
	init_feature_filterbar();

	max_popin_height = $(window).height()-180;
	init_popin()
	check_url_for_popin();
	
	// rolovers... the old skool / new skool way...
	$('.rollover').mouseover(function() {
		$(this).attr('src', $(this).attr('src').replace('_off','_on'));
	});
	$('.rollover').mouseout(function() {
		$(this).attr('src', $(this).attr('src').replace('_on','_off'));
	});
});

function init_popin() {
	 $('a.popin').click(function() {
		$('body').attr('scroll','no');
	 	var t = this.title || this.name || null;
	 	var a = this.href;// || this.alt;
	 	var g = this.rel || false;
	 	tb_show(t,a,g);
	 	localized_url = a.substring(a.indexOf('/features/')+1, a.indexOf('TB_')-1);
	 	localized_url_with_TB = a.substring(a.indexOf('/features/')+1);
	 	top.window.location.hash=localized_url_with_TB;
	 	tool_buttons  = ' <a class="toolbar_links" id="TB_credits_links" onclick="toggle_credits();return false;" href="#">credits</a>';
	 	breakout_btn  = ' <a class="toolbar_links" style="float:right;font-size:16px;color:#fff;border:1px solid #fff;background-color:#777;padding:1px 4px" id="TB_breakout" href="'+localized_url_with_TB+'">break out</a> ';
	 	breakout_btn  = ' <a class="toolbar_links" style="color:#fff" id="TB_breakout" href="'+localized_url_with_TB+'">Full Screen</a> ';
	 	$('#TB_window').append('<div id="TB_toolbar_bottom"><div id="TB_toolbar_content"></div>'+tool_buttons+'</div>');
//		$('#TB_closeAjaxWindow').append(breakout_btn)
	 	$('#TB_toolbar_content').load('_credits.php?f='+localized_url, {}, function() {
	 		if($('#TB_toolbar_content').html() == '') {
	 			$('#TB_credits_links').hide();
	 		}
	 	});
	 	this.blur();
	 	return false;
	 });
}

function adjust_body_height() {
	if($('body').hasClass('home')) {
		body_height = 600;
	} else if ($('body').hasClass('kids_families')) {
		body_height = $('div#at_the_museum_events_list').height() + $('div#at_the_museum_events_list').position().top;
	} else if ($('body').hasClass('search')) {
		body_height = $('div#search_results').height() + $('div#search_results').position().top;
	} else {
		body_height = $('div#more_content').height() + $('div#more_content').position().top;
	}
	$('div#page').height((50+body_height)+'px');
}
function init_feature_filterbar() {
	$('div#more_content_bar span#toggle_all').click(function() {
		$("#more_content li").fadeIn();
		$('div#more_content_bar span').css('background-image','url(images/checkbox_off.png)')
		$(this).css('background-image','url(images/checkbox_on.png)')
	});
	$('div#more_content_bar span#toggle_stufftodo').click(function() {
		$("#more_content li").fadeOut();
		$("#more_content li.stufftodo").fadeIn()
		$('div#more_content_bar span').css('background-image','url(images/checkbox_off.png)')
		$(this).css('background-image','url(images/checkbox_on.png)')
	});
	$('div#more_content_bar span#toggle_NOTstufftodo').click(function() {
		$("#more_content li").fadeOut();
		$("#more_content li:not('.stufftodo')").fadeIn();
		$('div#more_content_bar span').css('background-image','url(images/checkbox_off.png)')
		$(this).css('background-image','url(images/checkbox_on.png)')
	});
}


function check_url_for_popin() {
	//# this is the BOOKMARK reader... get the info off the URL and open that URL in the popin
	if(document.location.hash != '' && document.location.hash != '#' && document.location.hash != '#channel') {
		incoming_url = ''+document.location.hash.substr(1, document.location.hash.indexOf('?')-1);
		incoming_url = ''+document.location.hash.substr(1);
		btn = $('a.popin[href*=\''+incoming_url+'\']');	// select the BUTTON for this feature
		if(btn.length > 0) {							// if there is a button for it!
			btn.triggerHandler('click');				// click it 
		} else {										// otherwise
			tb_show(null,incoming_url,false);			// show it the crappy way
		}
//		tb_show(null,incoming_url,false);
	}

}

function open_edit(feature_url) {
	t = window.open('edit.php?url='+feature_url, 'edit', 'height=600,width=640,scrollbars=yes');
	t.focus();
}

function popin_maximize() {
	$('#TB_window').attr('style','display:block');
//	$('#TB_window').css({marginLeft:'-50%',marginTop:'-36%',width:'100%'})
	$('#TB_window').css({left:'0',top:'0',width:'100%'})
	$('iframe#TB_iframeContent').height($(window).height());
	$('iframe#TB_iframeContent').width($(window).width());
	$('iframe#TB_iframeContent').css({position:'absolute',margin:'0px',top:0,left:0});
	$('div#TB_content').css({top:'0'});
}
function popin_breakout(feature_url) {
	location.href=feature_url;
}

function toggle_credits(feature_url) {
	h = $('#TB_toolbar_content').outerHeight();
	if($('#TB_toolbar_content').is(':visible')) {
		$('#TB_toolbar_content').slideUp(200, function() {
			$('iframe#TB_iframeContent').css('height', ($('iframe#TB_iframeContent').height() + h )+'px');
		});
	} else {
		$('iframe#TB_iframeContent').css('height', ($('iframe#TB_iframeContent').height() - h )+'px');
		$('#TB_toolbar_content').slideDown(400);
	}
}

function change_remote(direction) {
	if($.cookie('new_spot')==null) {
		new_spot = 0;
	} else {
		new_spot = parseInt($.cookie('new_spot'));
		$('div#scrolling_navigation div#slider').css("margin-top",new_spot+'px');
	}
	new_spot = new_spot += direction*42;
//	$.cookie('nav_index', nav_index);
	if(new_spot >= 0) {
		new_spot = 1;
		$('div.arrow a.up').addClass('dim');
	} else {
		$('div.arrow a.up').removeClass('dim');
	}
	if(new_spot <= $('div#scrolling_navigation').height()-$('div#scrolling_navigation div#slider').height()) {
		new_spot = $('div#scrolling_navigation').height()-$('div#scrolling_navigation div#slider').height()-4;
		$('div.arrow a.down').addClass('dim');
	} else {
		$('div.arrow a.down').removeClass('dim');
	}
	$('div#scrolling_navigation div#slider').animate({marginTop: new_spot+'px'}, 250 );
	$.cookie('new_spot', new_spot);

}

