var addthis_config = {
     ui_offset_top: 1,
     ui_offset_left: -5,
	 services_compact : 'twitter, facebook, buzz, googlebuzz, delicious, digg, email, favorites, print, more'
}

var addthis_share = {
  templates: { twitter: '"{{title}}" by James Sarmiento {{url}}' }
}
	
$(document).ready(function(){	
	/* Browser sniff */
	if($.browser.msie && $.browser.version == 6){
		$("body").addClass("ie6");
	}

	$('.ie6 ul#navigation > li').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(e){
		$(this).removeClass('hover');		
	});


	$('a').each(function(){
		try{
			if((this.href.indexOf('jamessarmiento.com') + this.href.indexOf('192.168')) < -1 && this.href != ''){			
				$(this).attr('target','_blank')
			}
		}catch(err){}
	});
	
	var searchInputDefault = "Search this website" ;
	$('.searchInput').attr('value',searchInputDefault).focus(function(){
		if($.trim(this.value)==searchInputDefault){
			this.value='';
		}															   
	}).blur(function(){
		if($.trim(this.value)==''){
			this.value=searchInputDefault;
		}
	}).each(function(){
		this.parentNode.onsubmit = function(){
			var searchString = $.trim($('.searchInput').attr('value')).toLowerCase();
			if(searchInputDefault.toLowerCase()==searchString || ''==searchString){
				return false;
			}
		};
	});
	
	$('a.popup').click(function(){									
		var leftPos =  (screen.width/2)-(800/2);
		var topPos = (screen.height/2) -(600/2);
		var newWindow = window.open(this.href, '', 'width=800, height=600,left='+leftPos+',top='+topPos);
		newWindow.focus();
		return false;
	});

	$('.showPhotosetModal').click(function(){										   
		var strId = this.href.toString().match(/sets\/\d+/).toString().replace(/sets\//,'');		
		showModalSlideshow('set', strId);
		return false;
	});


	$(".timestamp").each(function(){
		this.tempTitle = this.title.replace(/\+00:00/,'Z');		
		this.tempInnerHTML = this.innerHTML;				
	});

	if($('pre.js').length > 0){
		$('<link rel="stylesheet" href="/common/syntaxhighlighter/styles/shCore.css" type="text/css" media="screen" /><link rel="stylesheet" href="/common/syntaxhighlighter/styles/shThemeDefault.css" type="text/css" media="screen" />').appendTo('head');
		SyntaxHighlighter.all();				   
	}	
	
	$('.thumbnail a').each(function(){
		if($(this).height() > 59){
			while($(this).height() > 59){
				this.innerHTML = this.innerHTML.substring(0, this.innerHTML.length-1);
			}
			this.innerHTML = this.innerHTML.substring(0, this.innerHTML.length-3).replace(/[\s|,|.]$/,'') + '...';		
		}		
	});
	
	$('#dashboard .coverContainer').click(function(){
		showModalSlideshow('tag', 'cover');
		return false;		
		
	});
	
	runPrettyDate();


	$.ajax({
	   type: "GET",
	   cache: true,
	   url: "http://connect.facebook.net/en_US/all.js",
	   dataType: "script",
	   success : function(){
			$('<div id="fb-root"></div>').appendTo('body');
			FB.init({ appId: '134368676576075', status: true, cookie: true,
					xfbml: true
				});					   
	   }
	 });

});

function runPrettyDate(){
	$(".timestamp").each(function(){									  
		this.title = this.tempTitle;
		var date = humane_date(this.title);
		if(date){
			this.innerHTML = date;
			this.title = this.tempInnerHTML;					
		}	
		else{
			this.title = "";
		}
	});
	setTimeout(runPrettyDate,  60000);		  	
}

