$(function () {
    $('#frmweergave > select').change(function (e) {
        $('#frmweergave').submit();
    });

    //toggle show/hide	
    $(".toggle").click(function (e) {
        var ids = $(this).attr("id").split("_")
        var id = ids[1]

        if ($("#slidingDiv_" + id).hasClass('showMe')) {
            //close
            $(".showMe").animate({
                height: ['toggle', 'swing'],
                opacity: 'toggle'
            });

            $('.child').removeClass('showMe');
        }
        else {
            //open
            $('.showMe').animate({ height: ['toggle', 'swing'], opacity: 'toggle' }).removeClass('showMe');
            $("#slidingDiv_" + id).addClass('showMe');

            $('.showMe').animate({
                height: ['toggle', 'swing'],
                opacity: 'toggle'
            });
        }
    });
});

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');

    if (isNaN(num))
        num = "0";

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();

    if (cents < 10)
        cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.' +
	num.substring(num.length - (4 * i + 3));

    return (((sign) ? '' : '-') + '\u20AC ' + num + ',' + cents);
}


//speciale aktie
var total_divs = 0;
var className = 'fadeMe';

$(document).ready(function () {
    $('#soort').change(function (e) {
        $.getJSON(
			'/ajax/price.aspx?id=' + $('#soort option:selected').val(),
			function (data) {
			    $('#price').text(formatCurrency(data.price * 0.01));
			    $('.dtls').text(data.type + " " + data.color)
			}
		);
    });

    $("." + className).attr("id", function (arr) {
        total_divs++;
        return "fade" + (arr + 1);
    });

    $("#productFoto .small").mouseover(function () {

        $(".mainImage").attr({
            src: this.src.replace("-f-", "-t-")
        });

        $(this).attr({
            style: 'border: 1px solid #1A91D5; margin: 3px; width: 42px; height: 42px;'
        });
    });

    $("#productFoto .small").mouseout(function () {
        $(this).attr({
            style: 'border: 1px solid #ccc; margin: 3px; width: 42px; height: 42px;'
        });
    });


    $('#radio > input').each(function (i, v) {
        $(v).click(function (e) {
            $obj = $(v);
            var enable = false;

            if ($obj.val() == "0") {
                $('#bezorgadres').slideDown("slow");
                enable = true;
            } else {
                $('#bezorgadres').slideUp("slow");
                enable = false;
            }


            $.each(Page_Validators, function (index, validator) {
                switch (validator.controltovalidate) {
                    case 'ctl00_Content_txtAltName':
                        ValidatorEnable(validator, enable);
                    case 'ctl00_Content_txtAltSurname':
                        ValidatorEnable(validator, enable);
                    case 'ctl00_Content_txtAltStreet':
                        ValidatorEnable(validator, enable);
                    case 'ctl00_Content_txtAltNumber':
                        ValidatorEnable(validator, enable);
                    case 'ctl00_Content_txtAltPostal':
                        ValidatorEnable(validator, enable);
                    case 'ctl00_Content_txtAltCity':
                        ValidatorEnable(validator, enable);
                };
            });
        });
    });


    // click
    $('.add').click(function (e) {
        $input = $($(this).parent().parent().find("input[type = text]"));
        $input.val(parseInt($input.val()) + 1);
    });

    $('.substract').click(function (e) {
        $input = $($(this).parent().parent().find("input[type = text]"));

        if (parseInt($input.val()) > 0)
            $input.val(parseInt($input.val()) - 1);
    });


    $('#zoekwoord').keyup(function (e) {
        if (e.keyCode == 13) {
            $('frmZoek').submit(); 
        }
    });
	
	$('.quickOrderInput').click(function (e) {
        $input = $($(this).parent().parent().find("input[type = text]"));
        $input.val(parseInt($input.val()) + 1);
    });


    var scroll = new scroller($("#movelistwrapper"), "left_button", "right_button");

    $('.left_button').click(function (e) {
        scroll.moveRight(1, e);
    });

    $('.right_button').click(function (e) {
		if(!$('.right_button').hasClass("disable")) {
			$('.left_button').removeClass("disable");
			scroll.moveLeft(1, e);
		}
    });
})

fader(0);

function fader(x) {
    var y = x

    if (x == total_divs) y = 1; else y++;
    $("#fade" + x).hide("slow");
    $("#fade" + y).show("slow");
    setTimeout('fader(' + y + ')', 5000);
}


/* Move left & right */

function scroller(ulid, leftclass, rightclass) {
    this.where = 0 // Where?
    this.lastDisable = null;

    this.moveLeft = function (times, e) {
        this.move(times, e);
    };

    this.moveRight = function (times, e) {
        this.move(0 - times, e);
    };

    this.move = function (to, e) {
        var where = this.where + to; // Add one to items
        var items = this.items() - this.hiddenItems();

        if (((items + where) <= this.items()) && (this.where + to) >= 0) {
            this.ulid.children("ul").children("li").each(function (k, v) {
                if (k >= where && k < (where + items)) {
                    $(v).removeClass("hide");

                    if (k == (where + items) - 1)
                        $(v).addClass("last");
                    else
                        $(v).removeClass("last");
                } else {
                    $(v).removeClass("last");
                    $(v).addClass("hide");
                }
            });

            this.where += to;

            if (this.lastDisable != null) {
                $(this.lastDisable).removeClass("disable");
                this.lastDisable = null;
            }

            // Check if last
            if ((items + this.where) == this.items() || this.where == 0) {
                $(e.target).addClass("disable");
                this.lastDisable = $(e.target);
            } 
        }
    };

    // Count total LI elements
    this.items = function () {
        return this.ulid.children("ul").children("li").size();
    };

    // Count amount hidden
    this.hiddenItems = function () {
        return this.ulid.children("ul").children(".hide").size();
    };

    this.ulid = ulid;
};