$(function(){
    $('div.productInfo select').change(function(){
        var $this = $(this);
        var selected = circulations[$this.val()]; 
        var next = circulations[$this.find('option[value=' + $this.val() + ']').next().attr('value')];
        var isLast = $this.find('option[value=' + $this.val() + ']').is('.last');
        
        if (isLast) 
        {
            $('a.addButton').hide();
            $('div.productInfo').find('div.addMore').hide();
        }
        else
        {
            $('a.addButton').show();
            $('div.productInfo').find('div.addMore').show().find('span.price').text((parseInt(next.price) - parseInt(selected.price)) + 'ð.')
            $('div.productInfo').find('div.addMore').find('span.count').text(next.count + 'øò');
        }
        $('div.productInfo').find('div.price span').text('Öåíà: ' + selected.price + 'ð.');        
    }).trigger('change');
    $('div.productInfo').find('a.addButton').click(function(){
        var $this = $('div.productInfo select');
        $this.find('option[value=' + $this.val() + ']').removeAttr('selected').next().attr('selected', 'selected');
        $this.trigger('change');
    });
    
    $('#modalDialog1').dialog
    (
        {
            autoOpen: false,
            bgiframe: true,
            closeOnEscape: true,
            draggable: false,
            resizable: false,
            modal: true,
            width: 400
        }
    );
    
    $('a[href*=ajax=][ajax!=off]').not('.noajax').click
    (
        function ()
        {
            mrcore_dialog_openAjaxModalWindow(this.href);
            return false;
        }
    );
});

var link = null;

function mrcore_add_to_cart_onSuccess(data)
{
    var answer  = mrcore_getResultFromAjaxRequest(data);
    
    if (parseInt(answer['code']) > 0)
    {	
	    var $cartSize  = $('.cartSize');
	    
	    var $message = $('div.ajaxMessage').text(answer['message']).parent().fadeIn();
	    if ($cartSize.find('span').size() == 0)
	    {$cartSize.html('(<span>1</span>)');}
	    else
	    {$cartSize.html('(<span>' + (parseInt($cartSize.find('span').text())+1) + '</span>)');}
	    setTimeout(function(){
	        $message.fadeOut();
	    }, 800);
    }
}

function mrcore_add_to_cart(href, goodId, obj)
{
    var request = '';
    link = obj;
    
    if ($('div.productInfo select').size() > 0)
    {
        href += '&circulationId=' + $('div.productInfo select').val();
    }
    
    if ('undefined' != typeof(goodId))
    {
        request = 'goodId=' + goodId;
        mrcore_ajaxRequest(href, mrcore_add_to_cart_onSuccess, request);
    }
}
