/*
 * Script that will change the content of subsections dynamically
 * -
 * FG - Mars 2010
 */

var current_location = '';

$(document).ready(function() {

    fix_us_links();

    //fix_subsection_links();
    $('#SubSectionLinks li a').click(load_content);

    $('.simpletooltip').simpletooltip();

    current_location = window.location.href;

    setTimeout("checkIfLocationChanged()", 200);

    if (window.location.href.split('#').length > 1) {
        current_location = 'reload_ajax_content';
        checkIfLocationChanged();
    }


});


/*
 * The super nice function, by FG for KBFTech
 */
function checkIfLocationChanged()
{
    if (current_location != window.location.href)
    {
        current_location = window.location.href;

        var a = current_location.split('#');
        if (a.length == 1) {
            load_subsection_content(current_location);
        } else {
            var tag = a.pop();
            var href = a.pop().split('/');
            href.pop();
            var new_href = href.join('/') + '/' + tag;
            load_subsection_content(new_href);
        }
    }


    setTimeout("checkIfLocationChanged()", 200);
}

function fix_subsection_links()
{
    $('#SubSectionLinks li a').each(function() {
        var href = $(this).attr('href');
        a = href.split('/');
        name = a.pop();

        $(this).attr('href','#' + name).attr('name',href);

    });
}

function load_content(event)
{

    var href = $(event.target).attr('href');
    a = href.split('/');
    name = a.pop();

    $(event.target).attr('href','#' + name).attr('name',href);


    var href = $(event.target).attr('name');
    
    load_subsection_content(href);
    // return false prevent the real href 
    return true;
}


function load_subsection_content(href)
{
    $.get(href, null, function(data) {

        var subsection_data = $('#SubSection', data).html();
        $('#SubSection').html(subsection_data);
        // affect onclick to the new subsection menu
        $('#SubSectionLinks li a').click(load_content);
        //fix_subsection_links();

        var top_right_menu = $('#FixedTitleRight', data).html();
        $('#FixedTitleRight').html (top_right_menu);

        var bannerimg = $('#VarTitleImage', data).html();
        $('#VarTitleImage').html (bannerimg);

        fix_us_links();

    });
}


function fix_us_links()
{

    var path = window.location.pathname;

    us_re = /^\/us\//;
    if ( ! us_re.test(path)) {
        return;
    }
    en_re = /\/en\/$/;
    $('a').each (function(i, e) {
        var href = $(this).attr('href');
        
        if ((href) && (en_re.test(href) == false)) {
            href = href.replace('/en/','/us/');
            $(this).attr('href',href);
        }
    });

}
