$(function() {

    $('a[rel*=external]').click( function() {
	window.open(this.href);
	return false;
    });

    $('a.submit').click(function() {

	form = $(this).parents('form').get(0);

	if (checkForm(form)) {
	    $(form).submit();
	}

	return false;
    });

    $('a[href=#]').click(function() {
	return false;
    })

    /* Ajax links */
	$('a.ajax').live('click', function() {

	    var a_href = $(this).attr('href');
	    var a_rel = $(this).attr('rel');
	    var a_top = a_rel + '_top';

	    if ($(this).hasClass('scrolltop')) {
		window.scrollTo(0, 1);
	    }

	    $(a_rel + ' .ajax_loading').show();

	    $.get(a_href, {}, function(data) {
		$(a_rel).replaceWith($(data).filter(a_rel));

		initborders();
		update_paginators();
		update_maximize();
	    });

	    return false;
	});

    initborders();

    $('.ccheckable').click(function() {

	$(this).find('.ccheckbox:eq(0)').toggleClass('checked');

	if ($(this).find('.ccheckbox:eq(0)').hasClass('checked')) {
	    $(this).find('input.ccheckbox_input').attr('checked', 'checked');
	} else {
	    $(this).find('input.ccheckbox_input').removeAttr('checked');
	}

	callback = $(this).metadata().callback;
	id = $(this).metadata().id;

	callback(id);

    });

    $('table.filter_list tfoot a').click(function() {
	
	t = $(this).closest('table');

	t.find('.ccheckbox').removeClass('checked');
	t.find('input.ccheckbox_input').removeAttr('checked');

	callback = t.metadata().callback;
	callback();

	return false;
    });

    update_maximize();

    $('select.orange').customStyle();

    $('div.radio_orange > label').click(function() {
	$(this).closest('div.radio_orange').find('label').removeClass('checked');
	$(this).closest('div.radio_orange').find('input').removeAttr('checked');
	$(this).addClass('checked');

	f = $(this).attr('for');
	$('#' + f).attr('checked', 'checked');
	v = $('#' + f).val();

	callback = $(this).closest('div.radio_orange').metadata().callback;
	callback(v);
	
    });

    /* Dropdown city box */
	$('#main_header div.city_selector div.b').click(function() {
	    if ($(this).hasClass('search')) {
		$('#header_city').submit();
	    } else {
		$('#main_header div.city_selector div.c').toggle('slide', {direction: 'up'}, 300);
	    }
	});

	$('#main_header div.city_selector div.c a').click(function() {
	    $('#frm_header_city').val($(this).text());
	    $('#frm_header_city_busy').show();
	    $('#main_header div.city_selector div.c').hide('slide', {direction: 'up'}, 300);
	    return true;
	})
    
    /* City search box */
	$('#frm_header_city').focus(function() {
	    old = $(this).data('old_value');
	    if (!old) $(this).data('old_value', $(this).val());

	    $(this).val('');
	    $('#main_header div.city_selector div.b').css('backgroundPosition', '-28px 0px').addClass('search');
	    $('#main_header div.city_selector div.c:visible').hide('slide', {direction: 'up'}, 300);
	})

	$('#frm_header_city').blur(function() {
	    if ($(this).val() == '') $(this).val($(this).data('old_value'));
	    $('#main_header div.city_selector div.b').css('backgroundPosition', '0px 0px').removeClass('search');
	});

    $('#main_header div.city_selector div.b, #main_header div.city_selector div.c, .noselect').disableSelection();

    $(window).resize(update_footer);
    
    update_paginators();
    update_footer();

});

function update_footer() {
    
    h1 = $(window).height();
    h2 = $('#main_header').height();
    h3 = $('#main_container').height();
    h4 = $('#main_footer').height();

    if (h2 + h3 + h4 < h1) {
	$('#main_footer').css({position: 'fixed', bottom: '0px', left: '0px', right: '0px'});
    } else {
	$('#main_footer').css({position: 'relative', bottom: 'auto', left: 'auto', right: 'auto'});
    }

}

/* Range selectors */
var $el_location_range;
function update_range(el) {

    $el_location_range = $(el);

    $el_location_range.attr('disabled', 'disabled');

    $.post(url_set_location_range + '/' + $el_location_range.val(), function(data) {

	$el_location_range.removeAttr('disabled');
	callback = $el_location_range.metadata().callback;
	if (callback != undefined) callback();

    });

};

function update_paginators() {
    $('ul.paginator_bullets:not(#focus_action_paginator) li a').text('');
    $('ul.paginator_bullets:not(#focus_action_paginator) li.current').text('');
}

function update_maximize() {
    if ($('div.maximize').length > 0) {

	h = 0;
	
	$('div.maximize').css('height', 'auto');

	$('div.maximize').each(function(i, o) {
	    h = Math.max(h, parseInt($(o).height()));
	});

	$('div.maximize').each(function(i, o) {

	    max = $(o).css('maxHeight');
	    max = (max == 'undefined' ? h : parseInt(max));
	    newh = (h > max ? max : h);

	    $(o).height(newh + 'px');
	    $(o).find('#gmap_canvas').height(newh + 'px');

	});
	
    }
}

(function($){
    $.fn.extend({
    customStyle : function(options) {
        if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)) {
            return this.each(function() {
                var currentSelected = $(this).find(':selected');
                $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
                var selectBoxSpan = $(this).next();
                //var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));            
		var selectBoxWidth = 210;
                var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                selectBoxSpan.css({display:'inline-block'});
                selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
                var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
                $(this).height(selectBoxHeight).change(function() {
                    selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
                });
         });
        }
    }
    });
})(jQuery);

function clearCookies() {
    if (confirm('Cookies opnieuw instellen?')) {
	$.get('/cookies-opnieuw-instellen', function(data) {
	    alert('Cookies zijn opnieuw ingesteld');
	});
    }
}

function initborders() {
    $('div.cblock:not(.mini):not(.logoview):not(.borderinit),div.addborder_top,.addborders').append('<div class="eborder" style="top: 0px; height: 1px; left: 0px; right: 0px;"></div>'); //boven
    $('div.cblock:not(.mini):not(.logoview):not(.borderinit),div.addborder_left,.addborders').append('<div class="eborder" style="top: 0px; bottom: 0px; width: 1px; left: 0px;"></div>'); //links
    $('div.cblock:not(.mini):not(.logoview):not(.borderinit),div.addborder_right,.addborders').append('<div class="eborder" style="top: 0px; bottom: 0px; width: 1px; right: 0px;"></div>'); //rechts
    $('div.cblock:not(.mini):not(.logoview):not(.borderinit),div.addborder_bottom,.addborders').append('<div class="eborder" style="bottom: 0px; left: 0px; height: 1px; right: 0px;"></div>'); //onder
    $('div.cblock:not(.mini):not(.logoview):not(.borderinit),.addborders').addClass('borderinit');
}

