// JavaScript Document

function image(src,id,cur,source,s,h){
	if(s==undefined) s = "";
	if(h==undefined) h = "";
	saveImg = $.ajax({
		global : false,
		beforeSend : function(){
		},
		type: "GET",
		url: base_url+"image",
		//kalo mau more pake ini
		data: { src : src, id : id, s : s, h : h },
		success: function(msg){
		}
	});	
	if(cur && source){
		cur.src = source;	
		saveImg = null;
		return true;
	}
}

function jsInit(){
	$searchBox = $("#searchBoxInput");
	$dialogLogin = $("#dialog-login");
}

function ajax(urlSet,userID){
	//init	
	urlx = base_url+"ajax/ajaxGlobal";
	postUrl = base_url+"slide/submit";
	urlUser = base_url+"user";
	login = userID;
	if(key!='welcome'){
		$(window).hashchange( function(){
			var spPath = window.location.pathname.split("/");
			if(spPath[1]!="single" && spPath[1]!="launcher" && spPath[1]!="about" && spPath[1]!="faq" && spPath[1]!="tos" && spPath[1]!="privacy" && spPath[1]!="business" && spPath[1]!="welcome" && spPath[1]!="register" && spPath[1]!="search" && spPath[1]!="complete" && spPath[1]!="deal" && spPath[1]!="submit" && spPath[1]!="freebies" && spPath[1]!="account" && spPath[1]!="responses"){
					urlSearch = location.search;				
					urlHash = location.hash;
					if(urlHash==""){
						if(window.location.pathname!="/"){
							var repl = "/#!"+window.location.pathname;
							if(ref)
								repl = repl +'/'+ref;
							var newUrl = window.location.href.replace(window.location.pathname+urlSearch,repl);
							window.location.replace(newUrl);
							return false;
						}
					}
					if((urlHash=="#!/feed" || urlHash=="#!/welcome" || urlHash=="") && login=="login"){
						if(urlSearch)
							window.location.href = base_url+"welcome"+urlSearch;
						else
							window.location.href = base_url+"welcome";
					}else
						ajaxContainer(urlHash);
			}else{
				//call first page stream
				if(spPath[1]=="account")
					account(login);	
				else if(spPath[1]=="deal"){
					urlHash = location.hash;	
					if(urlHash=="#!/reports"){
						//$("a[action=\'getReports\']").click();
						$("div.memberCode").show();
						//$("div.qrCode").slideDown();	
						$("a[action=\'getReports\']").html("hide reports");	
						$("a[action=\'getReports\']").attr("title","hide reports");
						$("a[action=\'getReports\']").attr("action","hideReports");
						$("#search_code").defaultvalue("dfMembCode",'search code...');
					}
				}
				callStream(page);
			}
		});
		$(window).hashchange();
	}
	jsInit();
	$("#addNewTopic-input").defaultvalue("dftopic",dftopic);
	$searchBox.defaultvalue("dfsearch",dfsearch);
	$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
	/*$("#posttoolAreaLinkBox-input").livequery("focus", function(event){										  	
		$("#shareLink").show();
	});*/
	
	//front
	front();
	//action function (delete,retweet,hide)
	action();
	//rocks function
	rocks();
	//stream function (paging, category, etc)
	stream();
	//cek post
	btnNewPost();
	//comment box
	comment();
	//store
	store();
}

function front(){
	if(key=='welcome')
		$("#loginBox").show();
	
	$("#loginToggle").livequery("click", function(event){
		$("#loginBox").toggle();
		event.stopPropagation();
	});
	
	$("#loginBox").livequery("click", function(event){
		event.stopPropagation();
	});
	
	$("#reg_email_user").defaultvalue("dfuname","Username");
	$("#reg_password").defaultvalue("dfpas","Password");
	
	$("#reg_pass, #reg_password").livequery("focus", function(event){
		//if($("#reg_pass").val()!='')
		this.type = 'password';
	});
	$("#reg_pass, #reg_password").livequery("blur", function(event){
		if($(this).val()=='Password')
			this.type = 'text';
	});
	loginForm();
}

function loginForm(){
	$("form.loginForm").ajaxForm({
		global : false,
		beforeSubmit: cekLogin,
		beforeSend : function(){
			$("#loginBox button").html("Validating...");
			$("#loginBox button").attr("disabled","disabled");
			$("#loginBox input").attr("disabled","disabled");
			//$("#loginBox a").hide();
		},
		success: function(data) {
			if(data){
				var sp = data.split("##");
				if(sp[0]=="error"){
					alert(sp[1]);
					$("#loginBox button").html("Log in");
					$("#loginBox button").removeAttr("disabled");
					$("#loginBox input").removeAttr("disabled");
					$("#reg_email_user").defaultvalue("dfuname","Username");
					$("#reg_password").defaultvalue("dfpas","Password");
				}else
					location.replace(data);	
			}
		}
	});
}

function cekLogin(formData, jqForm, options){
	if($("#reg_email_user").fieldValue()=="Username" || $("#reg_email_user").fieldValue()=="" || $("#reg_password").fieldValue()=="Password" || $("#reg_password").fieldValue()==""){
		alert("Please enter your username/password to continue.");
		return false;
	}
}

function ajaxContainer(container){
	var css = $("link[rel=\'stylesheet\']").addClass("curCss");
	var cek = container.split("/");
	if(cek[0]=="#!" || cek[0]==""){
		if(ajaxChgContainer) ajaxChgContainer.abort();
		container = container.replace("#!/","");
		ajaxChgContainer = $.ajax({
			global : false,
			beforeSend : function(){
				$("div.mainContent").hide();
			},
			type: "POST",
			url: urlx+"/chgContainer",
			timeout: 30000,			
			data: { container : container, status : "init" },
			success: function(msg){
				$("title").html(pagetitle);
				var spl = msg.split("##");
				if(spl[0]){
					if(ajaxCallStream) ajaxCallStream.abort();
					time="";
					cate="";
					type="";
					tag="";
					//$(".mainContent").html("");
					var newTitle = spl[0];
					var newCss = spl[1];
					$("title").html(newTitle);
					$(spl[1]).insertAfter("link.curCss");
					newCss = $("link.newCss").attr("href");
					if(newCss!=undefined){
						$("link.curCss").remove();
						$("link.newCss").removeClass("newCss");
					}
					ajaxChgContainerLeft = $.ajax({
						global : false,
						type: "POST",
						url: urlx+"/chgContainer",
						data: { container : container, status : "left" },
						timeout: 120000,
						dataType: 'html',
						success: function(left){
							if(left){
								$("#contentLeft").html(left);
								var conspl = container.split("/");
								switch(conspl[0]){
									case 'settings':
										settings('account');
										key = "";
										break;
									case 'blogs':
									case 'popular':
									case 'deals':
										$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
										key = container;
										break;
									case 'feed':
									case '':
										$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
										key = (login!='login' && (login!=49 || container=='feed')) ? 'feed' : 'public';
										break;
									case 'notification':
										$("a[class^=\'notif\']").attr("class","notifOff chgContainer");
										key = "";
										break;
									case 'topic':
										key = container;
										break;
									default:
										if(conspl[1]==undefined) conspl[1] = "rocktofolio";
										switch(conspl[1]){
											case 'commented':
											case 'rocked':
												key = container;
												break;
											case 'feed':
											case 'badges':
											case 'business':
											case 'rocktofolio':
												key = conspl[0];
												break;
											default:
												key = '';
												break;
										}
										break;	
								}
								
								page = 1;
								callStream(page);
								$searchBox.defaultvalue("dfsearch",dfsearch);
								$("a[rel*=\'lightbox\']").lightBox();
								$("div.mainContent").show();
					
								loadTips(cek[0]);
							}else{
								$("div.mainContent").hide();
								$.jGrowl("Something went wrong, please try again. <a href=\"#\" class=\"action\" action=\"refreshContainer\">refresh</a>", { sticky: true });
							}
							ajaxChgContainerLeft = null;
						},
						error: function(request, errorType, errorThrown){
							 try {
								if (errorType != null) {
									$("div.mainContent").hide();
									$.jGrowl("Something went wrong, please try again. <a href=\"#\" class=\"action\" action=\"refreshContainer\">refresh</a>", { sticky: true });
								}
							}
							 catch (e) {}
						}
					});
					
					ajaxChgContainerRight = $.ajax({
						global : false,
						beforeSend : function(){
							$("#contentRight").hide();
							//$(".overlay").show();
						},
						type: "POST",
						url: urlx+"/chgContainer",
						data: { container : container, status : "right", ref : ref },
						timeout: 30000,
						success: function(right){	
							if(right){
								$("#contentRight").html(right);
								$("#contentRight").show();
	
								//quick reg
								$("#reg_email").defaultvalue("dfemail","Email");
								$("#reg_username").defaultvalue("dfunameq","Username");
								$("#reg_pass").defaultvalue("dfpass","Password");
								$("#addNewTopic-input").defaultvalue("dftopic",dftopic);
								//tag
								topic();
								
								var conspl = container.split("/");
								switch(conspl[0]){
									case 'blogs':
										newPost = 'blogs';
										id = conspl[1];
										$("#topicsAdd").hide();
										break;
									default:
										randno = Math.floor ( Math.random() * suggestion.length ); 
										newPost = suggestion[randno];
										id = null;
										$("#topicsAdd").show();
										break;
								}
								if(login!='login'){
									$.ajax({
										type: "POST",
										url: urlx+"/suggestion",
										data: {status:"all",post:newPost,id:id},
										success: function(msg){
											$("#topicsRecommendation").html(msg);
											$("#topicsRecommendation").fadeTo("fast",1);
											if(conspl[0]=="blogs")
												$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
										}
									});
								}
							}
							ajaxChgContainerRight = null;
						}
					});
					ajaxChgContainer = null;
				}
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType != null) {
						$.jGrowl("Something went wrong, please try again. <a href=\"#\" class=\"action\" action=\"refreshContainer\">refresh</a>", { sticky: true });
					}
				}
				 catch (e) {}
			}
		});
	}
}

