/* Author: Reto Ryter */

jQuery('document').ready(function($) {
    var navilinks = $('#main nav a');
    navilinks.each(function() {
        var pieces = this.innerHTML.split(' ');
        for (var i in pieces) {
            if (i == 3) {
                pieces[i] = "<br/><strong>" + pieces[i] + "</strong>";
            }
        }
        this.innerHTML = pieces.join(' ');
    });

    if ($('#main nav li.active a.net').length > 0) {
        var element = $('h1 span');
        var pieces = $(element).html().split('.');
        pieces[0] = "<span class=bold>" + pieces[0] + "</span>";
        $(element).html(pieces.join('.'));
    }
    if ($('#main nav li.active a.work').length > 0) {
        var element = $('h1 span');
        var pieces = $(element).html().split('.');
        pieces[1] = "<span class=bold>" + pieces[1] + "</span>";
        $(element).html(pieces.join('.'));
    }
    if ($('#main nav li.active a.value').length > 0) {
        var element = $('h1 span');
        var pieces = $(element).html().split('.');
        pieces[2] = "<span class=bold>" + pieces[2] + "</span>";
        $(element).html(pieces.join('.'));
    }

    if ($('#main nav a.net').length > 0) {
        var element = $('#main nav a.net');
        var pieces = $(element).html().split('.');
        pieces[0] = "<strong>" + pieces[0] + "</strong>";
        $(element).html(pieces.join('.'));
    }
    if ($('#main nav a.work').length > 0) {
        var element = $('#main nav a.work');
        var pieces = $(element).html().split('.');
        pieces[1] = "<strong>" + pieces[1] + "</strong>";
        $(element).html(pieces.join('.'));
    }
    if ($('#main nav a.value').length > 0) {
        var element = $('#main nav a.value');
        var pieces = $(element).html().split('.');
        pieces[2] = "<strong>" + pieces[2] + "</strong>";
        $(element).html(pieces.join('.'));
    }

    if ($('#picture').hasClass('home')) {
        var flashvars = {};
        flashvars.xmlPath = "images/content/Emediately_VideoBackground/FullscreenBackground/video_playlist.xml";
        var params = {};
        params.scale = "noscale";
        params.salign = "tl";
        params.bgcolor = "#000000";
        params.allowfullscreen = "true";
        params.wmode = "opaque";
        var attributes = {};
        swfobject.embedSWF("images/content/Emediately_VideoBackground/FullscreenBackground/loop.swf?" + Math.round(Math.random() * 10000), "swfVideo", "100%", "100%", "9.0.0", false, flashvars, params, attributes);
    }
    
    if ($('a.more').length > 0) {
        var moreText = $('a.more').html();
        var lessText = $('a.less').html();
        $('a.less').remove();

        $('a.more').toggle(function(event) {
            event.stopPropagation();
            $(this).html(lessText).css('backgroundPosition', 'right -1px').siblings('div.more').slideDown();
        }, function() {
            $(this).html(moreText).css('backgroundPosition', 'right -13px').siblings('div.more').slideUp();
            event.stopPropagation();
        });
    }

    var timeout;
    $('#sharing a.facebook').hover(function(event) {
        $('#moreGP').fadeOut();
        clearTimeout(timeout);
        $('#moreFB').stop(true, true);
        event.stopPropagation();
        $('#moreFB').fadeIn();
    }, function(event) {
        timeout = setTimeout(function() {
            $('#moreFB').stop(true, true);
            event.stopPropagation();
            if ($(event.relatedTarget).attr('id') != "moreFB") {
                $('#moreFB').fadeOut();
            }
        }, 200);
    });

    $('#sharing a.google').hover(function(event) {
        $('#moreFB').fadeOut();
        clearTimeout(timeout);
        $('#moreGP').stop(true, true);
        event.stopPropagation();
        $('#moreGP').fadeIn();
    }, function(event) {
        timeout = setTimeout(function() {
            $('#moreGP').stop(true, true);
            event.stopPropagation();
            if ($(event.relatedTarget).attr('id') != "moreGP") {
                $('#moreGP').fadeOut();
            }
        }, 200);
    });

    $('#moreGP').hover(function(event) {
        clearTimeout(timeout);
        $(this).stop(true, true);
        event.stopPropagation();
        $('#moreGP').fadeIn();
    }, function(event) {
        timeout = setTimeout(function() {
            $(this).stop(true, true);
            event.stopPropagation();
            if ($(event.relatedTarget).attr('id') != "moreGP") {
                $('#moreGP').fadeOut();
            }
        }, 200);
    });

    $('#moreFB').hover(function(event) {
        clearTimeout(timeout);
        $(this).stop(true, true);
        event.stopPropagation();
        $('#moreFB').fadeIn();
    }, function(event) {
        timeout = setTimeout(function() {
            $(this).stop(true, true);
            event.stopPropagation();
            if ($(event.relatedTarget).attr('id') != "moreFB") {
                $('#moreFB').fadeOut();
            }
        }, 200);
    });

    var picture = $('#soBackground');
    var proportions = $('#picture img').height() / $('#picture img').width();
    $(window).resize(function() {
        if (proportions < $(window).height() / $(window).width()) {
            picture.css('height', $('#picture').height());
            picture.css('width', $('#picture').height() / proportions);
        } else {
            picture.css('width', $('#picture').width());
            picture.css('height', $('#picture').width() * proportions);
        }
    });
    $(window).resize();
});
