// JavaScript for whole site

$(document).ready(function($){
	
	//update user's presence online
	setInterval(function(){	$.get("/actions/updateUserOnline.php") }, 5000)
	
	//change search url for male or female
	$("#find_a_girl, #find_a_guy").click(function(){
		var o   = $(this);
		var frm = o.closest("form");
		if($("#find_a_girl").is(":checked") && $("#find_a_guy").is(":checked")){
			frm.attr("action", "/dating/meet")
		}
		else if($("#find_a_girl").is(":checked")){
			frm.attr("action", "/dating/meet/women")
		}
		else if($("#find_a_guy").is(":checked")){
			frm.attr("action", "/dating/meet/men")
		}
		else {
			frm.attr("action", "/dating/meet")
		}
	})
	
	//close said parent: close btn has class "closeParent" and object to close className supplied as it's rel
	$(".closeParent").click(function(){
		var parent = $(this).attr("rel");
		$(this).parents(parent).fadeOut("fast");
	})
	
	//my tabs, first get default tab [works on alerts page]
	$(".tabs_head li").click(function(){
		var o = $(this);
		if(o.attr("class") != "active"){
			var content = "#"+o.attr("title");
			var file 	= "/views/home/"+o.attr("title")+'.php';
			$(".tabs_head li").removeClass("active");
			o.addClass("active");
			$(".tab_content").fadeOut("fast").delay(300);
			$.get(file, {u: o.attr("title")}, function(msg){
				//alert(file);
				$(content).html(msg);
				$(content).fadeIn();
			})
			location.hash = o.attr("title");
			$.get("/actions/home/updateAlert.php",{u: o.attr("title")})
		}
	})
	
	//alert(location.hash); ------- runs when alerts page loads to select which tab to show
	if($(location.hash).length && location.hash != "#messages"){
		var hash = location.hash+"";
		hash = hash.replace("#", "");
		var o = '.tabs_head li[title="'+hash+'"]';
		$(o).click();
	}
	else
		$(".tab_content:first").show();
	
	//elastic textareas
	$("textarea:not('.inelastic')").live("focus blur", function(event){
		var o = $(this);
		if(event.type=="focus" || event.type=="focusin"){
			if($.trim(o.val()).length == 0 && parseInt(o.css("height")) < 50)
				o.animate({height: "50px"}, "fast");
		}
		else if(event.type=="blur" || event.type=="focusout"){
			if($.trim(o.val()).length == 0 && parseInt(o.css("height")) == 50)
				o.animate({height: "18px"}, "fast");
		}
		return 0;
	})
	
	//save and post comments
	$('#comment_save').click(function(){
		var comment = $('#comment_content').val();
		var post = $('#comment_post').val();
		var category = $('#comment_category').val();
		var author = $('#comment_author').val();
		//var pic = $('#comment_post').val();
		//var author_name = $('#comment_author').val();
		$('#comment_content').val('');
		if(comment.length > 0){
			$.post("/actions/save_comment.php", {
				comment: comment, post: post, cat: category, author: author},
				function(msg){ $("#current_post_comments").append(msg); }
			)
		}
	})
	
	//not being used, but just incase
	$('.like, .dislike').click(function(){
		var count = $(this).text();
		var table = $(this).attr('id');
		var action = $(this).attr('class')+"s";
		var post = $(this).attr('rel');
		var current_object = $(this);
		$.ajax({
			url: "/actions/like_dislike.php",
			type: "get",
			data: "count="+count+"&post="+post+"&table="+table+"&action="+action,
			success: function(msg){
			//alert(msg);
			 current_object.text(msg);
		   }
		});
	})
	
	//keep # links useless
	$('a[href="#"]').click(function(){ return false; })
	
	//chat now
	$(".chat_now").live("click", function(){
		var id = $(this).attr("id");
		var n  = $(this).attr("rel");
		chatWith(id, n);
		return false;
	})
	
	//position the feedback button
	var f = $(".give_feedback");
	f.css("top", ($(window).height()-30));
    $(window).scroll(function(){ 
		//alert(f.css("bottom"));
		var y = ($(window).height()-30);
        var offset = y+$(document).scrollTop()+"px";
        f.animate({top:offset}, {duration:200,queue:false});  
    });  
})
