

jQuery(document).ready(function(){
	
	try{  //on shtml pages, we define the current nav link in js
		if (nav_current_link !==  ''){
			jQuery('div#innerNav a[href*="'+nav_current_link+'"]').addClass('current');
		}
	} catch(err){}


	
	//email signup on homepage
	jQuery('input#programSearchBox, input#programSearchBoxLarge').focus(function(){ jQuery(this).removeClass('empty'); jQuery(this).val('');	});
	jQuery('input#programSearchBox, input#programSearchBoxLarge').blur(function(){ 
		if (!jQuery(this).attr('value')){jQuery(this).addClass('empty'); 	jQuery(this).val('recipes, ingredients, shows...');}
	}); 
	

	jQuery('ol li').each(function(){  //lets us have list items with color numerals
		jQuery(this).wrapInner(document.createElement("b"));  //add a b element to the documents
	});
	
	
	jQuery('.cycle').each(function(){
		if (jQuery(this).prev().hasClass('cycleNav')){
			var cycleNav = jQuery(this).prev();
			
			var myCycle = Math.round(Math.random()*100000);
			

			cycleNav.prepend("<div id='cycleNext"+myCycle+"' class='arrow next'>&rsaquo;</div>");
			cycleNav.prepend("<div id='cyclePager"+myCycle+"' class='pager'></div>");
			cycleNav.prepend("<div id='cyclePrev"+myCycle+"' class='arrow prev'>&lsaquo;</div>");
			var pagerWidth = 14*(jQuery(this).children().length);
			var myCycleWidth = pagerWidth + 30;
			jQuery('#cyclePager'+myCycle).css('width',pagerWidth);
			cycleNav.css('width',myCycleWidth);
			
			
			jQuery(this).cycle({
				containerResize: 1,
				pause: 1,
				timeout: 4500,
				pager: "#cyclePager"+myCycle,
				next: "#cycleNext"+myCycle,
				prev: "#cyclePrev"+myCycle
			});
			
		} else {
			//no pager specified
			jQuery(this).cycle({
				containerResize: 1,
				pause: 1,
				timeout: 5000
			});
		}
		
			

	});
		
	var image = "";
	var desc = "";
	apmShareThis(image,desc);
	
});



function apmShareThis(image,desc){
	
	//get the image and description, as best we can
	try  {	  	image = jQuery(jQuery("#content").find('img')[0]).attr('src');	 }
	catch(err){	image = ''; }
	
	try  {	  	desc = jQuery(jQuery("#content").find('p')[0]).text();	 }
	catch(err){	desc = ''; }
	
	//tumblr
	jQuery('.sharing.tumblr').bind('click',function(){
		u=location.href;
		t=document.title;	
		window.open('http://www.tumblr.com/share?v=3&u='+encodeURIComponent(u) +'&t='+encodeURIComponent(t) +'&s='+encodeURIComponent(desc),'sharer',"toolbar=0,resizable=0,status=1,width=450,height=430");
		return false;
	});
	
	//delicious
	jQuery('.sharing.delicious').bind('click',function(e){
		console.log('bound');
		window.open('http://delicious.com/save?jump=yes&url='+encodeURIComponent(window.location)+'&title='+encodeURIComponent(document.title));
		return false;
	});
	
	//google bookrmakrs
	jQuery('.sharing.google').bind('click',function(e){
		window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(window.location)+'&title='+encodeURIComponent(document.title));
		return false;
	});

	
	//facebook
	jQuery('.sharing.facebook').bind('click',function(){
		u=location.href;
		t=document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
		'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
	});
	
	//myspace
	jQuery('.sharing.myspace').bind('click',function(){
		t=document.title;
		c='<img src="'+image+'" /><br />'+desc;
		u=location.href;
		window.open('http://www.myspace.com/index.cfm?fuseaction=postto&u='+encodeURIComponent(u)+
			'&t='+encodeURIComponent(t)+'&c='+encodeURIComponent(c)+'&l=3');
		return false;
	});
	
	
	//twitter
	jQuery('.sharing.twitter').bind('click',function(){
		
		jQuery.getJSON("http://api.bit.ly/shorten?login=americanpublicmedia&apiKey=R_32f185e3485b00d35ad9f37a72c02d41&format=json&version=2.0.1&callback=?", 
				{longUrl: location.href},
				function(data){
					for (var r in data.results) {	first_result = data.results[r]; break;}     
					if (first_result){	
						shortUrl = first_result.shortUrl; 						
						theTitle = document.title;
						message = theTitle + " @SplendidTable - " + shortUrl;
						window.open('http://twitter.com/home/?status='+encodeURIComponent(message));
						return false;
					}
				}
			);
		return false;
	});


	
}


	
