if(!mapness){
	var mapness = {};
}

mapness.share = new function Share(){
	var that = this;
	this.emailCounter = 0;
	this.emailMax = 50;
	this.url = window.location.href;
	
// email sharing address book contacts harvesting (Karol Janyst) :
	this.buildUrl = function(){
		if(!mapness.journey || !mapness.journey.j_id || mapness.journey.j_id == null){
			this.url = window.location;
		}else{
			this.url = window.location.hostname + window.location.pathname + '/' + mapness.journey.j_id;
		}
	}

	this.addNewTargetInput = function(email){
    
		if (this.emailCounter <= this.emailMax) {
			var moreBoxHeight = $(".share-emailMoreBox").height();
			if (moreBoxHeight < 150) { $(".share-emailMoreBox").height(moreBoxHeight + 25); }
    
			$(".shareTarget:last").parent().after('<div class="share-emailInput"><label>'+ mapness.dict.words.global_shareByEmailAlsoSend +'</label><input type="text" class="text shareTarget"></div>');
			$(".shareTarget:last").val(email);
			$(".shareTarget:last").focus();
			this.emailCounter++;
			if (this.emailCounter == this.emailMax) {
				$("#share-emailMore").hide();
			}
		}		
	}
    
	this.authMail = function(mail) {
		var link = $.ajax({url: "/xhr/authMail.php", async: false, data : {link : mail}}).responseText;
		window.open(link, "Authorization", "width=800,height=600,toolbar=false,locationbar=false,directories=false,status=false,menubar=false,scrollbars=true,resizable=false,copyhistory=false");
	}
    
	$.fn.addEmail = function() {
		var email = $(this).children(".share-emailAddress").text();
		$(this).children(".share-actionEmail").text("Remove");
		$(this).addClass("selected");
		$(this).click(function() {
			$(this).removeEmail();
		});
		mapness.share.addNewTargetInput(email);
	}
    
	$.fn.removeEmail = function() {
		var email = $(this).children(".share-emailAddress").text();
		$(this).children(".share-actionEmail").text("Add");
		$(this).removeClass("selected");
		$(this).click(function() {
			$(this).addEmail();
		});
		$(".shareTarget").each(function() {
			if ($(this).val() == email && $(this).val() != "") {
				$(this).parent().remove();
			}
		});
		var moreBoxHeight = $(".share-emailMoreBox").height();
		if (moreBoxHeight > 150) { $(".share-emailMoreBox").height(moreBoxHeight - 50); }
		mapness.share.emailCounter=-2;
	}
    
	this.emailFilter = function() {
		$("#share-emailList li").each(function(){  
			var t = $(this).text().toLowerCase(); //all row text  
			$("<span class='share-index'></span>").hide().text(t).appendTo(this);  
		});//each tr  
		$("#share-emailFilter").keyup(function(){  
			var s = $(this).val().toLowerCase().split(" ");  
			//show all rows.  
			$("#share-emailList li:hidden").show();  
			$.each(s, function(){  
				$("#share-emailList li:visible .share-index:not(:contains('"  
				+ this + "'))").parent().hide();  
			});//each  
		});//key up.  
	}
    
	this.importContacts = function(contacts) {
		var list = eval(contacts);
	            $.each(list, function() {
			$("#share-emailList").append('<li>' + this.name + '<br/> ' + this.email + ' <span class="share-emailAddress">' + this.email + '</span> <a class="share-actionEmail" href="javascript:;">Add</a></li>');
	            });
		$("#share-Services").hide();
		$("#share-List").show();
		$("#share-emailList li").click(function() {
			$(this).addEmail();
		});
		mapness.share.emailFilter();
	}
// END email sharing address book contacts harvesting (Karol Janyst)
	
	this.shareByEmail = function(){
		that.buildUrl();
		var targets = []; 
		$(".share-box").parent().find(".error").remove();
		$(".shareTarget").each(function(){
			targets.push($(this).val());
		});
		var message = $(".global_shareByEmailMessageLabel textarea").val();
		var shareTempContent = $(".share-box").html();
		
		var request = new RequestO("post", "/xhr/share.php", { method:"email", "email" : targets, "message" : message, "url" : that.url	},{
				"loading": function(){
					$(".share-box").html('<div class="share-sending"><img src="/images/ajax-loader2.gif"><span class="note">'+ mapness.dict.words.global_sending +'</span></div>');
				},
				"complete": function(response) {
					var response = json_parse(response);
					if(response.isSuccess){
						$(".share-box").html('<div class="success">'+mapness.dict.words.global_shareByEmailSuccess+'</div>');
						pageTracker._trackPageview('/sharing/email-share'); // Google Analytics tracking
						setTimeout(function(){
							mapness.ui.modal.destroy();
						}, 3000);
					}else{
						$(".share-box").html(shareTempContent);
						$(".global_shareByEmailMessageLabel textarea").val(message);
						var i = 0;
						$(".shareTarget").each(function(){
							$(this).val(targets[i]);
							i++;
						});
						$(".share-box").prepend('<div class="error">'+response.callback.error+"</div>");
					} 
				}
			});
	}
	
	this.shareByTwitter = function(){
		that.buildUrl();
		//http://tinyurl.com/api-create.php?url=http://scripting.com/
		var targets = []; 
		$(".share-box").parent().find(".error").remove();
		var message = $(".share-shareMessage textarea").val();
		var login = $("#twitterLogin").val();
		var password = $("#twitterPassword").val();
		var shareTempContent = $(".share-box").html();
		
		var request = new RequestO("post", "/xhr/share.php", { method:"twitter", "login" : login, "password":password, "message" : message, "url" : that.url	},{
				"loading": function(){
					$(".share-box").html('<div class="share-sending"><img src="/images/ajax-loader2.gif"><span class="note">'+ mapness.dict.words.global_sending +'</span></div>');
				},
				"complete": function(response) {
					var response = json_parse(response);
					if(response.isSuccess){
						$(".share-box").html('<div class="success">'+mapness.dict.words.global_shareByTwitterSuccess+'</div><p>'+message+'</p>');
						pageTracker._trackPageview('/sharing/twitter-share');  // Google Analytics tracking
						setTimeout(function(){
							mapness.ui.modal.destroy();
						}, 3000);
					}else{
						$(".share-box").html(shareTempContent);
						$("#twitterLogin").val(login);
						$(".share-shareMessage textarea").val(message);
						$(".share-box").prepend('<div class="error">'+response.callback.error+"</div>");
					}
				}
			});
		
	}
	
	this.byFacebook = function(){
		that.buildUrl();
		var facebookWindow = window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(that.url)+'&t='+encodeURIComponent(document.title),'MapnessShare','toolbar=0,status=0,width=706,height=436,resizable=1');
	}
	
	this.byTwitter = function(mode, draggable){
		that.buildUrl();
		if(!mode){
			var draggable = true;
			var defaultMsg = mapness.dict.words.global_shareByTwitterMessage;
		}else{
			var draggable = false;
			var defaultMsg = mapness.dict.words.journey_shareByTwitterMessage; 
		}
		var formHtml = '<div class="share-twitterIconBig"></div>'+
						'<h2 class="share">'+mapness.dict.words.global_shareByTwitterHeader+'</h2>'+
						'<p class="minor">'+mapness.dict.words.global_shareByTwitterNotice+'</p>'+
						'<div class="share-twitterBox share-box">'+
							'<div class="share-login">'+
								'<label>'+mapness.dict.words.global_shareByTwitterLogin+'</label>'+
								'<input type="text" class="text" id="twitterLogin">'+
							'</div>'+
							'<div class="share-pass">'+
								'<label>'+mapness.dict.words.global_shareByTwitterPassword+'</label>'+
								'<input type="password" class="text" id="twitterPassword">'+
							'</div>' +
							'<div class="share-shareMessage">' +
								'<label>'+mapness.dict.words.global_shareByTwitterMessageLabel+'</label>'+
								'<textarea onKeyUp="mapness.util.limitText(this, 120);" onKeyDown="mapness.util.limitText(this, 120)">'+defaultMsg+'</textarea>' +
							'<div class="clearer"></div></div>' +
							'<div class="share-bodyNote"><p class="minor">'+mapness.dict.words.global_shareByTwitterMessageNote+'</p></div>'+
							'<div class="share-submit">'+
								'<a class="button2 send" onclick="mapness.share.shareByTwitter();" href="javascript:;"><span>'+mapness.dict.words.global_shareByTwitterSend+'</span></a>'+
							'</div>'+
						'</div>'; 
		mapness.ui.modal.init(formHtml, {width: 440, height: 330, addOverlay: false, draggable: draggable});
	}
	
	this.byEmail = function(mode){
		that.buildUrl();
		this.emailCounter = 0;
		if(!mode){
			var draggable = true;
			var defaultMsg = mapness.dict.words.global_shareByEmailMessage;
		}else{
			var draggable = false;
			var defaultMsg = mapness.dict.words.journey_shareByEmailMessage;
		}
		var formHtml = '<div class="share-emailIconBig"></div> \
						<h2 class="share">'+ mapness.dict.words.global_shareByEmailHeader +'</h2> \
						<div class="share-emailBox share-box"> \
							<div class="global_shareByEmailMessageLabel"> \
								<label>'+ mapness.dict.words.global_shareByEmailMessageLabel +'</label> \
								<textarea>'+ defaultMsg +'</textarea> \
								<div class="clearer"></div> \
							</div> \
							<div class="share-emailMoreBox"> \
								<div class="share-emailInput"> \
									<label>'+ mapness.dict.words.global_shareByEmailSendToLabel+ '</label> \
									<input type="text" class="text shareTarget"> \
								</div>\
								<a id="share-emailMore" class="button2 add" href="javascript:;" onclick="mapness.share.addNewTargetInput();"><span>'+ mapness.dict.words.global_shareByEmailAddMore + '</span></a>\
								<div class="clearer"></div>\
							</div>\
							<div class="share-submit">\
								<a class="button2 send" onclick="mapness.share.shareByEmail();" href="javascript:;"><span>'+ mapness.dict.words.global_shareByEmailSend +'</span></a>\
							</div>\
						</div>\
						<div class="share-emailImport">\
							<div id="share-Services">\
								<h3 class="note2">Add recipients from your email address book:</h3>\
								<ul class="note2 minor">\
									<li><a href="javascript:;" onclick="mapness.share.authMail(\'gmail\');">Gmail</a></li>\
									<li><a href="javascript:;" onclick="mapness.share.authMail(\'hotmail\');">Hotmail</a></li>\
									<li><a href="javascript:;" onclick="mapness.share.authMail(\'yahoo\');">Yahoo mail</a></li>\
								</ul>\
							</div>\
							<div id="share-List">\
								<h3 class="note2">Add recipients from address book:</h3>\
								<input type="text" class="text" id="share-emailFilter" name="share-emailFilter" />\
								<div class="share-emailImportContacts minor">\
									<ul id="share-emailList"></ul>\
								</div>\
							</div>\
						</div>';
		mapness.ui.modal.init(formHtml, {width: 740, height: 435, addOverlay: false, draggable: draggable});
	}
}



