$(document).ready(function() {

    var current_v_index = 0;
    var current_h_index = 0;
    var v_distance = 147;
    var h_distance = 185;
    var navi_locked = false;

    function navi_goto(target_v_index, target_h_index) {
        if (navi_locked) return;
        navi_locked = true; $("#lock").show();
        load_content(target_v_index, target_h_index);
        v_delta = (target_v_index - current_v_index) * v_distance * (-1);

        //Poista selected-class
        $("#navi li.h").removeClass("h_selected");
        $("#navi li.v").removeClass("v_selected");

        //Tarvitseeko vaihtaa pystysuuntaista itemiä
        if (target_v_index != current_v_index) {
            //Sulje vaakanavi
            $("#navi_h_container_inner").hide();
            $("#navi_h"+current_v_index).fadeOut(150, function() {
            $("ul.navi_h").css("left", 0);

                //Animoi pystynavi
                $('#navi').animate({
                    top: "+="+v_delta
                }, 750, function() {
                    //Animation complete, set new index
                    current_v_index = target_v_index;
    
                    //Avaa vaakanavi
                    $("#navi_h_container_inner").fadeTo(150, 0.5, function() {
                        $("#navi_h"+current_v_index).fadeIn(150);                    
                    });
                    if (current_v_index == 0) {
                        $("a#link_reduce_v_index").hide();
                    }
                    else {
                        $("a#link_reduce_v_index").show();                
                    }                    
                    //Siirrä vaakanavi oikeaan kohtaan
                    navi_horizontal_goto(target_h_index, true);
                    $("li#v"+current_v_index).addClass("v_selected");
    
                });

            });
        } else { //pystynavia ei tarvitse siirtää
            //Siirrä vaakanavi oikeaan kohtaan
            $("#navi_h_container_inner").fadeTo(150, 0.5, function() {
                $("#navi_h"+current_v_index).fadeIn(150);                    
            });
            if (current_v_index == 0) {
                $("a#link_reduce_v_index").hide();
            }
            $("li#v"+current_v_index).addClass("v_selected");
            navi_horizontal_goto(target_h_index, false);
        }
    
    }

    function navi_horizontal_goto(target_h_index, reset) {
        if (reset) {
            current_h_index = 0;
            $('#navi_h_container_inner').css('left', 0);
        }
        h_delta = (target_h_index - current_h_index) * h_distance * (-1);

        //Tarvitseeko vaihtaa vaakasuuntaista itemiä
        if (target_h_index != current_h_index) {
            //Animoi vaakanavi
            $('#navi_h'+current_v_index+', #navi_h_container_inner').animate({
                left: "+="+h_delta
            }, 500, function() {
                //Animation complete, set new index
                current_h_index = target_h_index;
                if (current_h_index == 0) {
                    $("a#link_reduce_h_index").hide();
                }
                else {
                    $("a#link_reduce_h_index").show();                
                }
                $("li#v"+current_v_index+"_h"+current_h_index).addClass("h_selected");
                navi_locked = false; $("#lock").hide();
            });          
        } else {
            if (current_h_index == 0) {
                $("a#link_reduce_h_index").hide();            
            }
            $("li#v"+current_v_index+"_h"+current_h_index).addClass("h_selected");
            navi_locked = false; $("#lock").hide();
        }
    }
    /*
    function set_navi_lock(lock_status) {
        navi_locked = lock_status;
        if (lock_status == true) {
            $("#lock").show();
        } else {
            $("#lock").hide();
        }
    }
    */
    function load_content(target_v_index, target_h_index) {
        document.location.hash = "#/"+target_v_index+"/"+target_h_index+"/"
        $("#content").empty();
        //$("#ajax_loader").show();
        $("#content").hide();      
        
        $.get("/content_proxy/"+target_v_index+"/"+target_h_index+"/", function(data) {
            $("#content").html(data);      
            //$("#ajax_loader").hide();
            $("#content").fadeIn(500);
        });
    
    }


    //Vertikaalinapin klikkaus
    $('#navi a.v').click(function() {
        target_v_index = $(this).attr("rohea:v");
        navi_goto(target_v_index, 0);
    });
    //Horisontaalinapin klikkaus
    $('#navi a.h').click(function() {
        target_h_index = $(this).attr("rohea:h");
        navi_goto(current_v_index, target_h_index);
    });

    //Nuolet ja home
    $("#link_home").click(function() {
        navi_goto(0, 0);    
    });
    $("#link_reduce_v_index").click(function() {
        if (current_v_index > 0) {
            navi_goto((current_v_index-1), 0);    
        }
    });
    $("#link_reduce_h_index").live('click', function() {
        if (current_h_index > 0) {
            navi_goto(current_v_index, (current_h_index-1));
        }
    });

    //Sivulatauksella aseta oletustila lukemalla ankkurit url:sta
    anchor = document.location.hash;
    if (anchor.length > 0) {
        anchor = anchor.substr(2);
        anchor_array = anchor.split("/");
        navi_goto(anchor_array[0], anchor_array[1]);    
    }
    else {
        navi_goto(0, 0);        
    }

 
});