function callStream(page){
	if(key!='single' && key!='submit'){
		if(page==1 && ajaxCallStream) ajaxCallStream.abort();
		if(page==1 && ajaxCekNewPost) ajaxCekNewPost.abort();
		//if(ajaxChgContainer) ajaxChgContainer.abort();
		//if(ajaxChgStream) ajaxChgStream.abort();
		if(ajaxRockedList) ajaxRockedList.abort();
		if(ajaxCommentList) ajaxCommentList.abort();
		if(ajaxResponseList) ajaxResponseList.abort();
		var conClass = $("a.chgContainer").attr("class");
		if(key){
			ajaxCallStream = $.ajax({
				global : false,
				beforeSend : function(){
					$("div.errorStream").remove();
					$(".paging").show();
					$("input.more").attr("class","more-hide");
					$("span.ajaxMore").show();
				},
				type: "POST",
				url: urlx+"/stream",
				timeout: 120000,			
				data: { status : key, page : page, time : time, cate : cate, type : type, tag : tag, login : login },
				dataType: 'text',
				success: function(msg){
					var spl = msg.split("##");
					if(spl[0]!="error"){
						$(".paging").remove();
						if(page==1){
							$("div.subTool ul").removeClass("hide");
							$("div.rockto-stream").html(spl[0]);
						}else{
							$(spl[0]).appendTo("div.rockto-stream");
						}
						$(spl[2]).appendTo("div.rockto-stream");					
						$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
						$("div.response-textfield").find("textarea").elastic();
						$(".tips-south").tipsy({gravity: 's',opacity:0.8});
						$("a[rel*=\'lightbox\']").lightBox();
	
						var avail = $("div.rockto-stream h2").html();
						var nextPage = $("input.more").attr("page");
						ajaxCallStream = null;
						if(avail!='No Post available' && avail!='No More Posts available' && avail!='No More Topics available' && avail!='No More Rockers available' && avail!='No Deals available' && avail!='No More Deals available' && nextPage!=0 ){
							if(page==1){
								page = nextPage;
								callStream(page);
							}
							cekNewPost();
							loadOther();
						}else{
							if(key!="blogs")
								$("input.more").remove();	
							cekNewPost();
							loadOther();
						}
					}
				},
				error: function(request, errorType, errorThrown){
					 try {
						if (errorType != null) {
							if(page==1){
								$("div.rockto-stream").html(errorStream);
								$("a[action=\'refreshStream\']").attr("page",page);
							}
							else{
								$(".paging").hide();
								$(errorStream).appendTo("div.rockto-stream");
								$("a[action=\'refreshStream\']").attr("page",page);
							}
							ajaxCallStream = null;
							$("span.ajaxMore").hide();
							//$(".more-hide").attr("class","more");
						}
					}
					 catch (e) {}
				}
			});	
		}
	}else{
		$("#reg_email").defaultvalue("dfemail","Email");
		$("#reg_username").defaultvalue("dfunameq","Username");
		$("#reg_pass").defaultvalue("dfpass","Password");
		$("a[rel*=\'lightbox\']").lightBox();
		loadOther();	
	}
}

function loadTips(cek){
	if(cek=="feed" || cek==""){
		ajaxTips = $.ajax({
			global : false,
			type: "POST",
			url: urlx+"/other",
			data: { status : "tips", login : login },
			//dataType: 'html',
			success: function(tips){
				//if(login==2)
//					alert(tips);
				if(tips)	
					$(tips).prependTo("#contentLeft");
			}
		});
	}	
}

function loadOther(){
	//$(".post").livequery("each", function(event){
	if(ref!='adsfly'){
		$("div.cached").each(function(index) {
			var postID = $(this).find("input[name=\'postID\']").val();
			var postKey = $(this).find("input[name=\'key\']").val();
			if(postID){
				loadRockedList(postID,postKey);
				loadResponseList(postID);
				loadCommentList(postID,postKey);
				loadEmbedLy();
				$(this).removeClass("cached");
			}
		});
	}
}

function loadEmbedLy(){
	$('a.embedly').embedly({
			key:'ce27ea98b6bb11e0a0724040d3dc5c07',
			maxWidth:510
   });	
}

function loadRockedList(postID,postKey){
	var classs = "div."+postID;
	var curRckd = $(classs).find("div.rockedList").html();
	if(curRckd==""){
		ajaxRockedList = $.ajax({
			global : false,
			type: "POST",
			url: urlx+"/other",
			data: { postID : postID, status : "rockedList", postKey : postKey, login : login },
			success: function(msg){
				if(msg){
					var spl = msg.split("##");
					$(classs).find("span.countRock").text(spl[0]);
					$(classs).find("div.rockedList").html(spl[1]);
				}
			}
		});		
	}
}

function loadCommentList(postID,postKey){
	var classs = "div."+postID;
	var curComment = $(classs).find("div.commentList").html();
	if(curComment==""){
		ajaxCommentList = $.ajax({
			global : false,
			type: "POST",
			url: urlx+"/other",
			data: { postID : postID, status : "commentList", postKey : postKey, login : login },
			success: function(msg){
				//alert(msg);
				if(msg){
					$(classs).find("div.commentList").html(msg);
				}
			}
		});		
	}
}

function loadResponseList(postID){
	var classs = "div."+postID;
	var curResp = $(classs).find("div.responsePost").html();
	if(curResp==""){
		ajaxResponseList = $.ajax({
			global : false,
			type: "POST",
			url: urlx+"/other",
			data: { postID : postID, status : "responseList", login : login },
			success: function(msg){
				if(msg){
					$(classs).find("div.responsePost").html(msg);
				}
			}
		});		
	}
}

