////Prototype
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 );
}
////


var currentEvent = "";
var sharethis_prev_step = "";

////////////////////////////////

function open_new_window(url)
{
	window.open(url);
}

function set_sharethis_prev_step(step)
{
	sharethis_prev_step = step;
}

function sharethis_load_email()
{
	popup_div('popup_share_this');
	login_type('email');
	set_sharethis_prev_step('');
}

function sharethis_close()
{
	$('#popup_sharethis_main').show();
	$('#popup_login_share').hide();
	
	if(sharethis_prev_step == "")
	{
		popup_div("popup_share_this");
	}
	else
	{
		sharethis_prev_step = "";
	}
}
 
/////////////////////////////////////

function make_friendly_url(url)
{		
	var index = -1;
	/*
	// Hiep comment
	index = url.indexOf('&curPg=');
	if(index != -1)
	{
		url = url.substring(0, index);
	}
	*/

	var replace_hash = location.hash;
	if(url.indexOf('#curPg=') == -1)
	{
	  url += replace_hash;
	}

	url = url.replaceAll('#', '&');
	url = url + replace_hash;
	url = encodeURIComponent(url);
	//alert(url);
	return url;
}

function link_type(type, url_client)
{
	if (url_client==null)
	{
		if (share_url != "")
		{
			url_client = share_url;
		}
		else
			url_client = window.location.href;
	}
	
	//url_client = url_client + "&lang=" + LANG;
	url_client = make_friendly_url(url_client);

	var url ="";
	switch(type)
	{
		case 'yahoo':
		{
			url = "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=" + url_client + "&t=" + share_title;
			break;
		}

		case 'facebook':
		{
			
			url = "http://www.facebook.com/sharer.php?s=100&p[medium]=106&p[title]=" + share_title + "&p[summary]=" + share_title_facebook + "&p[url]=" + url_client  + "&p[images][0]=" + encodeURIComponent(site_image_url);
			//custom on project dvv
			//url = url + "&p[images][1]=" + encodeURIComponent(site_image_url_1);
			//url = url + "&p[images][2]=" + encodeURIComponent(site_image_url_2);
			//url = url + "&p[images][3]=" + encodeURIComponent(site_image_url_3);
			
			//url = "http://www.facebook.com/sharer.php?t=" + share_title + "&d=" + site_description + "&u=" + url_client;
			break;
		}

		case 'delicious':
		{
			url = "http://delicious.com/save?url=" + url_client + "&title=" + share_title;
			break;
		}
		case 'linkedin':
		{
			url= "http://www.linkedin.com/shareArticle?mini=true&url=" + url_client + "&title=" + share_title + "&summary=" + site_description + "&source=" + url_client;
			break;
		}
		case 'windowslive':
		{
			url = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + url_client + "&title=" + share_title + "&top=1";
			break;
		}
		case 'google':
		{
			url = "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + url_client + "&title=" + share_title;
			break;
		}
		case 'diigo':
		{
			url = "http://secure.diigo.com/post?url=" + url_client + "&title=" + share_title;
			break;
		}
		case 'stumbleupon':
		{
			var current_share_url = decodeURIComponent(url_client);
			//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=" + url_client + "&title=" + share_title;
			break;
		}
		case 'twitter':
		{
			url = "http://twitter.com/?status=" + share_title_twitter;
			break;
		}
		case 'myspace':
		{
			url = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + url_client + "&t=" + share_title;
			break;
		}
		case 'tumblr':
		{
			url = "http://www.tumblr.com/login?s=&t=" + share_title + "&u=" + url_client + "&v=3";
			break;
		}
		case 'planyp':
		{
			var current_share_url = decodeURIComponent(url_client);
			//current_share_url = current_share_url.replace("index.php","share_this.php").replaceAll("&",",");

			url ="http://planyp.us/plans/new/?url=" + url_client + "&title=" + share_title + "&description=" + current_share_url;
			break;
		}
		case 'blogger':
		{
			url = "http://www.blogger.com/blog_this.pyra?t=" + share_title + "&u=" + url_client + "&n=" + share_title + "&pli=1";
			break;
		}
		case 'netlog':
		{
			url = "http://www.netlog.com/go/manage/links/?view=save&origin=external&url=" + url_client + "&description=" + share_title;
			break;
		}
		case 'netvibes':
		{
			url = "http://www.netvibes.com/share?title=" + share_title  + "&url=" + url_client;
			break;
		}
	}
	
	if (url != "")
		open_new_window(url);
}

////////////////////////////////

function login_type(type)
{
	do_hide_all();

	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');
	do_hide('popup_sharethis_main');
}


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 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;
}
var is_sending = false;
function do_submit()
{
	var isReturn = false;
	
	var url_client = "";
	if (share_url != "")
		url_client = share_url;
	else
		url_client = window.location.href;
	
	url_client = url_client + "&lang=" + LANG;
	url_client = make_friendly_url(share_url);
	
	switch(currentEvent)
	{
		case 'wordpress':
		{
			if(!do_check('wordpress'))
			{
				var oWordpress = $("#wordpress").val();
				var oComment = share_title + " " + url_client + "\n" + $("#comment").val();

				url = "" + oWordpress + "/wp-admin/press-this.php?u=" + url_client + "&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 = encodeURIComponent($("#your_name").val());
				var from_email = encodeURI($("#your_email").val());
				var to_name = encodeURIComponent($("#friend_name").val());
				var to_email = encodeURI($("#friend_email").val());
				var comment = encodeURIComponent($("#comment").val());
				var oShare_link = encodeURIComponent(url_client);
				if(!isEmail(from_email))
				{
					$("#your_email").focus();
					return;
				}
				if(!isEmail(to_email))
				{
					$("#friend_email").focus();
					return;
				}

				// call send email
				if(is_sending == false)
				{
					 $.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("");
								}
								is_sending = false;
								popup_div('popup_message');
							} ,
							beforeSend: function(){ 
								is_sending = true;
							}
						});
				}
				break;
			}
		}
	}
	if(isReturn)
	{
		do_return();
	}

}



function do_after_callback(result)
{
	alert(result);
	do_return();
}
