var slider_pos = 'up';
var page;
$(document).ready(function($){

$('#nav_slide').css('opacity',0.9);
$('#map').toggle(function() {mapLoad();}, function() {closeMap();});
$('#photos, #latest_photos').click(function() {slideHeaderdown('photos');});
$('#videos, #latest_videos').click(function() {slideHeaderdown('videos');});
$('#articles').click(function() {slideHeaderdown('articles');});
$('#backgroundswitch').toggle(function() {$('#nav_slide, #intro').css('visibility', 'hidden');$('#backgroundswitch').html('Return to content');
}, function() {$('#nav_slide, #intro').css('visibility', 'visible');$('#backgroundswitch').html('See the background image');});
$('.max').toggle(function() {$(this).parent().find('.dropdown').eq(0).css('visibility', 'visible');$(this).html('Close stats');
}, function() {$(this).parent().find('.dropdown').eq(0).css('visibility', 'hidden');$(this).html('See stats');});
$('.rss').toggle(function() {$('#subscribe').css('display', 'block');$(this).html('Close');
}, function() {$('#subscribe').css('display', 'none');$(this).html('Subscribe to the blog');});
});
var num;


function mapLoad() {jQuery.getScript('http://www.walkingtheamazon.com/map.php', function() {openMap();});}
function openMap() {if (effects == 'on') {$('#container').fadeOut(1000, function () { openMapextras(); });} else {openMapextras();}}
function openMapextras() {$('#container').hide(); /*$('#close_map').show();*/ $('#close_map').click(function() {closeMap();});$('#map_wrapper').show();onLoad();}
function closeMap () {$('#close_map').hide();GUnload();if (effects == 'on') {$('#map_wrapper').fadeOut(1000);$('#container').fadeIn(1000);} else {$('#map_wrapper').hide();$('#container').show();}}
function slideHeaderdown (page) {
var pageurl = page + '.php';
if (slider_pos == 'down') {$('#slider').load('http://www.walkingtheamazon.com/'+pageurl, function() {putin(page);});
} else {if (effects == 'on') {$('#slider').animate({height:550}, 1000);
			 } else {$('#slider').height(550);}
       $('#slider').html('<img src="http://www.walkingtheamazon.com/loading.gif" alt="loading" />');
       $('#slider').load('http://www.walkingtheamazon.com/'+pageurl, function() {putin(page);});
}
}
function putin (page) {
       $('#slider').append('<div id="close_slide"></div>');
       $('#close_slide').click(function() {slideHeaderup();});
       $('#close_slide').show();
       slider_pos = 'down';
}
function slideHeaderup () {
if (effects == 'on') {$('#slider').animate({height:0}, 1000);
} else {$('#slider').height(0);}
       $('#slider').html('');
       slider_pos = 'up';
}


var orig_width;
var orig_height;
var orig_left;
var orig_top;

function setCoverflowVideo ($e, clipid) {
		//$e = jQuery(e);
		
		
		orig_width = $e.width();
		orig_height = $e.height();
		orig_left = $e.css('left').replace(/px/,'');
		orig_top = $e.css('top').replace(/px/,'');
		
		$parent = jQuery('#cf-player').parent();
		
		var cover_width = $parent.width();
		var cover_height = $parent.height();
		
		var est_x = Math.round((cover_width - orig_width) / 2);
		
		if(Math.abs(orig_left - est_x) > 30){
			//probably not the middle one, so fail
			return;
		}
		
		
		$('.vidlink').removeClass('vidplaying');
		$e.addClass('vidplaying');
		
		jQuery('#cf-player').hide();
		var inputhtml = '<object width="400" height="300" type="application/x-shockwave-flash" data="http://vimeo.com/moogaloop.swf?clip_id='+clipid+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=77aa00&amp;fullscreen=1">';
		inputhtml += "<param name='allowfullscreen' value='true' /><param name='allowscriptaccess' value='always' /><param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id="+clipid+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=77aa00&amp;fullscreen=1' />";
		inputhtml += '</object>';
		jQuery('#cf-player').html(inputhtml);
		
		jQuery('#edvid-close').remove();
		$close = jQuery(document.createElement('div')).attr('id','#edvid-close').append('X');
		$close.css('font-size','32px');
		$close.css({position:'absolute',right:7,top:2,color:'white',cursor:'pointer'});
		jQuery('#cf-player').parent().append($close);
		$close.click(function(){
			$now = $($('.vidplaying')[0]);
			$now.show();
			$now.animate({left:orig_left,top:orig_top,width:orig_width,height:orig_height});
			jQuery('#cf-player').hide().html('');
			$close.remove();
		});
		
		 
		
		
		
		var final_width = 400;
		var final_height = 300;
		var final_left = Math.floor((cover_width - final_width) /2);
		var final_top = Math.floor((cover_height - final_height) /2);
		
		//alert(orig_left + '->' +  final_left + ' ' + orig_width + ' ' + cover_width);
		
		$e.animate({left:final_left,top:final_top,width:final_width,height:final_height}, {complete:function(){
			jQuery('#cf-player').show();
			$(this).fadeOut();
		}});
		
		jQuery('#cf-player').css('left', final_left + 'px');
		jQuery('#cf-player').css('top', final_top + 'px');
		jQuery('#cf-player').css('width', final_width + 'px');
		jQuery('#cf-player').css('height', final_height + 'px');
		jQuery('#cf-player').css('z-index','99999');
		
		
}