function comment(){
	$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
	$("div.response-textfield").find("textarea").elastic();
	$("div.response-textfield").find("textarea").livequery("focus", function(event){
		$(this).css({"height":"50px","color":"#000"});																  	
		$(this).parent().find("input.submit").show();
	});
	$("div.response-textfield").find("textarea").livequery("blur", function(event){
		if($(this).val()==dfcomment){
			$(this).css({"color":"#ccc"});		
		}
	});
	$("div.response-textfield").find("input.submit").livequery("click", function(event){
		var com = $(this).parent().find("textarea").val();
		var post_id = $(this).parent().find("textarea").attr("id");
		var status = $(this).attr("id");
		if((com!="")&&(com!=dfcomment)){	
			var classs = "div."+post_id;
			$.ajax({
				global : false,
				beforeSend : function(){
					$("div.response-textfield").find("textarea").attr("disabled","disabled");
					$("div.response-textfield").find("input.submit").attr("disabled","disabled");
					$("div.response-textfield").find("input.submit").attr("value","submitting...");
				},		   
				type: "POST",
				url: urlx+"/comment",
				data: { id : post_id, com : com, status : status, login : login },
				timeout: 60000,
				success: function(msg){
					$("div.response-textfield").find("textarea").removeAttr("disabled");
					$("div.response-textfield").find("input.submit").removeAttr("disabled");
					$("div.response-textfield").find("input.submit").attr("value","Reply");
					var spl = msg.split("##");
					if((msg!="")&&(spl[0]!="error")){
						$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
						$(classs).find("div.commentList").html(spl[0]);
						commentEmail(post_id,com);
						cekResponse(post_id,com,status);
					}else{
						$.jGrowl("Something went wrong, please try again.");
					}
					return false;	
				},
				error: function(request, errorType, errorThrown){
					 try {
						if (errorType != null) {
							$("div.response-textfield").find("textarea").removeAttr("disabled");
							$("div.response-textfield").find("input.submit").removeAttr("disabled");
							$("div.response-textfield").find("input.submit").attr("value","Reply");
							$.jGrowl("Something went wrong, please try again.");
						}
					}
					 catch (e) {}
				}
			});
		}
		return false;
	});
}

function commentEmail(post_id,com){
	$.ajax({
		global : false,   
		type: "POST",
		url: urlx+"/commentOther",
		data: { id : post_id, com : com, login : login },
		success: function(msg){	
		}
	});	
	
	$.ajax({
		global : false,   
		type: "POST",
		url: urlx+"/commentEmail",
		data: { id : post_id, com : com, login : login },
		success: function(msg){	
		}
	});	
}

function cekResponse(post_id,com,status){
	$.ajax({
		global : false,   
		type: "POST",
		url: urlx+"/commentResponse",
		data: { id : post_id, com : com, login : login },
		timeout: 60000,
		success: function(msg){
			if(msg){
				var spl = msg.split("##");
				if(spl[0]!='existed'){
					var resplink = spl[0];
					doAttach(resplink,'',post_id,status);
				}else{
					doResponse(spl[1],post_id);	
				}
			}
			return false;	
		}
	});		
}

function doResponse(resp,post_id,resp_id,old){
	var classs = "div."+post_id;	
	$(classs).find("div.responsePost").html(resp);
	if((resp_id!='')&&(old!='')){
		$.ajax({
			global : false,   
			type: "POST",
			url: urlx+"/commentUpdate",
			data: { post_id : post_id, resp_id : resp_id, old : old, login : login },
			timeout: 60000,
			success: function(msg){
				if(msg){
					var spl = msg.split("##");
					var comm = ".comment-"+spl[0];
					var txtmsg = spl[1];
					$(comm).find("span").html(txtmsg);
				}
			}
		});
	}
}	

function cekNewPost(){
	if(login!="login"){
		//new post		
		$("div.rockto-stream").everyTime(240000,'chk_new',function() {
			var latest = $("#latest").val();
			if((latest!="")&&(latest!=undefined)){
				var sp = latest.split("-");
				ajaxCekNewPost = $.ajax({
					type: "POST",
					url: urlx+"/cekNewPost",
					data: { id : latest, login : login },
					success: function(msg){
						var s = msg.split("##");
						if(s[0]!=0 && s[0]!="" && s[0]!="0"){
							if(parseInt(s[0])>20){
								$("div.rockto-stream").stopTime("chk_new");
								s[0] = 20;
							}
							if(sp[1]=="feed"){
								var newTime = $("input.newTime").val();
								if(newTime!="block"){
									$("a.newPostNotif").remove();
									$("div.newPost").remove();
									$("input.newTime").remove();
									newTime = s[2]+"-"+s[3]+"-"+s[4]+"-"+s[5];
									if(newTime!=latest){
										$(s[1]).prependTo("div.rockto-stream");
										$("<input type=\"hidden\" class=\"newTime\" value=\""+newTime+"\"/>").prependTo("div.rockto-stream");
										if(s[0]!=0 && s[0]!="0") $("title").html("("+s[0]+") "+pagetitle);
									}
								}
								$("#newPostTab").hide();
								$("#newPostTab span").html(0);
							}else{
								switch(sp[1]){
									case 'public':
										$(".btnMyFeed").html("My Feed ("+s[0]+")");
										break;
									case 'feed':
									default:
										$("#newPostTab span").html(s[0]);
										$("#newPostTab").show();
										break;
								}
								if(s[0]!=0 && s[0]!="0")
									$("title").html("("+s[0]+") "+pagetitle);
							}
						}
						ajaxCekNewPost = null;
					}
				});
			}else{
				$("div.rockto-stream").stopTime("chk_new");	
			}
		});
	}
}	

function btnNewPost(){
	$("a.newPostNotif").livequery("click", function(event){
		if(ajaxCallStream) ajaxCallStream.abort();
		if(ajaxCekNewPost) ajaxCekNewPost.abort();
		if(ajaxChgContainer) ajaxChgContainer.abort();
		if(ajaxChgContainerLeft) ajaxChgContainerLeft.abort();
		if(ajaxChgContainerRight) ajaxChgContainerRight.abort();
		if(ajaxRockedList) ajaxRockedList.abort();
		if(ajaxCommentList) ajaxCommentList.abort();
		if(ajaxResponseList) ajaxResponseList.abort();
		var newTime = $("input.newTime").val();
		$("input.newTime").val("block");
		$("div.rockto-stream").stopTime("chk_new");									   
		$(".newPost:not(.hide)").show();
		$("div.newPost").removeClass("newPost");
		$("a.newPostNotif").remove();
		$("#latest").val(newTime);
		$("title").html(pagetitle);
		$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
		$("div.response-textfield").find("textarea").elastic();
		$(".tips-south").tipsy({gravity: 's',opacity:0.8});
		$("a[rel*=\'lightbox\']").lightBox();
		$("a.newPostNotif").remove();
		$("div.newPost").remove();
		$("#latest").oneTime(20000,'startCheck', function(){
			$("input.newTime").remove();
			loadOther();
			cekNewPost();
		});
		return false;
	});		
}

