// 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, url_client)
{
	//press_detail
	//news_detail
	if (url_client==null)
		url_client = window.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 (url_client);
	//alert (share_url);
	//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?s=100&p[medium]=106&p[title]=" + share_title + "&p[summary]=" + site_description + "&p[url]=" + share_url  + "&p[images][0]=" + site_image_url;
			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;
		}
		case 'netlog':
		{
			url = "http://www.netlog.com/go/manage/links/?view=save&origin=external&url=" + share_url + "&description=" + share_title;
			break;
		}
		case 'netvibes':
		{
			url = "http://www.netvibes.com/share?title=" + share_title  + "&url=" + share_url;
			break;
		}
	}
	
	open_new_window(url);
}

function show_share_this()
{
	$("#table_detail").hide();
	
	var position = $('#img_share_this').offset();
	position.top = position.top - $("#popup_share_this").height()- 3;
	popup_div_sharethis('popup_share_this', position);
}

function show_wordpress_control()
{
	$("#tr_blog_url").show();
}

function show_send_to_friend_control()
{
	$("#tr_your_name").show();
	$("#tr_your_email").show();
	$("#tr_friend_name").show();
	$("#tr_friend_email").show();
}

function do_hide_all()
{
	$("#tr_blog_url").hide();
	$("#tr_your_name").hide();
	$("#tr_your_email").hide();
	$("#tr_friend_name").hide();
	$("#tr_friend_email").hide();
}

function login_type(type)
{
	do_hide_all();
	
	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;
	
	switch(currentEvent)
	{
		case 'email':
		{
			show_send_to_friend_control();
			$("#your_name").focus();
			break;
		}
		case 'wordpress':
		{
			show_wordpress_control();
			$("#wordpress").val('http://yoursite.wordpress.com');
			$("#wordpress").focus();
			break;
		}
		
	}
	do_show('popup_login_share');
}

function do_show(id)
{
	$("#" + id).show();
}

function do_hide(id)
{
	$("#" + id).hide();
}

function do_return()
{
	 popup_div('popup_detail');
}

function do_check(controlID)
{
	//username incorrect
	var isError = false;
	var oControl = $('#' + controlID).val();
	if (!oControl)
	{
		$('#' + controlID).focus();
		isError = true;
	}

	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('your_name') && !do_check('your_email') && !do_check('friend_name') && !do_check('friend_email'))
			{
				
				var from_name = encodeURI($("#your_name").val());
				var from_email = encodeURI($("#your_email").val());
				var to_name = encodeURI($("#friend_name").val());
				var to_email = encodeURI($("#friend_email").val());
				var comment = encodeURI($("#comment").val());
				var oShare_link = encodeURI(share_url);
				if(!isEmail(from_email))
				{
					$("#your_email").focus();
					return;
				}
				if(!isEmail(to_email))
				{
					$("#friend_email").focus();
					return;
				}

				// call send email
				 $.ajax({ 
						type: "POST", 
						url: "ajax/sendtofriend.php", 
						data: "lang=" + LANG + "&from_name=" + from_name + "&from_email=" + from_email + "&to_name=" + to_name + "&to_email=" + to_email + "&message=" + comment + "&share_url=" + oShare_link +'&current_module='+encodeURI(current_module),
						success: function(msg){
							$("#popup_message_text").html("");
							msg = eval("" + msg);
							if(msg == 1)
							{
								$("#popup_message_text").html( (LANG == 'fr')?"Bravo ! <br>Votre message a bien &#233;t&#233; envoy&#233;.":"Gelukt! <br>Uw e-mail is goed verzonden.");
							}
							else
							{
								$("#popup_message_text").html( (LANG == 'fr')?"Attention ! <br>L&#146;envoi de votre message a &#233;chou&#233;.":"Opgelet! <br>Het verzenden van uw boodschap is mislukt.");
							}
							
							if(msg == 1)
							{
								$("#friend_name").val("");
								$("#friend_email").val("");
								$("#comment").val("");
							}
							popup_div('popup_message');
						} ,
						beforeSend: function(){ }
						});
				break;
			}
		}
	}
	if(isReturn)
	{
		do_return();
	}

}

function do_after_callback(result)
{
	alert(result);
	do_return();
}
