APIMain = function() {
	return {

		initTabs: function() {
			$('.tabs a').click(
				 function(e) {
					$('.tabs a').removeClass('current');		
					$(this).addClass('current');
					target = '#content_' + this.id.split('_')[1];
					$('.details div').addClass('hidden');
					$(target).removeClass('hidden');
					
					if (this.id.split('_')[1] == 'stats') {	
						$('.details div#chart').removeClass('hidden');
						APIMain.updateChart();
					}
					
					return false;
					
				}
			);
		},
		
		fadeMsg: function() {
			$('.action').fadeOut(3000);
		},
		
		showCreateAccount: function () {
			$('form.new').show(500, function() {
				 						$('li.color1').height(223);
	 									$('a#create').hide();
	 									$('li.color1 div.header').width(550).height(40);				 						
				 					});
			return false;
		},
		
		updateChart: function() {

			var params = {
				'keyword' : APIMain.keyword,
				'start' : APIMain.start,
				'end' : APIMain.end
			};			
			url = '/json/chart.php';
			
			data = $.getJSON(url, params, function(data) {
				APIMain.renderChart($('#chart'), data);
			});					
		},
		
		
		updateChartKeyword: function(keyword) {

			var params = {
				'keyword' : keyword,
				'start' : APIMain.start,
				'end' : APIMain.end
			};			
			url = '/json/chart.php';
			
			data = $.getJSON(url, params, function(data) {
				APIMain.renderChart($('#' + keyword), data);
			});					
		},	
		
		renderChart: function(chart, data) {
			unit = 'day';
			
    		$.plot(chart, 
    			data, 
    			{ 
    				xaxis: 
    				{ 
    					mode: "time", 
    					tickFormatter: function(val, axis) {
							var d = new Date(val);
		
							if ( unit === 'hour' ) {
								var hours = d.getHours();
		
								if (hours === 0) {
									return '12am';
								} else if (hours === 12) {
									return '12pm';
								} else if (hours > 12) {
									return (hours - 12) + 'pm';
								} else {
									return hours + 'am';
								}
							} else {
								return (d.getUTCMonth() + 1) + "/" + d.getUTCDate();
							}
						}, 
    					minTickSize:["1", unit] 
    				}, 
    				yaxis:{
    					min: 0, 
    					minTickSize:1, 
    					tickDecimals:0, 
    					autoscaleSkipPointsOutside: true
    				}, 
    				lines: {show: true},
    				colors: ["#df5a32", "#433c2d"]
    			});		
		},
		
		initBillRowToggle: function() {
			$('a.billDetails').click(
				function(e) {
					row = $(this).parent().parent().next();
					if (row.is(':visible')) {
						row.addClass('hidden');
						$(this).html('details');
					} else {
						row.removeClass('hidden');			
						$(this).html('hide');
					}
				});		
		},
		
		toggleBillRow: function(row, link) {

			if (row.is(':visible')) {
				
			} else {
				row.removeClass('hidden');			
				link.html('hide');
			}
		},
		
		normalizeHeight: function() {
			var menu = $('.menu').height();
			var content = $('.right').height();
			
			if (menu > content) {
				$('.right').height(menu + 110);
			}
		},
		
		deleteKeyword: function() {
			return confirm("Are you sure you want to delete this keyword?");
		}	
	
	}
}();