function action(){
	$(window).scroll(function(){
		var bottom = ($(document).height() - $(window).height()-1500);
		//if($(window).scrollTop() == bottom && login!="login"){
		if($(window).scrollTop() >= bottom && login!="login"){
			var newPage = $("input.more").attr("page");
			if(newPage!=undefined){
				page = newPage;
				callStream(page);
			}
		}
	});
	
	$("#btnBackToTop").livequery("click", function(event){
		$('html, body').animate({scrollTop:0}, 'slow');	
		return false;
	});
	
	$(".fol-twit").livequery("click", function(event){
		var cID = $(this).attr("id");
		alert(cID);
		if(cID){
			$.ajax({
				type: "POST",
				url: urlx+"/folTwit",
				data: { login : login, cID : cID },
				success: function(msg){
					alert(msg);
				}
			});
		}
	});
	
	//new notif
	$("#notifAlert").everyTime(120000,'chk_notif',function() {
		$.ajax({
			type: "POST",
			url: urlx+"/cekNewNotif",
			data: { login : login },
			success: function(msg){
				if(msg!='login'){
					var notif = (msg!=0) ? "notifOn" : "notifOff";
					$("a[class^=\'notif\']").attr("class",notif);
	//				if(msg!=0) $("#menuNotif").html("Notifications ("+msg+")");
				}else{
					login = "login";
					window.location.href = base_url+"welcome?logout=session";	
				}
			}
		});
	});
	
	$("body").livequery("click", function(event){
		$("#homeStatMore").hide();
		$("#homeMenuMore").hide();
		$("#loginBox").hide();
		$("#btnMenuMore").removeClass("selected2");
	});
	
	$("#homeStat").livequery("click", function(event){
		$("#homeStatMore").toggle();	
		event.stopPropagation();
	});
	
	$("#btnMenuMore").livequery("click", function(event){
		if($(this).hasClass("selected2")){
			$(this).removeClass("selected2");
		}else{
			$(this).addClass("selected2");
		}
		$("#homeMenuMore").toggle();	
		event.stopPropagation();
	});
	
	$(".content-menu-btn").livequery(function(event){
		$(this).hover(function() {
			$(this).find("a.delTab").show();
		}, function() {
			$(this).find("a.delTab").hide();
		});
	});
	
	$("a.addTab").livequery("click", function(event){
		var id = $(this).attr("tabID");
		var name = $(this).html();
		var cur = $(this);
		$.ajax({
			global : false,
			beforeSend : function(){
				$("#smallLoader").show();
				$("#btnPlus").hide();
				$(cur).hide();
			},
			type: "POST",
			url: urlx+"/addTab",
			data: { name : name, id : id },
			success: function(msg){	
				if(msg){
					$("#smallLoader").hide();
					$("#btnPlus").show();
					$(cur).remove();
					$(".content-menu-btn").removeClass("selected");
					$(msg).insertBefore("#btnMenuMore");
					$("#btnMenuMore").click();
					var list = $("#homeMenuMore a").length;
					if(list<1)
						$("#btnMenuMore").hide();
					var status = $(cur).attr("href").replace("/","");
					var type = status;
					var lastStatus = status;
					ajaxChgStream = $.ajax({
						global : false,
						beforeSend : function(){
							$(".stream").html("<br/>"+streamLoad);
						},
						type: "POST",
						url: urlx+"/stream",
						timeout: 50000,
						data: { status : status, login : login },
						success: function(msg){	
							$(".content-menu-btn").removeClass("selected");
							$("a[type=\'"+type+"'\]").parent().addClass("selected");
							var spl = msg.split("##");
							$(".stream").html(spl[0]);
							page = 1;
							key = status;
							callStream(page);
							$(".rocker-list img").fadeTo("fast", 0.8);
							if((lastStatus=="feed")||(lastStatus=="blogs")){
								$("#filter").html(spl[1]);
								$("#filter").show();
							}else{
								$("#filter").html("");
								$("#filter").hide();
							}
							
							$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
							if(lastStatus=="blogs"){
								$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
							}else{
								$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
								$("div.response-textfield").find("textarea").elastic();
							}
							$(".tips-south").tipsy({gravity: 's',opacity:0.8});
							$("a[rel*=\'lightbox\']").lightBox();
							ajaxChgStream = null;
						},
						error: function(request, errorType, errorThrown){
							 try {
								if (errorType != null) {
									$(".stream").html(lastCur);
									$.jGrowl("Something went wrong, please try again.");
								}
							}
							 catch (e) {}
						}
					});
				}
			}
		});
		window.location = "#"+$(this).attr("href");
		return false;
	});
	
	$("a.delTab").livequery("click", function(event){
		var answer = confirm("Are you sure want to remove this tab?");
		if (answer){
			var id = $(this).attr("tabID");
			var name = $(this).next().html();
			var cur = $(this);
			$.ajax({
				global : false,
				beforeSend : function(){
					$(cur).removeClass("delTab");
				},
				type: "POST",
				url: urlx+"/delTab",
				data: { name : name, id : id },
				success: function(msg){	
					$(cur).addClass("delTab");
					if(msg){
						if($(cur).parent().hasClass("selected")){
							$(".content-menu-btn").find("a.btnStream[stream=\'feed\']").click();
							$("#homeTab").addClass("selected");
						}
						$(cur).parent().remove();
						$(msg).insertAfter("#homeMenuTop");
						$("#btnMenuMore").show();
					}
				}
			});
		}
	});
	
	$("a.delTips").livequery("click", function(event){
		//var answer = confirm("Are you sure want to remove this tab?");
//		if (answer){
			var cur = $(this);
			var tips = $(cur).attr("tips");
			var general = $(cur).attr("general");
			$.ajax({
				global : false,
				beforeSend : function(){
					$(cur).parent().remove();
				},
				type: "POST",
				url: urlx+"/delTips",
				data: { tips : tips, login : login, general : general },
				success: function(msg){
				}
			});
		//}
	});
	
	$("#addBusiness").livequery("click", function(event){
		$("#btnMenuMore").click();
		$.ajax({
			global : false,
			beforeSend : function(){
				//$(cur).parent().next().html('<a href="#" style="color:#FFFFFF">Loading</a');
				$("div.ajax-load-page").show();
				$("#content-menu, #content").fadeTo("fast", 0.25);
				$("div.rockto-stream").stopTime("chk_new");
				if(ajaxCekNewPost) ajaxCekNewPost.abort();
			},
			type: "POST",
			timeout: 50000,
			url: urlx+"/addBusiness",
			data: {},
			success: function(msg){
				$("title").html(pagetitle);
				$("body").css({"overflow-y":"hidden"});
				$("div.ajax-load-page").hide();
				$("div.overlay").show();
				$("div.modalPostFeed").html(msg);
				$("div.modalPostFeed").show();
				$("div.overlay").animate({scrollTop:0}, 'slow');
				$("#business_name").defaultvalue("dfname","Business Name");
				if($("#business_contact").val()=="") $("#business_contact").defaultvalue("dfcontact","Contact Name");
				if($("#business_mail").val()=="") $("#business_mail").defaultvalue("dfmail","Business Email");
				if($("#business_address").html()=="") $("#business_address").defaultvalue("dfaddress","Business Address");
				if($("#business_phone").val()=="") $("#business_phone").defaultvalue("dfphone","Business Phone");
				$("#business_web").defaultvalue("dfweb","http://");
				$("#business_desc").defaultvalue("dfdesc","Business Description");
				$("a.btnConfirm").livequery("click", function(event){
					var id = $(this).attr("id");
					if(id!=undefined){
						switch(id){
							case 'submitBusiness':
								submitBusiness();
								break;
							case 'closeBusiness':
								var business = $("#dialogBusiness").html();
								if(business!=undefined){
									$("#dialogBusiness").remove();
									$("#content-menu, #content, div.post").fadeTo("fast", 1);
									$("div.overlay").hide();
									$("div.modalPostFeed").hide();
									$("body").css({"overflow-y":"auto"});
									cekNewPost();
								}
								break;	
						}
						return false;
					}
				});
			}
		});
		//window.location = "#"+$(this).attr("href");
		return false;
	});
	
	$("#dialogBusinessForm input[name=\'business_name\']").livequery("change", function(event){
		businessNameExist = "";
		var name = $(this).val();
		cekBusinessName(name);
	});
	
	$("a.chgContainer").livequery("click", function(event){
		if(ajaxCekNewPost) ajaxCekNewPost.abort();
		if(ajaxChgContainer) ajaxChgContainer.abort();
		if(ajaxChgContainerLeft) ajaxChgContainerLeft.abort();
		if(ajaxChgContainerRight) ajaxChgContainerRight.abort();
		if(ajaxChgStream) ajaxChgStream.abort();
		var container = $(this).attr("chgTo");
		doChgContainer(container,$(this));
		return false;
	});
	
	//$("#blogList").livequery("change", function(event){
//		var id = $(this).val();
//		if(id){
//			$(".delBlogs").removeClass("hide");
//			$(".delBlogs").attr("post",id);
//			$("a.chgStream").removeClass("selected");
//			$("div.rockto-stream").html(streamLoad);
//			key = "blogs/"+id;
//			page = 1;
//			callStream(page);	
//			window.location = "#/blogs/"+id;
//			return false;
//		}
//	});

		$(".blogList").livequery(function(event){
			$(this).hover(function() {
				var postID = $(this).attr("post");
				$(this).append("&nbsp;<a href=\"\" class=\"delBlogs action\" action=\"deleteBlogs\" post=\""+postID+"\">x</a>");
			}, function() {
				$(".delBlogs").remove();
			});
		});
	
	$(".action").livequery("click", function(event){
		if(login!="login"){
			var action = $(this).attr("action");
			var id = $(this).attr("post");
			switch(action){
				case 'refreshStream':
					page = $(this).attr("page");
					if(page==1)
						$("div.rockto-stream").html("<br/>"+streamLoad);
					callStream(page);
					break;
				case 'refreshContainer':
					$("#jGrowl div.close").click();
					ajaxContainer(urlHash);
					break;
				case 'startCampaign':
				case 'stopCampaign':
				case 'deleteCampaign':
				case 'hideQRCode':
				case 'getQRCode':
				case 'getReports':
				case 'hideReports':
				case 'getDetails':
					campaignAction($(this),action,id);
					break;
				case 'refreshSingle':
				case 'refreshSuggest':
				case 'hideTag':
				case 'showTag':
				case 'followTopic':
				case 'unfollowTopic':
					doTag(action,$(this),id);
					break;
				case 'deletePost':
					doDelete(id);
					break;
				case 'deleteBlogs':
					doDeleteBlogs($(this).parent(),id);
					break;	
				case 'deleteComment':
					doDeleteComment(id);
					break;
				case 'blogFilter':
					doBlogFilter($(this),id);
					break;
			}
		}else{
			$dialogLogin.jqmShow();
		}
		return false;								 
	 });
	
	$("div.similar a").livequery("click", function(event){
		var id = $(this).attr("id");
		var status = $(this).attr("status");
		var classs = "div."+status+"-"+id+":not(.newPost)";
		$(classs).show();
		$("a[id=\'"+id+"\'][status=\'"+status+"\']").parent().remove();
		//$(this).parent().remove();
		return false;
	});
	
	$(".rocker-list img, .btn-int, .badges-gallery li a img, .btn.sharing img").livequery(function(event){	
		$(this).hover(function() {
			$(this).fadeTo("fast", 1);
		}, function() {
			if($(this).attr("class")=="btn-int"){
				$(this).fadeTo("fast", 0.6);
			}else{
				$(this).fadeTo("fast", 0.8);
			}
		});
	});
	$("div.playVid").livequery("click", function(event){
		var id =$(this).attr("id");
		var classs = "div."+id;
		var vid = $(classs).find("input.vid").val();
		$.ajax({
			type: "POST",
			url: urlx+"/video",
			data: { vid : vid, id : id },
			success: function(msg){	
				if(msg){
					$(classs).find(".player:not(.info)").html(msg);
					$(classs).find(".player").show();
					$(classs).find(".noPlayer").hide();
				}
			}
		});
	});
	$("div.playMp3").livequery("click", function(event){
		var id = $(this).attr("id");
		var classs = "div."+id;
		var mp3 = $(classs).find("input.mp3").val();
		$.ajax({
			type: "POST",
			url: urlx+"/audio",
			data: { mp3 : mp3, id : id },
			success: function(msg){	
				if(msg){
					$(classs).find(".player:not(.info)").html(msg);
					$(classs).find(".player").show();
					$(classs).find(".noPlayer").hide();
				}
			}
		});
	});
}

