$(document).ready(function(){
	activeColumnContent();
	
	//tooltip
	refreshTooltip();
});

swbc.get_column=function(mode, cid, page, category){
	$("#"+mode+"_content_more").hide();
	
	var loading = $("#"+mode+"_loading").html();
	$("#"+mode+"_content").html(loading);
	
	scroll(0,0);
	
	var type = 'text';
	
	if (mode=='popular_media') type = 'media';
	
	if ($("#"+mode+" #type_show").length>0){ 		
		type = $("#"+mode+" #type_show").val();				
	}
	
	var order_sort = $("#"+mode+"_order_sort").val();
	
	xajax_get_column(mode, cid, page, category, type, order_sort);
	
}
swbc.get_column_process=function(mode){ 
	activeColumnContent();
	
	$("#"+mode+"_content_more").show();
	$(".status_reply_textarea").autogrow();
}

swbc.get_syncronized_media=function(nid){ 
	var mode = 'popular_media';	
	
	$(".timeline_r").removeClass("timeline_r_hightlight");
	$("#timeline_r_"+nid).addClass("timeline_r_hightlight");
	
	if ($("#popular_media .controller #syncronized_media_order_sort").length==0){
		$("#popular_media .controller").html($("#syncronized_media_controller").html());		
	}
	
	$("#syncronized_media_order_sort").attr("nid", nid);
	
	$("#"+mode+"_content_more").hide();
	
	var loading = $("#"+mode+"_loading").html();
	$("#"+mode+"_content").html(loading);
	
	scroll(0,0);
	
	var order_sort = $("#syncronized_media_order_sort").val();
	
	xajax_get_syncronized_media(nid, order_sort);
}

swbc.sort_syncronized_media=function(){
	var nid = $("#syncronized_media_order_sort").attr("nid");	
	if (nid) swbc.get_syncronized_media(nid);
}

swbc.get_replies=function(mode, cid, nid, page){ 	
	var comments_opened = false;
	if ($("#reply_view_all_"+nid+":visible").length>0) comments_opened = true;
	
	if ((page==1) && ($("#"+mode+"_reply_to_"+nid+":visible").length>0) && !comments_opened){
		$("#"+mode+"_reply_to_"+nid).hide();
		$("#"+mode+"_reply_to_"+nid).html('');
	}
	else{ 
		if (page==1){
			$("#"+mode+"_replies_loading_"+nid).show();
		}
		else {
			var loading= $("#small_loading").html();
			$("#reply_more_"+nid).html(loading);
		}
		
		var reply_num = $("#timeline_r_"+nid).attr("reply_num");
		
		var since_id = 0;
		if ($("#reply_content_"+nid+" .timeline_r").length>0 && !comments_opened){
			since_id = $("#reply_content_"+nid+" .timeline_r:last")[0].id.substr(11);			
		}
		
		var max_id = 0;
		if ($("#reply_new_"+nid+" .timeline_r").length>0 && !comments_opened){			
			max_id = $("#reply_new_"+nid+" .timeline_r:first")[0].id.substr(11);			
		}
		
		xajax_get_replies(mode, cid, nid, reply_num, page, since_id, max_id);
	}
}

swbc.get_replies_process=function(mode, cid, nid){
	$("#"+mode+"_replies_loading_"+nid).hide();
	$("#"+mode+"_reply_to_"+nid).show();
	
	var parentid = "#"+mode+"_reply_to_"+nid;
		
	$(parentid+" #status_textarea").autogrow();
	
	if ($(parentid+" #status_textarea").length) {	
		$(parentid+" #status_textarea").bind("keyup", function(){
			counter(parentid);
		});
		
        // run once in case there's something in there
        counter(parentid);
		
        // set the focus
        $(parentid+" #status_textarea").focus();
	}
	
}

swbc.fresh_replies=function(mode, cid, nid){ 	
	$("#"+mode+"_replies_loading_"+nid).show();
	
	xajax_get_replies(mode, cid, nid);
}

swbc.login_dialog=function(){ 	
	swbc.dialog_open("login", 2);
	xajax_show_login_dialog();
}

swbc.cancel_reply=function(mode, cid, reply_to){
	$("#"+mode+"_reply_loading_"+reply_to).hide();
	$("#"+mode+"_reply_submit_button_"+reply_to).attr("disabled", "");
}

swbc.new_t8m_dialog=function(cid){ 
	//swbc.new_t8m_dialog_clear();
	swbc.dialog_open("new_t8m", 2);
	xajax_show_new_t8m_dialog(cid);
}

