$(document).ready(function() {
	statusAlert.init('#cols-filter');
	
	checkWOStatus();
	
	//	Фильтр колонок
	initColsFilter();

	//	Фильтр групп
	initGroupsFilter();

	//	Пока в тестовых целях - назначаем вторую колонку основной, и с ней
	//	все сравниваем.
	
	$('.b-compare th').click(function() {
		var iPosition = $(this).attr('position');
		clearActiveCol();
		setActiveCol(iPosition);
	});
	
	$('.b-flag').hover(function() {
		if(!$(this).hasClass('b-flag-active')){
			$(this).addClass('b-flag-hover');
		}
	}, function() {
		$(this).removeClass('b-flag-hover');
	});
	
	$('.b-flag').click(function() {
		clearActiveCol();
		setActiveCol($(this).attr('col'));
	});
	
	if(getParam('col')){
		setActiveCol(getParam('col'));
	}
	
	initPrintedVersion();
	
});

function setActiveCol( iCol ){
	$('.b-compare td[position="'+ iCol +'"]').addClass('selected');
	$('.b-compare th[position="'+ iCol +'"]').addClass('selected');
	
	$('.b-compare tr').each(function(index) {
		var iValue = $(this).find('td.selected').attr('value')*1;
		var iBiggerValue = 0;
		
		if(iValue > 0){
			$(this).find('td').each(function(index) {
				if($(this).attr('value')*1 < iValue){
					$(this).addClass('less');
				}
				if($(this).attr('value')*1 > iValue){
					if(iBiggerValue == 0 || $(this).attr('value')*1 <= iBiggerValue){
						$(this).addClass('more');
						iBiggerValue = $(this).attr('value')*1;
					}else{
						$(this).addClass('much_more');
					}
				}
			});
		}
	});
	
	$('td.flag .b-flag').each(function(index) {
		$(this).removeClass('b-flag-active');
		$(this).removeClass('b-flag-hover');
	});
	
	$('td.flag div[col="'+ iCol +'"]').each(function() {
		$(this).addClass('b-flag-active');
	});
}

function clearActiveCol(){
	$('.b-compare td').removeClass('selected');
	$('.b-compare th').removeClass('selected');
	$('.b-compare td').removeClass('much_more');
	$('.b-compare td').removeClass('more');
	$('.b-compare td').removeClass('less');
}

/*	Фильтр колонок
***********************************************************/
function initColsFilter(){
	$('#cols-filter label').click(function() {
		if($.browser.msie){
			if($(this).prev().attr('checked') == ''){
				$(this).prev().attr('checked', 'checked');
			}else{
				$(this).prev().attr('checked', '');
			}
		}
		
		if($(this).prev().attr('id') == 'chb-colsall'){
			$('#cols-filter input').attr('checked', '');
		}
		toggleColsFilters();
	});
	
	$('#cols-filter input').click(function() {
		if($(this).attr('id') == 'chb-colsall'){
			$('#cols-filter input').attr('checked', '');
		}
		toggleColsFilters();
	});
	
	toggleColsFilters();
}

function toggleColsFilters(){
	var bShowHeaders = true;
	
	if($('#cols-filter input:checked').length - $('#chb-colsall:checked').length > 0){
		bShowHeaders = false;
		$('.b-compare td[position],.b-compare th[position]').hide();
	}else{
		$('.b-compare td,.b-compare th').show();
	}
	
	$('#cols-filter input:checked').each(function(index) {
		var name = $(this).attr('name');
		if(name != 'chb-colsall'){
			$('.b-compare td[position="'+ name +'"],.b-compare th[position="'+ name +'"]').each(function(index) {
				$(this).show();
			});
		}
	});
	
	if(bShowHeaders)
		$('#chb-colsall').attr('checked', 'checked');
	else
		$('#chb-colsall').attr('checked', '');
	
	$('#cols-filter input').each(function(index) {
		if($(this).is(':checked'))
			$(this).parent().addClass('selected');
		else
			$(this).parent().removeClass('selected');
	});
}

/*	Фильтр групп
***********************************************************/
function initGroupsFilter(){
	$('#groups-filter label').click(function() {
		if($.browser.msie){
			if($(this).prev().attr('checked') == ''){
				$(this).prev().attr('checked', 'checked');
			}else{
				$(this).prev().attr('checked', '');
			}
		}
		
		if($(this).prev().attr('id') == 'chb-groupsall'){
			$('#groups-filter input').attr('checked', '');
		}
		toggleGroupsFilters();
	});
	
	$('#groups-filter input').click(function() {
		if($(this).attr('id') == 'chb-groupssall'){
			$('#groups-filter input').attr('checked', '');
		}
		toggleGroupsFilters();
	});
	
	toggleGroupsFilters();
}