function submitBusiness(){
	if(login!='login'){
		var status = new Array(1,1,1,1,1,1);
		var ffocus = 0;
		var name = $("#dialogBusinessForm input[name=\'business_name\']").val();
		if(name=="" || name=="Business Name"){
			status[0] = 1;
			$("#dialogBusinessForm #errorName").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorName\" class=\"errorForm\">Please enter the official name of this business</span>").insertAfter("#dialogBusinessForm input[name=\'business_name\']");
			if(!ffocus){
				$("#dialogBusinessForm input[name=\'business_name\']").focus();
				ffocus = 1;
			}
		}else{
			cekBusinessName(name);
			if(businessNameExist){
				status[0] = 1;
				$("#dialogBusinessForm #errorName").remove();
				$("&nbsp;<span style=\"color:red;\" id=\"errorName\" class=\"errorForm\">This name has already been used by another business</span>").insertAfter("#dialogBusinessForm input[name=\'business_name\']");
				if(!ffocus){
					$("#dialogBusinessForm input[name=\'business_name\']").focus();
					ffocus = 1;
				}
			}else{
				status[0] = 0;	
				$("#dialogBusinessForm #errorName").remove();
			}
		}
		var contact = $("#dialogBusinessForm input[name=\'business_contact\']").val();
		if(contact=="" || contact=="Contact Name"){
			status[1] = 1;
			$("#dialogBusinessForm #errorContact").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorContact\" class=\"errorForm\">Please enter the contact name for this business</span>").insertAfter("#dialogBusinessForm input[name=\'business_contact\']");
			if(!ffocus){
				$("#dialogBusinessForm input[name=\'business_contact\']").focus();
				ffocus = 1;
			}
		}else{
			status[1] = 0;	
			$("#dialogBusinessForm #errorContact").remove();
		}
		var email = $("#dialogBusinessForm input[name=\'business_mail\']").val();
		if(email=="" || email=="Business Email"){
			status[2] = 1;
			$("#dialogBusinessForm #errorMail").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorMail\" class=\"errorForm\">Please enter the email for this business</span>").insertAfter("#dialogBusinessForm input[name=\'business_mail\']");
			if(!ffocus){
				$("#dialogBusinessForm input[name=\'business_mail\']").focus();
				ffocus = 1;
			}
		}else{
			if(!isValidEmail(email)){
				status[2] = 1;
				$("#dialogBusinessForm #errorMail").remove();
				$("&nbsp;<span style=\"color:red;\" id=\"errorMail\" class=\"errorForm\">Please enter the valid email format</span>").insertAfter("#dialogBusinessForm input[name=\'business_mail\']");
				if(!ffocus){
					$("#dialogBusinessForm input[name=\'business_mail\']").focus();
					ffocus = 1;
				}
			}else{
				status[2] = 0;	
				$("#dialogBusinessForm #errorMail").remove();
			}
		}
		var addr = $("#dialogBusinessForm textarea[name=\'business_address\']").val();
		if(addr=="" || addr=="Business Address"){
			status[3] = 1;
			$("#dialogBusinessForm #errorAddress").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorAddress\" class=\"errorForm\">Please enter the address of this business</span>").insertAfter("#dialogBusinessForm textarea[name=\'business_address\']");
			if(!ffocus){
				$("#dialogBusinessForm textarea[name=\'business_address\']").focus();
				ffocus = 1;
			}
		}else{
			status[3] = 0;	
			$("#dialogBusinessForm #errorAddress").remove();
		}
		var phone = $("#dialogBusinessForm input[name=\'business_phone\']").val();
		if(phone=="" || phone=="Business Phone"){
			status[4] = 1;
			$("#dialogBusinessForm #errorPhone").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorPhone\" class=\"errorForm\">Please enter the phone number of this business</span>").insertAfter("#dialogBusinessForm input[name=\'business_phone\']");
			if(!ffocus){
				$("#dialogBusinessForm input[name=\'business_phone\']").focus();
				ffocus = 1;
			}
		}else{
			status[4] = 0;	
			$("#dialogBusinessForm #errorPhone").remove();
		}
		var web = $("#dialogBusinessForm input[name=\'business_web\']").val();
		var desc = $("#dialogBusinessForm textarea[name=\'business_desc\']").val();
		if(desc=="" || desc=="Business Description"){
			status[5] = 1;
			$("#dialogBusinessForm #errorDesc").remove();
			$("&nbsp;<span style=\"color:red;\" id=\"errorDesc\" class=\"errorForm\">Please enter the description of this business</span>").insertAfter("#dialogBusinessForm textarea[name=\'business_desc\']");
			if(!ffocus){
				$("#dialogBusinessForm textarea[name=\'business_desc\']").focus();
				ffocus = 1;
			}
		}else{
			status[5] = 0;	
			$("#dialogBusinessForm #errorDesc").remove();
		}
		if(status=="0,0,0,0,0,0"){
			$.ajax({
				global : false,
				beforeSend : function(){
					$("div.ajax-load-page").show();
					$("#dialogBusiness").remove();
					$("div.modalPostFeed").hide();
					$("div.overlay").hide();
				},
				type: "POST",
				url: urlx+"/submitBusiness",
				data: { name : name, contact : contact, email : email, addr : addr, phone : phone, web : web, desc : desc },
				success: function(msg){	
					$("div.ajax-load-page").hide();
					$("div.overlay").show();
					$("div.modalPostFeed").html(msg);
					$("div.modalPostFeed").show();
					$("#dialog-redeem").jqmShow();
					$("a.btnConfirm").livequery("click", function(event){
						var id = $(this).attr("id");
						if(id=='closeBusiness'){
							var business = $("#dialogBusiness").html();
							if(business!=undefined){
								$("#dialogBusiness").remove();
								$("#content-menu, #content, div.post").fadeTo("fast", 1);
								$("div.overlay").hide();
								$("div.modalPostFeed").hide();
								$("body").css({"overflow-y":"auto"});
								cekNewPost();
							}
							return false;
						}
					});
				}
			});
		}
	}
	return false;
}

