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 = $('#shortmessagetext').val();
    
    $('#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);
                },"json"
            );

        } else {
            alert($('#language_701').text());
            $('#shortmessagetext').focus();
        }

        return false;
    });

    if ($('div.shortmessageItem').length > 5) {
        loadRandomSMs();
        var exp = $("<div class='expand' id='expander'>"+$('#language_700').text()+"</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
     */


	$('a.settoprofilepic').click(function() {
		var fotoid = $(this).attr("myid");
		$.post("/ajaxcalls/setprofilepic.html", {foto_id:fotoid},
                function(data) {
        			window.location = window.location.href;
                },"json"
            );    
        return false;
	});
	
	var ck_id = 0;
	$('.myckeditor').each(function() {
		$(this).attr("id","textarea_"+ck_id);
		
		$('#textarea_'+ck_id).ckeditor(function() { 
			
		},{'toolbar':'Standard'});
		ck_id++;
	});
	
	
    $('select.countryselectdd').change(function() {
    	var valu = $('option:selected',$(this)).val();
    	$('select',$('tr.bundesland_select')).hide();
    	if (valu=="AT" || valu == "IT" || valu=="DE") {
    		var sel = "select.bos_bundesland_"+valu;
    		$('tr.bundesland_select').show();
    		$(sel,$('tr.bundesland_select')).show();
    	} else {
    		$('tr.bundesland_select').hide();
    	}
    	
    });
    
    
	$('.fancybox').fancybox();

    sortDD('.countryselectdd');
    
});

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;
}

function sortDD(id) {
	var $dd = $(id);
	if ($dd.length > 0) { // make sure we found the select we were looking for
	
	    // save the selected value
	    var selectedVal = $dd.val();
	
	    // get the options and loop through them
	    var $options = $('option', $dd);
	    var arrVals = [];
	    $options.each(function(){
	        // push each option value and text into an array
	        arrVals.push({
	            val: $(this).val(),
	            text: $(this).text()
	        });
	    });
	
	    // sort the array by the value (change val to text to sort by text instead)
	    arrVals.sort(function(a, b){
		    if(a.text>b.text){
		        return 1;
		    }
		    else if (a.text==b.text){
		        return 0;
		    }
		    else {
		        return -1;
		    }
		});
	
	    // loop through the sorted array and set the text/values to the options
	    for (var i = 0, l = arrVals.length; i < l; i++) {
	        $($options[i]).val(arrVals[i].val).text(arrVals[i].text);
	    }
	
	    // set the selected value back
	    $dd.val(selectedVal);
	}
}
