$(document).ready(function(){
	//sidebar stuff
	$('#sidebaradvsearch').hide();
	$('.moretoggle a').toggle(function(){
			$('#sidebaradvsearch').slideDown('slow'); 
			$(this).html("&#x25BC; hide options");
		},function() {
			$('#sidebaradvsearch').slideUp('slow');
			$(this).html("&#x25BA; show options");
		}
	);
	
	$('#sidebarsearch .sidebaradv-morelink').click(function() {
		$(this).remove();
		$('.sidebaradv-morecontainer').slideDown('slow');
		return false;
	});
	
	$('#show_searchfilter').click(function() {
		$('.searchfilter').slideDown('slow');
		return false;
	});
	$('#people_close').click(function() {
		$('.searchfilter').slideUp('slow');
		return false;
	})
	//ie specific workarounds (pseudo-selector class names)
	if ($.browser.msie) {
		$('input[type="submit"]').addClass('submit');
		$('input[type="text"]').addClass('text');
		$(':last-child').addClass('last-child');
		$(':first-child').addClass('first-child');
		//new pagenav hovers
		$('ul#newpagenav li').hover(function() {
			$(this).addClass('over')
		}, function() {
			$(this).removeClass('over');
		});
		$('#sidebarnav li').hover(function() {
			$(this).addClass('over')
		}, function() {
			$(this).removeClass('over');
		});
		$('#sidebaradvsearch input[value="Search Publications"]').addClass('searchpubs');
   	}

	$('.sidebarquicktext').example('Service, name, etc');
	$('.rhs-newslettertext').example('email@domain.com');
	$('#keywords, #pub_search_keywords').example('Name or keyword');
	
	
	//EVENT TRACKING
	$('.profile_phone .phnum').click(function() {
		//phone numbers - profile pages
		$(this).text($(this).attr('ph'));
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/phone-query']);				
	});
	
	$('.articlephone .phnum').click(function() {
		//phone numbers - article pages
		$(this).text($(this).attr('ph'));
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/phone-query/'+$(this).parent().parent().children('.articleauthor').children().children('a').text()]);	
		//console.log('/event/phone-query/'+$(this).parent().parent().children('.articleauthor').children().children('a').text());	
		//$(this).parent().parent().children('.articleauthor').children().children('a').text('logged');		
	});

	
	$('.profile_email a').click(function() {
		//emails - profile pages
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/email-query']);				
	});
	
	$('.articleemail a').click(function() {
		//emails - article pages
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/email-query/'+$(this).parent().parent().children('.articleauthor').children().children('a').text()]);
		//console.log('/event/email-query/'+$(this).parent().parent().children('.articleauthor').children().children('a').text());
	});

	
	$('.profile_download a').click(function() {
		//cv download - profile pages
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/cv-download']);				
	});
	
	$('.profile_vcard a').click(function() {
		//vcard download - profile pages
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/vcard-download']);				
	});
	
	$('.tools_download a').click(function() {
		//article download - article pages
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/article-download']);				
	});

    
	$('.tools_tweet a').click(function() {
		//article download - article tweet
		_gaq.push(['_trackPageview', location.href.replace('http://www.davies.com.au/','')+'/event/article-tweet']);				
	});


	//homepage banners 
	//wheelOfFortune = Math.floor(Math.random()*4);
	//wheelOfFortune = wheelOfFortune + 1;
	//$('.homepagebanner').html('<img src="images/banner-home-'+wheelOfFortune+'.png" alt="Davies Collison Cave" />');

    //homepagebanner 
	$('#pagebanner ul').cycle({  
		fx: 'fade',
		speed: 1000,
		timeout: 6000
  	});

	//Navigation location aware thinger (make class='current')
	thisNewPage = $("#pagecontent").attr("class").replace('last-child','');
	if (thisNewPage != "") {
		$("#topnav").children("li#"+thisNewPage+"").addClass("current").children('a').attr('title','foo');
		$("#sidebarnav ul li#"+thisNewPage+"").each(function() {
			$(this).addClass('current');
		});		
	}
	
	if ($("#pagecontent").attr("class") === 'index') {
		$('body').addClass('home');
	}
	
	thisURL = location.href.replace('http://www.davies.com.au/','');
	//$('#pagecontent').addClass(thisURL);
	$('#newpagenav li a[href="'+thisURL+'"]').addClass('current');
	
	//$('#searchbox input').bind('keypress', function(e) {});
/*	$('#keywords').keypress(function(e) {
		 var code = (e.keyCode ? e.keyCode : e.which);
		 if(code == 13) { //Enter keycode
			 publicationUrl();
		 }	
		
	});*/
	$('#pub_search_submit').click(function() {
		publicationUrl();
	});

	$('#publication_reset').click(function() {
		$('#surname li').removeClass();
		$('#surname_letter').val(''); 
		$('#first_name_letter').val(''); 
		$("#year").val('');
		$("#id_service").val('');
		$("#id_type").val('');
		$("#id_skill").val('');
		$("#keywords").val('Name or keyword');
		$("#keywords").addClass('example');
		return false;
	});	
	$('#people_search_submit').click(function() {
		peopleUrl();
	});
	$('#people_reset').click(function() {
		//document.people_search.reset();
		$('#first_name li').removeClass();
		$('#surname li').removeClass();
		$('#surname_letter').val(''); 
		$('#first_name_letter').val(''); 
		
		$("#id_location").val('');
		$("#id_position").val('');
		$("#id_service").val('');
		$("#id_speciality").val('');
		$("#keywords").val('Name or keyword');
		$("#keywords").addClass('example');
		return false;
	});	
	});	
