var isMobileBrowser = false;
$(document).ready(function() {
	if( navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPad/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/mobile/i)  ) {
			isMobileBrowser = true;
	}

	if(!isMobileBrowser) {
		init_popin();
		init_rollovers();
		init_highlights()
		div_switcher('tv_feature_switcher', 'next', '', 8000);
		div_switcher('advanced_science_switcher', 'next', 'div#advanced_science_switcher_buttons a.AS_clickers', false);
		adjust_body_height();
	}
	change_remote(0);
	init_feature_filterbar();

	if($('.channelinset_panel').size() > 1) {
		div_switcher('channelinset_rotator', 'next', false, 10000);
	}
	div_switcher('didyouknow', 'next', false, 10000);
	check_url_for_popin();

	
});

function init_highlights() {
	$(document).mousemove(function(e) {
		mouse_pos_x = e.pageX
		mouse_pos_y = e.pageY;
		$('#feature_hilite').css({top:(mouse_pos_y+1)+'px',left:(mouse_pos_x+1)+'px'});
	});

	$('popin').mouseout(function() {
		$('#feature_hilite').hide();
	});
}
function init_popin() {
	 $('a.popin').click(function() {
		$(window).resize(function() {
		 	popin_setmargin();
		});
		
	 	var t = this.title || this.name || null;
	 	var a = this.href;// || this.alt;
	 	var g = this.rel || false;

	 	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;
		$('#popin').remove();

		$('body').prepend('<div id="darken"></div><div id="popin" class="ui-widget-content"></div>');
		$('#darken').css({'z-index':99, 'position':'fixed', 'float':'left', 'display':'none','background-color':'#000', 'height':'2000px', 'width':'2000px'})
		$('#darken').fadeTo('slow',.8);
		$('#popin').css({'position':'fixed', 'top':'50px', 'z-index':'100'});
		$('#popin').html('<div id="iFrame_top_toolbar"><span class="tool_buttons" id="iFrame_window_closer">close</span><span class="tool_buttons" id="iFrame_window_print">print friendly version</span></div><iframe name="OLogy_iFrame" id="OLogy_iFrame" frameborder="0" src="'+localized_url_with_TB+'"></iframe><div id="iFrame_bottom_toolbar"></div><div id="iFrame_credits"></div>');
		popin_setsize();

		$('#iFrame_window_print').click(function() {
			location.href = window.frames['OLogy_iFrame'].location;//localized_url;
		})
		$('#darken').click(function() {
			$('#darken').remove();
			$('#popin').remove();
			document.location.hash = '';
		})
		$('#iFrame_window_closer').click(function() {
			$('#darken').remove();
			$('#popin').remove();
			document.location.hash = '';
		});
		
		$('#popin').resizable({ 
			alsoResize: '#OLogy_iFrame',
			handles: 'se',
			// maxHeight:max_popin_height-10,
			// maxWidth:$(document).width() - 10,// - parseInt($('#popin').css('left')),
			minWidth:550,
			minHeight:400,
			start: function(event,ui) {
					$('#popin .ui-resizable-se').css({'padding':'100px 100px 0 0','background-position': '100px 100px'});
					$('#popin').bind('mouseout', function() {
	//					$('#popin .ui-resizable-se').mouseup();
					}) 
				},
			resize: popin_setmargin,
			stop: function(event, ui) {
				$('#popin .ui-resizable-se').css({'padding':'0','background-position': '0 0'});
				popin_setmargin()} 
		 });
		$('#iFrame_popout').click(function() {
			$('#iFrame_window_closer').click();
			//location.href='/ology/'+localized_url;
		})

	 	$('#iFrame_credits').load('_credits.php?f='+localized_url, {}, function() {
	 		if($('#iFrame_credits').html() != '') {
				$('#iFrame_bottom_toolbar').append('<div id="iFrame_toolbar_button_credits"><a class="toolbar_links" id="TB_credits_links" onclick="toggle_credits();return false;" href="#">credits</a></div>')
	 			$('#iFrame_credits').hide();
			}
		})
//		$('#popin').resizable({ alsoResize: '#OLogy_iFrame' });
	 	this.blur();
	 	return false;
	 });
}
function init_rollovers() {
	// 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_feature_filterbar() {
	$('div#more_content_bar span#toggle_all').click(function() {
		$("#more_content li").show('slow');
		$('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").hide('slow');
		$("#more_content li:not('.stufftodo')").hide('slow');
		$("#more_content li.stufftodo").show('slow')
		$('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").hide('slow');
		$("#more_content li:not('.stufftodo')").show('slow');
		$("#more_content li.stufftodo").hide('slow')
		$('div#more_content_bar span').css('background-image','url(images/checkbox_off.png)')
		$(this).css('background-image','url(images/checkbox_on.png)')
	});
}

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 getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function popin_setmargin() {
	$('#popin').css({
		'left':($(document).width()-$('#OLogy_iFrame').width())/2
	});
}

function popin_setsize() {
	max_popin_height = $(window).height()-100;
	url_params = getUrlVars();
	use_this_height = (url_params['height'] > max_popin_height)?max_popin_height:url_params['height'];
	$('#popin').resizable( "option", "maxHeight", use_this_height );
	$('#popin').resizable( "option", "maxWidth", $(document).width() - 10 );
	$('#popin').css({
		// 'height': 'auto',//$('#OLogy_iFrame').height()+80,
		// 'width':'auto'//url_params['width']+'px'
		'height': parseInt(use_this_height)+30+'px',//$('#OLogy_iFrame').height()+80,
		'width':(26+parseInt(url_params['width']))+'px'
	});
	$('#OLogy_iFrame').css({
		'height': parseInt(use_this_height)+'px',//$('#OLogy_iFrame').height()+80,
		'width':(20+parseInt(url_params['width']))+'px'
	});
	popin_setmargin();
	// $('#popin').css({
	// 	'left':($(document).width()-$('#OLogy_iFrame').width())/2,
	// 	'top':'70px'
	// });
}

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 && !isMobileBrowser) {							// if there is a button for it!
			btn.triggerHandler('click');				// click it 
		} else {										// otherwise
			location.href = incoming_url;			// show it the crappy way
			
		}
	}

}

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 = 15 + $('#iFrame_credits').outerHeight();// + $('#iFrame_bottom_toolbar').outerHeight();
	if($('#iFrame_credits').is(':visible')) {
		$('#iFrame_credits').slideUp(200, function() {
			$('iframe#OLogy_iFrame').css('height', ($('iframe#OLogy_iFrame').height() + h )+'px');
		});
	} else {
		$('iframe#OLogy_iFrame').css('height', ($('iframe#OLogy_iFrame').height() - h )+'px');
		$('#iFrame_credits').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);

}
function show_hilite(feature_div) {
	$('#feature_hilite').html(feature_div.children('a').html());
	$('#feature_hilite').children('.hilite').show();
	hiliter = setTimeout(function() {$('#feature_hilite').show();},500);
	feature_div.bind('mouseout', function() {
		hide_hilite();
	});
}
function actually_show_hilite()	{
	$('#feature_hilite').show();
}
function hide_hilite() 	{
	clearTimeout(hiliter);
	$('#feature_hilite').hide();
}

