/* 
 * Content slider
 *
 * Sets news / login box etc
 * 
 */

// current state of slider
var contentstate = 'closed';
$(function(){
    // calculate available height for content window.
    // menu ( navi ) == 100 px
    // content slider height == window height - menu - margin ( 40 )
    calced_height = $(window).height() - 100 -70;
    max_height  = 420;
    height_available = ( calced_height > max_height ) ? max_height : calced_height;
    
    // open slider ( if not open )
    $(".contentslider").live('click', function(e) {
        if ( $(this).attr('class') != 'external' ) {
            e.preventDefault();
            url = $(this).attr('href');
            // if state is already open.. trigger event to close it first.
            if ( contentstate == 'open' ) {
                $("#tekstContainer").animate({
                    height:0
                }, function () {
                    $("#tekstContainer").addClass('hidden');
                    $("#tekstContainer").removeAttr('style');
                    $("#tekstContainer p").html(''); // empty placeholder
                    setTimeout(function(){
                        contentstate = 'open';
                        setContent(url, false);
                        $("#tekstContainer").removeClass('hidden');
                        $("#tekstContainer").animate({
                            height:height_available
                        });
                    }, 500);
                });
            } else {
                contentstate = 'open';
                setContent(url, false);
                $("#tekstContainer").removeClass('hidden');
                $("#tekstContainer").animate({
                    height:height_available
                });
            }
        }
    });


    // get clicks in panel
    $(".newsItem a").live('click',function(e) {
        e.preventDefault();
        // get href
        href = $(this).attr('href');
        type = $(this).attr('type' );
        if ( type ) {
            setContent(href, false);
        } else {
            setContent(href, true);
        }
    });
    
    $(".contentlink").live('click', function ( e ) {
        e.preventDefault();
        href = $(this).attr('href');
        setContent ( href, false );
    });
    
    // close slider on click
    $("a.sluiten").click(function(e) {
        e.preventDefault();
        contentstate = 'closed';
        $("#tekstContainer").animate({
            height:0
        }, function () {
            $("#tekstContainer").addClass('hidden');
            $("#tekstContainer").removeAttr('style');
            $("#tekstContainer p").html(''); // empty placeholder
        });

    });

    // append content to slider
    function setContent ( uri, addback ) {
        $.ajax  ({
            url: uri,
            async:true,
            type:'GET',
            success:function (data) {
                if ( data.logged && data.logged == 'Y' ) {
                    document.location=settings.base_address + 'persoonlijke-pagina';
                }
                if ( addback ) {
                    data += '<p><a href="' + settings.base_address +'nieuws" class="contentlink" type=newsback>Vorige pagina</a></p>';
                }
                $("#tekstContainer p").html(data);
                $("#tekstContainer").scrollTo( 0, { duration:0 });
            },
            complete: function () {
                return true;
            }
        });
    }


    // hunt login submit
    $("#loginSubmitForm").live('submit', function(e){
        e.preventDefault();
        // reset possible error bar
        $("#errorLoginbar").addClass ( 'forcehidden' );
        
        // try to authenticate.
        $.ajax  ({
            url: settings.base_address + 'authenticate/authenticate',
            data: {
                username: $("#username").val(),
                password: $("#password").val()
            },
            async:true,
            type:'POST',
            success:function (data) {
                if ( data.success == true ) {
                    // suc6 redirect
                    window.location=settings.base_address + 'personal';
                } else {
                    $("#errorLoginbar p").html(data.error);
                    $("#errorLoginbar").removeClass('forcehidden' );
                }
            }
        });
    });
});
