// JavaScript Document

function slide(){	
	//attach function below
	attach();
	selectTopic();
	//selectThumb();
	
	//change function
	change();
	
	//post function below
	post();
	
	//blog action
	blogs();
	
	$("#no-img").livequery("click", function(event){
		var val = $(this).prop("checked");
		if(val){
			$(".polaroid").hide();
			$(".new-img").hide();
		}else{
			$(".polaroid").show();
			$(".new-img").show();
		}
	});		
	
	if(dosubmit){
		var posted = $("#posttoolAreaLinkBox-input").val();
		var id = $("#shareLink").attr("status");	
		doAttach(posted,id);
	}
}
		
	function attach(){
		$("#shareLink").livequery("click", function(event){
			var posted = $("#posttoolAreaLinkBox-input").val();
			var id = $(this).attr("status");
			$(".post").fadeTo("fast", 1);
			$("#new-post").remove();
			if(posted!="" && posted!=defdrop)
				doAttach(posted,id);
			return false;
		 });	
	}

	function doAttach(posted,id,response,postst){
		$.ajax({
			global : false,
			beforeSend : function(){
				//$(cur).parent().next().html('<a href="#" style="color:#FFFFFF">Loading</a');
				$("#content-menu, #content").fadeTo("fast", 0.25);
				$("a.newPostNotif").remove();
				$("div.newPost").remove();
				$("title").html("Attaching url. Please wait.");
				$("div.ajax-load-page").show();
				$("div.rockto-stream").stopTime("chk_new");
				if(ajaxCekNewPost) ajaxCekNewPost.abort();
			},
			type: "POST",
			timeout: 50000,
			url: postUrl,
			data: {status:"attach",link:posted, id:id, response:response, postst:postst},
			success: function(msg){
				$("div.ajax-load-page").hide();
				$("title").html(pagetitle);
				var spl = msg.split("##");
				spl[0] = spl[0].replace("  error","error");
				if(spl[0]!="error"){
					$("body").css({"overflow-y":"hidden"});
					$("div.overlay").show();
					$("div.modalPostFeed").html(msg);
					$("div.modalPostFeed").show();
					$("div.overlay").animate({scrollTop:0}, 'slow');
					$("#new-post").find("#postdesc").elastic();
					var type = $("#new-post").find("#type").val();
					var posted = $("#new-post").find("#postedurl").val();
					
					$("#new-post #to").defaultvalue("autocomp","Type tag here (max: 4)");
					$("#new-post #to").autocomplete({
						minLength: 2,
						delay: 0,
						//define callback to format results
						source: function(req, add){
							if(req!="Type tag here (max: 4)"){
								//pass request to server
								topicAutoComplete = $.getJSON(urlx+"/auto?callback=?", req, function(data) {
						
									//create array for response objects
									var suggestions = [];
						
									//process response
									$.each(data, function(i, val){
									if(val){
										suggestions.push(val.name);
									}else{
										$(".ui-menu-item").remove();
											
									}
								});
						
								//pass array to callback
								add(suggestions);
							});
						}
					},
					
					//define select handler
					select: function(e, ui) {
						$("#new-post #to").val("");
						cek = true;
						if($("#friends span").length!=0){
							if($("#friends span").length==4){
								cek = false;
								alert("Sorry, maximum tags reached");
								$("#new-post #to").hide();
								$("ul.ui-autocomplete").hide();
							}else{
								$("#friends span").each(function(index) {
									cur = $(this).find("a").attr("title").replace("Remove ","");
									if(ui.item.value==cur){
										cek = false;
										alert("tag selected already");
										$("#new-post #to").val("");
									}
								});
							}
						}
						//create formatted friend
						if(cek){
							var friend = ui.item.value;
							span = $("<span>").text(friend),
							a = $("<a>").addClass("remove").attr({
								href: "javascript:",
								title: "Remove " + friend
							}).text("x").appendTo(span);
							hid = $("<input>").attr({
								name: "sel[]",
								value: friend,
								type: "hidden"
							}).appendTo(span);
					
							//add friend to friend div
							span.insertBefore("#to");
							$("#new-post #to").val("");
							if($("#friends span").length==4){
								$("#new-post #to").hide();
								$("ul.ui-autocomplete").hide();
							}
							if(topicAutoComplete) topicAutoComplete.abort();
							topicAutoComplete = null;
						}
						return false;
					},
					
						//define select handler
						change: function() {
					
							//prevent 'to' field being updated and correct position
							$("#new-post #to").val("").css("top", 2);
						}
					});
					
					//add click handler to friends div
					$("#friends").click(function(){
					
						//focus 'to' field
						$("#new-post #to").focus();
					});
					
					//add live handler for clicks on remove links
					$(".remove", document.getElementById("friends")).live("click", function(){
					
						//remove current friend
						$(this).parent().remove();
						$("#new-post #to").show();
					
						//correct 'to' field position
						if($("#friends span").length === 0) {
							$("#new-post #to").css("top", 0);
						}
					});
					$(".removeTopic", document.getElementById("friends")).live("click", function(){
							
						//remove current friend
						cur = $(this).next().val().replace("#","");
						$("#new-post").find("li.hide"+cur).show();
						$(this).parent().remove();
						$("#new-post #to").show();
						//correct 'to' field position
						if($("#friends span").length === 0) {
							$("#new-post #to").css("top", 0);
						}
					});
					
					if((type!=4)&&(type!=5)&&(type!=2)){
						cekThumb(posted);
					}
				}else{
					if(spl[1]==undefined){
						$.jGrowl("Error. Invalid Url.");
					}else{
						$.jGrowl(spl[1]);
					}
					$("div.post").fadeTo("fast", 1);
					$("#content-menu, #content").fadeTo("fast", 1);
					$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
					$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
				}
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType == 'timeout') {
						$("div.ajax-load-page").hide();
						$("title").html(pagetitle);
						$.jGrowl("Sorry there's something wrong. Please Refresh Your Browser.");
						$("div.post").fadeTo("fast", 1);
						$("#content-menu, #content").fadeTo("fast", 1);
						$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
						$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
						cekNewPost();
					}
				}
				 catch (e) {}
			}
		});
	}
	
	function postLink(){
		var title = $("#new-post").find("#posttitle").val();
		var desc = $("#new-post").find("#postdesc").val();
		var posted = $("#new-post").find("#postedurl").val();
		var key = $("#new-post").find("#category").val();
		var fb_check = $("#new-post").find("#fb_check").prop("checked");
		var tweet_check = $("#new-post").find("#tweet_check").prop("checked");
		//var cate = $("#new-post").find("#category").val();
		var img = $("#new-post").find("#cur-img").val();
		var attach_id = $("#new-post").find("#attach-id").val();
		var response_id = $("#new-post").find("#response-id").val();
		var ori = $("#new-post").find("#oriurl").val();
		var postst = $("#new-post").find("#postst").val();
		var type = $("#new-post").find("#type").val();
		if($("#friends span").length!=0){
			var tag = [];
			$("#friends span input[type=\'hidden\']").each(function(index) {
				tag.push($(this).val());						 
			});
			if((title!="")&&(desc!="")&&(title!="Click here to enter title")){
				$.ajax({
					global : false,
					beforeSend : function(){
						$("div.overlay").hide();
						$("title").html("Posting url. Please wait.");
						$("div.ajax-load-page").show();
						$("#new-post").remove();
						$("div.modalPostFeed").hide();
						$("body").css({"overflow-y":"auto"});
						$("div.newPost").removeClass("newPost");
						$("a.newPostNotif").remove();
					},
					type: "POST",
					url: postUrl,
					timeout: 90000,
					data: {status:"post",title:title,desc:desc,link:posted,img:img,type:type,attach_id:attach_id,response_id:response_id,tag:tag},
					success: function(msg){
						$("div.ajax-load-page").hide();
						var spl = msg.split("##");
						$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
						$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
						$("title").html(pagetitle);
						$("#new-post").remove();
						if((attach_id=="feed")&&(postst!="single" || postst=="")){
							$(spl[1]).prependTo(".rockto-stream");
							$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
							$("div.response-textfield").find("textarea").elastic();
							$("a[rel*=\'lightbox\']").lightBox();
						}else{
							$.jGrowl("Post successfully added to feed!");
						}
						$("#content-menu, #content, div.post").fadeTo("fast", 1);
						
						if(login!="login"){
							$.ajax({
								type: "POST",
								async: false,
								url: postUrl,
								data: {status:"posturl",link:spl[3]},
								success: function(msg){	
									if(fb_check)
										postFB(spl[3]);
									if(tweet_check)
										postTweet(spl[3]);
								}
							});
						}
						
						if(attach_id=="submit"){
							spl[0] = spl[0].replace("  ","");
							$(".rockto-stream").html("<div class=\"column span-15 box last\" align=\"center\"><h3 style=\"color:#dddddd;letter-spacing:-1px\">Post successfully added to feed!</h3></div>");
							window.location.href = base_url+"single/"+spl[0];
							window.close();
						}
						
						if(response_id!="")
							doResponse(spl[2],response_id,spl[0],ori);	
							
						loadOther();
						cekNewPost();
					},
					error: function(request, errorType, errorThrown){
						 try {
							if (errorType == "timeout") {
								$("div.ajax-load-page").hide();
								$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
								$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
								$("title").html(pagetitle);
								$.jGrowl("Sorry there's something wrong. Please Refresh Your Browser.");
								$("div.post").fadeTo("fast", 1);
								cekNewPost();
							}
						}
						 catch (e) {}
					}
				});
			}else{
				alert("Please enter the title first");	
			}
		}else{
			alert("Please choose at least one topic");
		}
	}
	
	function postFB(posted){
		$.ajax({
			type: "POST",
			url: postUrl,
			data: {status:"post_fb",link:posted},
			success: function(msg){	
			}
		});
	}
	
	function postTweet(posted){
		$.ajax({
			type: "POST",
			url: postUrl,
			data: {status:"post_tweet",link:posted},
			success: function(msg){	
			}
		});
	}
	
	function selectTopic(){		
		$("#new-post").find("a.topics").livequery("click", function(event){								   
			var id = $(this).attr("id");
			var friend = $(this).html();
			//$("div.topics-container").find("li").removeClass("selected");
			//$("#new-post").find("#category").val(id);
			//$(this).parent().addClass("selected");
			cek = true;
			if($("#friends span").length!=0){
				if($("#friends span").length==4){
					cek = false;
					alert("Sorry, maximum tags reached");
					$("#new-post #to").hide();
				}else{
					$("#friends span").each(function(index) {
						cur = $(this).find("a").attr("title").replace("Remove ","");
						if("#"+friend==cur){
							cek = false;
							alert("tag selected already");
							$("#new-post #to").val("");
						}
					});
				}
			}
			//create formatted friend
			if(cek){
				//var friend = ui.item.value;
				span = $("<span>").text("#"+friend),
				a = $("<a>").addClass("removeTopic").attr({
					href: "javascript:",
					title: "Remove " + "#"+friend
				}).text("x").appendTo(span);
				hid = $("<input>").attr({
					name: "sel[]",
					value: "#"+friend,
					type: "hidden"
				}).appendTo(span);
		
				//add friend to friend div
				span.insertBefore("#to");
				$("#new-post #to").val("");
				$(this).parent().addClass("hide"+friend);
				$(this).parent().hide();
				if($("#friends span").length==4)
					$("#new-post #to").hide();
			}
			return false;
		});	
	}
	
	function selectThumb(){
		$("div.imgItem").find("div.polaroid-125").livequery("click", function(event){	
			if(!($(this).hasClass("non-selectable"))){										 	
				if(!($(this).hasClass("selected"))){
					$("div.polaroid-125").removeClass("selected");
					var src = $(this).find("img.img-polaroid-125").attr("src");
					$("#new-post").find("#cur-img").val(src);
					$(this).addClass("selected");
				}else{
					$("div.polaroid-125").removeClass("selected");
					$("#new-post").find("#cur-img").val("");
				}
			}
		});
	}
	
	function cekThumb(posted){
		ajaxCekThumb = $.ajax({
			global : false,
			beforeSend : function(){
				$("title").html("Checking for thumbnail. Please wait.");
				$("#new-post").find("div.post-thumbgallery").html("<div class=\"loading\" align=\"center\" style=\"position:relative;top:40px\"><img src=\"http://www.rockto.com/img/COMMON/ajax-waitamin.gif\" />  <span style=\"color:#eee;font-family:Arial,Helvetica,sans-serif;font-size:36px;font-weight:400;letter-spacing:-1px;margin-left:0.75em;position:relative;top:-15px;text-shadow:#333333 1px 1px;\">Checking thumbnails..</span></div>");
			},
			type: "POST",
			timeout: 120000,
			url: postUrl,
			data: {status:"img",link:posted},
			success: function(msg){
				$("title").html(pagetitle);
				if((msg!="")&&(msg!="  ")&&(msg!="error")&&(msg!="  error")){
					$("#new-post").find("div.post-thumbgallery").html(msg);
					$("#new-post").find("div.post-thumbgallery").show();
					selectThumb();	
				}else{
					$("div.post-thumbgallery").hide();
				}
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType != null) {
						$("title").html(pagetitle);
					}
				}
				 catch (e) {}
			}
		});
	}

	function change(){
				
		$("body").livequery("click", function(event){
			//alert("body");
			$(".do-change").hide();
			$(".new-change").show();
		  });
		
		//hover
		$(".new-change").livequery(function(event){
			$(this).hover(function() {
				$(this).find("span").css({"background-color":"#FFCC66","cursor":"pointer"});
			}, function() {
				$(this).find("span").css({"background-color":"transparent"});
			});
		}); 
		
		//click to change
		$(".new-change").livequery("click", function(event){
			var id = $(this).attr("id");
			var curval = $(this).find("span").html();
			switch(id){
				case 'title':
					$("#new-post").find("#posttitle").val(curval);
					$("#new-post").find("#posttitle").show();
					break;
				
				case 'desc':
					$("#new-post").find("#postdesc").val(curval);
					$("#new-post").find("#postdesc").show();
					break;
			}
			$(this).hide();
			
			event.stopPropagation();

		});
		
		//click to change
		$(".do-change").livequery("click", function(event){
			event.stopPropagation();
		});
		
		$(".do-change").livequery("blur", function(event){
			var id = $(this).attr("id");
			var val = $(this).val();
			if(val!=""){
				var curval = "<span>"+val+"</span>";
				switch(id){
					case 'posttitle':
						$("#new-post").find("#title").html(curval);
						break;
					
					case 'postdesc':
						$("#new-post").find("#desc").html(curval);
						break;
				}
			}else{
				switch(id){
					case 'posttitle':
						var curval = $("#new-post").find("#title").find("span").html();
						$("#new-post").find("#posttitle").val(curval);
						break;
					
					case 'postdesc':
						var curval = $("#new-post").find("#desc").find("span").html();
						$("#new-post").find("#postdesc").val(curval);
						break;
				}
			}
		});
	}
	
	function blogs(){
		$(".blogPost").livequery("click", function(event){
			var posted = $("#addNewBlog-input").val();
			addrss(posted);
			return false;
		 });	
		$(".connectBlog").livequery("click", function(event){
			var posted = $("input[name=\'rss\']").val();
			addrss(posted);
			return false;
		});
	}
	
	function post(){
		$("button").livequery("click", function(event){
			var cur = $(this).parent();
			var val = $(this).html();
			var type = $("#new-post").find("#type").val();
			switch(val){
				case 'cancel':
					$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
					$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
					$("div.overlay").hide();
					$("body").css({"overflow-y":"auto"});
					$("div.modalPostFeed").hide();
					$("#content-menu, #content").fadeTo("fast", 1);
					$("#new-post").remove();
					$("title").html(pagetitle);
					cekNewPost();
					if(ajaxCekThumb) ajaxCekThumb.abort();
					break;	
				case 'submit':
				case 'connect':
					switch(type){
						case 'recentRss':
							connectBlog();
							break;
						
						case 'noRss':
							manualRss();
							break;
						
						case 'newRss':
							newRss();
							break;
							
						default:
							if(login!="login"){
								postLink();
							}else{
								var answer = confirm("WARNING - You're about to post as \"AlienRockers\".\r\nThis is because either your login session has expired or you're not login yet (Or you're not even registered).\r\n\r\nOK to Continue? (or choose Cancel to refresh this page)");
								if (answer){	
									postLink();
								}else{
									$("body").fadeTo("fast",0.5);
									location.reload();	
								}
							}
							break;
					}
					break;
			}
			return false;
		 });	
	}
		
	function addrss(posted){
		if((posted!="")&&(posted!=dfblog)){
			$.ajax({
				global : false,
				beforeSend : function(){
					$("#content-menu, #content").fadeTo("fast", 0.25);
					$("title").html("Checking url for rss. Please wait.");
					$("div.ajax-load-page").show();
					$("div.rockto-stream").stopTime("chk_new");
					if(ajaxCekNewPost) ajaxCekNewPost.abort();
				},
				type: "POST",
				timeout: 10000,
				url: postUrl,
				data: {status:"addRssFirst",posted:posted},
				success: function(msg){
					$("div.ajax-load-page").hide();
					$("title").html(pagetitle);
					var spl = msg.split("##");
					if(spl[0]!="error"){
						//$(msg).prependTo("#content-wrapper");
						$("body").css({"overflow-y":"hidden"});
						$("div.overlay").show();
						$("div.modalPostFeed").html(msg);
						$("div.modalPostFeed").show();
						$("div.overlay").animate({scrollTop:0}, 'slow');
						type = $("#new-post").find("#type").val();
						if(type=="noRss")
							$("#rss").defaultvalue("default-noRss","Direct rss url...");
						else if(type=="newRss"){
							$("#new-post #to").defaultvalue("autocomp","Type tag here (max: 4)");
							$("#new-post #to").autocomplete({
								minLength: 2,
								delay: 0,
								//define callback to format results
								source: function(req, add){
									if(req!="Type tag here (max: 4)"){
										//pass request to server
										topicAutoComplete = $.getJSON(urlx+"/auto?callback=?", req, function(data) {
								
											//create array for response objects
											var suggestions = [];
								
											//process response
											$.each(data, function(i, val){
											if(val){
												suggestions.push(val.name);
											}else{
												$(".ui-menu-item").remove();
													
											}
										});
								
										//pass array to callback
										add(suggestions);
									});
								}
							},
							
							//define select handler
							select: function(e, ui) {
								$("#new-post #to").val("");
								cek = true;
								if($("#friends span").length!=0){
									if($("#friends span").length==4){
										cek = false;
										alert("Sorry, maximum tags reached");
										$("#new-post #to").hide();
										$("ul.ui-autocomplete").hide();
									}else{
										$("#friends span").each(function(index) {
											cur = $(this).find("a").attr("title").replace("Remove ","");
											if(ui.item.value==cur){
												cek = false;
												alert("tag selected already");
												$("#new-post #to").val("");
											}
										});
									}
								}
								//create formatted friend
								if(cek){
									var friend = ui.item.value;
									span = $("<span>").text(friend),
									a = $("<a>").addClass("remove").attr({
										href: "javascript:",
										title: "Remove " + friend
									}).text("x").appendTo(span);
									hid = $("<input>").attr({
										name: "sel[]",
										value: friend,
										type: "hidden"
									}).appendTo(span);
							
									//add friend to friend div
									span.insertBefore("#to");
									$("#new-post #to").val("");
									if($("#friends span").length==4){
										$("#new-post #to").hide();
										$("ul.ui-autocomplete").hide();
									}
									if(topicAutoComplete) topicAutoComplete.abort();
									topicAutoComplete = null;
								}
								return false;
							},
							
								//define select handler
								change: function() {
							
									//prevent 'to' field being updated and correct position
									$("#new-post #to").val("").css("top", 2);
								}
							});
							
							//add click handler to friends div
							$("#friends").click(function(){
							
								//focus 'to' field
								$("#new-post #to").focus();
							});
							
							//add live handler for clicks on remove links
							$(".remove", document.getElementById("friends")).live("click", function(){
							
								//remove current friend
								$(this).parent().remove();
								$("#new-post #to").show();
							
								//correct 'to' field position
								if($("#friends span").length === 0) {
									$("#new-post #to").css("top", 0);
								}
							});
							$(".removeTopic", document.getElementById("friends")).live("click", function(){
									
								//remove current friend
								cur = $(this).next().val().replace("#","");
								$("#new-post").find("li.hide"+cur).show();
								$(this).parent().remove();
								$("#new-post #to").show();
								//correct 'to' field position
								if($("#friends span").length === 0) {
									$("#new-post #to").css("top", 0);
								}
							});		
						}
					}else{
						$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
						if(spl[1]==undefined)
							$.jGrowl("Error. Invalid Url.");
						else
							$.jGrowl(spl[1]);
						$(".post").fadeTo("fast", 1);
						$("#content-menu, #content").fadeTo("fast", 1);
					}
				},
				error: function(request, errorType, errorThrown){
					 try {
						if (errorType == 'timeout') {
							$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
							$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
							$("div.overlay").hide();
							$("body").css({"overflow-y":"auto"});
							$("div.modalPostFeed").hide();
							$("#content-menu, #content").fadeTo("fast", 1);
							$("#new-post").remove();
							$("div.ajax-load-page").hide();
							$("title").html(pagetitle);
							cekNewPost();
							if(ajaxCekThumb) ajaxCekThumb.abort();
							$.jGrowl("Sorry there's something wrong. Please Try Again.");
						}
					}
					 catch (e) {}
				}
			});
		}
	}
	
	function connectBlog(){
		var blogID = $("#new-post").find("#rss").val();
		if(blogID){
			$.ajax({
				global : false,
				beforeSend : function(){
					$("div.overlay").hide();
					$("title").html("Connecting Blog. Please wait.");
					$("div.ajax-load-page").show();
					$("#new-post").remove();
					$("div.modalPostFeed").hide();
					$("body").css({"overflow-y":"auto"});
					$("div.newPost").removeClass("newPost");
					$("a.newPostNotif").remove();
				},
				type: "POST",
				url: postUrl,
				timeout: 90000,
				data: {status:"connectRss",blogID:blogID},
				success: function(msg){
					$("div.ajax-load-page").hide();
					$("title").html(pagetitle);
					var spl = msg.split("##");
					if(spl[0]!="error"){
						$("div.rockto-stream").html(streamLoad);
						$page = 1;
						key = 'blogs/'+blogID;
						callStream(page);
						$("#topicsRecommendation").html(spl[0]);
						$("#new-post").remove();
						$("#content-menu, #content, div.post").fadeTo("fast", 1);
						$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
						$(".notConnectBlog").remove();
						$.jGrowl("Blog Successfully connected!");
						window.location = "#/blogs/"+blogID;
					}
				},
				error: function(request, errorType, errorThrown){
					 try {
						if (errorType == "timeout") {
							$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
							$("title").html(pagetitle);
							$("#new-post").remove();
							$.jGrowl("Sorry there's something wrong. Please Refresh Your Browser.");
							$("#content-menu, #content, div.post").fadeTo("fast", 1);
						}
					}
					 catch (e) {}
				}
			});
		}
	}
	
	function newRss(){
		var rss = $("#new-post").find("#rss").val();
		var title = $("#new-post").find("#posttitle").val();
		var desc = $("#new-post").find("#postdesc").val();
		var id = $("#new-post").find("#secret").val();
		var favicon = $("#new-post").find("#favicon").val();
		if($("#friends span").length!=0){
			var tag = [];
			$("#friends span input[type=\'hidden\']").each(function(index) {
				tag.push($(this).val());						 
			});
			if((title!="")&&(title!="Click here to enter title")&&(desc!="")){
				$.ajax({
					global : false,
					beforeSend : function(){
						$("div.overlay").hide();
						$("title").html("Connecting Blog. Please wait.");
						$("div.ajax-load-page").show();
						$("#new-post").remove();
						$("div.modalPostFeed").hide();
						$("body").css({"overflow-y":"auto"});
						$("div.newPost").removeClass("newPost");
						$("a.newPostNotif").remove();
					},
					type: "POST",
					url: postUrl,
					timeout: 90000,
					data: {status:"newRss",posted:rss,title:title,desc:desc,id:id,favicon:favicon,tag:tag},
					success: function(msg){
						$("div.ajax-load-page").hide();
						$("title").html(pagetitle);
						var spl = msg.split("##");
						if(spl[0]!="error"){
							$("div.rockto-stream").html(streamLoad);
							$page = 1;
							key = 'blogs/'+spl[0];
							callStream(page);
							$("#topicsRecommendation").html(spl[1]);
							$("#new-post").remove();
							$("#content-menu, #content, div.post").fadeTo("fast", 1);
							$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
							$.jGrowl("Blog Successfully connected!");
							window.location = "#/blogs/"+blogID;
						}
					},
					error: function(request, errorType, errorThrown){
						 try {
							if (errorType == "timeout") {
								$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
								$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
								$("div.overlay").hide();
								$("body").css({"overflow-y":"auto"});
								$("div.modalPostFeed").hide();
								$("#content-menu, #content").fadeTo("fast", 1);
								$("#new-post").remove();
								$("div.ajax-load-page").hide();
								$("title").html(pagetitle);
								cekNewPost();
								if(ajaxCekThumb) ajaxCekThumb.abort();
								$.jGrowl("Sorry there's something wrong. Please Refresh Your Browser.");
							}
						}
						 catch (e) {}
					}
				});
			}else{
				alert("Please enter the title first");
			}
		}else{
			alert("Please choose at least one topic");
		}
	}
	
	function manualRss(){
		var rss = $("#new-post").find("#rss").val();
		var url = $("#new-post").find("#url").val();
		if(rss!="" && rss!="Direct rss url..."){
			$.ajax({
				global : false,
				beforeSend : function(){
					$("title").html("Checking direct rss. Please wait.");
					$("div.overlay").hide();
					$(".ajax-load-page").show();
					$("#new-post").remove();
					$("div.modalPostFeed").hide();
					$("body").css({"overflow-y":"auto"});
					$("div.newPost").removeClass("newPost");
				},
				type: "POST",
				url: postUrl,
				timeout: 90000,
				data: {status:"noRss",posted:rss,url:url},
				success: function(msg){
					var spl = msg.split("##");
					$("div.ajax-load-page").hide();
					$("title").html(pagetitle);
					if(spl[0]!="error"){
						$("body").css({"overflow-y":"hidden"});
						$("div.overlay").show();
						$("div.modalPostFeed").html(msg);
						$("div.modalPostFeed").show();
						$("div.overlay").animate({scrollTop:0}, 'slow');
						if(type=="noRss")
							$("#rss").defaultvalue("default-noRss","Direct rss url...");	
						else if(type=="newRss"){
							$("#new-post #to").defaultvalue("autocomp","Type tag here (max: 4)");
							$("#new-post #to").autocomplete({
								minLength: 2,
								delay: 0,
								//define callback to format results
								source: function(req, add){
									if(req!="Type tag here (max: 4)"){
										//pass request to server
										topicAutoComplete = $.getJSON(urlx+"/auto?callback=?", req, function(data) {
								
											//create array for response objects
											var suggestions = [];
								
											//process response
											$.each(data, function(i, val){
											if(val){
												suggestions.push(val.name);
											}else{
												$(".ui-menu-item").remove();
													
											}
										});
								
										//pass array to callback
										add(suggestions);
									});
								}
							},
							
							//define select handler
							select: function(e, ui) {
								$("#new-post #to").val("");
								cek = true;
								if($("#friends span").length!=0){
									if($("#friends span").length==4){
										cek = false;
										alert("Sorry, maximum tags reached");
										$("#new-post #to").hide();
										$("ul.ui-autocomplete").hide();
									}else{
										$("#friends span").each(function(index) {
											cur = $(this).find("a").attr("title").replace("Remove ","");
											if(ui.item.value==cur){
												cek = false;
												alert("tag selected already");
												$("#new-post #to").val("");
											}
										});
									}
								}
								//create formatted friend
								if(cek){
									var friend = ui.item.value;
									span = $("<span>").text(friend),
									a = $("<a>").addClass("remove").attr({
										href: "javascript:",
										title: "Remove " + friend
									}).text("x").appendTo(span);
									hid = $("<input>").attr({
										name: "sel[]",
										value: friend,
										type: "hidden"
									}).appendTo(span);
							
									//add friend to friend div
									span.insertBefore("#to");
									$("#new-post #to").val("");
									if($("#friends span").length==4){
										$("#new-post #to").hide();
										$("ul.ui-autocomplete").hide();
									}
									if(topicAutoComplete) topicAutoComplete.abort();
									topicAutoComplete = null;
								}
								return false;
							},
							
								//define select handler
								change: function() {
							
									//prevent 'to' field being updated and correct position
									$("#new-post #to").val("").css("top", 2);
								}
							});
							
							//add click handler to friends div
							$("#friends").click(function(){
							
								//focus 'to' field
								$("#new-post #to").focus();
							});
							
							//add live handler for clicks on remove links
							$(".remove", document.getElementById("friends")).live("click", function(){
							
								//remove current friend
								$(this).parent().remove();
								$("#new-post #to").show();
							
								//correct 'to' field position
								if($("#friends span").length === 0) {
									$("#new-post #to").css("top", 0);
								}
							});
							$(".removeTopic", document.getElementById("friends")).live("click", function(){
									
								//remove current friend
								cur = $(this).next().val().replace("#","");
								$("#new-post").find("li.hide"+cur).show();
								$(this).parent().remove();
								$("#new-post #to").show();
								//correct 'to' field position
								if($("#friends span").length === 0) {
									$("#new-post #to").css("top", 0);
								}
							});		
						}
					}else{
						$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
						$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
						$("div.overlay").hide();
						$("body").css({"overflow-y":"auto"});
						$("div.modalPostFeed").hide();
						$("#content-menu, #content").fadeTo("fast", 1);
						$("#new-post").remove();
						$("div.ajax-load-page").hide();
							$("title").html(pagetitle);
						cekNewPost();
						if(ajaxCekThumb) ajaxCekThumb.abort();
						if(spl[1]==undefined)
							$.jGrowl("Error. Invalid Url.");
						else
							$.jGrowl(spl[1]);
					}
				},
				error: function(request, errorType, errorThrown){
					 try {
						if (errorType == "timeout") {
							$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
							$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
							$("div.overlay").hide();
							$("body").css({"overflow-y":"auto"});
							$("div.modalPostFeed").hide();
							$("#content-menu, #content").fadeTo("fast", 1);
							$("#new-post").remove();
							$("div.ajax-load-page").hide();
							$("title").html(pagetitle);
							cekNewPost();
							if(ajaxCekThumb) ajaxCekThumb.abort();	
							$.jGrowl("Sorry there's something wrong. Please Refresh Your Browser.");
						}
					}
					 catch (e) {}
				}
			});
		}
	}