function campaignAction(cur,action,id){
	var classs = "div."+id;
	switch(action){
		case 'startCampaign':
			var answer = confirm("Are you sure want to start this campaign?");
			if (answer){
				$.ajax({
					global : false,
					beforeSend : function(){
					},		
					type: "POST",
					url: urlx+"/startCampaign",
					data: { id : id },
					success: function(msg){
						$(cur).html("stop campaign");
						$(cur).attr("title","Stop Campaign");
						$(cur).attr("action","stopCampaign");
						$(cur).parent().find("#stDate").html(msg);
						$.jGrowl("Campaign succesfully started");
					}
				});
			}
			break;
		case 'stopCampaign':
			var answer = confirm("Are you sure want to stop this campaign?");
			if (answer){
				$.ajax({
					global : false,
					beforeSend : function(){
					},		
					type: "POST",
					url: urlx+"/stopCampaign",
					data: { id : id },
					success: function(msg){
						$(cur).html("start campaign now");
						$(cur).attr("title","Start Campaign");
						$(cur).attr("action","startCampaign");
						$.jGrowl("Campaign succesfully stopped");
					}
				});
			}
			break;
		case 'deleteCampaign':
			var answer = confirm("Are you sure want to delete this campaign?\r\n(Note: This action is UNDOABLE)");
			if (answer){
				$.ajax({
					global : false,
					beforeSend : function(){
					},		
					type: "POST",
					url: urlx+"/deleteCampaign",
					data: { id : id },
					success: function(msg){
						if(msg=="success"){
							$(classs).fadeTo("fast",0);
							$(classs).slideUp();
							$(classs).remove();
							$.jGrowl("Campaign succesfully deleted");
						}else{
							$.jGrowl("Something went wrong, please try again later.");
						}
					}
				});
			}
			break;
		case 'getQRCode':
			$(classs).find("div.qrCode").show();
			//$("div.qrCode").slideDown();	
			$(cur).html("hide qrcode");	
			$(cur).attr("title","hide qrcode");
			$(cur).attr("action","hideQRCode");
			break;
		case 'hideQRCode':
			$(classs).find("div.qrCode").hide();
			//$("div.qrCode").slideUp();	
			$(cur).html("get qrcode");
			$(cur).attr("title","get qrcode");
			$(cur).attr("action","getQRCode");
			break;
		case 'getReports':
			$(classs).find("div.memberCode").show();
			//$("div.qrCode").slideDown();	
			$(cur).html("hide reports");	
			$(cur).attr("title","hide reports");
			$(cur).attr("action","hideReports");
			$("#search_code").defaultvalue("dfMembCode",'search code...');
			break;
		case 'hideReports':
			$(classs).find("div.memberCode").hide();
			//$("div.qrCode").slideUp();	
			$(cur).html("get reports");
			$(cur).attr("title","get reports");
			$(cur).attr("action","getReports");
			break;
		case 'getDetails':
			$.ajax({
				global : false,
				beforeSend : function(){
				},		
				type: "POST",
				url: urlx+"/getDetails",
				data: { id : id },
				success: function(msg){
					var spl = msg.split("##");
					if(spl[0]!="error"){
						$("#tableList").hide();
						$(msg).insertAfter("#tableList");
						$("#doShowAll").show();
					}else{
						$.jGrowl("Something went wrong, please try again later.");
					}
				}
			});
			break;
	}
}

function doBlogFilter(cur,id){
	if(id){
		var href = $(cur).attr("href");
		$(".delBlogs").removeClass("hide");
		$(".delBlogs").attr("post",id);
		$("a.blogList").removeClass("selected");
		$(cur).addClass("selected");
		$("div.rockto-stream").html(streamLoad);
		key = "blogs/"+id;
		page = 1;
		$('html, body').animate({scrollTop:0}, 'fast');	
		callStream(page);	
		window.location = "#"+href;
		return false;
	}
}

function topic(){
	$("#addNewTopic-input").autocomplete({
		minLength: 2,
		delay: 0,
		//define callback to format results
		source: function(req, add){
			if(req!=dftopic){
				//pass request to server
				$.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) {
		var cek = true;
		//create formatted friend
		if(cek){
			var friend = ui.item.value;
			if(friend!=undefined && friend!="" && friend!=dftopic){
				friend = friend.replace("#","");
				$.ajax({
					global : false,
					beforeSend : function(){
						//$(this).removeClass("addTopic");
					},
					type: "POST",
					url: urlx+"/addTopic",
					data: { newTopic : friend },
					success: function(msg){	
						//$(this).addClass("addTopic");
						if(msg!="error"){
							$("<li><span><a href=\"http://www.rockto.com/topic/"+friend+"\">"+friend+"</a><a href=\"#\" class=\"btnExcludeTopics action\" action=\"unfollowTopic\">x</a><span href=\"#\" class=\"notThis clearfix\"></span></span></li>").prependTo("#topicsFollowed ul");
							$(".remove", ".tagContainer").click();
						}else{
							alert("You've already followed this topic");		
						}
						$("#addNewTopic-input").val("");
					}
				});
			}
		}
		return false;
	},
	
		//define select handler
		change: function() {
	
			//prevent 'to' field being updated and correct position
			//$("#addNewTopic-input").val("").css("top", 2);
		}
	});
	
	//add click handler to friends div
	$(".tagContainer").livequery("click", function(event){	
	
		//focus 'to' field
		$("#addNewTopic-input").focus();
	});
	
	//add live handler for clicks on remove links
	$(".remove", ".tagContainer").live("click", function(){
	
		//remove current friend
		$(this).parent().remove();
	
		//correct 'to' field position
		if($(".tagContainer span").length === 0) {
			$("#addNewTopic-input").val("");
			$("#addNewTopic-input").defaultvalue("dftopic",dftopic);
			$("#addNewTopic-input").show();
			$("#addNewTopic-input").css("top", 0);
		}
	});
	
	$(".addTopic").livequery("click", function(event){	
		var newTopic = $(".tagContainer span input[name=\'topic\']").val();
		if(newTopic!=undefined && newTopic!=""){
			$.ajax({
				global : false,
				beforeSend : function(){
					$(this).removeClass("addTopic");
				},
				type: "POST",
				url: urlx+"/addTopic",
				data: { newTopic : newTopic },
				success: function(msg){	
					$(this).addClass("addTopic");
					if(msg!="error"){
						$("<li><span><a href=\"#\">"+newTopic+"</a><a href=\"#\" class=\"btnExcludeTopics\">x</a><span href=\"#\" class=\"notThis clearfix\"></span></span></li>").prependTo("#topicsFollowed ul");
						$(".remove", ".tagContainer").click();
					}else{
						alert("You've already followed this topic");		
					}
				}
			});
		}else{
			alert("Please choose the topic first!");	
		}
	});
}

function doTag(action,cur,post){
	switch(action){
		case 'showTag':
			$("li.tagHide").fadeTo("fast",1);
			$("li.tagHide").slideDown();
			//$(".tagHide").show();
			$(cur).html('Less');
			$(cur).attr("action","hideTag");
			$(cur).oneTime(30000,'timeTag', function(){
				$(".tagHide").hide();
				$(cur).html('More');
				$(cur).attr("action","showTag");
			});
			break;	
		case 'hideTag':
			$(cur).stopTime("timeTag");
			$("li.tagHide").hide();
			$(cur).html('More');
			$(cur).attr("action","showTag");
			break;
		case 'followTopic':
		case 'unfollowTopic':
			var id = $(cur).attr("id");
			var suggest = $("input[name=\'suggests\']").val();
			var friend = $(cur).prev().html();
			var topiclink = (action=='followTopic') ? urlx+"/addTopic" : urlx+"/removeTopic";
			$.ajax({
				global : false,
				beforeSend : function(){
					$(cur).parent().parent().fadeTo("fast",0.5);
					$(cur).removeAttr("action");
				},
				type: "POST",
				url: topiclink,
				data: { newTopic : friend },
				success: function(msg){	
					if(action=='followTopic'){
						$.ajax({
							type: "POST",
							url: urlx+"/suggestion",
							data: {status:"single",id:id,suggest:suggest,post:"topic"},
							success: function(msg){
								var spl = msg.split("##");
								$(cur).parent().parent().replaceWith(spl[0]);
								$("input[name=\'suggests\']").val(spl[1]);
							}
						});
					}else{
						$(cur).parent().parent().remove();	
					}
				}
			});
			break;
		case 'refreshSingle':
			var id = $(cur).attr("id");
			var suggest = $("input[name=\'suggests\']").val();
			$.ajax({
				beforeSend : function(){
					$(cur).parent().fadeTo("fast",0.5);
					$(cur).removeAttr("action");
				},		
				type: "POST",
				url: urlx+"/suggestion",
				data: {status:"single",id:id,suggest:suggest,post:post},
				success: function(msg){
					var spl = msg.split("##");
					$(cur).parent().replaceWith(spl[0]);
					$("input[name=\'suggests\']").val(spl[1]);
				}
			});
			break;
		case 'refreshSuggest':
			$.ajax({
				beforeSend : function(){
					$("#topicsRecommendation").fadeTo("fast",0.5);
					$(cur).removeAttr("action");
				},		
				type: "POST",
				url: urlx+"/suggestion",
				data: {status:"all",post:post},
				success: function(msg){
					$("#topicsRecommendation").html(msg);
					$("#topicsRecommendation").fadeTo("fast",1);
					$(cur).attr("action","refreshSuggest");
				}
			});
			break;
	}
	return false;	
}

function doDelete(id){
	var classs = "div."+id;
	var postKey = $(classs).find("input[name=\'key\']").val();
	var answer = confirm("Are you sure want to delete this post?\r\n(Note: This action is UNDOABLE)");
	if (answer){
		$.ajax({
			global : false,
			beforeSend : function(){
			},		
			type: "POST",
			url: urlx+"/deleteStream",
			data: { id : id },
			success: function(msg){
				if(msg=="success"){
					if(postKey!='single'){
						$(classs).fadeTo("fast",0);
						$(classs).slideUp();
						$(classs).remove();
					}else{
						window.location.href = base_url;
					}
					$.jGrowl("Post successfully deleted!");
				}else{
					$.jGrowl("Something went wrong, please try again later.");
				}
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType != null) {
						$.jGrowl("Something went wrong, please try again later.");
					}
				}
				 catch (e) {}
			}
		});
	}	
}

