var SC = {
	_formId		: 'comment-form',
	_comListId	: 'commentstext',
	_pagesId	: 'archpages',
	
	toggle : function () {
		$('#' + SC._formId).toggle();
		
	},
	
	send	: function () {
		var form = $('#' + SC._formId);
		if ($("textarea[name=user_comment]"	,form).val().length <= 1500) {
		SC.user_name	= $("input:text[name=user_name]"		,form).val();
		SC.user_comment	= $("textarea[name=user_comment]"	,form).val();
		SC.code			= $("input:text[name=code]"			,form).val();
		SC.toggle();
		$.post(
		document.URL,
		{
			user_name	: SC.user_name,
			user_comment: SC.user_comment,
			code		: SC.code
		},
		SC.success
		);
		} else {
			alert('Максимальное количество символов в комментарии 1500.')
		}
		return false;

	},

	success	: function(resp) {
		if (resp == '~code~') {
			alert('Код, указанный на картинке введен не правильно');
		} else {
//			alert(SC.user_name);
//			var last = resp.split('~*|');
			var list = $('#' + SC._comListId).children("dl");
			$(list).prepend("<dt><strong>" + SC.user_name + "</strong> // <span>" + SC.curDate() + "</span></dt><dd>" + SC.user_comment+ "</dd>");
			if ($('#' + SC._comListId).css('display') == 'none') {$('#' + SC._comListId).slideDown('normal');$('#commentshow').css('cursor', 'pointer');  $('#commentshow').bind('click', function () { SC.toggle() } ) };
			SC.clearform();
			SC.toggle();
		}
	},
	
	curDate : function (){
		var now = new Date();
		var day = (now.getDate() < 10 ?   '0' : '') + now.getDate();
		var m = now.getMonth() + 1;
		var month = (m < 10 ?   '0' : '') + m;
		var year = now.getFullYear();
		var hour = (now.getHours() < 10 ?   '0' : '') + now.getHours();
		var min =  (now.getMinutes() < 10 ? '0' : '') + now.getMinutes();
		var sec =  (now.getSeconds() < 10 ? '0' : '') + now.getSeconds();
		var date = day + '.' + month + '.' + year + ' ' + hour + ':' + min + ':' + sec;
		return date;
	},
	
	clearform : function() {
		var form = $('#' + SC._formId);	
		$("input:text[name=user_name]"	,form).val('Ваше имя');
		$("textarea[name=user_comment]"	,form).val('Комментарий');
		$("input:text[name=code]"		,form).val('');
	},
	
	pages : function (itemsInPage) {
		var ul = $('#' + SC._pagesId);
		var colpages = Math.ceil($(ul).attr('count')/itemsInPage);
		if (colpages > 1) {
			for (var i = 1; i <= colpages; i++) {
				if (i == 1) {
					$(ul).prepend('<li onclick="SC.load(' + i + ', this)" class="current"><span>' + i + '</span></li>')
				} else {
					$(ul).prepend('<li onclick="SC.load(' + i + ', this)"><span>' + i + '</span></li>')
				}
			}
		}
	},
	
	load : function (page, elem) {
		var list = $('#' + SC._comListId);
		var ul = $('#' + SC._pagesId);
		$(ul).children().removeClass();
		$(elem).addClass('current')
		var content = $.ajax({
			url: document.URL + '?commentpage=' + page,
			async: false
		}).responseText;
		$(list).html(content);
	}
	
}


var PH = {
	_pagesId	: 'phpages',
	_comListId	: 'photoreport',
	pages : function (itemsInPage) {
		var ul = $('#' + PH._pagesId);
		var colpages = Math.ceil($(ul).attr('count')/itemsInPage);
		if (colpages > 1) {
			for (var i = 1; i <= colpages; i++) {
				if (i == 1) {
					$(ul).prepend('<li onclick="PH.load(' + i + ', this)" class="current"><span>' + i + '</span></li>')
				} else {
					$(ul).prepend('<li onclick="PH.load(' + i + ', this)"><span>' + i + '</span></li>')
				}
			}
		}
	},

	load : function (page, elem) {
		var list = $('#' + PH._comListId);
		var ul = $('#' + PH._pagesId);
		$(ul).children().removeClass();
		$(elem).addClass('current')
		var content = $.ajax({
			url: document.URL + '?phpage=' + page,
			async: false
		}).responseText;
		$(list).html(content);
	}

}

