$("#article_toolbar_div1,#article_toolbar_div2,#article_toolbar_div3 .small,#article_toolbar_div3 .big,#article_toolbar_div4,.button_sm").hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")}).mouseup(function(){$(this).removeClass("click");}).mousedown(function(){$(this).addClass("click");});

// because IE6's cascading is completely broken, we have to add this seperately
//$(".at_share").hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")}).mouseup(function(){$(this).removeClass("share_click");}).mousedown(function(){$(this).addClass("share_click");})

var article_toolbar = {
	"state" : null,
	"email" : function(){
		if (this.state == "email"){
			$("#article_toolbar_dropdown_share").hide();
			$("#article_toolbar_dropdown").slideUp();
			$("#article_toolbar_div1").removeClass("article_toolbar_div1_on");
			$("#article_toolbar_email").removeClass("article_toolbar_email_on");
			this.state = null;
		} else {
			$(".at_dropdown_main").show();
			$(".at_dropdown_main_thankyou").hide();
			if (!this.hasinitforthefirsttime){
				this.init();
				this.hasinitforthefirsttime = true;
			}
			this.state = "email";
			$("#article_toolbar_div4").removeClass("article_toolbar_div4_on");
			$("#article_toolbar_share").removeClass("article_toolbar_share_on");
			$("#article_toolbar_div1").addClass("article_toolbar_div1_on");
			$("#article_toolbar_email").addClass("article_toolbar_email_on");
			$("#article_toolbar_dropdown_share").hide();
			$("#article_toolbar_dropdown").slideDown();
		}
	},
	"share" : function(){
		if (this.state == "share"){
			$("#article_toolbar_dropdown").hide();
			$("#article_toolbar_dropdown_share").slideUp();
			$("#article_toolbar_div4").removeClass("article_toolbar_div4_on");
			$("#article_toolbar_share").removeClass("article_toolbar_share_on");
			this.state = null;
		} else {
			$(".at_dropdown_main").show();
			$(".at_dropdown_main_thankyou").hide();
			this.state = "share";
			$("#article_toolbar_div1").removeClass("article_toolbar_div1_on");
			$("#article_toolbar_email").removeClass("article_toolbar_email_on");
			$("#article_toolbar_div4").addClass("article_toolbar_div4_on");
			$("#article_toolbar_share").addClass("article_toolbar_share_on");
			$("#article_toolbar_dropdown").hide();
			$("#article_toolbar_dropdown_share").slideDown();
		}
	},
	"hasinitforthefirsttime" : false,
	"init" : function(){
		$.ajaxSetup({ cache: false });
		var emailthis_addy = "/email-this" + $("#emailthis_path").val();
//		var emailthis_addy = "/email-this" + $("#emailthis_path").val()+"?cachebust="+Math.floor(Math.random()*100001);
		var article_id = $("#emailthis_article_id").val();
		// overriding this temporarily
		//emailthis_addy = "/email-this/blogs/food-bites/test-test-sf";
		$.get(emailthis_addy,function(data){
			article_toolbar.process_emailthis(data);
		});
	},
	"process_emailthis" : function(data){
		//var mydata = data.split("<div id=\"preview-benchmark\"")[0];
		var myObject = eval('(' + data + ')');
		article_toolbar.emailthis_object = myObject;
		/*	obj.save_success;
			obj.article_id;
			obj.logged_in;
			obj.friend_name;*/
		$("#emailthis_cap_expr").html(article_toolbar.emailthis_object.captcha_exp);
		$("#emailthis_cap_key").val(article_toolbar.emailthis_object.captcha_key);
		$("#emailthis_name").val(article_toolbar.emailthis_object.name);
		$("#emailthis_email").val(article_toolbar.emailthis_object.email);
		$("#emailthis_friendemail").val(article_toolbar.emailthis_object.friend_email);
		$("#emailthis_message").val(article_toolbar.emailthis_object.message);
		if (article_toolbar.emailthis_object.errorstring == "There was a problem with the email address used.  Please enter a valid email address.\n"){
			article_toolbar.emailthis_object.errorstring = "There is an error in one or more of the e-mail addresses you have provided";
		}
		if (article_toolbar.emailthis_object.errorstring == "Problem with CAPTCHA: Invalid answer for key"){
			article_toolbar.emailthis_object.errorstring = "Please make sure your answer is entered correctly";
		}
		if (article_toolbar.emailthis_object.errorstring == "Problem with CAPTCHA: "){
			article_toolbar.emailthis_object.errorstring = "Please make sure your answer is entered correctly";
		}
		if (article_toolbar.emailthis_object.errorstring == "There was a problem with in sending your email.  Please try again.\n"){
			article_toolbar.emailthis_object.errorstring = "There is something wrong with one or more of your friends' email addresses that you typed in.";
		}
		
		
		$("#emailthis_errorstring").html(article_toolbar.emailthis_object.errorstring);
		$("#emailthis_cap_input").val("");
		if (article_toolbar.emailthis_object.errorstring != ""){
			if (!checkEmail(article_toolbar.emailthis_object.email)){
				//$("#emailthis_email").css("background-color","#FF6600");
			}
			if (!checkEmail(article_toolbar.emailthis_object.friend_email)){
				//$("#emailthis_friendemail").css("background-color","#FF6600");
			}
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").show();
		}
		if (article_toolbar.emailthis_object.save_success == "1"){
			$(".at_dropdown_main").fadeOut("fast",function(){$(".at_dropdown_main_thankyou").fadeIn("fast")});
			this.hasinitforthefirsttime = false;
		}
		$("#emailthis_name").removeAttr("disabled");
		$("#emailthis_email").removeAttr("disabled");
		$("#emailthis_friendemail").removeAttr("disabled");
		$("#emailthis_message").removeAttr("disabled");
		$("#article_toolbar_dropdown_main_email_loading").hide();
	},
	"emailthis_object" : {},
	"email_post" : function(){
		// lets do a check if the name field is empty
		var namefield = $("#emailthis_name").val();
		if ((namefield.replace(/^\s+|\s+$/g, '')) === ""){
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").html("Please fill in your name").show();
			//alert("ERROR: Please fill in your name");
			return false;
		}
		if ($("#emailthis_email").val() == ""){
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").html("Please fill in your e-mail address").show();
			return false;
		}
		if (!checkEmail($("#emailthis_email").val())){
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").html("Your e-mail address as entered does not appear to be a correct e-mail address").show();
			return false;
		}
		if ($("#emailthis_cap_input").val() == ""){
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").html("Please fill in an answer").show();
			return false;
		}
		if ($("#emailthis_friendemail").val().replace(/^\s+|\s+$/g, '') === ""){
			$(".at_dropdown_main").css("height","220px");
			$("#emailthis_errorstring").html("There is something wrong with one or more of your friends' email addresses that you typed in.").show();
			return false;
		}
		if ($("#emailthis_name").attr("disabled")){
			$h.console.warn("EMAIL THIS: post execution disabled");
			return false;
		}
		
		
		
		
		$("#emailthis_name").attr("disabled","disabled");
		$("#emailthis_email").attr("disabled","disabled");
		$("#emailthis_friendemail").attr("disabled","disabled");
		$("#emailthis_message").attr("disabled","disabled");
		$("#article_toolbar_dropdown_main_email_loading").show();
		article_toolbar.emailthis_object.name = $("#emailthis_name").val();
		article_toolbar.emailthis_object.email = $("#emailthis_email").val();
		article_toolbar.emailthis_object.friend_email = $("#emailthis_friendemail").val();
		article_toolbar.emailthis_object.message = $("#emailthis_message").val();
		article_toolbar.emailthis_object.captcha_answer = $("#emailthis_cap_input").val();
		article_toolbar.emailthis_object.captcha_key = $("#emailthis_cap_key").val();
		
		$.post("/email-this/email-this", { "article_id" : article_toolbar.emailthis_object.article_id, "captcha_answer" : article_toolbar.emailthis_object.captcha_answer, "captcha_key" : article_toolbar.emailthis_object.captcha_key, "name" : article_toolbar.emailthis_object.name, "email" : article_toolbar.emailthis_object.email, "friend_email" : article_toolbar.emailthis_object.friend_email, "friend_name" : article_toolbar.emailthis_object.friend_name, "message" : article_toolbar.emailthis_object.message},function(data){article_toolbar.process_emailthis(data);});
	},
	"bookmark" : function(name){
		switch(name){
			case "digg":
				var url = document.location;
				window.open('http://digg.com/submit?url='+escape(url));
				break;
			case "facebook":
				var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1239291108&u='+e(l.href)+'&t='+e(d.title);
				1;
				try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);
				share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};
				if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)
				break;
			case "newsvine":
				window.open('http://www.newsvine.com/_wine/save?popoff=1&u='+document.location+'','newsvine','toolbar=no,width=590,height=600,resizable=yes,scrollbars=yes');
				break;
			case "delicious":
				f='http://delicious.com/save?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'&v=5&';
				a=function(){if(!window.open(f+'noui=1&jump=doclose','deliciousuiv5','location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550'))location.href=f+'jump=yes'};
				if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}
				break;
			case "magnolia":
				window.open("http://ma.gnolia.com/");
				break;
			case "reddit":
				window.open('http://www.reddit.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));
				break;
			case "ask":
				window.open('http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title),'ask','toolbar=no,width=590,height=600,resizable=yes,scrollbars=yes');
				break;
			case "mslive":
				t=document.title;
				u=escape(location.href);
				window.open('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url='+u+'&title='+t+'&top=0','live','toolbar=no,width=590,height=600,resizable=yes,scrollbars=yes');
				break;
			case "buzz":
				var yahooBuzzArticleHeadline = document.title;
				//var yahooBuzzArticleSummary = '';
				var yahooBuzzArticleCategory = "health";
				var yahooBuzzArticleType = "text";
				var yahooBuzzArticleId = window.location.href;
				window.open("http://buzz.yahoo.com/buzz?targetUrl="+encodeURIComponent(location.href));
				break;
			case "google":
				var a=window,b=document,c=encodeURIComponent,d=a.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+c(b.location)+"&title="+c(b.title),"bkmk_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=420px,width=550px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300)
				break;
			case "popsugar":
				var url = document.location;
				window.open("http://teamsugar.com/node/add/userlink?t=fabfind&url="+encodeURIComponent(location.href)+"&body="+""+"&title="+encodeURIComponent(document.title));
				//http://teamsugar.com/node/add/userlink?t=fabfind&url=http://www.cosmogirl.com/blog/blog/terrible-tattoos%3Fsrc%3Dteamsugar&body=What%20word%20comes%20to%20mind%20when%20you%20look%20at%20these%20terrible%20tattoos?&title=Together%20Forever
				break;
			case "stumbleupon":
				window.open("http://www.stumbleupon.com/submit?url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent(document.title));
				break;
			default:
				alert("UNKNOWN BOOKMARK");
				break;
		}
	},
	"print_this" : function(section_path,url_name){
		$("#article_toolbar_div1").removeClass("article_toolbar_div1_on");
		$("#article_toolbar_div4").removeClass("article_toolbar_div4_on");
		$("#article_toolbar_dropdown").hide();
		$("#article_toolbar_dropdown_share").hide();
		this.state = null;
		window.open ("/print-this"+section_path+url_name+"?kw=ist");
	}
}




function checkEmail(fieldValue){
	if(fieldValue != ""){
		var atSymbol = 0
		for(var a = 0; a < fieldValue.length; a++){
			if(fieldValue.charAt(a) == "@"){
				atSymbol++
			}
		}
		if(atSymbol > 1){return false}
		if(atSymbol == 1 && fieldValue.charAt(0) != "@"){
			var period = fieldValue.indexOf(".",fieldValue.indexOf("@")+2)
			var twoPeriods = (fieldValue.charAt((period+1)) == ".") ? true : false
			if(period == -1 || twoPeriods || fieldValue.length < period + 2 || fieldValue.charAt(fieldValue.length-1)=="."){
				return false
			}
		}else{return false}
	}else{return false}
	return true
}