swbc.show_new_t8m_dialog_process=function(){ 
	var tab = 'note';
	var parentid = "#new_t8m_tab_"+tab;	
	if ($(parentid+" #status_textarea").length) {	
		$(parentid+" #status_textarea").bind("keyup", function(){
			counter(parentid);
		});
		
        // run once in case there's something in there
        counter(parentid);
		
        // set the focus
        $(parentid+" #status_textarea").focus();
	}
}

swbc.new_t8m_menu_tab_click=function(tab){ 	
	$("#new_t8m_menu_note").removeClass("selected");
	$("#new_t8m_menu_photo").removeClass("selected");
	$("#new_t8m_menu_video").removeClass("selected");
	$("#new_t8m_menu_title").removeClass("selected");
	$("#new_t8m_menu_embed").removeClass("selected");
	$("#new_t8m_tab_note").hide();
	$("#new_t8m_tab_photo").hide();
	$("#new_t8m_tab_video").hide();
	$("#new_t8m_tab_title").hide();
	$("#new_t8m_tab_embed").hide();
	
	$("#new_t8m_menu_"+tab).addClass("selected");
	$("#new_t8m_tab_"+tab).show();
	
	var parentid = "#new_t8m_tab_"+tab;	
	if ($(parentid+" #status_textarea").length) {	
		$(parentid+" #status_textarea").bind("keyup", function(){
			counter(parentid);
		});
		
        // run once in case there's something in there
        counter(parentid);
		
        // set the focus
        $(parentid+" #status_textarea").focus();
	}
	
}

swbc.setting_menu_tab_click=function(tab){ 	
	$("#setting_menu_account").removeClass("selected");
	$("#setting_menu_channels").removeClass("selected");
	$("#setting_menu_friends").removeClass("selected");
	$("#setting_tab_account").hide();
	$("#setting_tab_channels").hide();
	$("#setting_tab_friends").hide();
	
	$("#setting_menu_"+tab).addClass("selected");
	$("#setting_tab_"+tab).show();
}

swbc.photo_upload_submit=function(){
	$('#photo_upload_url').val('');
	$('#photo_upload_loader').show();
	
	document.photo_frm.submit();
}

swbc.new_t8m=function(mode){ 
	$("#new_t8m_"+mode+"_error").html('');
	
	var cid = $("#new_t8m_"+mode+"_channel_id").val();
	var syncronized_media = 0;
	
	var column_category = null;
	
	var note = $("#new_t8m_"+mode+"_submit_form #status_textarea").val();
	if(note.length==0){ 
		if (mode=='note' || mode=='title'){
			$("#new_t8m_"+mode+"_error").html("Input Content");
			return false;
		}

		note = null;	
	}
	else if(note.length>140){ 
		$("#new_t8m_"+mode+"_error").html("Content is too Long");
		return false;
	}
	
	var photo = null;
	var video = null;
	
	if (mode == 'note'){
		syncronized_media = $("#syncronized_media").attr('checked') ? 1 : 0;;
	}
	else if (mode == 'photo'){
		photo = $("#new_t8m_"+mode+"_submit_form #photo_upload_url").val(); 
		if (photo.length==0) photo = $("#new_t8m_"+mode+"_submit_form #photo_url").val(); 
		if (photo.length==0){	
			$("#new_t8m_photo_error").html('Please post a photo');
			return false;
		}	
	}
	else if (mode == 'video'){
		video = $("#new_t8m_"+mode+"_submit_form #video_youtube_url").val();
		if (video.length==0){	
			alert('Please post a video');
			return false;
		}
	}else if (mode == 'embed'){
		video = $("#new_t8m_"+mode+"_submit_form #embed_textarea").val();
		if (video.length==0){	
			alert('Please post a embed code');
			return false;
		}else if (video.indexOf("<object ")<0 && video.indexOf("<embed ")<0){
			alert('This is not an embed code.');
			return false;
		}
	}
	
	var profile_id = $("#new_t8m_"+mode+"_submit_form #new_t8m_username").val();
	
	$("[new_t8m_"+mode+"_disable_flag]").attr("disabled","disabled");
	$("#new_t8m_"+mode+"_error").html("");
	$("#new_t8m_"+mode+"_loader").show();
	
	var sync_to = 0;
	if (!syncronized_media){
		sync_to = $("#new_t8m_"+mode+"_sync_to").val();
	}
	
	xajax_send_t8m(mode, cid, photo, video, note, profile_id, column_category, syncronized_media, sync_to);
}

swbc.send_t8m_process=function(mode, error){
	if (error.length>0){
		$("#new_t8m_"+mode+"_error").html(error); 
		$("#new_t8m_"+mode+"_submit_button").attr('disabled', '');
		$("#new_t8m_"+mode+"_loader").hide();
	}
	else{
		swbc.dialog_close('new_t8m', 2);
	}
}

