$(function() {
    /*if (readCookie('movepopupseen') != '1') {
        $(document.body).prepend($('<div style="display: none;"><a id="sitepopuptrigger" href="#sitepopup">Popup</a><div id="sitepopup"><h1>Vanaf 1 oktober is de KAM verhuisd</h1><img style="width: 203px; height: 135px;" src="BetterImageProcessor.ashx?src=%2fkamconcerten%2f_documents%2fwebsite%2fkam%2fhg51raam_klein.jpg&w=203&h=135" /><p>Ons nieuwe adres is:<br /><br />Stichting KAMermuziek Amsterdam<br />Herengracht 51<br />1015 BC Amsterdam<br />T: +31 20 6812100<br />www.kamconcerten.nl</p><div id="popupbuttons"><a id="popupclose" href="#">Sluiten</a><a id="rememberpopup" href="#">Herinner mij opnieuw</a></div></div></div>'));
        $('#popupclose').click(function() {
            createCookie('movepopupseen', '1', 90);
            $.fancybox.close();
            return false;
        });
        $('#rememberpopup').click(function() {
            createCookie('movepopupseen', '1', 0.5);
            $.fancybox.close();
            return false;
        });
        $('#sitepopuptrigger').fancybox();
        $('#sitepopuptrigger').click();
    }*/
    
	if (typeof Cms === 'undefined') {
		if ($('#homeimage').length) {
            $('#homeimage .ContentItemGalleryImage:first').css('display', 'block');
            window.setTimeout(function() {
                $('#homeimage').height($('#homeimage').height());
                var thisHomeImage = 0;
                var homeImages = $('#homeimage .ContentItemGalleryImage');
                homeImages.css('position', 'absolute');
                var nextImage = function () {
                    homeImages.eq(thisHomeImage).fadeOut(500);
                    thisHomeImage += 1;
                    if (thisHomeImage > homeImages.length - 1) thisHomeImage = 0;
                    homeImages.eq(thisHomeImage).fadeIn(500);
                };
                nextImage();
                window.setInterval(nextImage, 3000);
            }, 3000);
		}
		var $gallery = $('.ContentItemDetailWeblogItem .ContentImagesGallery a');
		if ($gallery.length) {
			var thisImage = 0;
			var initialized = false;
			window.setInterval(function() {
				if (!initialized) {
					var $img = $('.ContentItemWeblogItem .ContentImage');
					$img.css({width: $img.width() + 'px', height: $img.height() + 'px'});
					initialized = true;
				}
				thisImage += 1;
				if (thisImage >= $gallery.length) thisImage = 0;
				eval($gallery.eq(thisImage).attr('href').replace('javascript:', ''));
			}, 4000);
		}
	}
	
	$('.FormFieldSortOrder a').addClass('iframe').fancybox({
		padding: 5,
		margin: 5, 
		cyclic: true, 
		transitionIn: 'elastic', 
		transitionOut: 'elastic',
		easingIn: 'easeOutBack',
		overlayOpacity: 0.8,
		overlayColor: '#000',
		onStart: function() {
			this.width = Math.min($(document).width() - 160, 1100);
			this.height = $(document).height() - 80;
		}
	});
	
	$('.GroupSeries .FormDropDownList, .GroupSeries .Vriend input').change(function() {
		window.setTimeout(calculateForm, 200);
	});
	$('.GroupSend input, .GroupSeries .Vriend input').click(calculateForm);
	$('.GroupSeries .FormDropDownList').parent().append('<div class="ItemPrice">€ 0,00</div>');


    $('.FormRelatedItemsEditor .FromList').after($('<input type="text" class="ContentListBoxSearch" value="zoeken" />'));
    $('.FormRelatedItemsEditor .ContentListBoxSearch').each(function() {
        $(this).css('width', '45%').css('margin-left', ($(this).closest('.FormRelatedItemsEditor').find('.ToList').width() + 17) + 'px');
    }).focus(function() {
        if ($(this).val() == 'zoeken') $(this).val('');
    }).keyup(function() {
        var query = $(this).val().toLowerCase();
        var items = $(this).closest('.FormRelatedItemsEditor').find('.FromList tr');
        if (query == '') {
            items.css('display', '');
        } else {
            items.each(function() {
                var $this = $(this);
                if ($this.html().toLowerCase().indexOf(query) < 0) $this.css('display', 'none'); else $this.css('display', '');
            });
        }
    });
	
});

var rates = {
	KM1: 122,
	KM1plus: 147
}

function calculateForm() {
	var count = 0; var price = 0; var itemprice; var $this;
	$('.GroupSeries .FormDropDownList').each(function() {
		$this = $(this);
		count += ($this.val() * 1);
		itemprice = rates[$this.attr('class').split(' ')[1]] * $this.val();
		price += itemprice;
		$this.parent().find('.ItemPrice').html(currency(itemprice));
	});
	var admincost = Math.min(13.5, count * 2.25)
	var discount = 0;
	if ($('.Vriend input').get(0).checked) discount = count * 3.5;
	$('.Administratiekosten').val(currency(admincost));
	$('.Vriendenkorting').val(currency(discount));
	$('.Totaal').val(currency(price + admincost - discount));
}

function currency(n) {
	return '€ ' + n.toFixed(2).replace('.', ',');
}


function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