var ARCH = function () {
        this.LIST_ID = 'archtext';
        this.PAGES_ID = 'archpages';
        this.ARCHIEVE_ID = 'archive';
        this.NAVIGATION_ID = 'archnav';

        this.initAllElements();

        var now = new Date();
        this.nowYear = now.getFullYear();
        this.nowMonth = now.getMonth() + 1;

        this.currentYear = this.nowYear;
        this.currentMonth = this.nowMonth;

        this.itemsPerPage = 6;
        this.currentPage = 1;

        this.paint();
}

ARCH.prototype.initAllElements = function() {
        this.list = this.initElement(this.LIST_ID);
        this.ul = this.initElement(this.PAGES_ID);
        this.archieve = this.initElement(this.ARCHIEVE_ID);
        this.nav= this.initElement(this.NAVIGATION_ID);
}

ARCH.prototype.initElement = function (elementId) {
    return $('#' + elementId);
}

ARCH.prototype.getCountPages = function() { 
    this.ul = this.initElement(this.PAGES_ID);
    if (this.ul && $(this.ul)) {
        if (!$(this.ul).attr('count')) return 0;
        return Math.ceil($(this.ul).attr('count') / this.itemsPerPage);
    } else {
        return 0;
    }
}

ARCH.prototype.setItemsPerPage = function (ipp) {
    if (ipp > 0) {
        this.itemsPerPage = ipp;
        this.paint();
    }
}

ARCH.prototype.isLastMonth = function() {
    if (this.currentYear == this.nowYear && this.currentMonth >= this.nowMonth) {
        return true;
    }
    return false;
}

ARCH.prototype.goNextMonth = function() {
    if (this.currentYear == this.nowYear) {
    	this.currentMonth++;
        if (this.isLastMonth()) {
        	this.currentMonth = this.nowMonth;
            this.currentPage = 1;
            this.paint();
            return;
        }
        return;
    }
    if (this.currentMonth == 12) {
        this.currentMonth = 1;
        this.currentYear += 1;
    } else {
        this.currentMonth += 1;
    }
    this.currentPage = 1;
    this.paint();
}

ARCH.prototype.goPrevMonth = function() {
    this.lastMonth = false;
    if (this.currentMonth > 1) {
        this.currentMonth--;
    } else {
        this.currentYear--;
        this.currentMonth = 12;
    }
    this.currentPage = 1;
    this.paint();
}

ARCH.prototype.goPage = function(pageNum) {
    if (pageNum > this.countPages || pageNum < 1) {
        this.currentPage = 1;
    } else {
        this.currentPage = pageNum;
    }
    this.paint();
}

ARCH.prototype.paint = function () {
    this.paintNav();
    this.paintList();
    this.paintPages();
}

ARCH.prototype.paintList = function() {
    var content = $.ajax({
        url: document.URL + '?archpage=' + this.currentPage + '&archmonth=' + this.currentMonth + '&archyear=' + this.currentYear,
        async: false
    }).responseText;
    $(this.list).next().remove();
    $(this.list).remove();
    $(this.nav).after(content);
    this.list = this.initElement(this.LIST_ID);
}

ARCH.prototype.paintPages = function() {
    var pages = this.getCountPages();
    if (pages > 1) {
        $(this.ul).empty();
        for (var i = 1; i <= pages; i++) {
            if (i == this.currentPage) {
                $(this.ul).prepend('<li onclick="arch.goPage(' + i + ')" class="current"><span>' + i + '</span></li>')
            } else {
                $(this.ul).prepend('<li onclick="arch.goPage(' + i + ')"><span>' + i + '</span></li>')
            }
        }
    }
}

ARCH.prototype.paintNav = function() {
    var months = new Array('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь');
    if (this.isLastMonth()) {
        $(this.nav).html('<li><span onclick="arch.goPrevMonth()">&laquo;</span></li> <li id="archmonth">' + months[this.currentMonth - 1] + ' ' + this.currentYear + '</li> <li>&raquo;</li>');
    } else {
        $(this.nav).html('<li><span onclick="arch.goPrevMonth()">&laquo;</span></li> <li id="archmonth">' + months[this.currentMonth - 1] + ' ' + this.currentYear + '</li> <li><span onclick="arch.goNextMonth()">&raquo;</span></li>');
    }
}
