$(document).ready(function(){
	// 1 Share
	
	
	// Get pages url
	sh_url = window.location;
	// Get pages title
	sh_title = document.title;
	// Load our sharing html into the page
	// we do this so that search engines don't index
	// the share links as they will appear in a lot
	// of pages and might interfer with our keywords
	// and serps
	$("#share").hide();
	basepath = "http://www.balancerecruitment.com/";
	
	$("#share").load(basepath+"share",{ 'data[]': [sh_url, sh_title] },after_loaded);
	
	// If we wanted to do something anything after the
	// data has loaded we can add it here
	function after_loaded(){
	
		
		$("#share").slideDown('slow');
		
		 $("a#shorten").click(function(event){
			event.preventDefault();
			
			// Show parsing message
			$("a#shorten").parent().parent().html('One moment please...');
			
					
  			$.post(basepath+"maketiny", { sh_url: " "+sh_url },
			  function(data){
			  	
			  	
			  	// Tiny url returned from api via php - I know, JSON formatted from api was not liked by codeigniter or jquery
			  	// so load it via php through xml and the formatted manually into clean JSON format, a totally bitch!
			    tiny_url = data.tinyurl;
			    
			    // Buid Tweet
				tweet = 'http://twitter.com/home?status=';
				
				// Shorten title for twitter
				sh_title_split = sh_title.split(" - Balance Recruitment",1);
				
				
				
				tweet += sh_title_split[0]+', '+tiny_url;
				
				// Url encode
				tweet += escape(' #job #careers #hiring');
			    
			    // TO test JSON returned
			    // alert(tweet);
			    
			    // Send to twitter
			    window.location = tweet; 
			    
			  }, "json");	        
        
	      }
	 	);
	 	
	 	      
	}
	
	// 2 Lists
	
		$("#show_closed").click(
			function(event){
				event.preventDefault();
					//
					$("#show_closed").hide();
					$("#hide_closed").fadeIn('slow');
					
					$("table.my_list").find('tr.hide_item').fadeIn('slow');
					$("table.my_list").find('tr.hide_after_u_id').fadeIn('slow');
			}
		);	
		
		
	$("#hide_closed").click(
			function(event){
				event.preventDefault();
					//
					$("#hide_closed").hide();
					$("#show_closed").fadeIn('slow');
					
					$("table.my_list").find('tr.hide_item').fadeOut('slow');
					$("table.my_list").find('tr.hide_after_u_id').fadeOut('slow');
					
			}
		);
		
	// Hide ;
	$(".hide_after_u_id").fadeOut('slow');
	
	
	// 3. Jobs		
	$("#jobs dt").hover(
			function(){
				// Fix the size of the DL
				fh = $(this).parent().parent().height();
				$(this).parent().parent().css({'height' : fh, 'overflow' : 'hidden'});
				
				// close
				$(this).parent().find('dd').css({'position' : 'absolute', 'left' : '-999em'});
				
				// open
				$(this).next().css({'position' : 'relative', 'left' : 'auto'}).hide().fadeIn('slow');
	
			}
		);	
	
	// 4 Tabs
	$("#tablist a").click(
		function(event){
			event.preventDefault();
			
			// get the <li>
    		var li = $(this).parent();
    		// get the <li> array position
    		var index = $("#tablist li").index(li);
    		
					
			// Change tab styles
			$('#tablist a').removeClass('current');
			$(this).addClass('current').hide().fadeIn('slow');
			// Hide current content
			$('#tabcontent .current').hide();
			$('#tabcontent .current').removeClass('current');
			// As each div has an additional two divs inside we need to add 2 for every nth of the index
			// to make sure we get the right div, not super clean but good ennough
			var divindex = index + (index * 2);
			// Show selected content
			$('#tabcontent div:eq('+divindex+')').addClass('current').css({'position' : 'relative', 'left' : 'auto'}).hide().fadeIn('slow');			
		}
	);	
});