// our namespace
var MCO = window.MCO || {};

// create closure passing in the jQuery object
(function($){   
    
    $.striptags = function(str) {
    	return str.replace(/<\/?[^>]+>/gi,'');
    };

    $.trim = function(str) {
    	return str.replace(/\s+/g,' ')
    };
    
    MCO.setupArrowLinks = function(){
        $('p.arrow').each(function(){
            var img = $('<img />').attr('src','/static/img/arrow.png');
            var a = $(this).find('a:eq(0)');
            if (a.length) {
                $(this).append(a.clone().empty().addClass('iconArrow').append(img));
            }
        });
    };
    
    MCO.setupModalWindows = function(){
        var defaults = {
            speedIn: 600,
            speedOut: 200,
            overlayOpacity: 0.7,
            overlayColor: '#fff',
            padding: 10,
            centerOnScroll: false,
            scrolling: 'no',
            titlePosition: 'inside'
        };
        $(".modal").fancybox(defaults);
    };
    
    MCO.setupSearchForm = function(){
        var keyword = $("input[name='keyword']");
        var label = keyword.val();
        keyword.focus(function(e){
            if (keyword.val()==label){
                keyword.val('');
            }
        }).blur(function(e){
            if (keyword.val()==''){
                keyword.val(label);
            }
        });
        $('#site-search').submit(function(e){
            var value = $.trim($.striptags(keyword.val()));
            document.location.href = "/page/zoekresultaten/" + value;
            e.preventDefault();
        });
    };
    
    MCO.form = (function(){
        var setupPlaceHolders = function() {
            $('label.placeholder').each(function(){
                var labelElement = $(this);
                var label = labelElement.text();
                var input = $("input[id='"+labelElement.attr('for')+"']");        
                input.focus(function(e){
                    if (input.val()==label){
                        input.val('').removeClass('placeholder');;
                    }
                }).blur(function(e){
                    if (input.val()==''){
                        input.val(label).addClass('placeholder');
                    }
                });
                if (input.val()==''){
                    input.val(label).addClass('placeholder');
                }
                labelElement.remove();
            });
        };
        return {
            setupPlaceHolders : setupPlaceHolders
        };
    })();
    
    MCO.setupPopups = function(){
        $(".popup").each(function(index){
            $(this).click(function(e){
                var href = $(this).attr('href');
                var rel = $(this).attr('rel') || '';
                var dimensions = rel.split(',');
                var w = dimensions[0] || 400;
                var h = dimensions[1] || 300;
                var name = 'some_unique_popup_name_' + index;
                window.open( href, name, 'width=' + w + ',height=' + h );            
                e.preventDefault();
                return false;
            }).bind('mouseup ',function(e){
                e.preventDefault();
                return false;
            });
        });
    };
    
    MCO.setupExternalLinks = function() {    
        var h = window.location.host.toLowerCase();
		if (!$("a").hasClass('lang')) {
        	$("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "']), a[href$='.pdf'], a[rel='external']").attr("target","_blank");
		}
    };
    
    MCO.setupFileLinks = function() {
    	$("a[href$='.pdf']").addClass("pdf");
    	$("a[href$='.doc']").addClass("doc");
    };    
        
    // carousel module
    MCO.carousel = (function() {
        // private vars
        var timer = false,
            container = $([]),
            views = $([]),
            dots = $([]),
            numViews = 0,
            limit = 0,
            index = 0,
            previousIndex = 0,
            dotActiveClass = 'active',
            interval = 5000;
            
        // private methods
        function makeDotList() {
            var ul = $('<ul />').addClass('dots');
            for( var i = 0; i < numViews; i++){
                (function(v){
					$('<li />').text(i+1).appendTo(ul).click(function(e){
						select(v);
					});
				})(i);
            }
            return ul;
        }
        function updateIndex(){
            previousIndex = index;
            index++;
            if (index > limit){
                index = 0;
            }
        }
		function select(i){
			previousIndex = index;
            index=i;
			tweenView();
            hiliteDot();
		}
        function hiliteDot(){
            dots.eq(previousIndex).removeClass(dotActiveClass);
            dots.eq(index).addClass(dotActiveClass);
        }
        function tweenView(){            
            views.eq(previousIndex).fadeOut('fast',function(){
                views.eq(index).fadeIn('slow')
            });
        }   
        function doTween(){
            updateIndex();
            tweenView();
            hiliteDot();
        }
        function startAutoTween() {
            timer = setInterval(doTween,interval);
        }        
        function stopAutoTween() {
            window.clearInterval(timer);
            timer = false;
        }        
        function init() {
            container = $(".carousel");
            views = $(".carousel .view");
            numViews = views.length;
            if ( numViews > 0 ) {
                limit = numViews - 1;
                var dotList = makeDotList().appendTo(container);
                dots = dotList.children();
                dots.eq(index).addClass(dotActiveClass);
                views.hide().eq(index).show();
                if (numViews > 1){
                    startAutoTween();
                    container.mouseover(function(e){
                        stopAutoTween();
                    }).mouseout(function(e){
                        startAutoTween();
                    });
                }
            }
        }
        
        // return public methods
        return {
            init : init            
        };
        
    })();
    
    $(function(){
        MCO.carousel.init();    
       // MCO.setupArrowLinks();    
        MCO.setupExternalLinks();
        MCO.setupPopups();
        MCO.setupFileLinks();
        MCO.setupModalWindows();
        MCO.form.setupPlaceHolders();
        MCO.setupSearchForm();
    });

})(jQuery);
