$(document).ready(function(){
    initQuestionnaire();
    initQuestionDetailsToggle();
    initCommentBoxCount();
		initSideSlider();
      if ($.fn.autocomplete) { // check if Autocomplete script available
            $('div.sort-box input[@type=text]').autocomplete("/service/dynamic/getCities", {
                minChars:1,
                delay:10,
                matchSubset:1,
                matchContains:0,
                cacheLength:10,
                formatItem:formatItem,
                selectOnly:1,
                submitForm:submitDirectUniAccessForm // delegate to form's submit handler
            });
        }
     // hide submit buttons for university search (form submitted by pressing "enter" or select)
    $('form.ranking span.button').hide();
    var $jumpto = $('div.jumpto');
    if($jumpto.size()) {
        $jumpto = $('div.jumpto').get(0);
        $('span.button', $jumpto).hide();
        var $jumptoinput= $("input[@type=text]",$jumpto).click(function(evt) {
            evt.preventDefault();
            this.value = ""; // clear entry
        });
        if ($.fn.autocomplete) { // check if Autocomplete script available
            $jumptoinput.autocomplete("/service/dynamic/getCities", {
                minChars:1,
                delay:10,
                matchSubset:1,
                matchContains:0,
                cacheLength:10,
                formatItem:formatItem,
                selectOnly:1,
                submitForm:submitDirectUniAccessForm // delegate to form's submit handler
            });
        }
        $("form",$jumpto).submit(function(evt) {
            evt.preventDefault();
            return submitDirectUniAccessForm(this);
        });
    }
});

function submitDirectUniAccessForm(form) {
    var url = "/service/dynamic/getUnisForCity";
    var city = form.elements['suche'].value;
    $.post(url, {city: city}, function(data) {
        //data = getPrototypeData(data); // comment out this line on live system
        var $result = $("#uni-list");
        $result.empty();
        $result.append(data);
    });
    return false;
}


/*
 * Sets up star-rating and also mouseover highlighting.
 */
function initQuestionnaire() {
    if ($.fn.rating) { // check if Rating script available

       /*
        $('form .questions .yourrating .rating').rating();
        $('form .questions .yourrating .rating').init();
*/
        $('select.rating').rating();
        $('select.rating').initRating();
    }
}

/*
 *  Adds listener to to Comment links, which will
 *  toggle the textarea for the comments.
 */
function initQuestionDetailsToggle() {
    var f = function() {
        var $detailsElement = $('ul', $(this).parent().parent());
        if($detailsElement.css("display") == "block") {
            $detailsElement.slideUp("slow");
            $detailsElement.parent().removeClass("opened");
       } else {
            $detailsElement.slideDown("slow");
            $detailsElement.parent().addClass("opened");

        }
        return false;
    }
    var $survey = $('#survey');
    if($survey.size()){
        $survey = $survey.get(0);
        $('ul',$survey).hide();
        $('h4>a',$survey).attr("title", "Details anzeigen").click(f);
    }
}

/*
 *  Comment boxes on the questionnaire pages allow a maximum number of characters and
 *  the remaining number of characters are displayed.
 */
var charLimit = 300;
function initCommentBoxCount() {
    var $textareas = $('#survey textarea');
   $textareas.each( function(){
        $(this).siblings('span').html("noch " + (charLimit - this.value.length) + " Zeichen");
    });
    $textareas.keydown(handleCommentBoxKeyEvent);
    $textareas.keyup(handleCommentBoxKeyEvent);
}
function handleCommentBoxKeyEvent() {
    if (this.value.length > charLimit) { // it too long then trim string
        this.value = this.value.substring(0, charLimit);
    }
    else { // otherwise, update character counter
         $(this).siblings('span').html("noch " + (charLimit - this.value.length) + " Zeichen");
    }
}
function initSideSlider() {

	$('#nav-ranking span.show').css({display: 'block'}).html('<a>zeigen</a>')
	$('#nav-studypath span.show').css({display: 'block'}).html('<a>zeigen</a>');

	//per default

	var visRank = true;
	var visPath = false;
	// und was wollen wir eigentlich angezeigt bekommen
	hideStuff($('#nav-studypath'));


	$('#nav-studypath h3').click(function() {
		if(! visPath )
		{
			if(!visRank) {
				showStuff($('#nav-studypath'));
			}
			else
			{
				hideStuff($('#nav-ranking'));
				visRank = !visRank;/*
				window.setTimeout("showStuff($('#nav-studypath'))",200);*/
				showStuff($('#nav-studypath'));
			}
		}
		else
		{
			hideStuff($('#nav-studypath'));
		}
		visPath = !visPath;
	}).css({cursor: 'pointer'});;

	$('#nav-ranking h3').click(function() {
		if(! visRank )
		{
			if(!visPath)
			{
				showStuff($('#nav-ranking'))
			}
			else
			{
				hideStuff($('#nav-studypath'));
				visPath = !visPath;/*
				window.setTimeout("showStuff($('#nav-ranking'))",200)  */
				showStuff($('#nav-ranking'));
			}
		}
		else
		{
			hideStuff($('#nav-ranking'))
		}
		visRank = !visRank;
	}).css({cursor: 'pointer'});
}

function hideStuff(jqElem) {
	jqElem.find('h3:first').addClass('off');
	jqElem.find('span.hide:first').removeClass('hide').addClass('show').find('> a').html('zeigen');
	jqElem.find('> ul:first').animate({opacity:0,height:'hide'},'fast',true);
	jqElem.find('> div.sort-box:first').animate({opacity:0,height:'hide'},'fast',true);

}

function showStuff(jqElem) {
	jqElem.find('h3:first').removeClass('off');
	jqElem.find('span.show:first').removeClass('show').addClass('hide').find('> a').html('verstecken');
	jqElem.find('> ul:first').animate({opacity:1,height:'show'},'fast',true);
	jqElem.find('> div.sort-box:first').animate({opacity:1,height:'show'},'fast',true);
}
$('document').ready(function(){
	if(typeof Shadowbox != 'undefined'){

		Shadowbox.init({counterType:'skip'});
	}
});