function showModalSlideshow(strType, strId){
	var slideCode = '<object width="700" height="393"> <param name="flashvars" value="offsite=true&lang=en-us&page_show_url=%2Fphotos%2Fjamessarmiento%2Fsets%2F' + strId + '%2Fshow%2F&page_show_back_url=%2Fphotos%2Fjamessarmiento%2Fsets%2F' + strId + '%2F&set_id=' + strId + '&jump_to="></param> <param name="movie" value="http://www.flickr.com/apps/slideshow/show.swf?v=71649"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/slideshow/show.swf?v=71649" allowFullScreen="true" flashvars="offsite=true&lang=en-us&page_show_url=%2Fphotos%2Fjamessarmiento%2Fsets%2F' + strId + '%2Fshow%2F&page_show_back_url=%2Fphotos%2Fjamessarmiento%2Fsets%2F' + strId + '%2F&set_id=' + strId + '&jump_to=" width="700" height="393"></embed></object>';
	if(strType=='tag'){
		slideCode = '<object width="700" height="393"> <param name="flashvars" value="offsite=true&lang=en-us&page_show_url=%2Fphotos%2Fjamessarmiento%2Ftags%2Fcover%2Fshow%2F&page_show_back_url=%2Fphotos%2Fjamessarmiento%2Ftags%2Fcover%2F&user_id=15577723@N05&tags=cover&jump_to=&start_index="></param> <param name="movie" value="http://www.flickr.com/apps/slideshow/show.swf?v=71649"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/slideshow/show.swf?v=71649" allowFullScreen="true" flashvars="offsite=true&lang=en-us&page_show_url=%2Fphotos%2Fjamessarmiento%2Ftags%2Fcover%2Fshow%2F&page_show_back_url=%2Fphotos%2Fjamessarmiento%2Ftags%2Fcover%2F&user_id=15577723@N05&tags=cover&jump_to=&start_index=" width="700" height="393"></embed></object>';		
	}
	$.modal(slideCode,{
		containerCss :{
			backgroundColor:"#fff",
			borderColor:"#fff",
			height:393,				
			width:700,				
			padding:10						
		},
		opacity : 90
	});
}

/*
 * Javascript Humane Dates
 * Copyright (c) 2008 Dean Landolt (deanlandolt.com)
 * Re-write by Zach Leatherman (zachleat.com)
 
 * Adopted from the John Resig's pretty.js at http://ejohn.org/blog/javascript-pretty-date and henrah's proposed modification at http://ejohn.org/blog/javascript-pretty-date/#comment-297458
 * Licensed under the MIT license.
 */

function humane_date(date_str){
	var time_formats = [
		[60, 'Just now'],
		[90, '1 minute'], // 60*1.5
		[3600, 'minutes', 60], // 60*60, 60
		[5400, '1 hour'], // 60*60*1.5
		[86400, 'hours', 3600], // 60*60*24, 60*60
		[129600, 'Yesterday'], // 60*60*24*1.5
		[604800, 'days', 86400], // 60*60*24*7, 60*60*24
		[907200, '1 week'], // 60*60*24*7*1.5
		[2628000, 'weeks', 604800], // 60*60*24*(365/12), 60*60*24*7
		[3942000, '1 month'], // 60*60*24*(365/12)*1.5
		[31536000, 'months', 2628000], // 60*60*24*365, 60*60*24*(365/12)
		[47304000, '1 year'], // 60*60*24*365*1.5
		[3153600000, 'years', 31536000], // 60*60*24*365*100, 60*60*24*365
		[4730400000, '1 century'], // 60*60*24*365*100*1.5
	];

	var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," "),
		dt = new Date,
		seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000),
		token = ' ago',
		i = 0,
		format;

	if (seconds < 0) {
		seconds = Math.abs(seconds);
		token = '';
	}

	while (format = time_formats[i++]) {
		if (seconds < format[0]) {
			if (format.length == 2) {
				return format[1] + ((i > 1 && i != 6 ) ? token : ''); // Conditional so we don't return Just Now Ago
			} else {
				return Math.round(seconds / format[2]) + ' ' + format[1] + ((i > 1 && i != 6 ) ? token : '');
			}
		}
	}

	// overflow for centuries
	if(seconds > 4730400000)
		return Math.round(seconds / 4730400000) + ' centuries' + token;

	return date_str;
};