function doDeleteComment(id){
	var classs = "div.comment-"+id;
	var answer = confirm("Are you sure want to delete this comment?\r\n(Note: This action is UNDOABLE)");
	if (answer){
		$.ajax({
			global : false,
			type: "POST",
			url: urlx+"/deleteComment",
			data: { id : id, login : login },
			success: function(msg){
				if(msg=="success"){
					$(classs).fadeTo("fast",0);
					$(classs).slideUp();
					$(classs).remove();
					$.jGrowl("Comment successfully deleted!");
				}else{
					$.jGrowl("Something went wrong, please try again later.");
				}
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType != null) {
						$.jGrowl("Something went wrong, please try again later.");
					}
				}
				 catch (e) {}
			}
		});
	}	
}

function doDeleteBlogs(cur,id){
	if(id){
		var answer = confirm("Are you sure want to unsubscribe this blog?");
		if (answer){
			$.ajax({
				global : false,
				beforeSend : function(){
					//$("#blogList").attr("disabled","disabled");
					$('html, body').animate({scrollTop:0}, 'fast');	
					$("div.rockto-stream").html(streamLoad);
				},		
				type: "POST",
				url: urlx+"/deleteBlogs",
				data: { id : id },
				success: function(msg){
					//$("#blogList").removeAttr("disabled");
					//$("#blogList").find("option[value=\'"+id+"\']").remove();
					//$(".delBlogs").addClass("hide");
					$(cur).remove();
					key = "blogs";
					page = 1;
					callStream(page);
					window.location = "#/blogs";
					return false;
				}
			});
		}
	}
}

function rocks(){
	$("a.rocksOff").livequery("click", function(event){
		return false;											
	});
	$("a.btnRock").livequery("click", function(event){
		if(login!="login"){
			var id = $(this).attr("id");
			var classs = "div."+id;
			var title = $(classs).find(".title").html();	
			var status = $(this).attr("status");	
			if(status=="blogs"){
				var posted = $(this).prev().val();
				var id = $("#shareLink").attr("status");	
				doAttach(posted,id);
			}else{
				
				//tweet
				$.ajax({
					type: "POST",
					url: urlx+"/tweet",
					data: { id : id, login : login },
					success: function(msg){
					}
				});
				
				//fb
				$.ajax({
					type: "POST",
					url: urlx+"/fb",
					data: { id : id, login : login },
					success: function(msg){
					}
				});
				
				$.ajax({
					global : false,
					beforeSend : function(){
						//$("div.response-textfield").find("textarea").attr("disabled","disabled");
						$("a.btnRock").attr("class","rocksOff corner-3 br bl tr tl");
					},		 
					type: "POST",
					url: urlx+"/rocks",
					timeout: 60000,
					data: { id : id, status : status, login : login },
					success: function(msg){
						//$(classs).find(".rocksOff").find("#loader-gif").remove();
						$("a.rocksOff").attr("class"," btnRock corner-3 br bl tr tl");
						//$("div.response-textfield").find("textarea").removeAttr("disabled");
						var spl = msg.split("##");
						if(spl[0]!="error"){
							$(classs).find("span.countRock").text(spl[0]);
							$(classs).find("div.rockedList").html(spl[1]);
							$.jGrowl("You Rocked: <br/>'"+title+"'");
							
							
							//update
							$.ajax({
								type: "POST",
								url: urlx+"/rockOther",
								data: { id : id, login : login },
								success: function(msg){
								}
							});
						}else{
							$.jGrowl(spl[1]);
						}
					},
					error: function(request, errorType, errorThrown){
						 try {
							if (errorType != null) {
								$("div.response-textfield").find("textarea").removeAttr("disabled");
								$.jGrowl("Something went wrong, please try again.");
								$("a.rocksOff").attr("class"," btnRock corner-3 br bl tr tl");
							}
						}
						 catch (e) {}
					}
				});	
			}
		}else{
			$dialogLogin.jqmShow();
		}
		return false;
	});
}