function toggleGroupsFilters(){
	var bShowHeaders = true;
	
	if($('#groups-filter input:checked').length - $('#chb-groupsall:checked').length > 0){
		bShowHeaders = false;
		$('.b-compare tr[group]').hide();
	}else{
		$('.b-compare tr[group]').show();
	}
	
	$('#groups-filter input:checked').each(function(index) {
		var name = $(this).attr('name');
		if(name != 'chb-groupssall'){
			$('.b-compare tr[group="'+ name +'"]').each(function(index) {
				$(this).show();
			});
		}
	});
	
	if(bShowHeaders)
		$('#chb-groupsall').attr('checked', 'checked');
	else
		$('#chb-groupsall').attr('checked', '');
	
	$('#groups-filter input').each(function(index) {
		if($(this).is(':checked'))
			$(this).parent().addClass('selected');
		else
			$(this).parent().removeClass('selected');
	});
	
	fixFootnotesOrder();
}

/*	Смена порядка сносок
***********************************************************/
function fixFootnotesOrder(){
	var iCount = 0;
	
	$('table.b-compare table.footnote').each(function(index) {
		if($(this).parents('tr[group!=""]').is(':visible')){
			iCount++;
			$(this).prev().find('a').text(iCount);
		}
	});
}


function initPrintedVersion () {
	$("h1#docTitle").addClass("no-print");
	changePrintedTitle();
	$('<div style="display:none;padding-bottom:1em;color:#94999C;font-size:0.85em" class="print-anyway" id="printed-footnote"></div>').appendTo("table.layout div.width-fix div.content");
	$('<div style="display:none;padding-top:1em" class="print-anyway" id="printed-bottom" ></div>').appendTo("table.layout div.width-fix div.content");
	setTimeout(changePrintedBottom, 300);
	$("#cols-filter div.b-filteritem input").change(function(){setTimeout(changePrintedTitle, 300)});
	$("#groups-filter div.b-filteritem input").change(function(){setTimeout(changePrintedBottom, 300)});
	$("#cols-filter div.b-filteritem input,#groups-filter div.b-filteritem input").change(function(){setTimeout(changePrintedFootnote, 300)});
}
var _printedTitle = null;
var _printedBottom = null;
var _printedFootnote = null;
function changePrintedTitle () {
	if (!_printedTitle) {
		_printedTitle = $('<h1 style="display:none" class="print-anyway"></h1>').appendTo("div.content-hat");
	}
	if ($("#chb-colsall").attr("checked")) {
		_printedTitle.html("&#1058;&#1072;&#1088;&#1080;&#1092;&#1099; &#1087;&#1086; &#1055;&#1072;&#1082;&#1077;&#1090;&#1072;&#1084; &#1091;&#1089;&#1083;&#1091;&#1075;");
	} else {
		var _p = [];
		$("#cols-filter div.b-filteritem.selected").each(function(){
			_p[_p.length] = $(this).find("label span.link nobr").text();
		});
		if (_p.length == 1) {
			_printedTitle.html("&#1058;&#1072;&#1088;&#1080;&#1092;&#1099; &#1087;&#1086; &#1055;&#1072;&#1082;&#1077;&#1090;&#1091; &#1091;&#1089;&#1083;&#1091;&#1075; "+_p[0]);
		} else {
			_printedTitle.html("&#1058;&#1072;&#1088;&#1080;&#1092;&#1099; &#1087;&#1086; &#1055;&#1072;&#1082;&#1077;&#1090;&#1072;&#1084; &#1091;&#1089;&#1083;&#1091;&#1075; "+_p.join(', '));
		}
	}
}
function changePrintedBottom () {
	if (!_printedBottom) {
		_printedBottom = $("#printed-bottom");
	}
	if ($("#chb-groupsall").attr('checked')) {
		_printedBottom.html("");
	} else {
		var _p = [];
		$("#groups-filter div.b-filteritem.selected").each(function(){
			_p[_p.length] = $(this).find("label span.link").text();
		});
		_printedBottom.html("&#1058;&#1072;&#1088;&#1080;&#1092;&#1099; &#1087;&#1086; &#1087;&#1072;&#1088;&#1072;&#1084;&#1077;&#1090;&#1088;&#1072;&#1084;: "+_p.join(', ')+'');
	}
}
function changePrintedFootnote () {
	if (!_printedFootnote) {
		_printedFootnote = $("#printed-footnote");
	}
	var _p = [];
	$("table.b-compare tr:visible td:visible sup").each(function(){
		var $this = $(this),
			$a = $this.find('a');
		if ($a.length) {
			_p[_p.length] = $this.parent().find("table#"+$a.attr('id').replace(/link_/, '')+" td.footnote_text p").html();
		}
	});
	if (_p.length) {
		_printedFootnote.html('<div>&#1055;&#1088;&#1080;&#1084;&#1077;&#1095;&#1072;&#1085;&#1080;&#1103;:</div>'+_p.joinList());
	} else {
		_printedFootnote.html('');
	}
}
if (typeof Array.prototype.joinList == 'undefined') {
	Array.prototype.joinList = function () {
		var _result = "";
		for (var _i = 0; _i < this.length; _i++) {
			_result += '<div style="color:#94999C">'+(_i+1)+'. '+this[_i]+'</div>';
		}
		return _result;
	}
}

