$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};


	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);  
	};



$(document).ready(function(){

	// CAPTCHA
	$("#captcha_greetingbox").s3Capcha();

	// TOOLTIPS
	$(".tooltip").tipTip({defaultPosition: "bottom"});
	$(".tooltip_right").tipTip({defaultPosition: "right", activation:"focus"});
	$(".tooltip_top").tipTip({defaultPosition: "top"});
	
	// MENU DROPDOWN
	$(".signin").click(function(e) {          
		e.preventDefault();
	    $("div#signin_menu").slideToggle();
		$(".signin").toggleClass("menu-open");
	});
	
	$("div#signin_menu").mouseup(function() {
		return false;
	});
	$(document).mouseup(function(e) {
		if($(e.target).parent("a.signin").length==0) {
			$(".signin").removeClass("menu-open");
			$("div#signin_menu").slideUp();
		}
	});
	
		
		
	$(".next_session").css({ bottom: "-28px" });
	$(".next_session_show").css({ bottom: "0px" });
	
		
	$(".next_session_show").click(function () {
		$(".next_session").animate({ bottom: "0px" }, 300 );
		$.ajax({
			url : "http://radio.sb.x0t.org/info.php",
			type: "POST",
			data: "s=next_session_enable",
			success : function (data) { 
				$(".next_session_show").animate({ bottom: "-28px" }, 200, function () {
					$(".next_session").animate({ bottom: "0px" }, 200 );
				});
			}
		});
	});
	
	$(".next_session_hide").click(function () {
		$.ajax({
			url : "http://radio.sb.x0t.org/info.php",
			type: "POST",
			data: "s=next_session_disable",
			success : function (data) { 
				$(".next_session").animate({ bottom: "-28px" }, 200, function () {
					$(".next_session_show").animate({ bottom: "0px" }, 200 );
				});
			}
		});

	});
	
	$("#geetingbox_btn").click(function () {
		$("#geetingbox").fadeToggle('fast');
	});
	
	$("#geetingbox form .cancel").click(function () {
		$("#geetingbox").fadeToggle('fast');
	});
	
	$("#geetingbox form").submit(function () {
	
		var author 	= $("#geetingbox form .author").val();
		var greet  	= $("#geetingbox form .greet").val();
		var status 	= $("#geetingbox form .status");
		var button 	= $("#geetingbox form .submit_greet");
		var captcha = $("input[name='captcha_greet']:checked").val();
		var captcha_res = $("#geetingbox form .captcha_result").val();
		var captchabox = $("#captcha_greetingbox");
	
		$.ajax({
			url: "http://radio.sb.x0t.org/info.php",
		    type: "POST",
		    data: "s=add_greet&greet="+greet+"&author="+author,
		    beforeSend: function () {
				status
					.removeClass('red')
					.addClass('preloader')
					.html('');
				button.attr("disabled", "true");
			},
		    success: function(data){
				if(data == 'Thanks!') {
					if(captcha == captcha_res && captcha != '' && captcha != 'undefined') {
						$(':input','#geetingbox')
							.not(":button, :submit, input[name='author']")
							.val('');
						status
							.html('')
							.removeClass('preloader red')
						button.removeAttr("disabled");
						$("#geetingbox").fadeToggle('fast');
						$.ajax({
							url: "http://radio.sb.x0t.org/info.php",
						    type: "POST",
						    data: "s=get_captcha&name=greet",
						    beforeSend: function () { 
						    	captchabox.addClass('preloader').html(''); 
						    },
						    success: function(data){
						   		captchabox
						   			.removeClass('preloader')
						   			.html(data)
						   			.s3Capcha();
							}
						});
					} else {
						button.removeAttr("disabled");
						status
							.removeClass('green preloader')
							.addClass("red")
							.html('Captcha is wrong.');
					}
				} else {
					button.removeAttr("disabled");
					status
						.removeClass('green preloader')
						.addClass("red")
						.html(data);
				}
			}
		});
		
		return false;
	});
});

