/*init medewerkersoverzicht uitklap*/
var boxWidth = 0;
var boxHeight = 0;
var openDropbox = '';
var openDropboxType = '';

function resetImage(selectedImage, contentWrapper) {
        $(selectedImage).css("z-index", 1);
                $(contentWrapper).css("height", boxHeight);
                $(contentWrapper).css("width", boxWidth);
                $(contentWrapper).css("display", "none");
                boxHeight = 0;
                boxWidth = 0;
}

$(document).ready(function() {
		if ($('#wrap').length > 0) {  
		$(".subnav-section").css("margin-top", 0);	
		}					
        /*init medewerkersoverzicht uitklap*/
		$('.elements-collection-list').addClass('wpg-carousel-skin-tabbed');
 
        /*init carousel*/
        $('.wpg-carousel-skin-tabbed').find('ul').jcarousel({
                wrap: 'both',
                scroll: 1,
                auto: 4,
                buttonNextHTML: null,
                buttonPrevHTML: null
        });

        /*create tabs*/
        function fnCreateTabs(carousel) {
                var wpgCarouselTabbed = $('.wpg-carousel-skin-tabbed');

                if (wpgCarouselTabbed && wpgCarouselTabbed.length > 0) {
                        var length = $(wpgCarouselTabbed).find('li').length;
                        var tabs = '';
                        //create anchor as tabs
                        for(var i = 1; i <= length; i++){
                                tabs += '<a href="#">' + i + '</a>';
					

                        }
                        if (tabs != '') {
                                $(wpgCarouselTabbed).find('.jcarousel-container').prepend('<div class="jcarousel-control">'+tabs+'</div>');
                                $('.jcarousel-control a').bind('click', function() {
                                        carousel.scroll($.jcarousel.intval($(this).text()));
                                        return false;
                                });
                                //prevent autoscroll on hover
                                if (carousel.options.auto) {
                                        
										$(wpgCarouselTabbed).find('.jcarousel-container')
                                                .bind('mouseenter', function() { carousel.stopAuto(); })
                                                .bind('mouseleave', function() { carousel.startAuto(); });
												
                                }
                        }
                }
        
		}
		/* check if carousel is af */
	

        /*synchronize tabs*/
        function syncTabs(carousel, li, idx, action) {
                $(li).parents('.jcarousel-container').find('.jcarousel-control a')
                        .removeClass('current') //a a a ...
                        .parent() //.jcarousel-control
                                .children('a:eq('+(idx-1)+')') //a a a ...
                                        .addClass('current').focus();
                carousel.stopAuto();
                carousel.startAuto();
        }

        
        function dropboxcloser(closingDropbox, dropboxType)
		{
			var imageClick = $("img", closingDropbox);
			var pos = $("img", closingDropbox).position();
			var startPosX = pos.left+$(imageClick).width();
			var contentWrapper = $(".content-wrapper", closingDropbox);
            
			
			if (dropboxType == 'left') {
				$(".content-wrapper", closingDropbox).animate( {width:'0px', height:'0px', left:startPosX}, 300, function() {resetImage(imageClick, contentWrapper);});
            } else {
				$(".content-wrapper", closingDropbox).animate( {width:'0px', height:'0px'}, 300, function() {resetImage(imageClick, contentWrapper);});
            }
		}
                
        /*medewerkersoverzicht uitklap*/
        $(".media-overview-employee-overview li").click(function () {
						boxWidth = $(".content-wrapper", this).width();
                        boxHeight = $(".content-wrapper", this).height();
						var contentWrapper = $(".content-wrapper", this);
                        var imageClick = $("img", this);
                        var pos = $("img", this).position();
                        var startX = pos.left+$("img", this).width();
                        var endX = startX-boxWidth;
                        var type = 'right';
                        var positionLeft = pos.left + ((($(window).width() - $("#container").width()) / 2) + 8);
                        if (($(window).width() - boxWidth) < positionLeft) {
                                        type = 'left';
                        }

                        if ($("img", this).css("z-index") == 9) {
                                        if (type == 'left') {
                                                        $(".content-wrapper", this).animate( {width:'0px', height:'0px', left:startX}, 300, function() {resetImage(imageClick, contentWrapper);});
                                        } else {
                                                        $(".content-wrapper", this).animate( {width:'0px', height:'0px'}, 300, function() {resetImage(imageClick, contentWrapper);});
                                        }
                                        openDropbox = '';
                        } else {
										if (openDropbox != '')
										{
											dropboxcloser(openDropbox, openDropboxType);
										}
                                        $(".content-wrapper", this).css("width", 0);
                                        $(".content-wrapper", this).css("height", 0);
                                        $("img", this).css("z-index", 9);
										openDropbox = this;
										openDropboxType = type;

                                        if (type == 'left') {
                                                        $(".content-wrapper", this).attr("id", "left");
                                                        $(".content-wrapper", this).css("left", startX-40);
                                                        $(".content-wrapper", this).animate( { width:boxWidth+'px', height:boxHeight+'px' , left:endX+'px' }, { queue:false, duration:300 } );
                                        } else {
                                                        $(".content-wrapper", this).attr("id", "right");
                                                        $(".content-wrapper", this).animate( { width:boxWidth+'px', height:boxHeight+'px' }, { queue:false, duration:300 } );
                                        }
                        }
        });
        
        
    	/* Regeladvertenties */
    	calculateAmounts();
    	$('input[id^="advertisement_rule"]').change(calculateAmounts);
    	
    	function getTotalEuroAmount() {
    		return "&euro; " + ($('#advertisement_amount').val()/100).toFixed(2);
    	}
    	
    	function getSubEuroAmount() {
    		return "&euro; " + ($('#advertisement_subamount').val()/100).toFixed(2);
    	}
    	
    	function getVATEuroAmount() {
    		return "&euro; " + ($('#advertisement_vat').val()/100).toFixed(2);
    	}
    	
    	function calculateAmounts() {
    		//Count number of used rules:
    		var usedRules = $('input[id^="advertisement_rule"][value!=""]').length;
    		//Minimal amount is 3 rules:
    		if (usedRules < 3) {
    			usedRules = 3;
    		}
    		var fixedRuleAmount = $('#advertisement_fixedruleamount').val();
    		var fixedVAT = $('#advertisement_fixedvatpercentage').val();
    		var subAmount = usedRules * fixedRuleAmount;
    		var totAmount = Math.round(subAmount * ((100 + parseInt(fixedVAT))/100));
    		var vatAmount = totAmount - subAmount;
    		
    		$('#advertisement_subamount').val(subAmount);
    		$('#advertisement_amount').val(totAmount);
    		$('#advertisement_vat').val(vatAmount);
    		
    		$('#advertisement_subamount_lbl').html(getSubEuroAmount());
    		$('#advertisement_amount_lbl').html(getTotalEuroAmount());
    		$('#advertisement_vat_lbl').html(getVATEuroAmount());
    	}

});
				