function peopleUrl(){
	var url="staff";
	var arr=new Array();
	//$("#people_search > :input").each(function() {
		var people_search =$("#people_search").val();
		
		var first_name_letter =$("#first_name_letter").val();
		if (first_name_letter){
			url=url+"/first_name/"+first_name_letter.replace("first_name_","");
		}
		
		var surname_letter =$("#surname_letter").val();
		if (surname_letter){
			url=url+"/surname/"+surname_letter.replace("surname_","");
		}
		var id_location =$("#id_location").val();
		if (id_location){
			url=url+"/location/"+id_location;
		}			
		var id_position =$("#id_position").val();
		if (id_position){
			url=url+"/position/"+id_position;
		}			
		var id_service =$("#id_service").val();
		if (id_service){
			url=url+"/service/"+id_service;
		}			
		var id_speciality =$("#id_speciality").val();
		if (id_speciality){
			url=url+"/speciality/"+id_speciality;
		}			
		var keywords =$("#keywords").val();	
		keywords=keywords.replace("Name or keyword","");
		if (keywords!="" && keywords!="undefined"){
			url=url+"/keywords/"+keywords;
		}				
		window.location="http://www.davies.com.au/"+url;
		return false;
}
function publicationUrl(){
	var url="pub";
	var surname_letter =$("#surname_letter").val();
	if (surname_letter){
		url=url+"/surname/"+surname_letter.replace("surname_","");;
	}		
	var year =$("#year").val();
	if (year){
		url=url+"/year/"+year;
	}		
	var id_service =$("#id_service").val();
	if (id_service){
		url=url+"/service/"+id_service;
	}	
	var id_type = $("#id_type").val();
	if (id_type){
		url=url+"/type/"+id_type;
	}		
	var id_skill = $("#id_skill").val();
	if (id_skill){
		url=url+"/skill/"+id_skill;
	}
	var keywords =$("#keywords").val();			
	keywords=keywords.replace("Name or keyword","");
	if (keywords!="" && keywords!="undefined"){
		url=url+"/keywords/"+keywords;
	}				
	window.location="http://www.davies.com.au/"+url;
	//window.location=url;
	return false;		
}

