$(function(){
	
	var footer = $('#footer');
	var wrapper = $('#wrapper');
	var footer_margin = parseInt(footer.css('margin-top'));
	var win = $(window);
	var resizeTimer = null;
	var window_height = 0;
	var wrapper_height = wrapper.height();
	var homeTimer;
	
	win.resize(function(){
		setFooter();
		//if (resizeTimer) clearTimeout(resizeTimer);
		//resizeTimer = setTimeout(setFooter, 200);
	})
	
	function setFooter() {
		window_height = win.height();
		if (window_height > wrapper_height) {
			footer.css('margin-top', (window_height - wrapper_height + footer_margin) + 'px');
		}
	}
	
	
	function contact_page() {
		$('#contact_form textarea').focus(function(){
	    if ($(this).val() == 'Add a comment') $(this).val('');
	  })
		
		$("#map").gMap({
      latitude:37.773111,
      longitude:-122.404486,
      zoom: 16,
			controls: ["GSmallZoomControl3D"],
			markers: [{latitude: 37.773111, longitude: -122.404486}],
			icon: {
				image: '/images/map_marker.png',
				shadow: '/images/map_marker_shadow.png',
				iconsize: [38,57],
				shadowsize: [67,57],
				iconanchor: [19,57],
				infowindowanchor: [19,0]
			}
    });
    
	}
	
  
	function home_page() {
		var twitter_url = 'http://twitter.com/statuses/user_timeline/manmadenotebook.json?count=5&callback=?';
		$.getJSON(twitter_url,twitterCallback2);
		$('#project_info_holder').html($('.project_info:first').html());
		
		var scroll_api = $('#home_slides').scrollable({size: 1, speed: 500, clickable: false}).circular({api:true});
		
		scroll_api.onBeforeSeek(function(){
			$('#project_info_holder').hide();
			clearInterval(homeTimer);
			homeTimer = setInterval(function(){scroll_api.next()}, 9000);
		});
		
		scroll_api.onSeek(function(){
			var ind = this.getPageIndex() + 1;
			if ($.browser.msie) $('#project_info_holder').html($('.project_info:eq(' + ind + ')').html()).show();
			else $('#project_info_holder').html($('.project_info:eq(' + ind + ')').html()).fadeIn();
			//console.log(this.getPageIndex())
			//$('.project_info').fadeIn();
		});
		
	  //$('.home_slide, .project_info').hide().fadeIn();
		$('.home_slide').hide().fadeIn();
	
		$('.home_slide img:first-child').click(function(){
			window.location = $(this.parentNode).find('a').attr('href');
		});
		
		
		homeTimer = setInterval(function(){scroll_api.next()}, 9000);
	}
	
	
	function project_page() {
		var scroll_api = $('#slideshow').scrollable({size: 1, api:true, speed: 500, clickable: false});
    var image_index = $('#image_index');
    var index = 0;
    var prevButt = $('.prevPage img');
    var nextButt = $('.nextPage img');
		var bigPrevButt = $('#prevPageB');
		var bigNextButt = $('#nextPageB');
    var total = scroll_api.getSize();

    scroll_api.onSeek(function(){
      index = this.getIndex();
      image_index.html(index + 1);
      if (index > 0) {
				prevButt.attr('src', '/images/arrow_back.gif');
				bigPrevButt.show();
			} else {
				prevButt.attr('src', '/images/arrow_back_disabled.gif');
				bigPrevButt.hide();
			}
      if (index == total - 1) {
	 			nextButt.attr('src', '/images/arrow_forward_disabled.gif');
				bigNextButt.hide();
			} else {
				nextButt.attr('src', '/images/arrow_forward.gif');
				bigNextButt.show();
			}
      $('#slide_caption').html(this.getVisibleItems().find('p').html());
    });

		$('#slide_caption').html($('.slide_holder:first p:first').html());

    var f_img = $('#slideshow img:first');
		
		if ($.browser.msie) {
			
		} else {
		
	    f_img.data('org', f_img.attr('src')).removeAttr('src').load(function(){
	      $(this).fadeIn(500);
	    }).hide().attr('src', f_img.data('org'));
		}
		
		$('#slideshow').append('<i class="top_right"></i><i class="top_left"></i><i class="bottom_right"></i><i class="bottom_left"></i>');
	}
	
	
	function work_page() {
		$('a').live('click', function(){
	    var a = $(this);
	    if (a.attr('rel')) {
	      $('#' + a.attr('rel')).load(a.attr('href') + '&ajax=' + a.attr('rel'), function(){
	        general_effects();
					
					$('#grid_list').html($('#view_option_temp').html());
					
					footer.css('margin-top', footer_margin + 'px');
					wrapper_height = wrapper.height();
					setFooter();
					
					if (a.attr('href').indexOf('list=view')) $('#project_list').hide().fadeIn(500);
					
				});
	      return false;
	    }
	  })
	}
	

	function general_effects() {
		$('.fade').each(function(){
			var img = $(this);
			img.data('org', img.attr('src')).removeAttr('alt').removeAttr('src');
		
			//img.attr('src', '/images/ajax-loader.gif');
      img.load(function(){
      	img.fadeIn(500)
      }).hide().attr('src', img.data('org'));
		})
		//$('.fade').hide().fadeIn(500);
		$('.round').append('<i class="top_right"></i><i class="top_left"></i><i class="bottom_right"></i><i class="bottom_left"></i>');
	}
	
	$('#delete').click(function(){
		return confirm('Are you sure?');
	})
	
	
	
	setFooter();
	general_effects();
	
	var funcName = $('body').attr('id');
	if (eval("typeof " + funcName + " == 'function'")) eval(funcName + '()');
	
	
	
})


function twitterCallback2(twitters) {
	$('#twitter_update_list').hide();
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
		var source = twitters[i].source.replace(/(<([^>]+)>)/gi, "");
    statusHTML.push('<li><span class="twitter_time">'+relative_time(twitters[i].created_at)+' from '+source+'</span>'+status+'</li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	$('#twitter_update_list a').attr('target', '_blank');
	$('#twitter_update_list').fadeIn();
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}


