var lastaction;
var refreshing = 2*60*1000; // 60 * 1000 = 1 Minute (1000 = millisekunden, 60 = sekunden)
var refreshId=0;
	
$('document').ready(function() {
	
	/* refresh alle 2-3 minuten: `*/
	lastaction = new Date().getTime();
	//setRefreshTimeout(); 
	
	/*$('input').focus(function() { setRefreshTimeout(); });
	$('textarea').focus(function() { setRefreshTimeout(); });
	$('input').change(function() { setRefreshTimeout(); });
	$('textarea').change(function() { setRefreshTimeout(); });
	*/
    /*
     * SHORT MESSAGES:
     */

	$('#bestmatchsinglescount').click(function() {
		$('#bestmatchsinglespreview').slideToggle();
		return false;
	});

    var shortmessagedefault = "Was machst du? Wie geht es dir?...";
    $('#shortmessagetext').val(shortmessagedefault);
    
    $('#shortmessagetext').focus(function() {
        if ($(this).val()==shortmessagedefault) {
            $(this).val("");
        }
    }).blur(function() {
        if ($(this).val()=="" || $(this).val()==" ") {
            $(this).val(shortmessagedefault);
        }
    });

    $('#shortmessagesubmit').click(function() {
        var text = $('#shortmessagetext').val();
        var user_id = $('#current_user_id').val();
        
        if (text!="" && text!=shortmessagedefault && text!=" ") {
            var mess;
            $.post("/ajaxcalls/insertShortMessage.html", {user_id:user_id,text:text},
                function(data) {
                    mess = data.text;

                    $(mess).hide();
                    $('.shortmessageItem.random.act').removeClass('act').slideUp();
                    
                    $('.shortmessageItem.random:first').before(mess);
                    
                    $(mess).slideDown();
                    $('.shortmessageItem.random:first').addClass("act").addClass("random").addClass("last");

                    $('#shortmessagetext').val(shortmessagedefault);
                    //loadRandomSMs();
                },"json"
            );

        } else {
            alert("Bitte eine Message eingeben!");
            $('#shortmessagetext').focus();
        }

        return false;
    });

    if ($('div.shortmessageItem').length > 5) {
        loadRandomSMs();
        var exp = $("<div class='expand' id='expander'>mehr</div>");
        $('div.shortmessageItem:last').after(exp);
        $(exp).click(function() {
            if ($(this).hasClass('expand')) {
                $('div.shortmessageItem.random.act').hide();
                $('div.shortmessageItem.random.act').addClass('temp').removeClass('act');

                $('div.shortmessageItem.hideMe.last').slideDown();
                $(this).text("weniger");
            } else {
                $('div.shortmessageItem.hideMe.last').slideUp(function() {

                    $('div.shortmessageItem.random.temp').addClass('act').removeClass('temp').show();
                });
                $(this).text("mehr");
            }
            $(this).toggleClass("expand");
        });
    }

    /*
     * END SHORT MESSAGES
     */


    /*
     * BEGIN CHECK INVITATION CODE
     */

    if ($('#checkInvitationCode').html()!="") {
        $('#checkInvitationCode').fadeTo("fast",0.6);
        $('#checkInvitationCode')
            .bind("mouseenter",function() { $(this).fadeTo("normal",1); })
            .bind("mouseout",function() {$(this).fadeTo("normal",0.6); })
            .click(function() {
                var erg = checkInvitationCode(true);
                return false;
            });
            $('#bos_invitation_code').change(function() {
                $('input#bos_invitation_code').removeClass("correct").removeClass("incorrect");
            });
        $('form input.dosubmit').click(function() {
           var erg = checkInvitationCode();
           if (erg==true) {
                $('form').submit();
           }
           return false;
        });
    }



    /*
     * END CHECK INVITATION CODE
     */

    
});

function checkInvitationCode(checkIt) {
    if(!checkIt) {
        checkIt = false;
    }
    if ($('#email').val()=="") {
        alert("Bitte eine E-Mail-Adresse angeben!");
        return false;
    } else if($('input#bos_invitation_code').val()=="" && checkIt==true) {
        alert("Bitte einen Code eingeben!");
        return false;
    }
    if($('input#bos_invitation_code').val()=="" && checkIt==false) {
        return true;
    }
    var erg = false;
    $.ajax({
        url: "/ajaxcalls/checkUserInvitationCode.html",
        data: {code:$('input#bos_invitation_code').val(),email:$('#email').val()},
        success: function(data) {
            if (data.result=='ok') {
                if (checkIt) { alert("Code ist korrekt, danke!");                 
                     $('input#bos_invitation_code').addClass("correct");
                 }
                 erg = true;
            } else {
                alert("Falscher Einladungs-Code!");
                $('input#bos_invitation_code').addClass("incorrect");
                erg = false;
            }
        },
        async: false,
        type: 'post',
        dataType: 'json'
    });
    return erg;
}

function loadRandomSMs() {
    if ($('div.shortmessageItem.random.act').length<1) {
    	$('div.shortmessageItem.random:first').addClass('act').show();
    }
    if ($('div.shortmessageItem.random').length>1) {
        loadNewShortmessage();
    }
}
function loadNewShortmessage() {
    setTimeout("loadNewShortmessageitem();",5000);
}
function loadNewShortmessageitem() {
    var act = $('div.shortmessageItem.random.act');
    var next = null;
    if ($(act).next('.random').length>0) {
        next = $(act).next(".random");
    } else {
        next = $("div.shortmessageItem.random:first");
    }
    $('div.shortmessageItem').removeClass("act");
    $(act).fadeOut("fast",function() {
        $(next).addClass("act").fadeIn("fast");
        });
    
    loadNewShortmessage();
}

function setRefreshTimeout() {
	if (refreshId!=0) {
		clearTimeout(refreshId);
	}
	refreshId = setTimeout("refreshPage()",refreshing);
}
function refreshPage() {
	//window.location.href = window.location.href;
}