$(document).ready(function(){

	$.fn.media.defaults.flvPlayer = $.fn.media.defaults.mp3Player = SITE_ROOT_DIR + 'js/jquery/mediaplayer.swf';
	$.fn.media.defaults.bgColor = '#E7E8E9';
	// $('.audio.media').media({ width: 300, height: 20 });
	$('.video.media').media();

	$(".toggle_container").hide(); 
	$("h2.trigger").click(function(){
		$(this).toggleClass("active").next().slideToggle("slow");
		return false;
	});
	$("h2.searched").click();
	
	$("#archive").click(function(){
		$('.archive').slideToggle("slow");
		return false;
	});
	
	resizeDivs();
	
	$('#resultsTable tbody tr').hover(function(){
		$(this).css('backgroundColor', '#000080');
		$(this).css('color', '#fff');
		$(this).find('a').css('color', '#fff');
	}, function(){
		$(this).css('backgroundColor', '');
		$(this).css('color', '');
		$(this).find('a').css('color', '');
	});
	$('#resultsTable tr').click(function(e){
		openExpo($(this).find('a:first-child').attr('href'));
		e.preventDefault();
	});

	$('.rc #resultsTable tbody tr').hover(function(){
		$(this).css('backgroundColor', '#000');
		$(this).css('color', '#B2B4B6');
	}, function(){
		$(this).css('backgroundColor', '');
		$(this).css('color', '');
	});
	
	function openExpo(url){
		var win = 'win' + Math.random();
		var windowWidth = $(window).width() - 20;
		var windowHeight = $(window).height();
	   	var x = ((document.all)?window.screenLeft:window.screenX) + 0;
	    var y = ((document.all)?window.screenTop:window.screenY) + 20;
		window.open(url, win, 'scrollbars=1,resizeable=yes,status=0,toolbar=0,location=0,menubar=0,width=' + windowWidth + ',height=' + windowHeight + ',left=' + x + ',top=' + y);
	}
	
	$('.toexposition').click(function(e){
		openExpo($(this).attr('href'));
		e.preventDefault();
	});
	
	var prevSelected;
	$('a.popup').click(function(e){
		prevSelected = $('#nav a.selected');
		prevSelected.removeClass('selected');
		$(this).addClass('selected');
		$('#popup').show();
		fleXenv.fleXcrollMain('popupContent');
		e.preventDefault();
	});
	$('#popup a.close').click(function(e){
		$('a.popup').removeClass('selected');
		$('#popup').hide();
		prevSelected.addClass('selected');
		e.preventDefault();
	});
	
	$('#navSar a, .newsletterList a, #next a, #prev a').live('click', function(e){
		if($(this).parents().is("#navSar")){
			$("#navSar a").removeClass('selected');
		}
		$(this).addClass('selected');
		var url = $(this).attr('href');
		loadPopupContent(url);
		e.preventDefault();
	});
	
	$('a.membership').live('click', function(e){
		var url = $(this).attr('href');
		loadPopupContent(url);
		e.preventDefault();
	});
	
	$('#membershipForm').live('submit', function(e){
		membershipFormData = $(this).serializeArray();
		$.post($(this).attr('action'), $(this).serialize(), function(data){
			$('#popupContent').html(data);
		});
		e.preventDefault();
	});
	
	// $('#headerContent form').live('submit', function(e){
	// 	$.post($(this).attr('action'), $(this).serialize(), function(data){
	// 		console.log(data);
	// 		$('.col.dubbel').html(data);
	// 	});
	// 	e.preventDefault();
	// });
	
	$('.more').click(function(e){
		var link = $(this).find('a');
		var text = link.text() == 'More' ? 'Less' : 'More';
		link.text(text);
		$(this).next().slideToggle("slow");
		e.preventDefault();
	})
	
	$('#showExtra').click(function(e){
		var link = $(this).find('a');
		var text = link.text() == 'More' ? 'Less' : 'More';
		link.text(text);
		$('#extraContent').slideToggle("slow");
		e.preventDefault();
	})
	
	$('.abstract').click(function(e){
		$('.abstractPopup').hide();
		var popup = $(this).next();
		popup.show();
		popup.css('top', ($(window).scrollTop() + 20) + 'px');
	});

	$('.abstractPopup .close').click(function(e){
		$('.abstractPopup').hide();
		e.preventDefault();
	});

});

$(window).resize(function() {
	resizeDivs();
});

function resizeDivs(){
	
	// Issues
	if(document.getElementById('overflowWrap')){
		var height = document.documentElement.clientHeight;
		height -= document.getElementById('overflowWrap').offsetTop;
		// not sure how to get this dynamically
		height -= 17; /* whatever you set your body bottom margin/padding to be */
		document.getElementById('overflowWrap').style.height = height +"px";
	}

	// RC
	if(document.getElementById('rc')){
		var height = document.documentElement.clientHeight;
		height -= document.getElementById('rc').offsetTop;
		// not sure how to get this dynamically
		// height -= 10; /* whatever you set your body bottom margin/padding to be */
		document.getElementById('rc').style.height = height +"px";
	}

}

function loadPopupContent(url){
	$.get(url, function(data){
		$('#popupContent').remove();
		$('#popupHeader').after('<div id="popupContent" class="flexcroll"></div>');
		$('#popupContent').html(data);
		if(url.indexOf('/newsletters/view/') != -1){
			$('#popupContent').addClass('newsletter');
		}
		fleXenv.fleXcrollMain('popupContent');
		populateMembershipForm();
	});
}

var membershipFormData = [];
function populateMembershipForm(){
	for(var i=0; i < membershipFormData.length; i++){
		var id = '[name="' + membershipFormData[i].name + '"]';
		var value = membershipFormData[i].value;
		if($(id).attr('type') == 'checkbox'){
			$(id).attr('checked', 'checked');
		} else {
			$(id).val(value);
		}
	}
}

$(function() {
    var delay = 400;
    
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }
    
    $('.custom_button, .hover_menu').mouseenter(function() {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });    
    
});