swbc.new_hot_deals=function(){
	$("#new_t8m_photo_error").html("");
	
	var cid = $("#store_channel_id").val();
	
	var store_name = $("#store_name").val();
	var store_category_id = $("#store_category_id").val();
	var store_address = $("#store_address").val();	
	var store_phone = $("#store_phone").val();	
	var store_deal = $("#store_deal").val();
	
	if (store_name.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#store_name").focus();
		return false;
	}
	else if (store_category_id==0){
		$("#new_t8m_photo_error").html("Select a category");
		$("#store_category_id").focus();
		return false;
	}
	else if (store_address.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#store_address").focus();
		return false;
	}
	else if (store_phone.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#store_phone").focus();
		return false;
	}
	else if (store_deal.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#store_deal").focus();
		return false;
	}
	
	if ($("#photo_upload_loader:visible").length>0){
		$("#new_t8m_photo_error").html("Please wait while uploading your photo...");
		return false;
	}
	
	var store_map = $("#store_map").val();
		
	var photos = [];	
	$("#photo_upload_preview div:visible img").each(function(){
		photos.push($(this).attr('src'));
	});
	
	var notes = [];
	$("#photo_upload_preview div:visible textarea").each(function(){
		notes.push($(this).val());
	});
	
	if (cid && store_name && store_category_id && store_address && store_phone && store_deal){ 
		$("[new_t8m_photo_disable_flag]").attr("disabled","disabled");
		$("#new_t8m_photo_loader").show();
		xajax_send_hot_deals(cid, store_name, store_category_id, store_address, store_phone, store_deal, photos, notes, store_map);
	}
}

swbc.new_food=function(){
	$("#new_t8m_photo_error").html("");
	
	var cid = $("#food_channel_id").val();
	
	var photo = $("#new_t8m_photo_submit_form #photo_upload_url").val(); 
	if (photo.length==0) photo = $("#new_t8m_photo_submit_form #photo_url").val(); 
	if (photo.length==0){	
		$("#new_t8m_photo_error").html('Please post a photo');
		return false;
	}

	var food_dish_name = $("#food_dish_name").val();
	var food_restaurant = $("#food_restaurant").val();
	if (food_dish_name.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#food_dish_name").focus();
		return false;
	}
	else if (food_restaurant.length==0){
		$("#new_t8m_photo_error").html("Input Content");
		$("#food_restaurant").focus();
		return false;
	}
	
	var food_map = $("#food_map").val();

	if (cid && photo && food_dish_name && food_restaurant){ 
		$("[new_t8m_photo_disable_flag]").attr("disabled","disabled");
		$("#new_t8m_photo_loader").show();
		xajax_send_food(cid, photo, food_dish_name, food_restaurant, food_map);
	}
}

swbc.add_photo_desc=function(){ 
	alert('test2');
	$(this).parent().parent().parent().hide();
}

swbc.edit_t8m_dialog=function(nid){ 
	//swbc.new_t8m_dialog_clear();
	swbc.dialog_open("edit_t8m", 2);
	xajax_show_edit_t8m_dialog(nid);
}

swbc.show_edit_t8m_dialog_process=function(){ 
	var tab = 'note';
	if ($("#edit_t8m_tab_photo:visible").length>0) tab = 'photo';
	else if ($("#edit_t8m_tab_video:visible").length>0) tab = 'video';	
	else if ($("#edit_t8m_tab_title:visible").length>0) tab = 'title';	
	
	var parentid = "#edit_t8m_tab_"+tab;	
	if ($(parentid+" #status_textarea").length) {	
		$(parentid+" #status_textarea").bind("keyup", function(){
			counter(parentid);
		});
		
        // run once in case there's something in there
        counter(parentid);
		
        // set the focus
        $(parentid+" #status_textarea").focus();
	}
}

