/* Author: Justin Maier*/
$(document).ready(function(){
	site.setup();
});

site = {
	setup: function(){
		//references
		site.notes = $('.notes ul');
		site.stats = $('.stats');
		site.most = $('header h2');
		
		//event binding
		$('nav a').bind(site.nav);
		$(window).resize(site.resize);
		$(window).resize();
		
		//Rotating notes
		site.rotator.setup();
		
		//update handling
		get.updateTime();
	},
	resize: function(e){
		site.height = $(document).height();
		site.width = $(window).width();
		if($('.overlay').length>0)overlay.resize();
	},
	nav: {
		click: function(e){
			e.preventDefault();
			var type = this.href.split("#")[1];
			if(type != 'note'){give.setup(type);}
			else{note.setup();}
		}
	},
	rotator: {
		time:6000,
		setup: function(){
			site.notes.children().css('margin-left',-600);
			site.notes.children().first().css('margin-left',-40);
			site.rotator.timer = setTimeout(site.rotator.rotate,site.rotator.time);
		},
		rotate: function(){
			site.notes.children().first().animate({'margin-left':-600},{complete:function(){
				$(this).appendTo(site.notes);
				site.notes.children().first().animate({'margin-left':-40},{complete:function(){
					site.rotator.timer = setTimeout(site.rotator.rotate,site.rotator.time);
				}});
				
			}});
		}
	}
}

overlay = {
	hide: function(){
		$('.overlay').unbind('click');
		$('.overlay, .give, .note').fadeOut("slow",function(){
			$('.overlay, .give, .note').remove();
		});
	},
	show: function(parent){
		overlay.resize();
		if(!$.support.opacity){
			$('.overlay').fadeTo("slow",.6);
			$('.give, .note').fadeIn();
		}else{
			$('.overlay, .give, .note').fadeIn();
		}
		$('span.text').focus(function(){
			$(this).html('');
			$(this).unbind('focus');
		})
		//$('span.text').first().focus();
		setTimeout(function () {
		  $('span.text').first().focus();
		}, 100);
		$('.overlay').bind('click',overlay.hide);
	},
	resize: function(){
		$('.overlay').css({
			'width':site.width,
			'height':site.height
		});
	}
}

get = {
	lastUpdate: 0,
	update: function(){
		$.post('/update',{'date':get.lastUpdate}, function(data){
			$.each(data.notes, function(key, val){
				site.notes.children().first().after($('<li><p>'+val.content+'</p><h4 class="kiss1">'+val.name+'</h4></li>').css('margin-left',-600));
			});
			var k = data.kisses;
			var s = data.smacks;
			var m = data.max;
			$('img',site.stats).attr('src','http://chart.apis.google.com/chart?chf=bg,s,00000000&chs=280x280&cht=p&chco=af001e|d34848&chds=0,'+m+'&chd=t:'+k+','+s+'&chp=13');
			$('.kiss',site.stats).html('kisses<br />'+k);
			$('.smack',site.stats).html('smacks<br />'+s);
			site.most.html('I love him the most &ndash; '+data.most);
			get.updateTime();
		}, 'json');
	},
	updateTime: function(){get.lastUpdate = new Date().getTime();}
}

give = {
	setup:function(type){
		give.type = type;
		$.get('components/'+type+'.html', function(data){
			$(data).appendTo($('#container'));
			give.nameEl = $('p.name span.text');
			$('.give a.send').click(give.send);
			overlay.show(give);
		});
	},
	send: function(e){
		e.preventDefault();
		$.post('/'+give.type,{name:give.nameEl.text()}, function(){
			get.update();
			overlay.hide();
		});
	}
}

note = {
	setup: function(){
		$.get('components/note.html', function(data){
			$(data).appendTo($('#container'));
			note.name = $('h4.name');
			note.message = $('p.message');
			note.name.keypress(note.preventEnter)
			note.message.keypress(note.preventEnter)
			
			//Bind Events
			$('.note a.send').bind(note.send);
			overlay.show(note);
		});
	},
	preventEnter: function(e){
		if(e.which==13)e.preventDefault();
	},
	send:{
		click:function(e){
			e.preventDefault();
			$.post('/note',{message: note.message.text(), name: note.name.text(), time: new Date().getTime()}, function(){
				get.update();
				overlay.hide();
			});	
		}
	},
}
