// Ajax fix voor page reload
if (document.location.href.search(/#/) != -1) {
    var url = document.location.href.split("#");
    if (url[1]) document.location.href  =  url[1];
}

$(function() {
	init();
});

function init(container)
{
	if(typeof(container) == 'undefined') {
		container = null;
	}

	initBehaviour(container);

    return;
	if(typeof(initAjaxBehaviour) !== 'undefined') {
		initAjaxBehaviour(container);
	}
}

/**
 * Zet de eigenschappen voor een aantal elementen op basis van classes etc
 *
 */
function initBehaviour(container)
{
    // Ontkoppel eerst oude eigenschappen
    $('a, form, div', container).unbind();

	// Initieer datepicker velden
    $('.datepicker', container).each(function(){
    	if (!$.isFunction($.datepicker)) {
    		includeScript("/scripts/jquery/ui/jquery.ui.datepicker.js", function(){
    			$('.datepicker', container).datepicker({dateFormat: 'dd-mm-yy'});
    		});
    		includeScript("/scripts/jquery/ui/i18n/jquery.ui.datepicker-nl.js");
    	} else {
    		$('.datepicker', container).datepicker({dateFormat: 'dd-mm-yy'});
    	}
	});
    
    if(readCookie("alerted") == 'true') {
    } else {
        createCookie("alerted", "false"); 
    }

    if ($.browser.msie) {
        if($.browser.version < 8) {
            if(readCookie("alerted") == 'false') {
                alert('Deze site is gemaakt voor Chrome, Firefox 3, Internet Explorer 8 en hoger.\rBepaalde onderdelen van de site werken nu niet goed.\rHet is aan te raden uw browser te upgraden');
            }
            createCookie("alerted", "true"); 
            initIE7();
            return;
        } else {
            initIE8();
            return;
        }
    }

    initAll();
}
function initIE7()
{
    externalLinks();
	initMenuHeight();
	initActueelHeight();
    initCompetition();
}
function initIE8()
{
    externalLinks();
    initMenu(true, false);	
	initMenuHeight();
	initActueelHeight();
    initBrowse();
    initCompetition();
}
function initAll()
{
    externalLinks();
    initMenu();	
	initMenuHeight();
	initActueelHeight();
    initBrowse(); 
    initCompetition();
}

function initMarquee() {
    $('marquee')
        .marquee()
        .mouseover(function () {
            $(this).trigger('stop');
        })
        .mouseout(function () {
            $(this).trigger('start');
        })
    ;

}

function initFck(container) {
    $.fck.path = '/scripts/fckeditor/';

	if(container != null) {
		$('textarea.richtext', container).fck({
			  toolbar: 'Full',
			  height: 200
		});
	} else {
		$('textarea.richtext').fck({
			  toolbar: 'Full',
			  height: 200
		});
	}

}

function initActueelHeight() {
		$('#index_nieuwsbrief').height('auto');	
		$('#index_nieuws').height('auto');
		$('#index_agenda').height('auto');	

		index_nieuws_height = $('#index_nieuws').height();
		index_agenda_height = $('#index_agenda').height();

		total_left_height = index_nieuws_height;
		total_right_height = index_agenda_height;

		if(total_left_height < total_right_height) {
				new_nieuws_height = total_right_height;
				$('#index_nieuws').height(new_nieuws_height);
		} else {
				$('#index_agenda').height(total_left_height);	
		}
}

function pageHeight() {
		left_top_margin = 150;
		mid_top_margin = 100;
		
		$('#mid').height('auto');
		$('#left').height('auto');	
 
//        $('#mid').height($('#mid').height() + 2);

		mid_height = $('#mid').height();
		left_height = $('#left').height();

		mid_total_height = mid_height + mid_top_margin;
		left_total_height = left_height	+ left_top_margin;

		if(mid_total_height<left_total_height) {
//				$('#mid').height(left_height + left_top_margin - mid_top_margin);
		} else {
				$('#left').height(mid_height + mid_top_margin - left_top_margin);
		}
}
function initMenuHeight() 
{
		pageHeight();
        setTimeout('pageHeight()', 1000);
}

var menu_init = false;
function initMenu(doAjax, tegelijkToegestaan) {
    if(typeof(tegelijkToegestaan) == 'undefined') {
        tegelijkToegestaan = true;
    }
    if(typeof(doAjax) == 'undefined') {
        doAjax = true;
    }
	if(menu_init) {
	} else {
		$('.level2').hide();
		$('.level3').hide();
		menu_init = true;
	}
	$('.level1').unbind('click').click(function() {
            var animations_open =function(id) {
	    	    $('.level2').hide('slow');
		        $('.level3').hide('slow');
                setTimeout('initMenuHeight()',601);
            };
            var animations_closed =function(id) {
	    	    $('.level2').hide('slow');
		        $('.level3').hide('slow');
                $('#'+id).next('.level1b').children().show('slow');
                setTimeout('initMenuHeight()',601);
            };

        var animate = animations_open;
        if($(this).hasClass('menu_active') == false) {
            $('.menu_active').removeClass('menu_active');
            $(this).addClass('menu_active');
            var animate = animations_closed;
        } else {
           if($(this).next('.level1b').children().css('display') == 'none') {
            var animate = animations_closed;
           }
        }

        var child = $(this).children('a');
        var target = '' + $(child[0]).attr('target');
        if(target == '_blank'){
            animate($(this).attr('id'));
            return true;
        }
        var href = '' + $(child[0]).attr('href');
        if(doAjax) {
            if(tegelijkToegestaan) {
                animate($(this).attr('id'));
                ajax_load(href);
            } else {
                ajax_load(href, false, false, false, animate, $(this).attr('id')); 
            }
        } else {
            animate($(this).attr('id'));
            return true;
        }

        return false;
	});

	$('.level2').unbind('click').click(function() {
            var animations_open =function(id) {
		        $('.level3').hide('slow');
                setTimeout('initMenuHeight()',601);
            };
            var animations_closed =function(id) {
		        $('.level3').hide('slow');
                
                $('#'+id).next('.level2b').children().show('slow');
                setTimeout('initMenuHeight()',601);
            };

        var animate = animations_open;
        if($(this).hasClass('menu_active') == false) {
            $('.menu_active').removeClass('menu_active');
            $(this).addClass('menu_active');
            var animate = animations_closed;
        } else {
       if($(this).next('.level2b').children().css('display') == 'none') {
           var animate = animations_closed;
           }
        }

        var child = $(this).children('a');
        var target = '' + $(child[0]).attr('target');
        if(target == '_blank'){
            animate($(this).attr('id'));
            return true;
        }
        var href = '' + $(child[0]).attr('href');
        if(doAjax) {
            if(tegelijkToegestaan) {
                animate($(this).attr('id'));
                ajax_load(href);
            } else {
                ajax_load(href, false, false, false, animate, $(this).attr('id')); 
            }
        } else {
            animate($(this).attr('id'));
            return true;
        }

        return false;
	});
	$('.level3').unbind('click').click(function() {
        if($(this).hasClass('menu_active') == false) {
            $('.menu_active').removeClass('menu_active');
            $(this).addClass('menu_active');
        }

        var child = $(this).children('a');
        var target = '' + $(child[0]).attr('target');
        if(target == '_blank'){
            return true;
        }
        var href = '' + $(child[0]).attr('href');
        if(doAjax) {
            ajax_load(href);
        } else {
            return true;
        }

        return false;
	});

	}


// Laad een javascript
function includeScript(uri, callback) {
    var script = document.createElement("script");
    script.src = uri;
    script.type = "text/javascript";
    script.language = "javascript";
    if (callback != null) {
        script.onreadystatechange = function() {
            if (script.readyState == "complete") callback();
        };
        script.onload = callback;
    }
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;
}

// Laad een stylesheet
function includeStyle(uri) {
    $("head").append("<link rel='stylesheet' type='text/css' href='" + uri + "' />");
}

var notifier = 0;
function notify(message, type) {
    notifier++;
    if (!$('#notifier').attr('id')) {
        $('#content_page').append("<div id='notifier'></div>");
    }
    $('#notifier').prepend("<div id='not" + notifier + "' class='notification'><img src='/images/notification_mark.png' alt='' /> " + message + "</span>");
    //$('#notifier').css('top', 0 + $(window).scrollTop());
    $('#notifier').fadeTo('slow', 1);
    window.setTimeout("$('#not" + notifier + "').fadeOut()", 2000);
}

function externalLinks() {
    $('a').each(function(){
        var href = $(this).attr('href');
        var regex = new RegExp('^http://(www\.){0,1}svhermes\.net','i');
        href = href.replace(regex,'');
        $(this).attr('href',href);
        var regex = new RegExp('^(http|/public/files)','i');
        var result = regex.test(href);
        if(result){
           $(this).attr('target','_blank');
        }
    });
}

function marqueeStop(dit) {
		return; //toch niet mooi
		var html = $(dit).html();
		var id = $(dit).attr('id');
		domObj = $('#'+id);
		html = "<div id='reclame_after'>" + html + "</div>";
		$(domObj).after(html);
		$(domObj).attr('style','display: none');
}

tmp_browse = null;

function initBrowse() {
    $(".browse").bind("click", function(){
        tmp_browse = $(this);
        CKFinder.popup(null, null, null, setFileField);
    });
}

function setFileField(val) {
    $(tmp_browse).prev().attr('value', val);
}

function createCookie(name,value) {
    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;
}

function initCompetition() {
    min_programma_width = 160;
    new_programma_width = $('table.programma').width();
    if(new_programma_width < min_programma_width) {
        new_programma_width = min_programma_width;
        $('table.programma').width(min_programma_width);
    }
    $('h2.programma').width(new_programma_width);
    $('h2.stand').width($('table.stand').width());
    $('h2.uitslagen').width($('table.uitslagen').width());
    $('h2.komende_wedstrijden').width($('table.komende_wedstrijden').width());
    $('h2.laatste_uitslagen').width($('table.laatste_uitslagen').width());
}

