jQuery(document).ready(function($) {
	$("iframe[src=about:blank]").css("display","none");
	$('input[type=text]').addClass('input');
	$('textarea').addClass('textarea');
	$.fn.tweetify = function() {
	        this.each(function() {
	                $(this).html(
	                        $(this).html()
	                                .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
	                                .replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
	                                .replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
	                );
	        });
	        return $(this);
	}
	$('.article:last-child').addClass('last');
	//ie align fix
	$('img[align="right"]').addClass('right');
	$('img[align="left"]').addClass('left');
	
	  $('#article-rotate-wrap').cycle({
  		fx: 'scrollRight',
  		easing:  'easeOutBounce',
  		pager:  '#articles-nav',
  		timeout: '8000',
  		pause: 1
  	});
	
	//get latest tweet with date
	$.getJSON("http://twitter.com/statuses/user_timeline/forefront.json?callback=?", function(data) {
	    $("#twitter").html(data[0].text).tweetify();
		var twitter_data = $("#twitter").html();
		var short_twitter_date = twitter_data.substring(0, 140) + "...";
		$("#twitter").html(short_twitter_date);
		/*var date_tweet = new Date(data[0].created_at);
            var date_now   = new Date();
            var date_diff  = date_now - date_tweet;
            var hours      = Math.round(date_diff/(1000*60*60));
		if(hours >= 24) {
			hours = Math.floor(hours/24);
			$('#twitter-time').html(hours + " days ago.");
		}else {
			$('#twitter-time').html(hours + " hours ago.");	
		}*/
	});
	
	var fbObj = 'http://graph.facebook.com/ForeFrontWeb/feed&callback=?'; 
	/*function formatFBTime(fbDate){
	    var arrDateTime = fbDate.split("T"); 
	    var strTimeCode = arrDateTime[1].substring(0,  arrDateTime[1].indexOf("+")); 
	    var valid_date = new Date(arrDateTime[0])
	    var arrTimeCode = strTimeCode.split(":"); 
	    valid_date.setHours(arrTimeCode[0]);     
	    valid_date.setMinutes(arrTimeCode[1])
	    valid_date.setSeconds(arrTimeCode[2])
	    return valid_date;
	}*/
	$.getJSON(fbObj, function(json) { 
		$('#facebook').html(json.data[0].message).tweetify();
		$('#facebook a').each(function() {
			var text = $(this).text();
			var short_text = text.substring(0, 20) + "...";
			$(this).text(short_text);
		});
		/*var time = formatFBTime(json.data[0].created_time);
		var date_face = new Date(time);
            var date_now   = new Date();
            var date_diff  = date_now - date_face;
            var hours      = Math.round(date_diff/(1000*60*60));
		if(hours >= 24) {
			hours = Math.floor(hours/24);
			$('#facebook-time').html(hours + " days ago.");
		}else {
			$('#facebook-time').html(hours + " hours ago.");	
		}*/
	});
	$.getJSON("http://forefrontweb.com/blog/feed/?feed=json&jsonp=?", function(data){
	  var wp_title = data[0].title;
	  var short_wp_title = wp_title.substring(0, 35) + "...";
		var html = "<a href='" + data[0].permalink +"'>" + short_wp_title + "</a>";
		var wp_content = data[0].excerpt;
		var short_wp_content = wp_content.substring(0, 45) + "...";
		$('#wordpress').html(html); 
		$('#wordpress-content').html(short_wp_content)
		/*var date_word = new Date(data[0].date);
            var date_now   = new Date();
            var date_diff  = date_now - date_word;
            var hours      = Math.round(date_diff/(1000*60*60));
		if(hours >= 24) {
			hours = Math.floor(hours/24);
			$('#wordpress-time').html(hours + " days ago.");
		}else {
			$('#wordpress-time').html(hours + " hours ago.");	
		}*/
     	});

	//birds stuff show on hovers
	/*
	$('#bird-showcase').hover(
	function(){
		$('#bird-showcase-popup').fadeIn().stop(true,true);
	},
	function(){
		$('#bird-showcase-popup').delay(1000).fadeOut();
	});
	$('#bird-header').hover(
	function(){
		$('#bird-header-popup').fadeIn().stop(true,true);
	},
	function(){
		$('#bird-header-popup').delay(1000).fadeOut();
	});
	$('#bird-content').hover(
	function(){
		$('#bird-content-popup').fadeIn().stop(true,true);
	},
	function(){
		$('#bird-content-popup').delay(1000).fadeOut();
	});
	$('#bird-footer').hover(
	function(){
		$('#bird-footer-popup').fadeIn().stop(true,true);
	},
	function(){
		$('#bird-footer-popup').delay(1000).fadeOut();
	});
	//bird modules popups
	$('#bird-footer').click(function(){
		if( $('.module-footer').css("display") == "none" ){
			$('#bird-footer-popup').hide();
			$('#mod-overlay').fadeIn();
			$('.module-footer').fadeIn();
			
		}else {
			$('.module-footer').fadeOut();
			$('#mod-overlay').fadeOut();
		}
		
	});
	$('#bird-content').click(function(){
		if( $('.module-content').css("display") == "none" ){
			$('#bird-content-popup').hide();
			$('#mod-overlay').fadeIn();
			$('.module-content').fadeIn();
			
		}else {
			$('.module-content').fadeOut();
			$('#mod-overlay').fadeOut();
		}
		
	});
	$('#bird-header').click(function(){
		if( $('.module-header').css("display") == "none" ){
			$('#bird-header-popup').hide();
			$('#mod-overlay').fadeIn();
			$('.module-header').fadeIn();
			
		}else {
			$('.module-header').fadeOut();
			$('#mod-overlay').fadeOut();
		}
	});
	$('#bird-showcase').click(function(){
		if( $('.module-showcase').css("display") == "none" ){
			$('#bird-showcase-popup').hide();
			$('#mod-overlay').fadeIn();
			$('.module-showcase').fadeIn();
		}else {
			$('.module-showcase').fadeOut();
			$('#mod-overlay').fadeOut();
		}
	});
	$('#newsletter-signup').click(function(){
		if( $('.module-newsletter').css("display") == "none" ){
			$('#mod-overlay').fadeIn();
			$('.module-newsletter').fadeIn();
		}else {
			$('.module-newsletter').fadeOut();
			$('#mod-overlay').fadeOut();
		}
	});
	$('#birds-what').click(function(){
		if( $('.module-birds-what').css("display") == "none" ){
			$('#mod-overlay').fadeIn();
			$('.module-birds-what').fadeIn();
		}else {
			$('.module-birds-what').fadeOut();
			$('#mod-overlay').fadeOut();
		}
	});
	//overlay on click
	$('#mod-overlay').click(function(){
		if( $('#mod-overlay').css("display") == "none" ){
			//do nothing
		}else {
			$('.mod-wrap').fadeOut();
			$('#mod-overlay').fadeOut();
		}
	});
	*/
	$("a[title=submit]").click( function(){
		$(this).parents("form").submit();
	});
	
	//setup portfolio slides
	var index = 0, hash = window.location.hash;
	if (hash) {
		switch (hash) {
			case "#balletmet": index = 1; break;
			case "#rehab": index = 2; break;
			case "#clinton-county": index = 3; break;
			case "#discovery-district": index = 4; break;
			case "#cci": index = 5; break;
			case "#sgm": index = 6; break;
			case "#hidden-creek": index = 7; break;
			case "#lettuce": index = 8; break;
			case "#taneff": index = 9; break;
			case "#acme": index = 10; break;
			default: index = /\d+/.exec(hash)[0];
		}
		//index = /\d+/.exec(hash)[0];
		index = (parseInt(index) || 1) - 1; // slides are zero-based
	}

	$('#portfolio-details').cycle({
		fx: 'scrollHorz',
		speed: 1000,
		startingSlide: index,
		timeout: 0,
		next: '#port-nav-next', 
		prev: '#port-nav-prev',
		after: function(curr,next,opts) {
			var the_hash = opts.currSlide + 1;
			var end_hash
			switch (the_hash) {
				case 1: end_hash = "balletmet"; break;
				case 2: end_hash = "rehab"; break;
				case 3: end_hash = "clinton-county"; break;
				case 4: end_hash = "discovery-district"; break;
				case 5: end_hash = "cci"; break;
				case 6: end_hash = "sgm"; break;
				case 7: end_hash = "hidden-creek"; break;
				case 8: end_hash = "lettuce"; break;
				case 9: end_hash = "taneff"; break;
				case 10: end_hash = "acme"; break;
				default: end_hash = the_hash;
			}
			window.location.hash = end_hash;
		}
	});
	$('#portfolio-details2').cycle({
		fx: 'scrollHorz',
		speed: 1000,
		startingSlide: index,
		timeout: 0,
		next: '#port-nav-next', 
		prev: '#port-nav-prev',
		after: function(curr,next,opts) {
			var the_hash = opts.currSlide + 1;
			var end_hash
			switch (the_hash) {
				case 1: end_hash = "balletmet"; break;
				case 2: end_hash = "rehab"; break;
				case 3: end_hash = "clinton-county"; break;
				case 4: end_hash = "discovery-district"; break;
				case 5: end_hash = "cci"; break;
				case 6: end_hash = "sgm"; break;
				case 7: end_hash = "hidden-creek"; break;
				case 8: end_hash = "lettuce"; break;
				case 9: end_hash = "taneff"; break;
				case 10: end_hash = "acme"; break;
				default: end_hash = the_hash;
			}
			window.location.hash = end_hash;
			var load_place = '.port-detail.' + end_hash;
			$(".port-detail." + end_hash).load('/portfolio/'+ end_hash + '.html', function() {
				$(".port-detail." + end_hash + " .port-detail-images").cycle({
					fx: "scrollHorz",
					timeout: 6000,
					prev: ".port-images-nav-prev",
					next: ".port-images-nav-next"
				});
			});
		}
	});
	
	$(".accord").accordion({
		collapsible: true,
		autoHeight: false,
		active: false,
		header: "a.accord-head"
	});
});

