$(document).ready(function(){
	// initialise the tree on the ul class=collapse
	// this can be put on anything really, and can be called whatever
	//add one for each list
//	$('ul.collapse').quickTree();
	
	// showall/hideall buttons
	$('a.showall').click(function() {
		$('ul ul').show();
		$('.expand').addClass('contract');
	});
	
	if ($('.tools:first-of-type h4').length) {
		$('title').prepend($('.tools:first-of-type h4').text()+' - ');
	}
	
	
	$('a.hideall').click(function() {
		$('ul ul').hide();
		$('.expand').removeClass('contract');
	});
	
	// hide and show the contentleft navigation
	$('#contentleft-hider a').toggle(function(){
			$('#contentleft').hide('slow');	
			$('#contentleft-hider a img').attr("src","images/rarr.png");
			$('#contentleft-hider a').attr("title","Show Navigation");
			setTimeout(function() { $('#contentright').animate({width:"99%"}, 'fast'); }, 500);
		},function() {
			$('#contentleft').show('slow');	
			$('#contentleft-hider a img').attr("src","images/larr.png");
			$('#contentleft-hider a').attr("title","Hide Navigation");
			$('#contentright').animate({width:"79.3%"}, 'fast'); 
		}
	);
	
	//ie specific workarounds (pseudo-selector class names)
	if ($.browser.msie) {
		$('input[type="submit"]').addClass('submit');
		$('input[type="text"]').addClass('text');
		$('input[type="hidden"]').addClass('hidden');
		$(':last-child').addClass('last-child');
		$(':first-child').addClass('first-child');
   	}

	//sidebar links
	$('#addsidebarlinkTitle').example('Link Title');
	$('#addsidebarlinkURL').example('Link URL');
	$('.addsidebarlink').click(function(){
		$(this).hide();
		$('.addsidebarlinkform').show();
	});
	$('.addsidebarlinkform input[value="Cancel"]').click(function(){
		$('.addsidebarlinkform').hide();
		$('.addsidebarlink').show();
		return false;
	});

	$('.edistidebarlinks').toggle(function(){
		$(this).css('font-weight','bold');
		$(this).css('color', '#060');
		$(this).text('save');
		$('.sidebarlinks li:not(.addsidebarlink, .addsidebarlinkform) a').each(function(){
			thisHREF = $(this).attr('href');
			thisTitle = $(this).text();
			$(this).parent().html('<a href="#" title="Add Link"><form action=""><input type="text" id="addsidebarlinkURL" value="'+thisHREF+'"/><input type="text" id="addsidebarlinkTitle" value="'+thisTitle+'" /><input type="submit" value="Remove" class="button redbutton" /><input type="submit" value="Update" class="button greenbutton" /></form></a>');
		});
	}, function(){
		$(this).css('font-weight','normal');
		$(this).css('color', '#23374f');
		$(this).text('edit');
		$('.sidebarlinks li:not(.addsidebarlinkform, .addsidebarlink) a').each(function(){
			newHREF = $('#addsidebarlinkURL').val();
			newTitle = $('#addsidebarlinkTitle').val();
			$(this).parent().html('<a href="'+newHREF+'" >'+newTitle+'</a>');
		});
	});


	//edit in place 
	/*$('table tr td.editinplace a').click(function(){
		if ($(this).text() === 'Save') {
			$(this).parents('tr').children('td:not(.edit, .delete,)').each(function(){
				thisVal = $(this).children('input').val();
				$(this).html(thisVal);
				//AJAX majiggers will go here
			});
			$(this).text('Edit');
		} else {
			$(this).parents('tr').children('td:not(.edit, .delete,)').each(function(){
				$(this).html('<input type="text" value="'+$(this).text()+'" id="'+$(this).attr('class')+'" />');
			});
			$(this).text('Save');
		}
	});*/
	
	//add in place 
	/*$('table tr td.addinplace a').click(function(){
		$(this).parents('tr').children('td:not(.edit, .delete,)').each(function(){
			thisVal = $(this).children('input').val();
			$(this).html(thisVal);
		
			//AJAX majiggers will go here
		});
		$(this).text('Edit');
		$(this).parent().removeClass('addinplace');
		$(this).parent().addClass('editinplace');
	});*/

	//help screens
	$('.helplink').toggle(function(){ 
		$('.helpcontainer').slideDown('slow');
		$(this).children('a').css('color','#fff');
	}, function() {
		$('.helpcontainer').slideUp('slow');
		$(this).children('a').css('color','#23374f');
	});
	
	$('.helpclose').click(function(){
		$('.helpcontainer').slideUp('slow');
		$('.helplink').children('a').css('color','#23374f');
	});


	// Filter tables //
	
	//convert table to be filterable
	$("table tr:has(td)").each(function(){
		var t = $(this).text().toLowerCase(); //all row text
		$("<td class='indexColumn'></td>")
		.hide().text(t).appendTo(this);
	});//each tr
	
	//whilst typing
	$('input[name="FilterTextBox"]').keyup(function(){
		if ($(this).attr('class')) {
			tableToFilter = '#'+$(this).attr('class');
			//console.log(tableToFilter);
		} else {
			tableToFilter = '';
			//console.log('No table id');
		}
		var s = $(this).val().toLowerCase().split(" ");
		//show all rows.
		$("table"+tableToFilter+" tr:hidden").show();
		$.each(s, function(){
			$("table"+tableToFilter+" tr:visible .indexColumn:not(:contains('"+ this + "'))").parent().hide();
		});//each
	});//key up.

	// clear filter
	$("a#clearfilter").click(function() {
		$("#FilterTextBox").val(" ");	
		$("table tr:hidden").show();
		return false;
	});
	
	// sort table columns
    $("table").tablesorter(); 

	//hide success messages
	setTimeout(function() { $('.ok').hide('slow'); }, 5000);
	
	
	//date picker
	if ($('.date').length>0){
		$('.date').datePicker({clickInput:true});
	}
	
	//input masks
	$(".currency").maskMoney();

	//$("#portal-dashboard .module-large").sortable({
	//});
	
	
	// so it knows what page it's on (based on URL and href contents of the link - likely to fail)
	// get the current page
	//thisPage = location.href.substring((location.href.lastIndexOf("/"))+1); 
	//$('li a[href="'+thisPage+'"]').each(function() {
		// go to the parent of the list item that has a link to the current page we're on
		// and display it, make it bold, make it colourful, and change the + to a -
	//	$(this).parents('ul').css('display','block');
		//$(this).('font-weight','bold');
		//$(this).css('color','#ea6e23');
	//	$(this).addClass('current');
	//	$(this).siblings('.expand').addClass('contract');
	//});
	
	$('#applyRowLimitButton').click(function(){
		$(this).parent('form').submit();
		return false;
	});	
	
	
	//Send Login Details Tester Thing
	
	$('a.button.email_login').click(function(){
		alert($(this).attr("id"));
		return false;
	});
});


