var Nomiscript = function(attr) {

        if ($.cookie('nomi') == null) {
	   $.cookie('nomi', '[0,0,0," "]');
	}

	_this = this;
	this.selects = [];
	this.links = [];
	this.linksAttr = [];
	this.choiseClass = attr.choiseClass;
	this.linksParentId = attr.linksParentId;
	this.selectsId = attr.selectsId;
	this.cookieParse = attr.cookieParse || false;
	
	// Array for right blocks.
	this.dynamicBlocks = [];
	
	this.N = [];
	this.N[0] = [];

	for (var i = 0; i <= this.selectsId.length-1; i++) {
		this.selects.push($('#' + this.selectsId[i]));
		this.linksAttr.push(this.selectsId[i]);
		
		$('#' + this.selectsId[i]).change(function() {
			_this.compute(this.id, this.value);
			_this.setCookie();
		});
	}

	// Right block with default text.
	var defaultDynamyc = $('#dinamyc');
	defaultDynamyc.css('display', 'block');
	
	// Current color.
	this.color;
	$('#' + this.linksParentId).find('a').each(function(index, elem) {
		_this.links.push($(elem));
		_this.N[0].push($(elem));
		
		_this.dynamicBlocks[index] = $('#' +  $(elem).attr('rel'));
		
		$(elem).mouseover(function() {
			_this.dynamicBlocks[index].css('display', 'block');
			defaultDynamyc.css('display', 'none');
			_this.color = $(elem).css('color');
			$(elem).css('color', 'red');
		});
		
		$(elem).click(function() {
			_this.addCookie($(elem).attr('rel'));
		});		
		
		$(elem).mouseout(function() {
			_this.dynamicBlocks[index].css('display', 'none');
			defaultDynamyc.css('display', 'block');
			$(elem).css('color', _this.color);
		});
	});
	
	// Set selects values from cookie.
	this.setSelects(this.getCookie());
	
	// Pick links from cookie.
	var result = _this.selectChange();
	_this.join2(result);
	
	// Set  current link.
	this.setLink(this.getCookie());	
}

Nomiscript.prototype.compute = function(id, value) {
	var result = _this.selectChange(id, value);
	_this.join2(result);
}

Nomiscript.prototype.pick = function(array) {
	for(var i = 0; i <= array.length-1; i++) {
		array[i].css('color', 'black');
		
		// Set  current link.
		if (this.checkLink(array[i].attr('rel'))) {
			array[i].css('color', 'white');
		}	
	}
}

Nomiscript.prototype.clearAll2 = function() {
	for(var i = 0; i <= this.N[0].length-1; i++) {
		this.N[0][i].removeClass(this.choiseClass);
		
		// Set  current link.
		if (this.checkLink(array[i].attr('rel'))) {
			array[i].css('color', 'white');
		}			
	}
}

Nomiscript.prototype.clearAll = function() {
	for(var i = 0; i <= this.N[0].length-1; i++) {
		this.N[0][i].css('color', '#aebea8');
		
		// Set  current link.
		if (this.checkLink(this.N[0][i].attr('rel'))) {
			//this.N[0][i].css('color', 'white');
		}			
	}
}

Nomiscript.prototype.join = function(array1, array2) {
	var tempArr = [];
	
	for(var i = 0; i <= array1.length-1; i++) {
		for(var j = 0; j <= array2.length-1; j++) {
			if (array1[i].text() == array2[j].text()) {
				tempArr.push(array1[i]);
			}
		}		
	}
	
	return tempArr;
}

Nomiscript.prototype.join2 = function(arrays) {
	var tempArr = arrays[0];
	var count = 0;
	this.clearAll();
	for(var i = 1; i < arrays.length; i++) {
		if (arrays[i].length != 0) {
			tempArr = this.join(tempArr, arrays[i]);
			this.clearAll();
			this.pick(tempArr);
		} else {
			count++;
			if (count == this.selects.length) {				
				this.pick(arrays[0]);
			}
		}		
	}
}