swbc.edit_t8m=function(mode){
	var nid = $("#edit_t8m_"+mode+"_notice_id").val(); 
	var syncronized_media = 0;
	
	var column_category = null;
	var prev_column = null;	
	if (mode!='note') prev_column = $("#"+mode+"_prev_column").val();
	
	var note = $("#edit_t8m_"+mode+"_submit_form #status_textarea").val();
	if(note.length==0){ 
		if (mode=='note'){
			$("#edit_t8m_"+mode+"_error").html("Input Content");
			return false;
		}
		
		note = null;
	}
	else if(note.length>140){ 
		$("#edit_t8m_"+mode+"_error").html("Content is too Long");
		return false;
	}
	
	var photo = null;
	var video = null;
	if (mode == 'note'){
		syncronized_media = $("#syncronized_media").attr('checked') ? 1 : 0;;
	}
	else if (mode == 'photo'){
		photo = $("#edit_t8m_"+mode+"_submit_form #photo_upload_url").val(); 
		if (photo.length==0) photo = $("#edit_t8m_"+mode+"_submit_form #photo_url").val(); 
		if (photo.length==0){	
			$("#edit_t8m_photo_error").html('Please post a photo');
			return false;
		}	
	}
	else if (mode == 'video'){
		video = $("#edit_t8m_"+mode+"_submit_form #video_youtube_url").val();
		if (video.length==0){	
			alert('Please post a video');
			return false;
		}
	}else if (mode == 'embed'){
		video = $("#edit_t8m_"+mode+"_submit_form #embed_textarea").val();
		if (video.length==0){	
			alert('Please post a embed code');
			return false;
		}else if (video.indexOf("<object ")<0 && video.indexOf("<embed ")<0){
			alert('This is not an embed code.');
			return false;
		}
	}
	
	if (nid){		
		$("[edit_t8m_"+mode+"_disable_flag]").attr("disabled","disabled");
		$("#edit_t8m_"+mode+"_error").html("");
		$("#edit_t8m_"+mode+"_loader").show();
		
		xajax_edit_t8m(mode, nid, photo, video, note, column_category, syncronized_media, prev_column);
	}
	else{
		//
	}
}

swbc.edit_t8m_process=function(mode, error){ 	
	if (error.length>0){
		$("#edit_t8m_"+mode+"_error").html(error); 
		$("#edit_t8m_"+mode+"_submit_button").attr('disabled', '');
		$("#edit_t8m_"+mode+"_loader").hide();
	}
	else{
		swbc.dialog_close('edit_t8m', 2);
	}
}

swbc.reply_t8m=function(mode, cid, reply_to){
	var content = $("#"+mode+"_reply_to_"+reply_to+" #status_textarea").val();
	
	//alert(content.length);
	if (content.length==0){
		$("#reply_error_"+reply_to).html('Input Content');
	}
	else if (content.length>140){
		$("#reply_error_"+reply_to).html('Content is too Long');
	}
	else {	
		$("#"+mode+"_reply_submit_button_"+reply_to).attr("disabled","disabled");
		$("#reply_error_"+reply_to).html('');
		
		$("#"+mode+"_reply_loading_"+reply_to).show();
		
		var count = $("#timeline_r_"+reply_to).attr("reply_num");
		
		xajax_reply_t8m(mode, cid, content, reply_to, count);
	}
}

swbc.delete_reply=function(mode, cid, reply_to){ 
	var topic_reply_num = $("#timeline_r_"+reply_to).attr("reply_num");
	
	xajax_delete_reply(mode, cid, reply_to, topic_reply_num);
	
}

swbc.show_setting_dialog=function(){
	swbc.dialog_open("setting", 2);
	xajax_show_setting_dialog();
}

swbc.edit_account_submit=function(){ 
	$("#edit_account_error").html('');
	
	var profile_id = $("#setting_account_submit_form #profile_id").val(); 
	var username = $("#setting_account_submit_form #login_username").val();
	var password = $("#setting_account_submit_form #login_password").val();
	var foreign_id = $("#setting_account_submit_form #foreign_id").val();
	
	if (profile_id && username && password){ 
		$("#setting_account_submit_form #login_loader").show();
		xajax_edit_account(profile_id, username, password, foreign_id);
	}
}

swbc.check_url_available=function(){ 
	var name = $("#channel_address").val();
	$("#channel_address_error").html("");
	
	$("#check_url_loader").show();
	
	xajax_check_url_available(name);
}

swbc.edit_sharedurl_submit=function(){ 
	$("#sharedurl_error").html("");
	
	var user_id = $("#url_user_id").val();
	var username = $("#sharedurl_username").val();
	
	if (username.length==0){ 		
		$("#sharedurl_error").html("Please input username");		
	}else{		
		$("#edit_sharedurl_loader").show();
		
		xajax_edit_sharedurl(user_id, username);
	}
}

swbc.delete_icon=function(){
	$("#icon_preview").fadeOut('slow');
	$("#icon_garbage").val('1');
}

swbc.delete_banner=function(){
	$("#banner_preview").fadeOut('slow');
	$("#banner_garbage").val('1');
}

swbc.delete_background=function(){
	$("#background_preview").fadeOut('slow');
	$("#bg_garbage").val('1');
}

swbc.short_URLs=function(parentid){ 
 //alert(parentid);
	var content=$(parentid+" #status_textarea").val();
	if(content.length>0){
	    xajax_shortURL(content,parentid);
	}
	//counter(parentid);
}
