$(document).ready( function(){

    //Build a PHP like $_GET Array
    var $_GET = {};

    document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
        function decode(s) {
            return decodeURIComponent(s.split("+").join(" "));
        }

        $_GET[decode(arguments[1])] = decode(arguments[2]);
    });


    //CALENDAR START ////////////////////////////////////////////////////

    //Links in Minikalender entfernen
    /*
    $('.eventDay span.bold').each( function(){
    	$(this).text($(this).children('a').text());
    });
    $('.eventDay span.bold a').remove();
    */
    
    //a_ b_ präfix für Sortierung ausblenden
    calendarReplacePrefix($('.rootElement span'));
    calendarReplacePrefix($('.calCategoryContainer .V9G a'));
    

    //Style category entries
    if($('#frmCategories .rootElement')){
        var tmpRootElWidth = 0;
        $('#frmCategories .rootElement').each( function(){
            if($(this).width() > tmpRootElWidth){
                tmpRootElWidth = $(this).width();
            }

        });
        $('#frmCategories .rootElement').css("width", (tmpRootElWidth + 15) + "px");
        $('#divReloadCategories').hide();
    }

    //unselect category entries
    //but not after navigation or on dayview if >1 categories were selected
    if(!$_GET["cHash"] || ( ($_GET["id"] == 18 || $_GET["id"] == 75 || $_GET["id"] == 86) && !$_GET["tx_cal_controller[categorySelection]"]) ){
    	
    	categoryWasPreselected = false;
    	for(i=0;i<=100;i++){
    		if($_GET["tx_cal_controller[category][" + i + "]"] > -1 ){
    			categoryWasPreselected = true;
    		}
    	}
    	if(!categoryWasPreselected){
	        $('#frmCategories input[type=checkbox]').each( function(){
	           $(this).attr("checked", false) ;
	        });
    	}
    }

    //click on category-checkboxes submits the form
    $('#frmCategories input').click(function() {
        $('#frmCategories input').hide();
        $('#frmCategories').submit();
    });

    //STYLE FORMS
    $("select, :checkbox, :radio, :file").uniform();

    //CALENDAR END //////////////////////////////////////////////////////
});

function calendarReplacePrefix(nodes){
    nodes.each( function(){
        var text = $(this).text();
        var prefix = text.substring(0,2);
        $(this).text( text.replace(prefix, "") );
    });

}
