// Светофор, версия 1.0
// © 2008 Селезнёв Д. Л., , info@webfilin.ru, http://webfilin.ru

$(document).ready(function(){
	setCookie('test', '1');
	if (getCookie('test')) {
		deleteCookie('test');
	}
	else {
		alert('На этой странице должны быть включены Куки (Cookie).');
		return;
	}
	
	if (getCookie('vote')) {
		$('#results').show();
		return;
	}
	
	$('#green').fadeIn(1000, function() {$('#red').fadeIn(1000, function () {$('#yellow').fadeIn(1000);})});
	
	$('#vote').click(makeVote).get(0).disabled = true;
	
	$('#words li').draggable({stop: function () {checker();}, drag: lightColorText});

	$('#menu, #votefor').show();
	
	$('#show-do span.inside').click(function () {$('#do').toggle();});
	
	$('#do').click(function () {$('#do').toggle();});
	
	$('#show-results span.inside').click(function () {
		$('#results').show();
		$('#votefor').hide();
		$('#show-votefor').show();
		$('#show-results').hide();
		$('#show-do').css('visibility', 'hidden');
	});
	
	$('#show-votefor span.inside').click(function () {
		$('#results').hide();
		$('#votefor').show();
		$('#show-votefor').hide();
		$('#show-results').show();
		$('#show-do').css('visibility', 'visible');
	});	
	
	checker();
	
	var x = $('#words ul:first').position().left;
	$('#words ul').each(function (i) {
		var maxWidth = 0;
		$('li', this).each( function (i) {
			var liWidth = $(this).outerWidth();
			if (maxWidth < liWidth) maxWidth = liWidth;
			$(this).css('top', (i * (this.offsetHeight + 5)) + 'px'); // Верхний отступ
		});
		
		$(this).css('left', x + 'px')
		x += maxWidth + 30; // Левый отступ
		maxWidth = 0;
	});
});

function lightColorText(e, ui) {
	if (isInside($('#green'), ui.helper)) $('#green-text span').addClass('inside');
	else	$('#green-text span').removeClass('inside');
	if (isInside($('#red'), ui.helper)) $('#red-text span').addClass('inside');
	else	$('#red-text span').removeClass('inside');
}

function checker () {
	var ret = false;
	if (isDisabledButton()) ret = true;
	$('#vote')[0].disabled = ret;
	$('#red-text span, #green-text span').removeClass('inside');
}

function getRandom(n) {
	return Math.floor(Math.random() * n);
}

function isDisabledButton() {
	var ret = true;
	
	var n = 0;
	
	var green = $('#green');
	var red = $('#red');
	
	$('#words li').each(function () {
		if (isInside(green, this)) n++;
		if (isInside(red, this)) n++;
	});
	
	if (n)	ret = false;

	return ret;
}

function isInside(container, element) {
	var cl = $(container).offset().left;
	var ct = $(container).offset().top;
	var cw = $(container).width();
	var ch = $(container).height();
	
	var l = $(element).offset().left;
	var t = $(element).offset().top;
	var w = $(element).width();
	var h = $(element).height();
	
	var ret = false;
	if (l >= cl && l <= cl + cw && t >= ct &&  t <= ct + ch)	ret = true; // 1
	if (l + w >= cl && l + w <= cl + cw && t >= ct &&  t <= ct + ch)	ret = true; // 2
	if (l >= cl && l <= cl + cw && t + h >= ct &&  t + h <= ct + ch)	ret = true; // 3
	if (l + w >= cl && l + w <= cl + cw && t + h >= ct &&  t + h <= ct + ch)	ret = true; // 4
	
	return ret;
}

function makeVote() {
	if (!this.disabled) {
		$('#vote')[0].disabled = true;
		$('#ajax-results').load('index.php?action=results');
		var query = '';
		var red = $('#red');
		var green = $('#green');
		$('#words li').each(function () {	
			var znak = '';
			if (isInside(red, this))	znak = '-';
			if (isInside(green, this))	znak = '+';
			if (znak) {
				query = query + this.id.replace(/word\-/, '') + ':' + znak + ',';
			}
		});

		$('#vote').hide();
		setCookie('vote', '2', getExpDate(30, 0, 0));
		$.post('index.php?action=save', {query: query}, function() {$('#ajax-results').load('index.php?action=results'); $('#counter span').html(parseInt($('#counter span').html()) + 1); setCookie('vote', '1', getExpDate(30, 0, 0));});
		
		$('#menu').hide();
		if ($.browser.msie) {
			$('#votefor').hide();
			$('#results').show();
		}
		else {
			$('#results').fadeIn(1000);
			$('#votefor').fadeOut(1000);
		}
	}
}