jQuery(document).ready(function($){
	if($('.printthepage').length){
		window.print();
	}
	$('.printselected').click(function(){
		var selectedCategories = [];
		$('.filter a.selected').each(function(){
			var tempcategory = $(this).data('catid');
			if(tempcategory != undefined){
				selectedCategories.push(tempcategory);
			}
		})
		window.open('/arrangementliste?event_cat='+selectedCategories.join(','),"printwindow", 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=485,height=650');
		return false;
	})
	$(document).pngFix(); 

	$('#sponsors a').hover(function(){
			$('img',this).css('marginTop',-($('img',this).height()/2));
		},function(){
			$('img',this).css('marginTop',0);
		});
	//NEWSSLIDER
	
	$('#newsslider li:first').clone().appendTo('#newsslider ul');
	var storywidth = 495;
	var elementCount =$('#newsslider li').length;
	var containerwidth =elementCount*storywidth;
	var currentElement = 1;
	$('#newsslider ul').width(containerwidth);
	function changeStory(){
		clearTimeout(changeStoryTimeout);
				$('#newsslider ul').animate({
		'marginLeft':'-='+storywidth
		},
		function(){
			if(++currentElement == elementCount){
				$('#newsslider ul').css('marginLeft',0);
				currentElement = 1;
			}
			changeStoryTimeout = setTimeout(changeStory,8000);
		}
		)
		
	}
	function changeStoryBack(){
		clearTimeout(changeStoryTimeout);
		if(currentElement ==1){
		$('#newsslider ul').css('marginLeft',-(containerwidth-storywidth));
		currentElement = elementCount-1;
		$('#newsslider ul').animate({
				'marginLeft':'+='+storywidth
			},
		function(){
			changeStoryTimeout = setTimeout(changeStory,8000);
		}
			)
		}else{
			$('#newsslider ul').animate({
				'marginLeft':'+='+storywidth
			},
		function(){
			--currentElement;
			changeStoryTimeout = setTimeout(changeStory,8000);
		}
			)
		}
		
	}
	var changeStoryTimeout = setTimeout(changeStory,8000);
	$('<div id="slideleft"></div><div id="slideright"></div>').prependTo('#newsslider');
	$('#newsslider').hover(function(){
		$('#slideleft,#slideright').fadeIn('fast');
	},
	function(){
		$('#slideleft,#slideright').fadeOut('fast');
	});
	$('#slideleft').click(function(){
		changeStoryBack();
	});
	$('#slideright').click(function(){
		changeStory();
	});
	$('.eventlist').css('height',$('.eventlist').height());
	var categorytypes = [];
	var selectedCategories = [];
	$('.filter dd').each(function(){
		var tempcategories = [];
		$('a',this).each(function(){
			var tempcategory = $(this).data('catid');
			if(tempcategory != undefined){
				tempcategories.push(tempcategory);
			}
		})
		categorytypes.push(tempcategories);
	});
	$('.filter a').click(function(){
		if($(this).is(':first-child')){
			$(this).siblings().removeClass('selected');
		}
		$(this).toggleClass('selected');
		var selectedCategories = getSelected();
		$('#events li').each(function(){
			var elmCategories = $(this).data('categories');
			elmCategories = elmCategories.split(',');
			var showelem = true;
			$.each(selectedCategories,function(i,val){
				if(val.length){
					var intersect = array_intersect(val,elmCategories);
					if(intersect.length ==0){
						showelem = false;
						return false;
					}
				}
			});
			if(showelem){
				if(!$(this).is(':visible')){
					$(this).show();
					$(this).animate({height:220,width:165},'normal');
				}
			}else{
				if($(this).is(':visible')){
					$(this).animate({height:0,width:0},'normal',function(){$(this).hide()});
				}
			}
		})
		return false;
	})
	$('.current-menu-item').parent('.sub-menu').show().addClass('activesubmenu');
        $("#menu-hovedmenu > li").hover(
                function () {
                        $('.sub-menu:not(.activesubmenu)',this).stop(false,true).slideDown();
                },
                function () {
                        $('.sub-menu:not(.activesubmenu)',this).stop(false,true).slideUp();
                }
        );

});

function getSelected(){
	var selectedCategories = [];
	jQuery('.filter dd').each(function(){
		var tempcategories = [];
		jQuery('a.selected',this).each(function(){
			var tempcategory = jQuery(this).data('catid');
			if(tempcategory != undefined){
				tempcategories.push(tempcategory);
			}
		})
		selectedCategories.push(tempcategories);
		if(tempcategories.length){
			jQuery('a:first',this).removeClass('selected');
		}else{
			jQuery('a:first',this).addClass('selected');
		}
	});
	return selectedCategories;
}
function array_intersect () {
   // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    var arr1 = arguments[0],
        retArr = [];
    var k1 = '',
        arr = {},
        i = 0,
        k = '';
    arr1keys: for (k1 in arr1) {
        arrs: for (i = 1; i < arguments.length; i++) {
            arr = arguments[i];
            for (k in arr) {
                if (arr[k] == arr1[k1]) {
                    if (i === arguments.length - 1) {
                        retArr[k1] = arr1[k1];
                    }
                    continue arrs;
                }
            }
            continue arr1keys;
        }
    }
    return retArr;
}

