// JavaScript Document
/*

var share_url = "";
var share_title = "";
*/

var supernote = new SuperNote('supernote', {});

function animFade(ref, counter)
{
 //counter = Math.min(counter, 0.9); // Uncomment to make notes translucent.
 var f = ref.filters, done = (counter == 1);
 if (f)
 {
  if (!done && ref.style.filter.indexOf("alpha") == -1)
   ref.style.filter += ' alpha(opacity=' + (counter * 100) + ')';
  else if (f.length && f.alpha) with (f.alpha)
  {
   if (done) enabled = false;
   else { opacity = (counter * 100); enabled=true }
  }
 }
 else ref.style.opacity = ref.style.MozOpacity = counter*0.999;
};
supernote.animations[supernote.animations.length] = animFade;



var currentEvent = "";

function open_new_window(url)
{
	window.open(url);
}

String.prototype.replaceAll = function(
 strTarget, // The substring you want to replace
 strSubString // The string you want to replace in.
 ){
 var strText = this;
 var intIndexOfMatch = strText.indexOf( strTarget );

 // Keep looping while an instance of the target string
 // still exists in the string.
 while (intIndexOfMatch != -1){
 // Relace out the current instance.
 strText = strText.replace( strTarget, strSubString )

 // Get the index of any next matching substring.
 intIndexOfMatch = strText.indexOf( strTarget );
 }

 // Return the updated string with ALL the target strings
 // replaced out with the new substring.
 return( strText );
 }

function link_type(type)
{
	//press_detail
	//news_detail
	var url_client = location.href;
	var index = -1;
	if(index == -1)
	{
		index = url_client.indexOf('&curPg=');
		if(index != -1)
		{
			url_client = url_client.substring(0, index);
		}
	}

	var replace_hash = location.hash;
	if(url_client.indexOf('#curPg=') == -1)
	 {
	  url_client += replace_hash;
	 }

	url_client = url_client.replaceAll('#', '&');
	share_url = url_client + replace_hash;
	//alert(share_url);
	share_url = encodeURIComponent(share_url);

	var url ="";
	switch(type)
	{
		case 'yahoo':
		{
			url = "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=" + share_url + "&t=" + share_title;

			break;
		}

		case 'facebook':
		{
			url = "http://www.facebook.com/sharer.php?u=" + share_url + "&t=" + share_title;
			break;
		}

		case 'delicious':
		{
			url = "http://delicious.com/save?url=" + share_url + "&title=" + share_title;
			break;
		}
		case 'linkedin':
		{
			url= "http://www.linkedin.com/shareArticle?mini=true&url=" + share_url + "&title=" + share_title + "&summary=" + share_title + "&source=" + share_url;
			break;
		}
		case 'windowslive':
		{
			url = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + share_url + "&title=" + share_title + "&top=1";
			break;
		}
		case 'google':
		{
			url = "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + share_url + "&title=" + share_title;
			break;
		}
		case 'diigo':
		{
			url = "http://secure.diigo.com/post?url=" + share_url + "&title=" + share_title;
			break;
		}
		case 'stumbleupon':
		{
			var current_share_url = decodeURIComponent(share_url);
			current_share_url = current_share_url.replace("index.php","share_this.php").replaceAll("&",",");

			url = "http://www.stumbleupon.com/submit?url=" + current_share_url + "&title=" + share_title;
			break;
		}
		case 'digg':
		{
			url = "http://digg.com/submit?phase=2&url=" + share_url + "&title=" + share_title;
			break;
		}
		case 'twitter':
		{
			url = "http://twitter.com/home/?status=" + share_title + " " + share_url;
			break;
		}
		case 'myspace':
		{
			url = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + share_url + "&t=" + share_title;
			break;
		}
		case 'tumblr':
		{
			url = "http://www.tumblr.com/login?s=&t=" + share_title + "&u=" + share_url + "&v=3";
			break;
		}
		case 'planyp':
		{
			var current_share_url = decodeURIComponent(share_url);
			current_share_url = current_share_url.replace("index.php","share_this.php").replaceAll("&",",");

			url ="http://planyp.us/plans/new/?url=" + share_url + "&title=" + share_title + "&description=" + current_share_url;
			break;
		}
		case 'blogger':
		{
			url = "http://www.blogger.com/blog_this.pyra?t=" + share_title + "&u=" + share_url + "&n=" + share_title + "&pli=1";
			break;
		}
	}

	open_new_window(url);
}