function stream(){
	$("a.chgStream").livequery("click", function(event){
		var href = $(this).attr("href");
		var status = href;//href.replace("#/","");
		if(status){
			var spl = key.split("/");
			var cur = spl[0];
			if(cur=='popular') cur = 'public';
			page = 1;
			cate="";
			type="";
			tag="";
			$("div.rockto-stream").html(streamLoad);
			var latest = $("#latest").val();
			if(ajaxCekNewPost) ajaxCekNewPost.abort();
			$("div.rockto-stream").stopTime("chk_new");
			$("title").html(pagetitle);
			var cek = status.split("/");
			switch(cek[0]){
				case 'popular':
				case 'public':
					key = 'public';
					time = $(this).attr("time");
					if(time==undefined){
						time = "";	
						$("a.chgStream").removeClass("selected");
						$(this).addClass("selected");
					}
					$(".publicTime").removeClass("hide");
					break;
				case 'feed':
					time = "";
					if(login!="login"){
						key = 'feed';
						$(".btnMyFeed").html("My Feed");
					}else
						key = 'public';	
					$("a.chgStream").removeClass("selected");
					$(this).addClass("selected");
					$(".publicTime").addClass("hide");
					break;
				default:
					key = status;
					if(cek[0]=="search"){
						var t = key.split("?s=");
						time = t[1];
						key = t[0] + "/" + time;
					}else if(cek[0]=="blogs"){
						$(".delBlogs").addClass("hide");
					}
					$("a.chgStream").removeClass("selected");
					$(this).addClass("selected");
					break;
			}
			$("#shareLink").attr("status",key);
			callStream(page);
			if(ajaxCekNewPost) ajaxCekNewPost.abort();
			switch(cek[0]){
				case 'popular':
				case 'public':
				case 'feed':
					latest = latest.replace(cur,key);
					$("#latest").val(latest);
					$("#latest").oneTime(5000,'startCheck', function(){
						$("input.newTime").remove();
						if((latest!="")&&(latest!=undefined)){
							var sp = latest.split("-");
							ajaxCekNewPost = $.ajax({
								type: "POST",
								url: urlx+"/cekNewPost",
								data: { id : latest, login : login },
								success: function(msg){
									if((msg!=0)&&(msg!="")){
										var s = msg.split("##");
										if(key=='feed'){
											var newTime = $("input.newTime").val();
											newTime = s[2]+"-"+s[3]+"-"+s[4]+"-"+s[5];
											if(newTime!=latest){
												$("<input type=\"hidden\" class=\"newTime\" value=\""+newTime+"\"/>").prependTo("div.rockto-stream");
											}
											newTime = newTime.replace(cur,key);
											$("#latest").val(newTime);
										}
									}
									cekNewPost();
								}
							});
						}
					});
					break;
			}
			window.location = "#/"+href;
			return false;
		}
	});
	
	$(".btnStream").livequery("click", function(event){
		$('html, body').animate({scrollTop:0}, 'fast');	
		if(ajaxCallStream) ajaxCallStream.abort();
		if(ajaxChgStream) ajaxChgStream.abort();
		if(ajaxCekNewPost) ajaxCekNewPost.abort();
		if(ajaxChgContainer) ajaxChgContainer.abort();
		if(ajaxChgContainerLeft) ajaxChgContainerLeft.abort();
		if(ajaxChgContainerRight) ajaxChgContainerRight.abort();
		var status = $(this).attr("stream");	
		var href = $(this).attr("href");
		var lastStatus = status;
		var type = $(this).attr("type");
		var cek = status.split("/");
		if(cek[0]=="search"){
			var t = status.split("?s=");
			time = t[1];
			status = t[0] + "/" + time;
		}
		var cur = $(this);
		var lastCur = $(".stream").html();
		ajaxChgStream = $.ajax({
			global : false,
			beforeSend : function(){
				$(".stream").html("<br/>"+streamLoad);
			},
			type: "POST",
			url: urlx+"/stream",
			timeout: 50000,
			data: { status : status, login : login },
			success: function(msg){	
				$(".content-menu-btn").removeClass("selected");
				$("a[type=\'"+type+"'\]").parent().addClass("selected");
				var spl = msg.split("##");
				$(".stream").html(spl[0]);
				page = 1;
				key = status;
				$("#shareLink").attr("status",key);
				callStream(page);
				$(".rocker-list img").fadeTo("fast", 0.8);
				
				$("#posttoolAreaLinkBox-input").defaultvalue("defdrop",defdrop);
				switch(lastStatus){
					case 'blogs':
						var newPost = "blogs";
						$("#topicsAdd").hide();
						break;
					case 'feed':
					case 'popular':
						randno = Math.floor ( Math.random() * suggestion.length ); 
						newPost = suggestion[randno];
						$("title").html(pagetitle);
						$("#newPostTab").hide();
						$("#newPostTab span").html(0);
						$("div.response-textfield").find("textarea").defaultvalue("dfcomment",dfcomment);
						$("div.response-textfield").find("textarea").elastic();
						$("#topicsAdd").show();
						break;
					default:
						randno = Math.floor ( Math.random() * suggestion.length ); 
						newPost = suggestion[randno];	
						$("#topicsAdd").show();
						break;
				}
				$.ajax({
					type: "POST",
					url: urlx+"/suggestion",
					data: {status:"all",post:newPost},
					success: function(msg){
						$("#topicsRecommendation").html(msg);
						$("#topicsRecommendation").fadeTo("fast",1);
						if(lastStatus=="blogs")
							$("#addNewBlog-input").defaultvalue("dfblog",dfblog);
					}
				});
				if(cek[1]=="settings")
					settings(cek[2]);
				$(".tips-south").tipsy({gravity: 's',opacity:0.8});
				$("a[rel*=\'lightbox\']").lightBox();
				ajaxChgStream = null;
			},
			error: function(request, errorType, errorThrown){
				 try {
					if (errorType != null) {
						$.jGrowl("Something went wrong, please try again. <a href=\"#\" class=\"action\" action=\"refreshContainer\">refresh</a>", { sticky: true });
					}
				}
				 catch (e) {}
			}
		});
		window.location = "#"+href;		
		return false;
	});	
}

function store(){
	$(".small-img").livequery("click", function(event){	
		$("#dialog-redeem").jqm();
		var imgsrc = $(this).attr("src");
		var newimg = imgsrc.replace("_150","_300");
		$("#modalContentRedeem").html("<img src=\""+newimg+"\" width=\"300\" style=\"margin-bottom:10px\" /><ul class=\"horizontal left\" style=\"padding-left:205px;margin-bottom:2em\"><li><a href=\"#\" class=\"btnConfirm jqmClose\">Close</a></li><div class=\"clearfix\"></div></ul>");
		$("#dialog-redeem").jqmShow();
	});
	$(".btnRedeem").livequery("click", function(event){	
		if(login!="login"){										
			$("#dialog-redeem").jqm();										
			var id = $(this).attr("id");
			var postid = $(this).attr("post-id");
			var cont = true;
			//if(postid==83){
//				if(it<10){
//					cont = false;
//					it++;
//				}
//			}
			//var status = (postid!="") ? 'rocked' : 'cek';
			if(cont==true){
				var status = 'cek';
				$.ajax({
					global : false,
					beforeSend : function(){
						$("#dialog-redeem").jqmHide();
					},
					type: "POST",
					url: urlx+"/store",
					data: { status : status, id : id },
					success: function(msg){	
						$("#modalContentRedeem").html(msg);
						$("#dialog-redeem").jqmShow();
					}
				});
			}else
				$.jGrowl("Something went wrong, Server too busy. Please try again.");
		}else{
			$dialogLogin.jqmShow();	
		}
		return false;
	});
	$(".continue").livequery("click", function(event){	
		var id = $(this).attr("id");
		var status = $(this).attr("status");
		status = (status=="rocked") ? 'cek' : 'continue';
		$.ajax({
			global : false,
			beforeSend : function(){
				$(".ajax-load-page").css({"margin-left":"-290px","top":"35%"});
				$(".ajax-load-page").show();
				$("#dialog-redeem").jqmHide();
				$("title").html("Please wait...");
			},	
			type: "POST",
			url: urlx+"/store",
			data: { status : status, id : id },
			success: function(msg){	
				$("title").html(pagetitle);
				$("#modalContentRedeem").html(msg);
				$(".ajax-load-page").css({"margin-left":"-455px","top":"45%"});
				$(".ajax-load-page").hide();
				$("#dialog-redeem").jqmShow();
			}
		});
		return false;
	});
	$(".jqmClose").livequery("click", function(event){	
		$("#dialog-redeem").jqmHide();
		return false;
	});
	$(".submit-store").livequery("click", function(event){	
		var product_id = $("#modalContentRedeem input[name=\'product_id\']").val();
		if(product_id){
			var phone = $("#modalContentRedeem input[name=\'member_phone\']").val();
			var size = $("#modalContentRedeem input[name=\'member_size\']").val();
			var addr = $("#modalContentRedeem textarea[name=\'member_address\']").val();
			if(phone!="" && addr!=""){
				$.ajax({
					global : false,
					beforeSend : function(){
						$(".ajax-load-page").css({"margin-left":"-290px","top":"35%"});
						$(".ajax-load-page").show();
						$("#dialog-redeem").jqmHide();
						$("title").html("Please wait...");
					},	
					type: "POST",

					url: urlx+"/store",
					data: { status : "submit", product_id : product_id, phone : phone, addr : addr, size : size },
					success: function(msg){	
						$("title").html(pagetitle);
						$("#modalContentRedeem").html(msg);
						$(".ajax-load-page").css({"margin-left":"-455px","top":"45%"});
						$(".ajax-load-page").hide();
						$("#dialog-redeem").jqmShow();
					}
				});
			}else{
				alert("Please complete the required form. Thank you.");
			}
		}else{
			$.jGrowl("Something went wrong, please try again.");	
		}
		return false;
	});
}

function cekBusinessName(name){
	$.ajax({
		type: "POST",
		url: base_url+"ajax/ajaxRegis",
		data: { name : name, key : "businessname" },
		success: function(msg){
			businessNameExist = msg;
		}
	});
}

function isValidEmail(str){
	var s = $.trim(str);
	var at = "@";
	var dot = ".";
	var lat = s.indexOf(at);
	var lstr = s.length;
	var ldot = s.indexOf(dot);

	if (s.indexOf(at)==-1 ||
	   (s.indexOf(at)==-1 || s.indexOf(at)==0 || s.indexOf(at)==lstr) ||
	   (s.indexOf(dot)==-1 || s.indexOf(dot)==0 || s.indexOf(dot)==lstr) ||
	   (s.indexOf(at,(lat+1))!=-1) ||
	   (s.substring(lat-1,lat)==dot || s.substring(lat+1,lat+2)==dot) ||
	   (s.indexOf(dot,(lat+2))==-1) ||
	   (s.indexOf(" ")!=-1))
	{
	  return false;
	}

	return true;
}

// adding it as a prototype object enables it to be used from any array
Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}

