$(document).ready(function() {
	
	// main navigation dropdowns
  		
  		// give li a .over class for IE
		$("div#header ul li").hover( function() {
    		$(this).addClass("over");
  		}, function () {
    		$(this).removeClass("over");
  		});
  		
  		
	// IHS accordion script
		
		$("div.accordion div.inner").hide(); // hide all
		$("div.accordion h2").addClass("clickable"); // faux h2 clickable
		
		// reveal info
		$("div.accordion h2").click( function() {
			$("div.accordion div.inner").slideUp("slow"); // hide all
  			$("div.accordion h2").removeClass("click-active"); // remove all active classes
  			
  			// if current clicked text area is hidden... show it
  			if ($(this).next().is(":hidden")) {
  				$(this).next().slideDown("slow"); // show clicked	
  				$(this).addClass("click-active"); // set active class
  			}
		});
		
		// hovers
		$("div.accordion h2").hover( function () {
    		$(this).addClass("accordion-hover");
  		}, function () {
    		$(this).removeClass("accordion-hover");
  		});
  		
  	// Last element classes...
  		
  		$("div.news-item:last").addClass("last-news");
  		$("div.link-info:last").addClass("last-link");
  		
  	// Alternate our team profiles...
  		
  		$("div.team-info").each( function() {
  			$("div.inner div.team-member:odd", this).addClass("alt");
  		});
  		
  	// add read more link to home news excerpts...
  		
  		$("div.excerpt").each( function() {
  			$("p:last", this).append('... <a href="/news/" title="Read more">Read more</a>');
  		});
  		
  	// Twitter footer

		// pull JSON data from twitter API 
		$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=ourdoctors&count=3&callback=?", function(data){
		
			var showTweets = ""; // setup tweet list
			
			// cycle through tweets
			$.each(data, function(i,item){
				
				// grab the tweet text
				var tweetContent = item.text;
				var tweetTime = item.created_at;
				
				// convert URLs to clickable
				tweetContent = tweetContent.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "<a href=\"$1\" title=\"$1\" target=\"_blank\">$1</a>");
				// convert twitter @s too
				tweetContent = tweetContent.replace(/(^|\s)@(\w+)/g, "$1@<a href=\"http://www.twitter.com/$2\" title=\"@$2\" target=\"_blank\">$2</a>");
				
				// apend tweet to list
   				showTweets += "<div class=\"tweet\"><h4>" + parseTwitterDate(tweetTime) + "</h4><p>" + tweetContent + "</p></div>";
   				
			});
			
			// if we have tweets... show them
			if(showTweets != "") { $("#inner-tweets").html(showTweets); }
		});
		
		// format 'created_at' - http://www.quietless.com/kitchen/format-twitter-created_at-date-with-javascript/
		function parseTwitterDate($stamp) {			
			var date = new Date(Date.parse($stamp)).toLocaleString().substr(0, 16);
			var hour = date.substr(-5, 2);
			var ampm = hour<12 ? ' AM' : ' PM';
				if (hour>12) hour-= 12;
				if (hour==0) hour = 12;
				
			return date.substr(0, 11)+' - ' + hour + date.substr(13) + ampm;
		}
	
});

$(window).load(function() {
	
	// grab anchor if available
	
	/*
	if (window.location.hash) {
		var hash_value = window.location.hash.replace("#", ""); //set the value and remove the #
		
		$("div.accordion div.inner").hide(); // hide all
		$("div#"+ hash_value +" div.inner").show(); // show current hash div
		$("div#"+ hash_value +" h2").addClass("click-active"); // give current hash active class
	
	} else {
		$("div.accordion div.inner:not(:first)").hide(); // hide all except first
		$("div.accordion:first h2").addClass("click-active"); // give first active class
	}
	*/
			
});
