/**
 * Ajax fix voor page reload
 */
if (document.location.href.search(/#/) != -1) {
    var url = document.location.href.split("#");
    document.location.href  =  url[1];
}


$(document).ready(function(){
    setBehaviour();
    initHistory();
});

function setBehaviour() {
	$('a.ajax').click(function () {
        ajax_load(this.href);
        return false;
    });

    $('a').click(function() {
      this.blur();
    });

    $('a.form').submit(function () {
        ajax_form(this);
        return false;
    });
    
    $('#right').fadeTo("slow", "0.8");
    $('#left').fadeTo("slow", "0.8");
    
    $('#right').hover(
        function() {
            $(this).animate({height: "520px"}, { duration: 300, queue: false });
        },
        function() {
            $(this).animate({height: "25px"}, { duration: 300, queue: false });
        }        
    );


    $('#left').bind('mouseenter', function() {
        $(this).animate({height: "520px"}, { duration: 300, queue: false });
    });

    $('#left').bind('mouseleave', function() {
        $(this).animate({height: "25px"}, { duration: 300, queue: false });
    });

    $('.menuitem').hover(
        function(){$(this).toggleClass('menuitem_on')}, 
        function(){$(this).toggleClass('menuitem_on')}
    );

    $('.linkitem').hover(
        function(){$(this).addClass('linkitem_on')}, 
        function(){$(this).removeClass('linkitem_on')}
    );

    $('.onliner').click(function(){
        window.open("/bcq/write/?uid_to=" + $(this).attr('id'), "BCQ_write", "width=300,height=200");
    });
    
    $('.onliner').unbind('mouseenter','mouseleave');
    $('.onliner').hover(
        function(){$(this).addClass('onliner_on')}, 
        function(){$(this).removeClass('onliner_on')}
    );

    $('.brief').click(function(){
        window.open("/bcq/read/?message_id=" + $(this).attr('id'), "BCQ_"+ $(this).attr('id'), "width=300,height=200");
    });

    $('.brief_open').click(function(){
        window.open("/bcq/read/?message_id=" + $(this).attr('id'), "BCQ_"+ $(this).attr('id'), "width=300,height=200");
    });
    
    $('.brief').unbind('mouseenter','mouseleave');
    $('.brief').hover(
        function(){$(this).addClass('brief_open')}, 
        function(){$(this).removeClass('brief_open')}
    );

    $('#messages_in').click(function(){
        window.open("/bcq/messagesin", "BCQ_messages", "width=300,height=500,scrollbars=1");
    });

    $('#messages_out').click(function(){
        window.open("/bcq/messagesout", "BCQ_messages", "width=300,height=500,scrollbars=1");
    });

}


// History - AJAX calls
function initHistory() {
    // Start ajax history
    $.history.addListener(historyChange);
}

var historyChange = function(newLocation, historyData) {
   if (newLocation == "")  newLocation = "/";
    else if (oldLocation != newLocation) {
        ajax_load(newLocation);
    }
    oldLocation = newLocation;
}
var oldLocation;


/**
* Laadt een url via ajax
*/
function ajax_load(alink) {
    // Voor back/forward knoppen
    $.history.add(alink, alink);
    $.get(alink, function(data) {
        data = eval('('+data+')');
        $.each(data, function(i,div){
            if (div.id == 'notify') notify(div.message, div.type);
            else $("#"+div.id).html(div.html);
        });
        // Zet behaviour opnieuw
        //setBehaviour();
    });
}

function ajax_form(frm) {
	$.submitForm(function(data) {
        data = eval('('+data+')');
        $.each(data, function(i,div){
            if (div.id == 'notify') notify(div.message, div.type);
            else $("#"+div.id).html(div.html);
        });
    });
	
}

function startbcq() {
    var BCQ = window.open("/bcq", "BCQ", "width=200,height=400");
}

