$(document).ready(function(){
    $("#form_termine select").change(function(){
        termin_id = $(this).val();
        debug('test innen' + termin_id);
        url = $("#form_termine").attr("action");
        debug('url= ' + url);
        $.post(url, {
            termin_id: termin_id
        }, function(data){
            $("#termin_zeiten").html(data.zeiten);
            $("#termin_ort").html(data.name);
            $("#termin_merkzettel").attr('href', data.url);
        }, "json");
    });

    $("#select_termin").change(function(){
        termin_id = $(this).val();
        url = $("#form_termine").attr("action");
        $.post(url, {
            termin_id: termin_id
        }, function(data){
            $(".head_zeiten").html(data.zeiten);
            $(".head_ort").html(data.name);
        }, "json");
    });

    $('a.fancybox').fancybox({
        'hideOnContentClick': false,
        'autoScale': false,
        'width': 500,
        'height': 600,
        'transitionIn'	: 'none',
				'transitionOut'	: 'none'
    });

/*
		$("a.fancybox").click(function() {
			$.fancybox({
        'hideOnContentClick': false,
        'autoScale': false,
        'width': 500,
        'height': 600,
        'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')

			});
			return false;
		});
	*/



    id = $('#form_anmeldung select[name="rechnung"]').val();
    if (id == 1)
        $("#rechnung").fadeIn();

    id = $('#form_anmeldung select[name="bezahlung"]').val();
    if (id == 1)
        $("#bezahlung").fadeIn();

    // merkzetteljquery
    $('#merkzettel_list li').mouseover(function(){
        $(this).find(".merkzettel_details").css('display', 'block');
    }).mouseout(function(){
        $(this).find(".merkzettel_details").css('display', 'none');
    });


    custom_height = $('#custom').height();
    $('.fragment').css('height', custom_height);
});

$(function(){
    $('#form_anmeldung select[name="rechnung"]').change(function(){
        id = $(this).val();

        if (id == 1)
            $("#rechnung").fadeIn();
        else
            $("#rechnung").fadeOut();

    });
    $('#form_anmeldung select[name="bezahlung"]').change(function(){
        id = $(this).val();

        if (id == 1)
            $("#bezahlung").fadeIn();
        else
            $("#bezahlung").fadeOut();

    });

    doActionAndLocation();

});

/*** FUNKTIONEN ***/


/*
 debug('foobar');
 $(this).fancybox({
 'hideOnContentClick': true
 //			'href': url
 });
 */
function debug(error){
    if (window.console) {
        console.debug(error);
    }
}

function submitFancyForm(id){
    debug(id);
    $.fancybox({
        'hideOnContentClick': false,
        'autoScale': false,
        'width': 500,
        'height': 500,
        'href': $("#" + id).attr('action'),
        'data': {
            'hans': 1
        }
    });
}

function submitFancyPost(id){
    check = true;
    $("#" + id + ' input.required').each(function(){
        debug($(this).val());

        if ($(this).val() == '') {
            $(this).addClass('red');
            check = false;
        }

    });
    if (check == false)
        return false;

    $.ajax({
        type: "POST",
        cache: false,
        url: $("#" + id).attr('action'),
        data: $("#" + id + ' :input').serializeArray(),
        success: function(data)
        {

        debug('hallo ');
        		$.fancybox.close();

            $.fancybox(data);
        }
    });
}

function deleteMerkzettelEintrag(url){
    $.ajax({
        type: "POST",
        cache: false,
        url: url,
        success: function(data){
            $.fancybox(data);
        }
    });
}

function fancyLink(){
    $('.fancylink').click(function(){
        url = $(this).attr('url');
        debug(url);
        /*
         $.ajax({
         type		: "POST",
         cache	: false,
         url		: url,
         success: function(data) {
         $.fancybox(data);
         }
         });
         */
    });
}

function doActionAndLocation(){
    $('.action_and_location').click(function(){
        param = $(this).attr('ref');
        url = getAjaxUrl() + '&' + param;
        url_location = $(this).attr('href');

        debug(url);
        debug(url_location);

        $.ajax({
            type: "POST",
            cache: false,
            url: url,
            success: function(data){
                return false;
                //confirm(window.location.href = url_location);
            }
        });
    });
}

function showMerkzettelBox(text){
    $(".merkzettel").css("display", "block");
    $(".merkzettel .cm_link .cm_link_content .text").html(text);
}

function dropShadowButton(){
    /*** DropShadow Stuff ***/
    var dropShadowOptions = {
        left: 1,
        top: 1,
        blur: 1,
        opacity: .2,
        swap: false // no shadow over element
    }
    try {
        $("input.submit").redrawShadow(dropShadowOptions);
    }
    catch (e) {
    }

}

