$(function () {
    // Toggle between values.
    // (Intended for text- and password-inputs)
    $('.js-toggle-value').each(function () {
        $(this).data('original_value', $(this).val());

        $(this).focus(function () {
            if ($(this).val() === $(this).data('original_value')) {
                $(this).val('');
            }
        }).blur(function () {
            if ($(this).val() === '') {
                $(this).val($(this).data('original_value'));
            }
        });
    });

    // Indicate the current page in the navigation bar (if a link exists there).
    $("#navigation li a").each(function() {
        if(this.href == window.location) $(this).addClass("current");
    });

    // Fancy box
    $("a.fancybox").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'easingIn' : 'easeOutBack',
        'easingOut' : 'easeInBack'
    });

    // Tooltip
    $("img.tooltip").tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fixPNG: true,
        opacity: 0.95
    });

    // jQueryUI tabs
    $("#tabs").tabs();

    // Category menu
    $('#category_menu').treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });

    // Add to cart with javascript function from product list
    //$('.js-add-to-cart-button').click(function (event) {
        //if(put_in_cart(this, 1)) {
            //event.preventDefault();
        //}
    //});    


    // Add to cart with javascript function from product page
    $('#add-to-cart-form').submit(function (event) {
        var amount = $('input[name=amount]', this).val();
        $(this).attr('data-product-id', $('input[name=cartString]').val());
        put_in_cart(this, amount);
        event.preventDefault();
    });

    $('.js-language-button').click(function(event){
        var code = $(this).attr('data-iso-code');
        $.cookie(
            'UseLanguage',
            code,
            {'path': BASE_URL}
        );
    });

});

//function put_in_cart(sender, amount)
//{
    //var productId = $(sender).attr('data-product-id');
    //var result = null;
    //$.ajax({
        //type: "POST",
        //url: BASE_URL+"ajax/add_to_cart.php",
        //data: "product_id="+productId+"&amount="+amount,
        //async: false,
        //success: function(msg){
            //if(msg == "SUCCESS") {
                //put_in_cart_success(sender);
                //result = true;
            //} else {
                //result = false;
                //alert(msg);
            //}
        //}
    //});
    //return result;
//}

//function put_in_cart_success(sender)
//{
    //$.get(BASE_URL+'ajax/cart.php', function(data) {
        //var parent = $(sender).parent();
        //$(parent).effect('transfer',{to: ".js-sidebar-cart", className: 'js-cart-transition'},500, function() {
            //$('.js-sidebar-cart').html(data);
        //});
    //});
//}

//function put_in_cart_error(sender, msg)
//{
    //return false
//}



$(document).ready(function() {
    
     $("#deleteProdInCart").live("click", function(data) { 
		var productIDVal = $(this).attr('data-product-id');
        var result = null;
		$("#notificationsLoader").html('<img src="'+BASE_URL+'img/icons/loader.gif">');
	
		$.ajax({  
			type: "POST",  
			url: BASE_URL+"ajax/removeFromCart.php", 
			data: "product_id="+productIDVal, 
			success: function(theResponse) {
                    $("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
                    $("#notificationsLoader").empty();
                    $.get(BASE_URL+'ajax/cart.php', function(data) {
                        $('.js-sidebar-cart').delay(500).html(data);
                    });
                    
                    if(theResponse !=0)
                        $('.js-sidebar-cart').html(theResponse);
			
			}  
		});
        
		
	});

    // Add to cart with javascript function from product list
    $('.js-add-to-cart-button').click(function (event) {
        result = put_in_cart(this, 1);
        if(result == 'SUCCESS') {
            event.preventDefault();
            $('html, body').animate({scrollTop:0}, 'slow');
            $('#cart-box').effect('pulsate',{times:2}, 500);
        }
    });    

});

function put_in_cart(sender, amount)
{
    var result = null;
    var productIDVal = $(sender).attr('data-product-id');

    $.ajax({
        type: "POST",
        url: BASE_URL+"ajax/add_to_cart.php",
        data: "product_id="+productIDVal+"&amount="+amount,
        async: false,
        success: function(response) {
            if(response == 'SUCCESS') {
                put_in_cart_success(sender, response);
            }else{
                put_in_cart_error(sender, response);
            }
            result = response;

        }
    });
    return result;
}
var p;
function put_in_cart_success(sender, theResponse)
{
    $.get(BASE_URL+'ajax/cart.php', function(data) {
        $("#notificationsLoader").html('<img src="'+BASE_URL+'img/icons/loader.gif">');
        var productIDVal = $(sender).attr('data-product-id');
        var parent = $(sender).parent();
        var imageWrapper = $(sender).parent().parent().find('.image-wrapper');
        //console.log(imageWrapper);
        var productX = $(imageWrapper).offset().left;
        var productY = $(imageWrapper).offset().top;

        var basketX = $("#cart-box").offset().left;
        var basketY = $("#cart-box").offset().top;

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

        var newImageWidth = $(imageWrapper).width() / 3;
        var newImageHeight = $(imageWrapper).height() / 3;

        $("img", imageWrapper)
        .clone()
        .prependTo(imageWrapper)
        .css({
            'position' : 'absolute'
        })
        .animate({
            opacity: 0.4
        }, 100 )
        .animate({
            opacity: 0.1,
            marginLeft: gotoX,
            marginTop: gotoY,
            width: newImageWidth,
            height: newImageHeight
        }, 1200, function() {
            $(this).remove();
        });
        $('.js-sidebar-cart').html(data);
    });
}

function put_in_cart_error(sender, msg)
{
     alert("Tyv\u00e4rr inte s\u00e5 m\u00e5nga exemplar i lager. Eller s\u00e5 har du inte valt n\u00e5got alternativ.");
}

function flash(element, color, callback)
{

    $(element).animate({ 'a color': color }, 150).
        animate({ 'a color': "#fff" }, 150).
        animate({ 'a color': color }, 150).
        animate({ 'a color': "#fff" }, 150, function() {
            if (callback != 'undefined') {
                        return callback();
            }
        });
}


