var com = com || {};
com.as = com.as || {};
/**
 * @author juan.conde@altrasoluciones.com - Juan Carlos Conde
 * @version Date: Sep 2, 2010
 *          Time: 10:30:44 AM
 */
com.as.Archives = function() {
	this.firstMonth = 0;//January
	this.firstYear = 2010;
	this.currentMonth = 9;
	this.currentYear = 2010;
	this.topMonth = 8;
	this.topYear = 2010;
	this.m_names = new Array("January", "February", "March", "April", "May", "June", 
							 "July", "August", "September", "October", "November", "December");
};

com.as.Archives.prototype = {
	init:function(currentMonth,currentYear) {
		this.currentYear = currentYear;
		this.currentMonth = currentMonth - 1;
		this.topMonth = this.currentMonth;
		this.topYear = this.currentYear;
		this.showMonth();
		this.addEventNavMonth();
		
	},
	showMonth:function() {with (this) {
		var topM = topMonth;
		var topY = topYear;
		$("li[name=archives] a").each(function(n){
				var date = m_names[topM] + " " +topY;
				this.title = date;
				this.name = date;
				this.href = "blog.do?p=archive&d="+topY+"-"+leadZero(topM+1); 
				$(this).html(date);
				if (topM == 0) {
					topM = 12;
					topY--;
				}
				topM--;
			});
	}},
	isDateValid:function(topMonth,topYear,amountMonth) {
		topMonth -= amountMonth;
		if (topMonth <= 0) {
			topMonth = 12 + topMonth;//+(-topMonth)
			--topYear;
		}
		if ( (topMonth >= this.firstMonth && topYear >= this.firstYear) && (topMonth <= this.currentMonth && topYear <= this.currentYear) ) 
			return true;
		else 
			return false;
	},
	showMonthBack:function() {with (this) {
		$("#nav-right").css('display','block');
		var lista = $("li[name=archives] a"); 
		topMonth -= lista.size();
		if (topMonth < 0) {
			topMonth = 12 + topMonth;//+(-topMonth)
			--topYear;
		}
		if ( !isDateValid(topMonth,topYear,lista.size()) ) {
			topMonth = firstMonth + lista.size() - 1;
			if (topMonth >= 12) {
				topMonth -= 12;
				topYear = firstYear + 1;
			} else {
				topYear = firstYear;
			}
			$("#nav-left").css('display','none');
		} else {
			$("#nav-left").css('display','block');
		}
		var topM = topMonth;
		var topY = topYear;
		lista.each(function(n){
				var date = m_names[topM] + " " +topY;
				this.title = date;
				this.name = date;
				this.href = "blog.do?p=archive&d="+topY+"-"+leadZero(topM+1);
				$(this).html(date);
				if (topM == 0) {
					topM = 12;
					topY--;
				}
				topM--;
			});
	}},
	showMonthNext:function() {with (this) {
		$("#nav-left").css('display','block');
		var lista = $("li[name=archives] a"); 
		topMonth += lista.size();
		if (topMonth >= 12) {
			topMonth -= 12;
			++topYear;
		}
		if ( !isDateValid(topMonth,topYear,0) ) {
			topMonth = currentMonth;
			topYear = currentYear;
			$("#nav-right").css('display','none');
		} else {
			$("#nav-right").css('display','block');
		}
			
		var topM = topMonth;
		var topY = topYear;
		lista.each(function(n){
				var date = m_names[topM] + " " +topY;
				this.title = date;
				this.name = date;
				this.href = "blog.do?p=archive&d="+topY+"-"+leadZero(topM+1);
				$(this).html(date);
				if (topM == 0) {
					topM = 12;
					topY--;
				}
				topM--;
			});
	}},
	addEventNavMonth:function(){with (this) {
		$('#nav-left').bind('click',function(event){
				event.preventDefault();
				showMonthBack();
			});
		$('#nav-right').bind('click',function(event){
				event.preventDefault();
				showMonthNext();
			});
	}},
	leadZero:function(month) {
        if ( month < 10 ) {
        	return "0" + month;
        }
        return month;
	}
};
