$(function(){
	//Switch tab on the entire site
	$("div.tab_unit").click(function(){
		var rel = $(this).attr("rel");
		
		$("div.tab_content").hide();
		
		$("#"+rel+"_tab_content").show();
	})
	
	//Remove followed campaign/corporate fundraiser
	$(".remove_followed_object").live('click', function(){
		var t = $(this);
		$.post(
			"/ajax/unfollow_object",
			{object_type:$(this).attr("type"), object_id:$(this).attr("rel")},
			function(response){
				var data = response.split("/");
				$("#followed_"+data[0]+"_"+data[1]).slideUp();
				if (data[0] == 'campaign') {
					$("div.tab_unit[rel='my_programs'] div.tab_middle a span.count").html("(" + data[2] + ")");
				} else if (data[0] == 'corporate_fundraiser') {
					$("div.tab_unit[rel='my_clubs'] div.tab_middle a span.count").html("(" + data[2] + ")");
				}
			}
		)
	})
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		hideflash:true,
		theme:'dark_rounded'
	})
	
	//Report comments
	$(".report_comments").live('click', function(){
		var t = $(this);
		$.post('/ajax/report_comment', {thread_id : t.attr("rel")}, function(data){
			if (parseInt(data) == 1) {
				t.html("This comment has been reported").removeClass("report_comment");
			}
		})
	})
	
	//Pagination
	$("div.pagination a").live('click', function(){
		
		var body_id = $("body").attr("id");
		
		var t = $(this);
		
		var great_grandparent = t.parent().parent().parent();
		var grand_parent = t.parent().parent();
		
		var page = $(this).html();
			
		var type = $(this).parent().attr("rel");
		
		if (body_id == 'campaigns') {
			//Paginating campaigns
			
			$.post("/ajax/paginate_objects", {type:type, page:page, language:'en_CA'}, function(response){
				var r = $(response);
				if (type == 'campaigns') {
					var d = $("div.campaign_wrapper:eq(0)");
					d.children("div.campaign_list").remove();
					d.append(r.html());
				} else {
					var d = $("div.campaign_wrapper:eq(1)");
					d.children("div.campaign_list").remove();
					d.append(r.html());
				}
			})
		} else if (body_id == 'events') {
			//Paginating events
			$.post("/ajax/paginate_objects", {type:type, page:page, language:'en_CA'}, function(response){
				var r = $(response);
				var d = $("div.campaign_wrapper:eq(0)");
				d.children("div.campaign_list").remove();
				d.append(r.html());
			})
		}
		else if (body_id == 'campaign' || body_id == 'corporate_fundraiser') {
			
			if (type == 'members' || type == 'comments') {
				$.post("/ajax/paginate_objects", {type:type, page:page, body_id:body_id, object_id:$("#object_id").val(), language:'en_CA'}, function(response){
					if (type == 'members') {
						$("#members_tab_content").html(response);
					} else {
						var c = $("#comments_tab_content");
						c.children("div.comment_wrapper").remove();
						c.append(response);
					}
				})
			}
			
			if (type == 'campaign_details_top_fundraisers') {
				$.post("/ajax/paginate_objects", {type:type, page:page, body_id:body_id,
					supporting_corporate_fundraisers_ids:$("input[name='supporting_corporate_fundraisers_ids']").val(),
					campaign_id:$("#object_id").val(), language:'en_CA'}, function(response){
						var r = $(response);
						grand_parent.remove();
						great_grandparent.append(r.html());
				})
			}
		} else if (body_id == 'dashboard') {
			//Paginatig followed campaigns/corporate fundraisers on dashboard
			$.post("/ajax/paginate_objects", {type:$(this).parent().attr("rel"), page:$(this).html(), body_id:body_id, language:'en_CA'}, function(response){
				$("div.tab_content:visible").html(response);
			})
		}
		else if (body_id == 'news') {
			//Paginating blog index page
			$.post("/ajax/paginate_objects", {type:type, page:page, language:'en_CA'}, function(response){
				$("#blog_listing").html(response);
			})
		} else if (body_id == 'news_details') {
			//Paginating comments on a blog details page
			$.post("/ajax/paginate_objects", {type:type, page:page, language:'en_CA', 'news_id':$("input[name='news_id']").val()}, function(response){
				$("#news_comments").html(response);
			})
		} else if (body_id == 'corporate_fundraisers') {
			//Pagination on fundraisers page
			var search_type = $("input[name='search_type']").val();
			//console.log(search_type);
			
			var tab = $(this).parent().parent().parent().parent().attr("id");
			
			//console.log(tab);
			
			var t = $(this);
			
			if (search_type == 'all_fundraisers') {
				//var keywords = $("input[name='keywords']").val();
				
				//if (keywords == '') {
				$.post("/ajax/paginate_objects", {type:'all_fundraisers', page:$(this).html(), tab:tab, language:'en_CA'}, function(response){
//					var r = $(response);
//					grand_parent.remove();
//					great_grandparent.append(r.html());
					$("#"+tab).html(response);
				})
//				} else {
//					$.post("/ajax/paginate_search_results", {type:'all_fundraisers', page:$(this).html(), tab:tab, 
//						language:'en_CA', keywords:keywords}, function(response){
//						
//					})
//				}
				//console.log();
			} 
			else if (search_type == 'fundraisers_by_type') {
				var fundraiser_type = $("input[name='fundraiser_type']").val()
				$.post("/ajax/paginate_objects", {type:'fundraisers_by_type', 'fundraiser_type':fundraiser_type, 
					page:$(this).html(), tab:tab, language:'en_CA'}, function(response){
						$("#"+tab).html(response);
				})
			} 
			else if (search_type == 'campaign_fundraisers') {
				
				var supporting_corporate_fundraisers_ids = $("input[name='supporting_corporate_fundraiser_ids']").val();
				var campaign_slug = $("input[name='campaign_slug']").val();
				
				$.post("/ajax/paginate_objects", {type:'campaign_fundraisers', campaign_slug:campaign_slug,
					supporting_corporate_fundraisers_ids:supporting_corporate_fundraisers_ids,
					page:$(this).html(), tab:tab, language:'en_CA'}, function(response){
					//t.parent().parent().html(response);
						grand_parent.remove();
						great_grandparent.append(response)
				})
				
			} else if (search_type == 'corporate_fundraisers_by_name') {
				//var tab = $(this).parent().parent().parent().attr("id");
				//console.log(tab);
			}
		} else if (body_id == 'search') {

			var current_url = window.location.href;
			
			//var tab = $(this).parent().parent().parent().parent().attr("id").replace('_tab_content', '');
			
			var tab = $("div.tab_content:visible").attr("id").replace('_tab_content', '');
			
			var a = current_url.substr(0, current_url.lastIndexOf('&tab'));
			window.location.href = a + '&tab=' + tab + '&page='+$(this).html()
			//console.log(a + '&tab=' + tab + '&page='+$(this).html());
		}
	})
	
	//Search
	var sbm = $("#search_submit");
	
	sbm.live('click', function(){
		var keywords = $("#keywords").val();
		
		var search_type = $("input[name='search_type']").val(); 
		
		if (search_type == 'all_fundraisers') {
			
			window.location.href = '/en_CA/search/fundraiser?keywords='+escape(keywords)+'&tab=by_name&page=1';
			
		} else if (search_type == 'fundraisers_by_type') {
			
			window.location.href = '/en_CA/search/fundraisers_by_type?type=' + $("input[name='fundraiser_type']").val() 
			+ '&keywords='+escape(keywords)+'&tab=by_name&page=1';
			
		} else if (search_type == 'campaign_fundraisers') {
			
			var fundraiser_type = $("input[name='fundraiser_type']").val();
			
			//if (fundraiser_type) {
			window.location.href = '/en_CA/search/campaign_fundraisers?campaign=' + $("input[name='campaign_slug']").val()
			+ '&type=' + fundraiser_type + '&keywords=' + escape(keywords) + '&tab=by_name&page=1';
//			} else {
//				window.location.href = '/en_CA/search/campaign_fundraisers?campaign=' + $("input[name='campaign_slug']").val() 
//				+ '&keywords=' + escape(keywords) + '&tab=by_name&page=1';
//			}
		}
		
	})
	
	$("#keywords").keypress(function(event) {
		var code = event.keyCode ? event.keyCode : event.which;
		
		if (code == 13) sbm.trigger('click');
	})
	
	
	$('.follow-pop').click(function(){ $('#follow_details').show(); return false; });
	
	$('.share_overlay_close').click(function(){ $('#follow_details').hide(); return false; });
	
})
