var global_privacy=-1;
 

/*if (ws_ajax_path_prefix != ""){
	var tempLen = ws_ajax_path_prefix.split("/").length;
	tempLen = tempLen-1;
	if (tempLen > 2){
		ws_ajax_path_prefix="";
		for (var i=0;i<tempLen-2;i++) {
			ws_ajax_path_prefix += "../";	
		}
	}else{
		ws_ajax_path_prefix="";
	}
}else{
	ws_ajax_path_prefix="";
}

if (location.host.indexOf(".com")>-1){
	ws_ajax_path_prefix = "/";
}*/

function submitForm(){
	//alert("2");
	$('#urlReturn').val(document.location.href);
	if(document.location.href.indexOf('"+webDomain+"')>-1)
		{
			$('#commentAddBox form').attr("action",
			"http://www"+document.location.href.substring(document.location.href.indexOf('.'),
			document.location.href.indexOf('.com')+5)+"sites/"+$('#commentAddBox form').attr('action').replace(/\\.\\.\\//g,''));
			if($.trim($('#commentText').val())== '') 
				return false;
			else
				$('#commentAddBox form').submit();
		}
		else{
		$('#commentAddBox form').attr("action",
		 document.location.href.substring(0,(document.location.href.indexOf('//')+2+document.location.href.substring(document.location.href.indexOf('//')+2).indexOf('/')+1))+"sites/"+$('#commentAddBox form').attr('action').replace(/\\.\\.\\//g,''));
		 if($.trim($('#commentText').val())== '') 
		 	return false;
		 else 
		 	$('#commentAddBox form').submit();
		 	
		 };
}
$(document).ready(function() {


	addEditable();

});

function addEditable(){

	var ws_ajax_path_prefix = $("#thickboxContext").val();
	$("#titleBar .inline-edit .noteName").editable(ws_ajax_path_prefix+"changeTitle",{
	onblur:'submit',
	type:'text',
	dataType:'json',
	ajaxType:'jsonp',
	charLimit:30,
	style: 'width:210px;',
    cssclass : "editableSingleLineLabel1", 
	onkeyup:function (inputHandle) {	
			textLimit(inputHandle,30);
		},
	placeholder:'( Click here to add a site title )',
	callback:function (value){
		var responseStr = value.data;
		
		if (value.error != null && value.error != ""){
			if (value.data != value.targetName){
				var dialogueUrl = "#TB_inline?height=150&width=400&inlineId=MessageWindow";
				messageWindow("You already have a site titled '"+value.targetName+"'.  Please give a unique site title for this site.",dialogueUrl);
			}
		}else{
				if (responseStr.replace(/^\s*$/g,'') =="" || responseStr=="null" || responseStr == "( Click here to add a site title )"){
					$(this).attr("class","notEditedLabel noteName");
				}else{
					$(this).attr("class","hasBeenEditedLabel noteName");
				}
		}
	}
	});	
	
	var originalPageName = $(".pageName").text();
	
	$("#titleBar .inline-edit .pageName").editable(ws_ajax_path_prefix+"changeTitle",{
	onblur:'submit',
	type:'text',
	dataType:'json',
	ajaxType:'jsonp',
	charLimit:30,
	style: 'width:210px;',
    cssclass : "editableSingleLineLabel1", 
	onkeyup:function (inputHandle) {	
			textLimit(inputHandle,30);
			//charLimit(inputHandle);
		},
	placeholder:'( Click here to add a page name )',
	callback:function (value){
		var responseStr = value.data;
		
		if (value.error != null && value.error != ""){
			if (value.data != value.targetName){
				var dialogueUrl = "#TB_inline?height=150&width=400&inlineId=MessageWindow";
				messageWindow("You already have a page titled '"+value.targetName+"'.  Please give a unique page name for this site.",dialogueUrl);
			}
		}else{
			if (responseStr.replace(/^\s*$/g,'') =="" || responseStr=="null"){
				//$(this).attr("class","notEditedLabel");
				if ($(this).attr("name") == null){
					$(this).attr("name",originalPageName);
				}
					
					$(this).html($(this).attr("name"));
				
			}else{
				$(this).attr("class","hasBeenEditedLabel");
				$(this).attr("name",responseStr);
			}
		}
	}
	});	
	
	
	var commentFontSize = $("div.commentBody")[0] ? $("div.commentBody")[0].style.fontSize : null;
	commentFontSize = commentFontSize ? "font-size:"+commentFontSize+";" : "font-size:12px;";
	//if comment exists, make it editable
	$(".commentBody .inline-edit p").editable(ws_ajax_path_prefix+"cac",{
	onblur:'submit',
	type:'textarea',
	ajaxType:'jsonp',
	dataType:'json',
	style:commentFontSize+"line-height:1.2;",
	width:'99%',
    cssclass : "editableMultiLineLabel1", 
	rows:8,
	placeholder:'( Click here to add a comment )',
	callback:function (value){
	  value = value.data;
		if (value.replace(/^\s*$/g,'') =="" || value=="null"){
			$(this).attr("class","notEditedLabel");
		}
		adjustFooter();
	}
	}).click(function(){adjustFooter();});	
	
}
	
function checkshare(handle,privacy){

		var path = $(handle).attr("rel");

		var ws_ajax_path_prefix = $("#thickboxContext").val();
		path = path.substr(path.indexOf("/shareNote"));
		
		var crossUs = false;
		if ( window.location.host != ws_ajax_path_prefix.replace("http://","").replace("/","") ){
			//crossUs = true;
		}
		
		var trackUrl = window.location.href.replace("http://","");
		var a = path;
			if (crossUs){
				a += "&trackUrl="+trackUrl;
				a += "&TB_iframe=true";
				a = a.replace("shareNote","shareNoteIfr");
				
			}
									
			tb_initForce(a ,400);	
		

}	

		var twitterWrappter = {
			getTwitterHandle:function (specialCLassName){
			
				if (specialCLassName == null || specialCLassName == ""){
					return $(".object_twitter");
				}else{
					return $(".object_twitter").find("."+specialCLassName);
				}
			},
			buildShellItem:function  (twitterShellStyle,newItem){
				
				newItem.user.profile_background_image_url!=null ? twitterShellStyle.profile_background_image_url =newItem.user.profile_background_image_url : null;
				newItem.user.profile_background_color!=null ? twitterShellStyle.profile_background_color =newItem.user.profile_background_color : null;
				newItem.user.profile_sidebar_border_color!=null ? twitterShellStyle.profile_sidebar_border_color =newItem.user.profile_sidebar_border_color : null;
				newItem.user.profile_text_color!=null ? twitterShellStyle.profile_text_color =newItem.user.profile_text_color : null;
				newItem.user.profile_image_url!=null ? twitterShellStyle.profile_image_url =newItem.user.profile_image_url : null;
				newItem.user.screen_name!=null ? twitterShellStyle.screen_name =newItem.user.screen_name : null;
				newItem.user.name!=null ? twitterShellStyle.name =newItem.user.name : null;
				newItem.user.location!=null ? twitterShellStyle.location =newItem.user.location : null;
				newItem.user.url!=null ? twitterShellStyle.url =newItem.user.url : null;
				newItem.user.description!=null ? twitterShellStyle.description =newItem.user.description : null;
				newItem.user.followers_count!=null ? twitterShellStyle.followers_count =newItem.user.followers_count : 0;
				return twitterShellStyle;
			},
			cleanTwitterText: function(item){
				//add links to any text that looks like a link
				var text = item.text;
				var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
				text = text.replace(exp,"<a href='$1' target='_blank'>$1</a>");
				
				//makes @username link to www.twitter.com/username
				exp = /@([0-9a-z_]+)/i;
				text = text.replace(exp,"<a href='http://www.twitter.com/$1' target='_blank'>@$1</a>");
				return text;
			},
		    createStatus:function(item){
				//we need to add html links to things that look like links
				var text = this.cleanTwitterText(item);
				var subText = "";
				//make the subtext under the status text
				if(item.in_reply_to_screen_name != null && item.in_reply_to_status_id !=null ){
					var replyText =  " in reply to "+item.in_reply_to_screen_name;
					replyText = "<a href=\"http://twitter.com/"+item.in_reply_to_screen_name+"/status/"+item.in_reply_to_status_id+"\" target=\"_blank\">"+replyText+"</a>";
					subText = 	this.getTime(item.created_at)+" from "+item.source+" "+replyText;
				}
				else{
					subText = 	this.getTime(item.created_at)+" from "+item.source;	
				}
				//create the url to reply to this status
				var replyToMarkup = 
						"<a title=\"reply to "+
						item.user.screen_name+
						"\" href=\"http://www.twitter.com/home?status=@"+
						item.user.screen_name+
						"&amp;in_reply_to_status_id="+
						item.id+
						"&amp;in_reply_to="+
						item.user.screen_name+
						"\" target=\"_blank\">&nbsp;</a>"
		
		
				var twStatusTextStyle = "font-size:"+this.getTwitterHandle("twStatusContainer").css("fontSize")+";";
				var twBorder = this.getTwitterHandle("borderStyle").val();
				
				//twStatusTextStyle = twStatusTextStyle + twBorder;
				//alert(twBorder);
				//make a status from the link and add it
				var status = $("<li class=\"twStatus\">"+
									"<div class=\"twStatusText\" style=\""+twStatusTextStyle+"\">"+
										text+
									"</div>"+
									"<div class=\"twStatusSubText\">"+
									subText+
									"</div>"+
									"<div class=\"twReplyTo\">"+
									replyToMarkup+
									"</div>"+
								"</li>");
				this.getTwitterHandle("twStatusContainer").append(status);

				
			},
			getTime: function(item){
				var b=item.split(" ");
				item=b[1]+" "+b[2]+", "+b[5]+" "+b[3];
				var a=Date.parse(item);
				var d=(arguments.length>1)?arguments[1]:new Date();
				var e=parseInt((d.getTime()-a)/1000);
				e=e+(d.getTimezoneOffset()*60);
				if(e<60){return"less than a minute ago"}else{if(e<120){return"about a minute ago"}else{if(e<(60*60)){return(parseInt(e/60)).toString()+" minutes ago"}else{if(e<(120*60)){return"about an hour ago"}else{if(e<(24*60*60)){return"about "+(parseInt(e/3600)).toString()+" hours ago"}else{if(e<(48*60*60)){return"1 day ago"}else{return(parseInt(e/86400)).toString()+" days ago"}}}}}}
			},
			bindHoverStatus: function(){
			  $(".object_twitter li.twStatus").unbind().hover(
				  function () {
				   $(this).find(".twReplyTo a").show();
				  }, 
				  function () {
					$(this).find(".twReplyTo a").hide();
				  }
				);
			},
			setTwitterShell: function(twitterShellStyle){
					
				if(twitterShellStyle){
					//Util.log("Setting twitter shell with following twitterShellStyle: ");
					//console.log(twitterShellStyle);
				}
				else{
					//Util.log("Cleaning twitter shell...");
				}
	
				//if null, then reset the twitter object style
				if(twitterShellStyle==null){
					  var cssObj = {
						'background-image' : 'none',
						'background-color' : 'transparent',
						'border' : 'none'
					  }					
				}
				//if not null, then set the twitter object style
				else{
						
						//style for the shell
						var cssObj = {
							//'backgroundImage' : twitterShellStyle.profile_background_image_url == "none" ? "none" : "url("+twitterShellStyle.profile_background_image_url+")",
							//'background-repeat' : 'repeat',
							//'backgroundColor' : '#'+twitterShellStyle.profile_background_color,
							'border' : '1px solid #'+twitterShellStyle.profile_sidebar_border_color,
							'color' : '#'+twitterShellStyle.profile_text_color
						};
						
						if (this.getTwitterHandle().hasClass("noObjBorder")) {
								cssObj.border = "none";							   
						}
						
						if (this.getTwitterHandle().hasClass("transparentBg")) {
								cssObj.backgroundImage = "none";
								cssObj.backgroundColor = "transparent";	
								
						}else if (this.getTwitterHandle().hasClass("transparent")){
							this.getTwitterHandle("twContainer").css("backgroundImage",twitterShellStyle.profile_background_image_url == "none" ? "none" : "url("+twitterShellStyle.profile_background_image_url+")");
							this.getTwitterHandle("twContainer").css("background-repeat","repeat");
						}
						
						//erase user image
						this.getTwitterHandle("twPic").html("");
						//set new image
						$("<img/>").attr("src", twitterShellStyle.profile_image_url ? twitterShellStyle.profile_image_url : this.defaultUserImage).appendTo(this.getTwitterHandle("twPic"));
						
						
						//get username color
						var usernameStyle = "color:"+this.getTwitterHandle("twUsername").css("color")+";";
						
						//change username
						this.getTwitterHandle("twUsername").html(twitterShellStyle.screen_name ? "<a href=\"http://twitter.com/"+twitterShellStyle.screen_name+"\" style=\""+usernameStyle+"\" target=\"_blank\">"+twitterShellStyle.screen_name+"</a>" : "(User has no posts)");
						this.getTwitterHandle("twSimpleUsername").html(twitterShellStyle.screen_name ? "Twitter:"+twitterShellStyle.screen_name : "");
						//this.getTwitterHandle("twSimpleUsername").html(twitterShellStyle.screen_name ? "Twitter:"+"<a href=\"http://twitter.com/"+twitterShellStyle.screen_name+"\" style=\""+usernameStyle+"\" target=\"_blank\">"+twitterShellStyle.screen_name+"</a>" : "");
						
						
						
						//change details
						this.getTwitterHandle("twUserRealName").html(twitterShellStyle.name ? twitterShellStyle.name : "N/A");
						this.getTwitterHandle("twUserLocation").html(twitterShellStyle.location ? twitterShellStyle.location : "N/A");
						this.getTwitterHandle("twUserURL").html(twitterShellStyle.url ? twitterShellStyle.url : "N/A").attr("href",twitterShellStyle.url ? twitterShellStyle.url : "#");
						
						this.getTwitterHandle("twUserBio").html(twitterShellStyle.description ? twitterShellStyle.description : "N/A");
						this.getTwitterHandle("twFollowers").html(twitterShellStyle.followers_count ? twitterShellStyle.followers_count+" followers" : "" );
				
				}
				//set the visual style for the shell
				this.getTwitterHandle().css(cssObj);
				
			},
			toggleLoading: function(toggleMethod,username){
				if(toggleMethod == "show"){
					//disable the input
					$("#unTwitter").hide();
					$("#unTwitterLoading").show();
					
					//clear the twitter shell
					this.setTwitterShell(null);
					//clear the statuses
					this.getTwitterHandle("twStatusContainer").html("");
					
					//hide the content and show the loading gif
					this.getTwitterHandle("twContainer").hide();
					this.getTwitterHandle("twLoadingUser").html(username);
					this.getTwitterHandle("twLoading").show();
				}
				else{
					
					//enable the input
					$("#unTwitter").show();
					$("#unTwitterLoading").hide();
					
					//hide the loading and show the content
					this.getTwitterHandle("twLoading").hide();
					this.getTwitterHandle("twContainer").show();
				}
				
			}
			
		};


		

		


		
		var initTwitterObject = function() {
			//to blair: blair, given users will browse site via other domains, so it is neccessary to use absolute
			//path to our twitter proxy, mine absolute path is www.splashnote.com, you need to change yours on your machine.
			//var twitterServer = "http://www.splashnote.com/twitterProxy";
			var twitterServer = $("#thickboxContext").val()+"twitterProxy";
			var twUsername = $("#twitterun").val();
			var twCount = $("#twittercnt").val();
			if ($(".object_twitter").size()>0){
				       									
						//hide loading message and show the contents
						twitterWrappter.toggleLoading("show",twUsername);
						//alert(twitterServer+"?un="+twUsername+"&count="+5+"&callback=?");
						$.getJSON(twitterServer+"?un="+twUsername+"&count="+twCount+"&callback=?",function(data){
							
							
											
											//we are going to build the styles by picking the best of each status object
											var twitterShellStyle = {};
											
											//loop through each status for this user
											$.each(data, function(i,item){
												//based on the statuses we get, they may have null or not null styles for the shell, so we need to check each time
												twitterShellStyle = twitterWrappter.buildShellItem(twitterShellStyle,item);											
												//create a new status item
												twitterWrappter.createStatus(item);
											});	
												//bind status to a hover event so reply can show
												twitterWrappter.bindHoverStatus();
																			
												//set the twitter shell
												twitterWrappter.setTwitterShell(twitterShellStyle);
												
												//hide loading message and show the contents
												twitterWrappter.toggleLoading("hide",twUsername);

						});// end getJson	
			}

		}
		
		
var adjustProfilePosition = function (){
	var offsetProfileHeight = 0;
	//check friends
	if ( $(".object_friends").size() >0){
			var oldFriendHeight = $(".object_friends").height();			
			$(".object_friends").css("height","auto");
			var newFriendHeight = $(".object_friends").height();	
			offsetProfileHeight = 	newFriendHeight - 	oldFriendHeight;	
			
			if (offsetProfileHeight < 0 ) offsetProfileHeight = 0;
			//alert(offsetProfileHeight);
	}
	

	//check screen id
	if ( $(".profile_Screen_ID").size() >0){
			var profileFontSize = $(".profile_Screen_ID .profile_answer").css("font-size");
			profileFontSize = Number(profileFontSize.replace("px",""));
			if (profileFontSize == null) profileFontSize = 10;
			var strLen = $(".profile_Screen_ID .profile_answer").html();
			strLen = strLen.length;
			$(".profile_Screen_ID").parent().css("width",strLen*profileFontSize+"px");
			//var newFriendHeight = $(".object_friends").width();	
			//offsetProfileHeight = 	newFriendHeight - 	oldFriendHeight;													
	}	
	//check url
	if ( $(".profile_URL").size() >0){
			var profileFontSize = $(".profile_URL .profile_answer a").css("font-size");
			profileFontSize = Number(profileFontSize.replace("px",""));
			if (profileFontSize == null) profileFontSize = 10;		
			var strLen = $(".profile_URL .profile_answer a").html();
			strLen = strLen.length;
			$(".profile_URL").parent().css("width",strLen*profileFontSize+"px");
	}	
	//check address
	if ( $(".profile_Address").size() >0){
			var profileFontSize = $(".profile_Address .profile_answer a").css("font-size");
			profileFontSize = Number(profileFontSize.replace("px",""));
			if (profileFontSize == null) profileFontSize = 10;		
			var strLen = $(".profile_Address .profile_answer a").html();
			strLen = strLen.length;
			$(".profile_Address").parent().css("width",strLen*profileFontSize+"px");
	}		

	var footer = $("#canSurround div.displayFooter")[0];	
	
	if (footer != null){
		var footerHeight = $(footer).height();

		var footerTop = footer.offsetTop;
		var newFooterTop = footerTop + offsetProfileHeight + 20;
		var newCanvasHeight = newFooterTop + footerHeight ;

		//change height of the canvas if footer overlaps with comments
		$("#canSurround,#canSurround > div").css("height",newCanvasHeight  + "px");
		if ($("#userstatus").val() == "90"){
			$("#editorPane").css("height",newCanvasHeight  + "px");
		}
		//change top of the footer if overlapping
		footer.style.top = newFooterTop  + "px";
		

	}else{
		var canvasHeight =  $("#canSurround").height();
		var newFooterTop = footerTop + offsetProfileHeight;
		var newCanvasHeight = newFooterTop + 20;
		//change height of the canvas if footer overlaps with comments
		$("#canSurround,#canSurround > div").css("height",newCanvasHeight  + "px");
		if ($("#userstatus").val() == "90"){
			$("#editorPane").css("height",newCanvasHeight  + "px");
		}

	}
}

		
jQuery(document).ready(initTwitterObject);
//jQuery(document).ready(adjustProfilePosition);



