﻿jQuery(document).ready(function () {

    jQuery('#mycarousel').jcarousel();

    //Select all anchor tag with rel set to tooltip
    $('a[rel=tooltip]').mouseover(function (e) {

        //Grab the title attribute's value and assign it to a variable
        var tip = $(this).attr('title');

        //Remove the title attribute's to avoid the native tooltip from the browser
        $(this).attr('title', '');

        //Append the tooltip template and its value
        $(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');

        //Set the X and Y axis of the tooltip
        $('#tooltip').css('top', e.pageY - 500);
        $('#tooltip').css('left', e.pageX - 500);

        //Show the tooltip with faceIn effect
        $('#tooltip').fadeIn('000');
        $('#tooltip').fadeTo('00', 0.8);

    }).mousemove(function (e) {

        //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
        $('#tooltip').css('top', e.pageY + 10);
        $('#tooltip').css('left', e.pageX + 20);

    }).mouseout(function () {

        //Put back the title attribute's value
        $(this).attr('title', $('.tipBody').html());

        //Remove the appended tooltip template
        $(this).children('div#tooltip').remove();

    });

});

$('form#warenkorb').submit(function (event) {
    eval($(this).attr('onsubmit'));
    return false; 
});

$('form#ajaxForm').find('a.submit-link').click( function() {
    $('form#warenkorb').submit(function (e) {
        e.preventDefault();
        $.post($(this).attr("action"), $(this).serialize(), function (r) {
            $("#edit").html(r);
        });
    });
});


function addAmount(form, uve, ve, kleinerUveVe, muster, nomuster, articleNumber, warenkorb) {
    var amount = parseInt($('#amount').val());

    amount = amount + warenkorb;
			                            
    var goForSubmit = false;

    var mod = 0;

    if(uve > 0){
        mod = amount % uve;
    }else{
        mod = amount % ve;
    }

    var refVe = uve;
    if (refVe <= 0) refVe = ve;

    if(mod > 0){
        // es gibt eine restmenge
        if(amount < 6 && mod <= 5 && refVe > 5){
            //es gibt eine mustermenge
			alert(nomuster);
			return false;
            //goForSubmit = confirm(muster);
        }else if(mod < uve || mod < ve){
            goForSubmit = confirm(kleinerUveVe);
        }else{
            alert("error");
        }
    }else{
        // es gibt keine restmenge
        goForSubmit = true;
    }

    if(goForSubmit)
        $('#wait').show();
    $.ajax({
        type: "GET",
        url: "JSON/AddToShoppingCart/",
        data: { artNr: articleNumber, amount: amount },
        dataType: "json",
        error: function (xhr, status, error) {
            $('#wait').hide();
            //                alert (xhr);
            //                alert (status);
            //                alert (error);
        },
        success: function (json) {
            $('#wait').hide();
            $('#hideFirst').css("display", "block");
            $('#TotalArticles').html(json.articles);
            $('#TotalPrice').html(json.price);
            $('#cartAmount').html(json.amount);
            $('#amount').val('0');
            $('div#logo div.cart').css("background-image", "url('../../Content/Img/cart_anim.gif')");
        }
    });
        return false;
        //form.submit();

    //return false;
}