function checkWOStatus () {
	$("#cols-filter div.b-filteritem:has(input[name='chb-colsall'])").removeClass('selected').find('input').attr('checked', false);
	$("#cols-filter div.b-filteritem:not(:has(input[name='6'])):not(:has(input[name='chb-colsall']))").addClass('selected').find('input').attr('checked', true);
	toggleColsFilters();
	$("#cols-filter div.b-filteritem:has(input[name='6']) label").click(function(){
		statusAlert.showOnce();
	});
}


(function(window, undefined){
	var StatusAlert = function () {
		this._init();
	}
	StatusAlert.prototype.init = function (place) {
		this._placeFrame(place);
	}
	StatusAlert.prototype._init = function () {
		this._initFrame();
	}
	StatusAlert.prototype._initFrame = function () {
		this.objects = {
			frame: $(this._templates.frame),
			openCount: 0
		}
		this._initFrameEvents();
	}
	StatusAlert.prototype._initFrameEvents = function () {
		var self = this;
		this.objects.frame.find('img.status-alert').click(function(){self._closeFrame()});
	}
	StatusAlert.prototype._closeFrame = function () {
		this.objects.frame.hide();
	}
	StatusAlert.prototype._openFrame = function () {
		this.objects.frame.show();
	}
	StatusAlert.prototype.show = function () {
		this._openFrame();
		this.objects.openCount++;
	}
	StatusAlert.prototype.showOnce = function () {
		if (this.objects.openCount == 0) {
			this._openFrame();
			this.objects.openCount++;
		} else {
			this._closeFrame();
		}
	}
	StatusAlert.prototype._placeFrame = function (place) {
		this.objects.frame.appendTo($(place));
	}
	StatusAlert.prototype._templates = (function(){return{
		frame: '<div id="status-alert" class="frame-whole " style="display:none">'
			+'<div class="frame-whole-tl"></div><div class="frame-whole-tr"></div><div class="frame-whole-br"></div><div class="frame-whole-bl"></div>'
			+'<img class="status-alert" width="9" height="9" border="0" src="/f/1/i/i-closebtn.gif" title="&#1047;&#1072;&#1082;&#1088;&#1099;&#1090;&#1100;" alt="&#1047;&#1072;&#1082;&#1088;&#1099;&#1090;&#1100;" />'
			+'<div class="status-alert-content">'
				+'<p>&#1057;&nbsp;13&nbsp;&#1089;&#1077;&#1085;&#1090;&#1103;&#1073;&#1088;&#1103; 2010 &#1075;&#1086;&#1076;&#1072; &#1055;&#1072;&#1082;&#1077;&#1090; &#1091;&#1089;&#1083;&#1091;&#1075; &laquo;&#1057;&#1090;&#1072;&#1090;&#1091;&#1089;&raquo; &#1076;&#1083;&#1103; &#1085;&#1086;&#1074;&#1099;&#1093; &#1082;&#1083;&#1080;&#1077;&#1085;&#1090;&#1086;&#1074; &#1085;&#1077;&nbsp;&#1087;&#1088;&#1077;&#1076;&#1086;&#1089;&#1090;&#1072;&#1074;&#1083;&#1103;&#1077;&#1090;&#1089;&#1103;. &#1059;&#1089;&#1083;&#1086;&#1074;&#1080;&#1103; &#1086;&#1073;&#1089;&#1083;&#1091;&#1078;&#1080;&#1074;&#1072;&#1085;&#1080;&#1103; &#1082;&#1083;&#1080;&#1077;&#1085;&#1090;&#1086;&#1074; &#1089;&nbsp;&#1076;&#1077;&#1081;&#1089;&#1090;&#1074;&#1091;&#1102;&#1097;&#1080;&#1084; &#1055;&#1072;&#1082;&#1077;&#1090;&#1086;&#1084; &#1091;&#1089;&#1083;&#1091;&#1075; &laquo;&#1057;&#1090;&#1072;&#1090;&#1091;&#1089;&raquo; &#1086;&#1089;&#1090;&#1072;&#1102;&#1090;&#1089;&#1103; &#1087;&#1088;&#1077;&#1078;&#1085;&#1080;&#1084;&#1080;.</p>'
			+'</div>'
		+'</div>'
	}})();
	window.statusAlert = new StatusAlert();
	
	if (String.prototype.sprintf == undefined) {
		String.prototype.sprintf = function () {
			if (arguments.length) {
				var _arguments = arguments,
					_i = -1;
				return this.replace(/%s/g, function(){
					_i++;
					return _arguments[_i] == undefined ? '' : _arguments[_i] ;
				});
			} else {
				return this.replace(/%s/g, '');
			}
		}
	}
})(window);


