layout_props = {
    search: function () {
	var search = $('#logginForm .search').val();
	
	$('#zoeken').blur(function () {
	    if ($(this).val().length < 1) {
		$(this).val(search);
	    }
	});
	$('#zoeken').focus(function () {
	    if ($(this).val() === search) {
		$(this).val('');
	    }
	});
    },
    sexyYoutube: function (vid, width, height) {
	
	vid.each(function () {
	    var vids = $(this), curVidObj = vids.html(), isCustomImage;
	    if (vids.find('iframe').length > 0) {
                vidCode = vids.find('iframe').attr('src').split('/')[4];
                isCustomImage = vids.attr('title').length > 0 ? vids.attr('title') : 'http://i4.ytimg.com/vi/' + vidCode +  '/0.jpg';
                vids.attr('title', '');
		vids.html('<img src="' + isCustomImage + '" height="' + height + '" style="width: ' + width + 'px; height:' + height + ';"/><span class="playBtn"></span>');
		vids.click(function () {
		    createOwnPopUp.open({
			prefix: '<div class="ownInfo">',
			suffix: '</div>',
			content: curVidObj
		    });
		    createOwnPopUp.setVideoProps(450, 350);
		});
	    }
	});	
    },
    addLoader: function (dom) {
	if (dom.length > 0) {
	    dom.append('<img src="/thema/vnabv1/images/ajax-loader.gif" class="loader"/>');
	}
	return {
	    hide: function () {
		$('.loader').hide();
	    },
	    show: function () {
		$('.loader').show();
	    }
	}
    }	
};

