$(function(){
	
	//Картинка лапшы
	$('.cNoodles,.cAdditive').click(function(){
		
		if(goClearState) {
			$(this).removeAttr('checked');
		}
		goClearState = false;
		
		var additive = $(".cAdditive:checked");
		var noodle = $(".cNoodles:checked");
		if(additive.length > 0) {
			var additiveMayBe = additive.attr('id').substr(8);
			var newPossibleSrc = '/Images/products/additive/' + additiveMayBe + '-' + noodle.attr('id').substr(6) + '.jpg';
			if($('#MainImg').attr('src') != newPossibleSrc) {
				$('#MainImg').html('<img src="' + newPossibleSrc + '" />');
			}
		} else {
			var newPossibleSrc = '/Images/products/noodles/' + noodle.attr('id').substr(6) + '.jpg';
			if($('#MainImg').attr('src') != newPossibleSrc) {
				$('#MainImg').html('<img src="' + newPossibleSrc + '" />');
			}
		}
	});
	$('.cNoodles:checked').trigger('click');
	
	//Картинка риса
	$('.cRice,.cRiceAdditive').click(function(){
		
		if(goClearState) {
			$(this).removeAttr('checked');
		}
		goClearState = false;
		
		var additive = $(".cRiceAdditive:checked");
		var noodle = $(".cRice:checked");
		if(additive.length > 0) {
			var additiveMayBe = additive.attr('id').substr(13);
			var newPossibleSrc = '/Images/products/rice_additive/' + additiveMayBe + '-' + noodle.attr('id').substr(4) + '.jpg';
			if($('#MainImg').attr('src') != newPossibleSrc) {
				$('#MainImg').html('<img src="' + newPossibleSrc + '" />');
			}
		} else {
			var newPossibleSrc = '/Images/products/rice/' + noodle.attr('id').substr(4) + '.jpg';
			if($('#MainImg').attr('src') != newPossibleSrc) {
				$('#MainImg').html('<img src="' + newPossibleSrc + '" />');
			}
		}
	});
	$('.cRice:checked').trigger('click');
	
	//Возможность убрать радиоактивность :)
	var goClearState = false;
	$('.cAdditive,.cRiceAdditive').mousedown(function() {
		if($(this).attr('checked')) {
			goClearState = true;
		}
	});
	
	//Курсор в заказе
	$('#fgid0').focus();
	
	$('#cancelOrder').click(function(){
		if(confirm('Отменить Ваш заказ?')) return true;
		else return false;
	});
	
	
	//Всплывающие подсказки
	$('.OrderItem label').hoverbox();
	
	
	//Проверка на полный заказ
	$('#noodleOrderForm').submit(function(){
		if($(".cAdditive:checked").length == 0) {
			alert('Пожалуйста, выберите добавку к лапше →');
			return false;
		} else {
			return true;
		}
	});
	
	//Проверка на полный заказ
	$('#riceOrderForm').submit(function(){
		if($(".cRiceAdditive:checked").length == 0) {
			alert('Пожалуйста, выберите добавку к рису →');
			return false;
		} else {
			return true;
		}
	});
	
});




/*
 * jQuery Hoverbox 1.0
 * http://koteako.com/hoverbox/
 *
 * Copyright (c) 2009 Eugeniy Kalinin
 * Dual licensed under the MIT and GPL licenses.
 * http://koteako.com/hoverbox/license/
 */
jQuery.fn.hoverbox = function(options) {
    var settings = jQuery.extend({
        id: 'tooltip',
        top: -40,
        left: -100,
        delay: 500
    }, options);

    var handle;

    function tooltip(event) {
        if ( ! handle) {
            // Create an empty div to hold the tooltip
            handle = $('<div style="position:absolute" id="'+settings.id+'"></div>').appendTo(document.body).hide();
        }

        if (event) {
            // Make the tooltip follow a cursor
            handle.css({
                top: (event.pageY - settings.top) + "px",
                left: (event.pageX + settings.left) + "px"
            });
        }

        return handle;
    }

    this.each(function() {
        $(this).hover(
            function(e) {
                if (this.title) {
                    // Remove default browser tooltips
                    this.t = this.title;
                    this.title = '';
                    this.alt = '';

                    tooltip(e).html(this.t).fadeIn('fast');
                }
            },
            function() {
                if (this.t) {
                    this.title = this.t;
                    tooltip().hide();
                }
            }
        );

        $(this).mousemove(tooltip);
    });
};