Nomiscript.prototype.selectChange = function(attribute, value) {
	for (var i = 0; i < this.selects.length; i++) {
		if (this.selects[i].val() != 0) {
			this.N[i+1] = this.findLink(this.selects[i].attr('id'), this.selects[i].val());
		} else {
			// Clear array.
			this.N[i+1] = [];
		}
	}
	
	return this.N;
}

Nomiscript.prototype.findLink = function(attribute, value) {
	_this = this;
	var resultArray = [];
	$(this.links).each(function(index, elem) {
		$(elem).removeClass(_this.choiseClass);
		if (elem.attr(attribute)) {
			if (_this.stringSearch(elem.attr(attribute).split(','), value)) {
				resultArray.push( $(elem) );
			}
		}
	});
	
	return resultArray;
}

Nomiscript.prototype.stringSearch = function(array, value) {
	for (var i = 0; i < array.length; i++) {
		if (parseInt(array[i]) == value) {
			return true;
		}
	}
	
	return false;
}

// Set selects value from array.
Nomiscript.prototype.setSelects = function(array) {
	for (var i = 0; i < this.selects.length; i++) {
		this.selects[i].val(array[i]);
	}
}

// Set currnet link.
Nomiscript.prototype.setLink = function(array) {
	$('#' + this.linksParentId).find('a').each(function(index, elem) {	
		if ( $(elem).attr('rel') == array[_this.selects.length] ) {
		
			// Pish-pish. Olo-lo-lo!
			var arr = window.location.href.split("?");
			if (arr[1] == undefined) {
				$(this).css({'color': 'black'});
			} else if (arr[1] != 'id=202' && arr[1] != 'id=247') {
				$(this).css({'background': '#78b428', 'color': 'white', 'font-weight': 'bold', 'padding': '2px','text-decoration': 'none'});
				$(this).mouseover(function() {
					$(this).css('color', 'white');
				});
			} else {
				$(this).css({'color': 'black'});
			}
		}
	});
}

Nomiscript.prototype.checkLink = function(link) {
	var cookieArray = this.getCookie();
	if (link == cookieArray[this.selects.length]) {
		return true;
	}

	return false;
}

// Cookie methods.
Nomiscript.prototype.setCookie = function() {
	var cookieArray = [];
	var link = this.getCookie()[this.selects.length];
	var str = '[';
	for (var i = 0; i < this.selects.length; i++) {
		str += this.selects[i].val() + (i+1 == this.selects.length ? '' : ',');
	}
	str += ']';
	
	$.cookie('nomi', str);
	

	if (link !== undefined) {
		this.addCookie(link);
	}	
}

Nomiscript.prototype.getCookie = function() {
	var cookieArray = [];
	
	// Pish-pish. Olo-lo-lo!
	var arr = window.location.href.split("?");

	if (arr[1] == undefined) {
		cookieArray = eval($.cookie('nomi'));
		cookieArray[3] = '';
		return cookieArray;
	} else if (arr[1] != 'id=202' && arr[1] != 'id=247') {
		
	} else if ($.cookie('nomi')) {
		cookieArray = eval($.cookie('nomi'));
		cookieArray[3] = '';
		return cookieArray;
	} else if (!$.cookie('nomi')) {
		return [0,0,0,''];
	}
	
	cookieArray = eval($.cookie('nomi'));

	return cookieArray;
}

Nomiscript.prototype.addCookie = function(value) {
	var cookieArray = [];
	
	cookieArray = this.getCookie();
	cookieArray[this.selects.length] = '"' + value + '"';

	var str = '[';
	for (var i = 0; i < cookieArray.length; i++) {
		str += cookieArray[i] + (i+1 == cookieArray.length ? '' : ',');
	}
	str += ']';
	
	$.cookie('nomi', str);	
}
