function emailCheck(emailFormField, showerror) {

	var txt = emailFormField.value;
	var error = "";

	var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

	if (!(emailRe.test(txt))) {
		error = "Please enter a valid email address.\n";
	}

	var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (txt.match(illegalChars)) {
		error += "The email address contains illegal characters.\n";
	}

	if (error.length > 0) {
		if (showerror) {
			emailFormField.focus();
			alert(error);
		}
		return false;
	}
	return true;
}

$(function(){
	
	$("#ping").after($('<input>').attr({'name':'pong','value':$("#ping").val(),'class':'hide'}));
	
	var current_url = $('#fb-index').attr("title");
	$('#fb-index').html('<iframe src="http://www.facebook.com/plugins/like.php?href=' +current_url+ '&amp;layout=standard&amp;show_faces=false&amp;width=530&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" allowTransparency="true" class="facebook-like"></iframe>');

	$("ul#main li, .button, #submit, .text").hover(
		 function () {
			$(this).addClass('hover');
		 }, 
		 function () {
			$(this).removeClass('hover');
		 }
	);

	$('a[rel="external"], form[rel="external"]').attr({'target':'_blank'});
	
	//initBlogFeed();
	
	if($('#carousel').length) {
		$('#carousel div.carousel ul').bxCarousel({
		    display_num: 3,
		    move: 1,
		    margin: 20,
		    auto: false
		});
		$("#carousel ul.tabs").tabs("#carousel div.tab-body", {
			current: 'active',
			tabs : 'li.tabs'
		});
	}
	
	if($('#projects-carousel .pr-ul').children().length > 1) {
		$('#projects-carousel .pr-ul').bxCarousel({
		    display_num: 1,
		    move: 1,
		    margin: 0,
				auto_interval: 12500,
				speed: 600,
		    auto: true,
			autosize: true
		});
	}
	
	var f = $('#feeds');
	
	if (!$('html').hasClass('blogpage')) {
	
		$('#tw,#fb,#rss').click(function(event) {
			event.preventDefault();
			$('.social li').removeClass('active');
			$(this).parent().addClass('active');
			var target = $(this).attr('id'),
				rev = f.attr('rev');
			f.attr('rev',target);
			f.trigger((target == rev || rev == 'init')?'toggle':'switch');
			return false;
		})
	
		$('.closeme').click(function(event) {
			event.preventDefault();
			$('#feeds').trigger('hide');
		})
	
		setTimeout(function() {
			f
				.bind('hide',function() {
					f.animate({top:-600,avoidTransforms:true});
				})
				.bind('show',function() {
					$('section',f).hide().siblings('[rel="'+f.attr('rev')+'"]').show();
					f.animate({top:0,avoidTransforms:true});
				})
				.bind('toggle',function() {
					f.stop().trigger((f.position().top==0)?'hide':'show');
				})
				.bind('switch',function() {
					f.animate({top:-600,avoidTransforms:true},200,function() {
						f.trigger('show');
					});
				})
	
			initTwitter();
			initFacebook();
			initBlogFeed();
		}, 1000);
	}
		
})

function initTwitter() {
    $("#twitter-feed").tweet({
        count: 3,
        username: "hebs_nyc",
        loading_text: "",
        template: '<div><span class="t">{tweet_relative_time}</span><a href="{user_url}" target="_blank">{screen_name}</a></div><p>{text}</p><footer><a href="{tweet_url}" target="_blank" class="b">View this post on Twitter</a></footer>',
		callback: function() {}
    });
}

function initFacebook() {
    $('#facebook-feed').append('<ul/>');
	var ul = $('#facebook-feed ul');
	$.getJSON('http://www.google.com/json/index.php?m=facebook&profile_id=43533802854',function(json) {
		Date.format = "mm.dd.yyyy";
		$.each($.grep(json.data, function(a) {return ((a.type == 'status' || a.type == 'link') && (a.from.id == '43534802854'))}).slice(0, 3),function(i,fb) {
			var date = Date.parse(fb.created_time.replace('+0000','')).toString('MMMM dd @ hh:mmtt');
			ul.append('<li><div><span class="t">'+date+'</span><a>'+fb.from.name+'</a></div><p>'+fb.message+'</p><footer><a href="http://www.facebook.com/hebsdigital/posts/'+fb.id.replace('43534802854_','')+'" target="_blank" class="b">View this post on Facebook</a></footer></li>');
		})
	})
}

function initBlogFeed(){
	var url = 'http://www.linkedin.com/rss/nus?key=Fd6OQSTk1umqPZ6FZPF2yP4dq3OM7Hz6jeR3gdeanshZgz0UCTataGhnYDS_OjcuY';
	url = "http://www.hebsdigital.com/blog/feed/";
	$('#blog-feed').append('<ul/>');
	$('#blog-feed-sidebar').append('<ul/>');
		$.jGFeed(url,function(feeds){
		  if(!feeds){ return false }
//		console.log(feeds);
			var titlesize = 200;
		  for (var i=0; i<3; i++){
		    var entry = feeds.entries[i],
				d = entry.publishedDate.split(', ')[1],
				date = Date.parse(d).toString('MMMM dd @ hh:mmtt'),
				title = (entry.title.length > titlesize) ? entry.title.substring(0,titlesize) + '...' : entry.title,
				html1 = '<li><div><span class="t">'+date+'</span><h6>'+title+'</h6></div><p>'+entry.contentSnippet+'</p><footer><a href="' + entry.link + '" target="_blank" class="b">Read the rest of the story</a></footer></li>',
				html2 = '<li><h6>'+title+'</h6><p>'+entry.contentSnippet+'</p><a href="' + entry.link + '" class="rm">Read more...</a></li>';
								
				
				
				$('#blog-feed ul').append(html1);
				
				$('#blog-feed-sidebar ul').append(html2);

		  }

		}, 10);

		
}
