var url;

function get_el(id)
{
	if (document.getElementById)
	{
		var el=document.getElementById(id);
	}
	else if (document.all)
	{
		var el=document.all[id];
	}
	return el;
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

$.fn.check = function(mode) {
	var mode = mode || 'toggle'; // if mode is undefined, use 'toggle' as default
	return this.each(function() {
		switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
		}
	});
};

function enableSubs(){
	$('#closesubs').attr('disabled','');
}

$.fn.optAdd=function(name){
	this.prev('.'+name+':last').clone().insertBefore(this);
};


$(document).ready(function(){
	//$('#nav > ul >li:first > a').each(function(){url=$(this).attr('href');});
	url='';
	$('.noscript').hide();
	
	$.blockUI.defaults.css.cursor='';
	$('.datepicker').datePicker({startDate:'01/10/2006'});
	
	$(".timepicker").clockpick({
		starthour : 9,
		endhour : 22,
		layout : 'horizontal'
	});
	
	$('#eventselect').change(function(){
		$('.eventform').hide();
		$('#'+$(this).val()).slideDown("slow");
    }).change();
	
	$('.tableadd').click(function(event){
		$(this).optAdd('tablesize');
		event.preventDefault();
	});
	
	$('.optadd').click(function(event){
		$(this).optAdd('opt');
		event.preventDefault();
	});
	
	$('.goptadd').click(function(event){
		$(this).optAdd('gopt');
		event.preventDefault();
	});
	
	$('.goptionadd').click(function(event){
		var count=$('.goption:last').attr('id');
		count++;
		$('.goption:last').clone().insertAfter($('.goption:last'))
		.find('.goptadd').click(function(event){
			$(this).optAdd('gopt');
			event.preventDefault();
		}).end()
		.find('.gopt > input').attr('name','gopt['+count+'][]').end()
		.attr('id',count);
		$('.goption:last').find("input[name='gopt']").hide();
		event.preventDefault();
	});
	
	$("input[name='option']").change(function(){
		if ($(this).val()!='') $('#opt').fadeIn();
		else $('#opt').hide();
	}).change();
	
	$("input[name='goption']").change(function(){
		if ($(this).val()!='') $('#gopt').fadeIn();
		else $('#gopt').hide();
	}).change();
	
	$('input.name').change(function(){
		if ($(this).val()!='') $(this).removeClass('bg');
		else $(this).addClass('bg');
	}).change();
	
	$('input.diet').change(function(){
		if ($(this).val()!='') $(this).removeClass('bg');
		else $(this).addClass('bg');
	}).change();
	
	$('#emailselect').change(function(){
		$('.emailform').hide();
		switch ($(this).val())
		{
			case '2':
				$('#groupboxes').fadeIn();
				$('#yearboxes').fadeIn();
			case '0':
				$('#groupboxes').fadeIn();
		}
    }).change();
	
	$('.link a').addClass('nolink');
	
	$('.link').each(function(){
		var url=$(this).find('a').attr('href');
		if (url.length>9) $(this).click(function(){
			top.location=url;
		}).addClass('pointer');
	});
	
	$('.check').click(function(){
		var id=$(this).attr('id').replace('j_','');
		$('#'+id).check();
	});
	
	$('.slide').click(function(){
		var id=$(this).attr('id').replace('j_','');
		$('#'+id).slideToggle();
		$(this).text($(this).text().replace('Hide','Show'));
	});
	
	$('.sortable').sortable({
		connectWith: ['.sortable']
	});
	
	$(".person input[name='name[]']").attr('autocomplete','off');
	$(".person input[name='name']").attr('autocomplete','off');
	
	$('#subs input.submit').click(function(event){
		if (incomplete==1)
		{
			alert('You haven\'t selected a person from one of the drop down lists. You must click the name to add them to your submission.');
			event.preventDefault();
		}
	}).end();
	
	$('#closesubs').attr('disabled','disabled');
	
	$('#substime').countdown({
		until: '+'+$('#substime').attr('rel'),
		onExpiry: enableSubs
	});
	
	$('#newsevent').change(function(){
		top.location='admin_news.php?event='+$(this).val();
	});
	
	$('#submitted div.subs').draggable({
	    helper: "clone"
	});

	$('#mot').hide();

	$('#votetype').change(function(){
		$('.vtype').hide();
		$('#'+$(this).val()).fadeIn();
	}).change();
	
	$('input.submit.news').click(function(e){
		if (!confirm('Before sending this email out to college are you sure that:\n\n- All dates are present and correct?\n- Any prices are included and correct?\n- You\'ve not forgotten to say anything?\n\nClick OK to email the college, click Cancel to correct your email.')) e.preventDefault();
	});

});