function do_hide_all()
{
	$("#div_username").hide();
	$("#div_password").hide();
	$("#div_mail_to").hide();
	$("#div_location").hide();

	//$("#comment").val( share_title + " " + share_url );
}

function login_type(type)
{
	var url_client = location.href;
	var index = -1;

	if(index == -1)
	{
		index = url_client.indexOf('&curPg=');
		if(index != -1)
		{
			url_client = url_client.substring(0, index);
		}
	}

	var replace_hash = location.hash;

	if(url_client.indexOf('#curPg=') == -1)
	 {
	  url_client += replace_hash;
	 }

	url_client = url_client.replaceAll('#', '&');

	share_url = url_client + replace_hash;

	share_url = encodeURIComponent(share_url);
	currentEvent = type;
	do_hide_all();

	switch(currentEvent)
	{
		case 'wordpress':
		{
			$("#div_location").show();
			$("#msg_wordpress").text('');
			$("#wordpress").val('http://yoursite.wordpress.com');
			$("#input_wordpress").attr("class", "input_field");
			 popup_div('dvLogin');
			 $("#comment").focus();
			break;
		}
		case 'email':
		{
			$("#div_mail_to").show();
			$("#msg_mail_to").text('');
			$("#mail_to").val("");
			$("#comment").val("");
			$("#input_mail_to").attr("class", "input_field");
			 popup_div('dvLogin');
			 $("#mail_to").focus();
			break;
		}
	}
	do_show("dvLogin");
}

function do_show(id)
{
	$("#" + id).show(1000);
}

function do_hide(id)
{
	$("#" + id).hide(1000);
}

function do_return()
{
	//$("#dvLogin").hide(1000);
	 popup_div('dvLogin');
}

function do_check(controlID)
{
	//username incorrect
	var isError = false;
	var oControl = $('#' + controlID).val();
	if (!oControl)
	{
		$("#msg_" + controlID).text("" + controlID + " incorrect");
		$("#input_" + controlID).attr("class", "input_field incorrect");
		isError = true;
	}
	else
	{
		$("#msg_" + controlID).text('');
		$("#input_" + controlID ).attr("class", "input_field");
	}

	return isError;
}

function do_submit()
{
	var isReturn = false;
	switch(currentEvent)
	{
		case 'wordpress':
		{
			if(!do_check('wordpress'))
			{
				var oWordpress = $("#wordpress").val();
				var oComment = share_title + " " + share_url + "\n" + $("#comment").val();

				url = "" + oWordpress + "/wp-admin/press-this.php?u=" + share_url + "&t=" + share_title + "&s=" + oComment + "&v=2";
				open_new_window(url);
				 $("#comment").val("");
				isReturn = true;
			}
			break;
		}
		case 'email':
		{
			if(!do_check('mail_to'))
			{
				var oEmail = $("#mail_to").val();
				var oComment = $("#comment").val();
				var oShare_link = share_url;

				// call send email
				$.post("send_mail_sharethis.php", { mailto: "" + oEmail, comment: ""+ oComment,share_link: "" + decodeURIComponent(oShare_link) },
				   function(data){
					   if(data == "OK")
					   {
					   	    popup_div('dvLogin');
							popup_div('dvSuccess');
					   		isReturn = true;


						}
						else
						{
							//alert("Email is invalid.");
							$("#msg_mail_to").text('mail_to incorrect');
							$("#input_mail_to" ).attr("class", "input_field incorrect");

						}
				   });
				break;
			}
		}
	}

	if(isReturn)
	{
		do_return();
	}

}

function do_after_callback(result)
{
	alert(result);
	do_return();
}