(function($){
	$.fn.multiSelect=function(){
		var multiSelects=$(this);
		$(this).each(function(){
			var d=$(this);
			var width=d.width();
			d.hide().before('<div class="multiselect_input"></div>');
			var left=d.prev('div.multiselect_input').position().left;
			var top=d.prev('div.multiselect_input').position().top;
			d.css({position:'absolute',left:left,top:top+24,width:width});
			showText();
			d.prev('div.multiselect_input').click(function(){
				// hide all the other box
				multiSelects.hide();
				$(this).addClass('open');
				d.show();
				return false;
			});
			$('div.option span',d).click(function(){
				$(this).prev('input').attr('checked',$(this).prev('input').attr('checked')?'':'checked');
				showText();
				return false;
			});
			$('div.option input',d).click(function() {
				showText();
			});
			var mouseEnter=false;
			d.hover(function() {
				mouseEnter=true;
			}, function() {
				mouseEnter=false;
			});
			$('div.option.select_all input,div.option.select_all span',d).click(function() {
				if($('div.option.select_all input',d).attr('checked')){
					$('div.option:not(.select_all) input[type=checkbox]',d).attr('checked','checked');
				}else{
					$('div.option:not(.select_all) input[type=checkbox]',d).attr('checked','');
				}
				showText();
			});
			$('body').click(function(){
				if(!mouseEnter){
					d.hide();
					d.prev('div.multiselect_input').removeClass('open');
				}
			});
			function showText(){
				var options=$('div.option:not(.select_all)',d);
				//var name=$('input:first',options).attr('name');
				var name=d.prevAll('label').eq(0).text();
				var i=$('input:checked',options).length;
				var text='';
				if(i>0){
					text=''+i+' '+name+'(s) selected'
				}else{
					text='No selection'
				}
				d.prev('div.multiselect_input').text(text);
			}
		});
	};
	
	// Dom ready
	$(function(){
		
		if(article!=''){
			var articleIndex=$('.accordion>.title').index($('.accordion>.title[href*='+article+']'));
			articleIndex=(articleIndex==-1)?0:articleIndex;
		}
		$('.accordion').accordion({active:articleIndex,autoHeight:false,collapsible: true,animated:false}).bind('accordionchange',function(){
			if ($('.accordion>.title.ui-state-active').length==1&&$('.accordion>.title.ui-state-active').position().top<0) {
				$('.accordion').scrollTo($('.accordion>.title.ui-state-active'),{duration:1000});
			};
		});
		if (articleIndex>0) {
			$('.accordion').scrollTo($('.accordion>.title.ui-state-active'),{duration:1000});
		};
		//  Presistent visted items by cookie
		$('.accordion>.title').each(function() {
			if($(this).hasClass('ui-state-active')){
				$(this).addClass('visited');
				$.cookies.set($(this).attr('href'),'visited');
			}
			if($.cookies.get($(this).attr('href'))=='visited'){
				$(this).addClass('visited');
			}
			$(this).click(function() {
				$(this).addClass('visited');
				$.cookies.set($(this).attr('href'),'visited');
				return false;
			});
		});

		// Fix the height of list view
		function fixHeight(){
			$('.auto_height').each(function() {
				var height=$(window).height()-$(this).offset().top-$('#pagination').innerHeight()-$('.btm_info').innerHeight()-parseInt($(this).css('paddingTop'))-parseInt($(this).css('paddingBottom'));
				height=height>205?height:205;
				$(this).height(height);
			});
			// Fix the height of sidebar
			height=$(window).height()-$('#sidebar').offset().top;
			if($('#sidebar').height()>height){
				$('#sidebar').height(height);
			}
			else{
				$('#sidebar').height('auto');
			}
		}
		
		fixHeight();
		
		$(window).resize(function() {
			fixHeight();
		});

		// multiSelect
		$('div.multiselect').multiSelect();

		// Frontpage popup
		$('h3.i-title').click(function(){
			$('body>.popup-wrapper').remove();
			var href=$(this).next('div.i-text').children('a.button').attr('href');
			$(this).next('div.i-text').clone().removeClass('i-text').removeClass('box').addClass('popup').prepend('<h3>'+$(this).text()+'</h3>').wrap('<div class="popup-wrapper"></div>').parent().append('<div class="point_right"></div><div class="close_button"></div>').css({left:$(this).offset().left-170,top:$(this).offset().top-100}).appendTo('body');
			$('.popup-wrapper .close_button').click(function() {
				$('body>.popup-wrapper').remove();
			});
		});

	});
})
(jQuery);