app_interactive = {
    faq: function () {
      $('.faq_list li').each(function () {
         $(this).addClass('standart');
      }); 
      $('.faq_list ul').each(function () {
         $(this).closest('li').find('a').eq(0).addClass('parent');
      }); 
    },
    hover: function () {
	var over, out;
	over = function (el) {
	    var html = '<div id="app_menu_overlay"><div class="blocker"></div><div class="content"></div><div>',
	    curLink = el.find('a').eq(0), curList = el.find('ul');  
	    if (curList.length > 0) {
		$('body').append(html);
		$('#app_menu_overlay .content').append(el.html());
		$('#app_menu_overlay .content a').eq(0).width(curLink.width());
		$('#app_menu_overlay .content').css({
		    top: curLink.offset().top, 
		    left: curLink.offset().left
		    });
 		out();
	    }
	};
	out = function () {
	    $('#app_menu_overlay .content').mouseleave(function () {
		$('#app_menu_overlay').remove();
	    });
            $('#app_menu_overlay .blocker').mouseover(function () {
                $('#app_menu_overlay').remove();
            });
	};
        
        
        
	$('#headNav .nav li').mouseover(function () {
            over($(this));
	});
    },
    kruimelpad: function () {
        var kruimelPad = '<a href="/">home</a>', url = window.location.toString().split('/')[3];
        if ($('#mainSubMenu li.current').length > 0) {
            $('#mainSubMenu li.current').each(function () {
                if ($(this).find('a').length > 0) {
                    kruimelPad += '<span>&gt;</span> <a href="' + $(this).find("a").attr('href') + '">' + $(this).find("a").html() + '</a> ';
                } 
            });
        }
        $('#kruimels').html(kruimelPad);
    },
	subNavigation: function () {
        $('li.hasChildren').each(function () {
            
			$(this).click(function(){
				var ul = $(this).find("ul");
				if (ul.is(":hidden")) {
					ul.slideDown();
				} else {
					ul.slideUp();
				}
			});
        });
    },
    dateSelector: function () {
	var dates_values, datePicker, initPicker, loader, updateKruimel;
	
	    loader = layout_props.addLoader($('#form_datePicker'));
	    loader.hide();
	    $.datepicker.regional.nl = {
		closeText: 'Sluiten',
		prevText: 'â†',
		nextText: 'â†’',
		currentText: 'Vandaag',
		monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
		'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
		monthNamesShort: ['jan', 'feb', 'mrt', 'apr', 'mei', 'jun',
		'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
		dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
		dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
		weekHeader: 'Wk',
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional.nl);
	
	updateKruimel = function () {
	    var htmlAr = $('#kruimels').html().split('</span>'), nData = [];
	    nData = htmlAr.splice(0,2);
	    $('#kruimels').html(nData.join('</span>'));
	    
	};
	
	
	$( "#datePickerWrap" ).multiDatesPicker({
	    onChangeMonthYear: function(year, month, inst) { 
		 loader.show();
		 $.get('/agenda/?dataselect=' + month + '-' + year, function (data) {
		     loader.hide();
		     updateKruimel();
		     $('div#content div.collContent').html($(data).find('.collContent').html()).hide().fadeIn();
		 });
	
	    },
	    onSelect: function(dateText, inst) {
		loader.show();
		$.get('/agenda/?dataselect=' + dateText, function (data) {
		    loader.hide();
		    updateKruimel();
		     $('div#content div.collContent').html($(data).find('.collContent').html()).hide().fadeIn();
		 });
	    },
	    addDates: '/getdates.php'
	});

	

	$('#form_datePicker input').hide();
	$('#form_datePicker').submit(function () {
	   return false; 
	});
	
	
    },
    listHover: function (dom) {
	dom.find('li').live('mouseover', function () {
	    $(this).addClass('selected');
	});
	dom.find('li').live('mouseout', function () {
	    $(this).removeClass('selected');
	});
	dom.find('li').live('click', function () {
	   var link =  $(this).find('.excerptLink').attr('href'); 
	   window.location = link;
	})
	
    },
    handleFormSubmits: function () {
	var getFormVars, loader;
	
	getFormVars = function (input) {
	    var vars = {}; 
	    input.each(function () {
		vars[$(this).attr('name')] = $(this).val();
	    });
	    return vars;
	}
	loader = layout_props.addLoader($('#frmrclogin'));
	loader.hide();
	
	$('#frmrclogin').submit(function (){
	    loader.show();
	    $.post('/loginrisk.php?s=login&eabs=1', getFormVars($(this).find('input')), function (data) {
		console.log(data);
		loader.hide();
	    });
	    return false;
	});
    }
}

createOwnPopUp = {
    open: function (o) {
	var html = "<div id=\"ownPop\"><div class=\"ownOverlay\"></div><div class=\"ownContent\"></div></div>",
	_this = this;
	$('body').append(html);
	$('#ownPop').hide();
	$('#ownPop .ownContent').hide();
	$('#ownPop .ownContent').append('<div class="ownControllers"><a href="#">sluiten</a></div>');
	$('#ownPop .ownContent').append('<div class="ownContentHolder"></div>');
	if (o.content !== undefined) {
	    $('#ownPop .ownContent .ownContentHolder').append(o.prefix + o.content + o.suffix);
	}

	$('#ownPop .ownOverlay, #ownPop div.ownControllers a').click(function () {
	    _this.close();
	    return false;
	 });

	$('#ownPop').fadeIn('slow', function () {
	    $('#ownPop .ownContent').fadeIn('slow');
	});

	$(window).scroll(function () {
	    var top =  $(window).scrollTop() + ($(window).height() / 2);
	   $('#ownPop .ownContent').css({top: top});
	});

	$('#ownPop .ownContent').css({top:  $(window).scrollTop() + ($(window).height() / 2)});
    },
    close: function () {
	 $('#ownPop .ownContent').fadeOut('slow', function () {
	     $('#ownPop').fadeOut('slow', function () {
		$('#ownPop').remove();
	     });
	 });
    },
    setVideoProps: function (width, height) {
	$('#ownPop iframe').width(width);
	$('#ownPop iframe').height(height);
    }
};

app_etalage = (function (){

    function init (elem, time, props) {
        var length = $(elem).find('li').length, setToCurrent, curSlide = 0, speed = props.speed || 500,
        oldCur = 0, widthIndicator = 0;widthSlides = 0;full = (props.width !== undefined)? false : true;
	$(elem).css({overflow: 'visible'});
        prop = {};
        if (props === undefined) {
            prop.navigationArrows= true;
            prop.navigationList = true;
            prop.indicator = true;
            prop.width = $(window).width();
            prop.indicatorWidth = 80;
        } else {
            prop = props
	    prop.width = props.width || $(window).width();
	    prop.hideArrows = props.hideArrows || false;
        }



        $(elem).parent().append('<ol id="etalageModuleNav"></ol><div id="etalageIndicator"></div>');
        $(elem).wrap('<div id="etalageMask"></div>');
        widthIndicator = $('#etalageIndicator').width();

        $(elem).find('li').each(function (i) {
	    if (full) {
		$(this).css({
		    left: (i * prop.width),
		    width: prop.width
		});
	    }
        });

        $(elem).width(length * prop.width);



        setToCurrent = function (index) {
            var etalageElem = $(elem).find('li').eq(index);
            $('#etalageModuleNav li').removeClass('current');
            $('#etalageModuleNav li').eq(index).addClass('current');

            if (index >= length - 1){
                $('.etalageNav_right').hide();
            }else {
                $('.etalageNav_right').show();
            }
            if (index <= 0){
                $('.etalageNav_left').hide();
            } else {
                $('.etalageNav_left').show();
            }

            $(elem).stop().animate({
                left: -(index *  prop.width)
            }, speed);
        };


        if (prop.navigationList){
            app_utils.mapArray(length, function (i){
                var cur = i === 0 ? 'current' : '';
                $('#etalageModuleNav').append('<li class="etalage_btn_' + (i + 1) + ' ' + cur + '">' + (i + 1) + '</li>');
            });
            $('#etalageModuleNav li').click(function (){
                curSlide = $(this).index();
                stopIndicator();
                setToCurrent(curSlide);
            });
        }


        if (prop.navigationArrows){
            $(elem).parent().append('<span class="etalageNav_left etalage_arrowNavigation" style="display: none;">&lt;</span><span class="etalageNav_right etalage_arrowNavigation">&gt;</span>');
            $('.etalageNav_left').click(function (){
                curSlide -= 1
                setToCurrent(curSlide);
                stopIndicator();
            });

            $('.etalageNav_right').click(function (){
                curSlide += 1
                setToCurrent(curSlide);
                stopIndicator();
            });
        }

        function stopIndicator () {
            prop.indicator = false;
            $('div#etalageIndicator').stop();
            $('div#etalageIndicator').hide();
        }
        if (prop.indicator) {
            function restartNextSlide() {

                $('div#etalageIndicator').width(0);
                $('div#etalageIndicator$').animate({width: prop.indicatorWidth},
                time * 1000, function (){
                    curSlide = curSlide >= length - 1 ? 0 : curSlide + 1;
                    setToCurrent(curSlide);
                    if  ( prop.indicator ) {
                        restartNextSlide();
                    }
                })
            }
            restartNextSlide();
        } else {
           $('div#etalageIndicator').hide();
        }
    }


    return {
        start: function (elem, time, prop){
            init(elem, time, prop);
        }
    }
}());

app_utils = (function (){
    return {
        mapArray: function (length, callback){
            var i = 0;
            while (i < length) {
                callback(i);
                i += 1;
            }
        }
    };
}());


function closeMessage(){
		$('.messages').animate({
			opacity: 0.95,
			top: '-300px'
		}, 1000);
}


$(function () {
    layout_props.search();
    layout_props.sexyYoutube($('.etalage .media'), 415, 258);
    app_etalage.start($('#etaHolder .etalage'), 5, {
        navigationArrows: true,
        navigationList: true,
	width: 680,
	speed: 1000,
        indicator: true,
        indicatorWidth: 680
    });  
    app_interactive.hover();
    app_interactive.listHover($('.nieuwsRows, .agendaRows'));
    app_interactive.dateSelector();
    app_interactive.kruimelpad();
    app_interactive.faq();
    app_interactive.subNavigation();
	
    $('.messages').animate({
        opacity: 0.95,
        top: '0px'
    }, 1000, function() {
        var cl = setTimeout("closeMessage()",10000);
        $('#closeMessage').click(function(){
            $('.messages').animate({
                opacity: 0.95,
                top: '-300px'
            }, 1000);
        });
    });
    //app_interactive.handleFormSubmits();
});

function textCounter( field, countfield, maxlimit ) {
 if ( field.value.length > maxlimit ) {
  field.value = field.value.substring( 0, maxlimit );
  field.blur();
  field.focus();
  return false;
 } else {
  countfield.value = maxlimit - field.value.length;
 }
}
