

/**
 * @author Burns
 */
 function inc(filename){
    var head = document.getElementsByTagName('head').item(0);
    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';
    head.appendChild(script)
}

function split( val ) {
        return val.split( /,\s*/ );
}
function extractLast( term ) {
        return split( term ).pop();
}


// convert $_GET
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function(){
    function decode(s){
        return decodeURIComponent(s).replace(/\+/g, " ");
    }
    $_GET[decode(arguments[1])] = decode(arguments[2]);
});

jQuery(document).ready(function(){
	var topnav = 0;
	/* Navigation */
	$("#menu h1 a").mouseover(showTopnav)
	.click(function() {
		/*return false;/**/
	});
	
	
	function showTopnav() {
		if (!topnav) {
			console.log('showTopnav');
			topnav = 1;
			$('#menu .menu_closed').hide();
			/*$('#menu .menu_open ul').slideUp("fast", function() {
				$('#menu').addClass('onlytopnav').mouseleave(hideTopnav);
				$('#menu .menu_closed').show();
			});*/
			$('#menu .menu_open ul').hide("fast", function() {
				$('#menu').addClass('onlytopnav').mouseleave(hideTopnav);
				$('#menu .menu_closed').show();
			});
		}
	}

	function hideTopnav() {
		if (topnav) {
			console.log('hideTopnav');
			topnav = 0;
			$('#menu .menu_open ul').hide();
			/*$('#menu .menu_closed').slideUp("fast", function() {
				$('#menu').removeClass('onlytopnav');
				$('#menu .menu_open ul').show();
			});*/
			$('#menu .menu_closed').hide("fast", function() {
				$('#menu').removeClass('onlytopnav');
				$('#menu .menu_open ul').show();
			});
		}
	}
	
	/*
    $("#navigation .menu_open h3").addClass("findme");
    $("#navigation .menu_open").removeClass("menu_open");
	$('#menu h1').click(function(){
		$("#navigation").find("h3").parent().show();
	});
    $('#navigation').accordion({
        header: 'h3',
        active: $("#navigation .findme"), /* Selector for the active element. Set to false to display none at start. Needs collapsible: true.* /
		autoHeight: false, 
		fillSpace: true, 
		collapsible: true, /*Whether all the sections can be closed at once. Allows collapsing the active section by the triggering event (click is the default).* /
		change: function(event, ui) {
			$(this).find("h3:not(.ui-state-active)").parent().hide()
		}
    });
	*/
	
	$('#changeCountry').dialog({
		autoOpen: false,
		modal: true,
		draggable: false,
		resizable: false,
		width: 530,
		/*height: 240,*/
		dialogClass: "changeCountryDialog"
	});
	$('.changeCountryDialog').removeClass('ui-corner-all');
	
	/* Landauswahl weil keines gefunden */
	if ($('#changeCountry').hasClass('showme')) {
		$('.changeCountryDialog .ui-dialog-titlebar-close').hide();
		$('#changeCountry').dialog( "option", "closeOnEscape", false )
		.dialog('open').show();
		return false; 
	}
	
	/* Landauswahl auf Knopfdruck */
	$('#currentCountry').click(function() {
		$('#changeCountry').dialog('open').show();
            $('#changeCountry input:checked').focus();
            return false;
	});
	
	/* search-autocomplete */
	var s = $("#searchForm input");
	var standard = 'Suche';
	if (s.val() == '') {
		s.val(standard).addClass('leer');
	}
	s.focus(function() {
		if ($(this).val() == standard) {
			$(this).val('').removeClass('leer');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(standard).addClass('leer');
		}
	}).autocomplete({
		minLength: 3, 
		delay: 100,
                source: function( request, response ) {
                        $.getJSON( "/term", {
                                term: extractLast( request.term )
                        }, response );
                },
                search: function() {
                        // custom minLength
                        var term = extractLast( this.value );
                        if ( term.length < 2 ) {
                                return false;
                        }
                },
                focus: function() {
                        // prevent value inserted on focus
                        return false;
                },
                select: function( event, ui ) {
                        var terms = split( this.value );
                        // remove the current input
                        terms.pop();
                        // add the selected item
                        terms.push( ui.item.value );
                        // add placeholder to get the comma-and-space at the end
                        terms.push( "" );
                        this.value = terms.join( ", " );
                        return false;
                }



	}).data( "autocomplete" )._renderItem = function( ul, item ) {
		if ( item.piclink == '' ) { 
			$piclink = '';
		} else {
			$piclink = "<img src='/" + item.piclink + "-0000.jpg' />";
		}
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a href='#'>" + $piclink + "<span>" + item.value + "</span></a>" )
			.appendTo( ul );
	};


});
