/*on document ready*/

var $j = jQuery.noConflict();

$j(document).ready(function(){

    $j('a[rel^="lightbox"]').colorbox({
        current: 'Foto {current} z {total}',
        transition: 'fade',
        transition: 'elastic',
        maxWidth: '90%',
        maxHeight: '90%'
    });

    $j('input.buybutton').click(function() {
        var productIDValSplitter    = (this.id).split("_");
        var productIDVal            = productIDValSplitter[1];

        var productX        = $j("#product_image").offset().left;
        var productY        = $j("#product_image").offset().top;

        var basketX         = $j("#basket_title").offset().left+55;
        var basketY         = $j("#basket_title").offset().top-5;
            
        ajax_data           = {product: productIDVal, action: "addToBasket", items: $j('input#count_'+productIDVal).val()}

        var gotoX           = basketX - productX;
        var gotoY           = basketY - productY;

        var newImageWidth   = $j("#product_image").width() / 10;
        var newImageHeight  = $j("#product_image").height() / 10;
        
        $j("#product_image")
        .clone()
        .prependTo("#product_image")
        .css({'position' : 'absolute'})
        .animate({opacity: 1}, 100 )
        .animate({opacity: 1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1000, function() {
            $j(this).remove();

            $j.ajax({
                type: "GET",
                url: "[~102~]",
                data: ajax_data,
                success: function(theResponse) {
                    var variant_price = parseFloat($j("#price_"+productIDVal).text());
                    var variant_count = parseInt($j("#count_"+productIDVal).val()); 
                    var add_price = Math.round((variant_price * variant_count)*100)/100;
                 
                    if(variant_count > 0) {
                        if($j('#basket-total-price').text()=='') {
                            $j('#basket-total-price').append('(<span id="price_value">'+add_price+'</span> Kč)');
                        }
                        else {
                            $j('#price_value').text(Math.round((parseFloat($j("#price_value").text())+add_price)*100)/100);
                        }
                    }
                    else {
                        if($j('#basket-total-price').text()=='') {
                            $j('#basket-total-price').append('(<span id="price_value">'+variant_price+'</span> Kč)');
                        }
                        else {
                            $j('#price_value').text(variant_price);
                        }
                    }
                }
            });

        });
    
        return false;
    });     
    
    // fill out antispam secure input format: CONV2010-5-1 (year-month-day without leading zeros)
    var curDate = new Date();
    var month = curDate.getMonth() + 1;
    $j('#serviceField').val('CONV' + curDate.getFullYear() + '-' + month + '-' + curDate.getDate());
    
});
