$(document).ready(function(){
		
		
	
	
	/* 
		Submit button replacement
	*/
	$("input[type='submit']").each(function(){
		$(this).wrap('<div class="submit" />');	
		if ($(this).hasClass('mini')) $(this).parent().addClass('mini');
		
		$(this).parent().click(function(){ 
			
			$(this).parents('form').submit();  //live
		});
		$(this).parent().html('<span>'+$(this).val()+'</span>');		
	});	
	
	$('form .submit').live('mousedown mouseup',function(){ $(this).toggleClass('active'); });
	
	/* 
		Select replacement
	*/
	
	$("select").each(function(){
		if ($(this).attr('class')!="undefined" && $(this).attr('class')!="")
			$(this).wrap('<div class="select '+$(this).attr('class')+'" />');			
		else
			$(this).wrap('<div class="select" />');
			
		$(this).parent().append('<div class="input-bg">'+$(this).find('option:selected').text()+'</div>');
		$(this).parent().append('<ul />');
		$(this).find('option').each(function(){
			$(this).parents('.select').find('ul').append('<li>'+$(this).text()+'</li>')
		});
		$(this).parent().append('<div class="btn" />');		
		$(this).hide();		
	});
	
	$('.select .btn, .select .input-bg, .select li').live('click',function(){
		var parent = $(this).parents('.select');
		
		//Adjust the width of the ul
		parent.find('ul').width(parent.width()-2);
		
		//Toggle slide and class
		parent.find('ul').slideToggle(100);
		parent.find('.btn').toggleClass('on');
		
		//On opening of the list, set the offset to the selected item
		var selected_offset = parent.find('li:contains('+parent.find('.input-bg').text()+')').position();	
		parent.find('ul').scrollTop(selected_offset.top);
		
		
		
	});
	
	$('.select li').live('mouseover',function(){
		$(this).addClass('hover');
	}).live('mouseout',function(){
		$(this).removeClass('hover');
	});
	
	$('.select li').live('click',function(){
		$(this).parents('.select').find('.input-bg').text($(this).text());
		$(this).parents('.select').find('option').removeAttr('selected');
		$(this).parents('.select').find('option:contains('+$(this).text()+')').attr('selected','selected');
		$(this).parents('.select').find('select').trigger('change');
	});
	
	
	/* 
		Checkbox replacement
	*/
	$("input[type='checkbox']").each(function(){
		$(this).wrap('<div class="checkbox" />');
		//Copy checked state
		if ($(this).attr('checked')=="checked") $(this).parent().addClass('on');
		var checkbox = $(this);
		$(this).parent().click(function(){
			$(this).toggleClass('on');
			checkbox.attr('checked',!checkbox.attr('checked'));
		});
		$(this).hide();
		
	});
	
	
	/* 
		Search Form 
	*/
	var faded = false;
	
	
	//Selecting of a search parameter
	$('#search .field select').live('change',function(){
		
		var parent = $(this).parents('.parameter');		
		var value = $(this).find('option:selected').val();
		parent.find('.value').fadeOut(250,function(){
			$(this).html($('#prepared .'+value).html());			
			$(this).fadeIn(250);
		});
		
		
	});
	
	//Adding a new search parameter
	$('#search .add').click(function(){
		var table = $(this).parents('.table');
		//Add new line
		$(this).parent().before('<div class="parameter hidden"><div class="control remove"></div><div class="field">'+$('#prepared .type').html()+'</div><div class="value">'+$('#prepared .keyword').html()+'</div><div class="clear"></div></div>');
		table.find('.parameter.hidden').slideDown(250,function(){ $(this).removeClass('hidden'); });
	});
	
	//Removing a search parameter
	$('#search .remove').live('click',function(){		
		$(this).parent().slideUp(250,function(){
			$(this).remove();
		});
	});
	
	//The whole .option div will be binded for click instead
	$('#search .publications .option .checkbox').unbind('click');	
	
	//Onload if "all" is checked
	$('#search .publications .option .checkbox').not('.all .checkbox').each(function(){
		if ($('.option.all .checkbox').hasClass('on')){
			$(this).find('input').attr('checked','checked');
			$(this).addClass('on');
			$(this).parents('.option').css({opacity:0.5});
			faded = true;
		}
	});
	
	//Handle selecting a publication on search page
	$('#search .publications .option').click(function(){	
		
		//Toggle checked
		var checkbox = $(this).find('input');
		$(this).find('.checkbox').toggleClass('on');
		checkbox.attr('checked',!checkbox.attr('checked'));
		
		
		//Special cases:		
		//Clicked on "all" to turn on OR all three are selected
		if ($(this).hasClass('all') && !faded || $(this).parent().find('.option').not('.all').find('input:checked').size() == 3 && !faded){				
			//Fade out options except "all"
			$(this).parent().find('.option').not('.all').fadeTo(250,0.5);
			//Turn every checkbox on
			$(this).parent().find('.option').find('.checkbox').addClass('on');
			$(this).parent().find('.option input').attr('checked','checked');
			faded = true;
		} 
		//Clicked on "all" to turn off
		else if ($(this).hasClass('all') && faded){
			$(this).siblings('.option').fadeTo(250,1).find('.checkbox').removeClass('on');
			$(this).siblings('.option').find('input').removeAttr('checked');
			faded = false;
		}
		//Clicked on others while "all" is selected
		else if (faded){
			$(this).parent().find('.option').fadeTo(250,1);
			$(this).find('.checkbox').addClass('on');
			$(this).find('input').attr('checked','checked');
			$(this).siblings('.option').find('.checkbox').removeClass('on');
			$(this).siblings('.option').find('input').removeAttr('checked');
			faded = false;
		}
		
		
	});
	

	$("#search select[name='sortby']").change(function(){
		if ($('.search-results ul li').size()>0)
			$(this).parents('form').submit();
		
	});
	
	$('#topBar .search select').change(function(){
		$(this).parents('.search').find('input[type="text"]').attr('name',$(this).val()+"[]");
	});

	

});


function getQueryVariable(variable) { 
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++) { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable) { 
      return pair[1]; 
    } 
  } 
}
