/*
-------------------------------------------------------------------------------
Tulkkipalvelut Sivupersoona Oy JavaScript
Author: Redflow Inc. (http://www.redflow.fi/)
Version: 02.01.2012

Copyright (c) 2011-2012 Redflow Inc. All rights reserved.
---------------------------------------------------------------------------- */

// Initialize the pages and player variables

var pages = [ 
    "/etusivu/", 
    "/sivupersoona/", 
    "/laatutyo/", 
    "/tulkit/", 
    "/rekry/", 
    "/toiminta-alue/", 
    "/tulkkitilaus/", 
    "/yhteystiedot/"
];

var player = null;

// Protect email-addresses from spam
function convert_email_addresses() {
    $("a[class*='mail']").each(function() {
        if (!$(this).hasClass("converted")) {
            var mail = $(this).text().split("").reverse().join("");

            $(this).text(mail);
            $(this).attr("href", "mailto:" + mail);
            $(this).addClass("converted");
        }
    });
}

// Set the document title
function document_title(title, use_default_title) {
    if (use_default_title == true) {
        document.title = title + " - Tulkkipalvelut Sivupersoona";
    } else {
        document.title = title;
    }
}

// Initialize the JW Player
function load_player() {
    if (!player) {
        if ($("#player").length > 0) {
            player = jwplayer("player").setup({
                bufferlength: 5, 
                controlbar: "none", 
                events: {
                    onMute: function(event) {
                        $("#toolbar #player-controls #toggle-mute").toggleClass("muted");
                    }
                }, 
                flashplayer: "/jwplayer/player.swf", 
                file: "http://www.youtube.com/watch?v=QRhRmjQqLEM", 
                image: "/media/img_sivupersoona_trailer.jpg", 
                height: 468, 
                width: 832, 
                stretching: "fill", 
                volume: 100
            });
        }
    }
}

// Setup external links (open external links in new window)
function setup_external_links() {
    $("a[rel*='external']").each(function() {
        $(this).attr("target", "_blank");
    });
}

// Setup the page (update the document title and navigation)
function setup_page() {
    switch($.address.path()) {
        case "/":
            document_title("Etusivu", true);
            slider_navigation("/yhteystiedot/", "/sivupersoona/");
            break;

        case "/sivupersoona/":
            document_title("Sivupersoona", true);
            slider_navigation("/", "/laatutyo/");
            break;

        case "/laatutyo/":
            document_title("Laatutyö", true);
            slider_navigation("/sivupersoona/", "/tulkit/");
            break;

        case "/tulkit/":
            document_title("Tulkit", true);
            slider_navigation("/laatutyo/", "/rekry/");
            break;

        case "/rekry/":
            document_title("Rekry", true);
            slider_navigation("/tulkit/", "/toiminta-alue/");
            break;

        case "/toiminta-alue/":
            document_title("Toiminta-alue", true);
            slider_navigation("/rekry/", "/tulkkitilaus/");
            break;

        case "/tulkkitilaus/":
            document_title("Tulkkitilaus", true);
            slider_navigation("/toiminta-alue/", "/yhteystiedot/");
            break;

        case "/yhteystiedot/":
            document_title("Yhteystiedot", true);
            slider_navigation("/tulkkitilaus/", "/");
            break;

        default:
            document_title("Etusivu", true);
            slider_navigation("/yhteystiedot/", "/sivupersoona/");
    }
}

// Update the content slider navigation links (previous & next)
function slider_navigation(previous, next) {
    $("#content .slider a.slider-nav.left").attr("href", previous);
    $("#content .slider a.slider-nav.right").attr("href", next);
}

// Setup the ticker for the blog articles
function setup_ticker() {
    if ($("#articles li").length > 1) {
        setInterval(function() {
            $("#articles li:first").slideUp("slow", function() {
                $(this).appendTo("#articles").slideDown();
            });
        }, 3500);
    }
}

$(document).ready(function() {
    // Setup the jQuery Address
    $.address.init(function(event) {
        $.address.state("/");
        $("#content .slider a.slider-nav").address();
        $("#lifts .slider ul li a").address();
        $("#footer #nav li a:not(a[rel*='external'])").address();

        // Slide to right slide
        if (window.location.hash) {
            $("#nav").find("a[href='" + window.location.hash.substring(1) + "']").click();
        } else {
            $("#nav").find("a[href='" + window.location.pathname + "']").click();
        }
    }).change(function(event) {
        // Setup external links and the page (update the document title and navigation)
        setup_external_links();
        setup_page();
    });

    // Initialize the ticker
    setup_ticker();

    // Initialize the sliders

    $("#content .slider ul li:first-child").remove();

    var slider = $("#content .slider").sudoSlider({
        ajax: pages, 
        ajaxLoadFunction: function(slide) {
            // Protect email-addresses from spam
            convert_email_addresses();

            // Initialize the JW Player
            load_player();
        }, 
        afterAniFunc: function(slide) {
            // Fix for the content auto-height bug in sudoSlider
            $("#content .slider").animate({ height: this.outerHeight(true) }, 400);
        }, 
        autowidth: false, 
        prevNext: false, 
        controlsShow: false, 
        history: true, 
        numeric: true, 
        numericText: pages.slice(), 
        preloadAjax: true
    });

    // Fix for the AJAX-load bug in sudoSlider (present 
    // only in Microsoft Internet Explorer)
    if ($.browser.msie) {
        var continuous = true;

        slider.removeSlide(1);

        $.get("/etusivu/", function(data) {
            slider.insertSlide(data, 0, "/etusivu/");
            load_player();
        });
    } else {
        var continuous = false;
    }

    // Fade-in the content slider navigation
    $("#content .slider .slider-nav").fadeIn(1000);

    $("#lifts .slider").sudoSlider({
        autowidth: false, 
        prevNext: false, 
        controlsShow: false, 
        continuous: continuous, 
        customLink: "#lifts a.slider-nav", 
        slideCount: 3
    });

    // Fade-in the toolbar and lift-elements
    $("#toolbar").fadeIn();
    $("#lifts").fadeIn();

    // Toggle JW Player's mute state
    $("#toolbar #player-controls #toggle-mute").click(function() {
        if (player.getMute() == false) {
            player.setMute(true);
        } else {
            player.setMute(false);
        }
    });

    // Initialize the Modal on data-remote links
    $("a[data-remote=true]").live("click", function() {
        $(this).modal();

        // Return false in order to break the event chain
        return false;
    });

    // Observe for the Modal close click
    $("#dialog .content .close").live("click", function() {
        $.modal.close();
        return false;
    });

    // Observe and block the Modal personnel image click
    $("#dialog .content a.image").live("click", function() {
        return false;
    });
});

