$(document).ready(function() {
    var options = {
        target:        '#content',   // target element(s) to be updated with server response
        beforeSubmit:  function() {
        	$('#content').html(" ");
        	$('#soaploading').fadeIn('slow');
        	$('#content').fadeOut();
        	$('.smssubmit').fadeOut();
        },
        success: function() {
            $.idleTimer(1000);
        },
        clearForm: true
    };

    $('#soapform').submit(function() {
    	if($('#msg').val().length > 0 && $('#handynr').val().length > 0)
    	{
        	$(this).ajaxSubmit(options);
        }
        else
        {
        	alert('Nicht alle Felder ausgefüllt!');
        };

        return false;
    });

    $('#searchform').submit(function() {
        $(this).ajaxSubmit(options);

        return false;
    });

	jQuery.fn.slowEach = function(interval, callback, callback2) {
		var items = this, i = 0;
		if(!items.length) return;
		function next() {
			(callback.call(items[i], i, items[i]) !== false && ++i < items.length) ? setTimeout(next, interval) : callback2 && callback2.call(items, i, items);
		}
		next();
	};

	$('input[type=radio].auto-submit-star').rating();

	$(".auto-submit-star").live("click", function(){

		if( $(this).attr('disabled') != "true" )
		{
			$("#rateInfo"+ $("#"+$(this).attr("id")+" a").attr("name")).hide();

			var arr = [ "5", "4", "3", "2", "1" ];
			var smsID = $("#"+$(this).attr("id")+" a").attr("name");
			jQuery.each(arr, function() {
				$("#sms_" + this + "_" + smsID).attr('disabled', 'true');
				$("#sms_" + this + "_" + smsID).fadeOut("slow");
			});

			$("#loader" + smsID).fadeIn("slow");

			$.post("update.php?rating="+$(this).attr("textContent")+"&smsID="+smsID, {rate: $(this).attr("textContent")}, function(db)
			{
				var intNewCheck = Math.round(db.avg);

				$("#rat" + smsID).attr('disabled', true);
				$("#avg" + smsID).text(db.avg);
				$("#votes" + smsID).text(db.votes);

				$("input[name='"+smsID+"']").rating('voted', intNewCheck-1);
				$("input[name='"+smsID+"']").rating('readOnly', 'true');

				$("#loader" + smsID).hide();


				var arr = [ "1", "2", "3", "4", "5" ];
				jQuery.each(arr, function() {
					$("#sms_" + this + "_" + smsID).fadeIn("slow");
				});

				$("#rateInfo"+smsID).fadeIn("slow");


			}, "json");
		};
	});

    $("#showmore").live("click", function () {
    	$("#moreloading").html('<br /><br /><img src="images/loader_more.gif" alt="Mehr Soaps" />');
		$.get("./?p=more", { type: "{$_GET['t']}", start: $("#showcount").attr('title'), limit: "10" }, function(data){
			if(data == "\r\n")
			{
				$("#moreloading").html("<br /><br />- Keine Weiteren Soaps vorhanden -");
			}
			else
			{
				$("#moreloading").before(data);
				$('input[type=radio].auto-submit-star').rating();

				var intCount = parseInt($("#showcount").attr('title')) + 1;
				$("#showcount").attr('title', intCount);
				$("#moreloading").html('<br /><br /><div id="showmore" title="Mehr Soaps..."></div>');
			}
		});

    });


	var validateUsername = $('#validateHandy');
	$('#os0').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue) {

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.

			if (this.timer) clearTimeout(this.timer);

			// show our holding text in the validation message space
			validateUsername.removeClass('error').html('<img src="images/loader_more.gif" height="16" width="16" /> checking availability...');

			// fire an ajax request in 1/5 of a second
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'validate.php',
					data: 'action=check_handy&reghandynr=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
						if(j.msg == "Diese Handynummer ist Korrekt")
						{
							$("#paythat").show();
						}
						else
						{
							$("#paythat").hide();
						}
						validateUsername.html(j.msg);
					}
				});
			}, 200);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});



});

$(document).bind("idle.idleTimer", function(){
	$('#content').fadeIn('slow');
	$('.smssubmit').fadeIn('slow');
	$('#soaploading').fadeOut('slow');
	$('input[type=radio].auto-submit-star').rating();
});
