// JavaScript sitewide scripts

// Client: SubSea Infrastructure
// URL: http://www.subseainfrastructure.com/
// Date: 30/11/2010
// Description: -
// Version: 1.1

// Author: Lewis Dexter Litanzios
// Author URI: http://www.ldexterldesign.co.uk/

// Tags: -
// Theme Name: -
// Theme URI: -

// Visual design copyright 2010. All Rights Reserved.


$(document).ready(function () {
    // Modernizr / http://www.modernizr.com/
    $('html').addClass('no-js');
    // Setup useful dimensions obj - we'll need these throughout!
    var dimensions = {
        // Viewport dimensions
        viewportWidth: $(window).width(),
        viewportHeight: $(window).height(),
        // Body dimensions
        bodyWidth: $(document).width(),
        bodyHeight: $(document).height()
    };
    // jPlayer / http://www.happyworm.com/jquery/jplayer/
    // Inline: k2-loop-downloads.php
    // Liquid layout
    var liquid = true;
    if (liquid) {
        // Relocate primary nav outside #page
        $('#nav-main').prependTo('body');
        // Adjust logo positioning @ 10% from viewport edge
        var adjustLogoPos = function () {
            var percentageOffset = 10;
            var temp = $(window).width();
            temp -= $('#nav-main .blog-title a').width();
            temp -= $('#nav-main ul.menu').width();
            temp = (temp / 100) * percentageOffset;
            $('#nav-main .blog-title').css({
                'border-left-width': temp + 'px'
            });
        };
        adjustLogoPos();
        // Adjust logo borders/padding
        var adjustLogoBorders = function () {
            var temp = $(window).width();
            temp -= $('#nav-main .blog-title a').width();
            temp -= $('#nav-main ul.menu').width();
            $('#nav-main .blog-title').css({
                'border-right-width': temp + 'px'
            });
        };
        adjustLogoBorders();
        // Adjust primary nav positioning @ 10% from viewport edge
        var adjustNavPos = function () {
            var percentageOffset = 20;
            var temp = $(window).width();
            temp -= $('#nav-main .blog-title a').width();
            temp -= $('#nav-main ul.menu').width();
            temp = (temp / 100) * percentageOffset;
            $('#nav-main ul.menu').css({
                'padding-right': temp + 'px'
            });
        };
        adjustNavPos();
        // On viewport resize fire all positioning adjustments
        $(window).bind('resize', function () {
            adjustLogoPos();
            adjustLogoBorders();
            adjustNavPos();
        });
    };
    // Interactive nav (right-hand side)
    // Pull content from elsewhere on the page and give it a unique class
    var navSection = $('.nav-section').clone().attr('id', 'js-nav-section').appendTo('#footer');
    // Get/set object dimensions
    var navSectionWidth = $('#js-nav-section').outerWidth();
    var navSectionGutter = $('#js-nav-section').css('padding-left').replace('px', '');
    navSectionWidth -= navSectionGutter;
    var navSectionPos = '-' + navSectionWidth + 'px';
    // Set inital position
    navSection.css({
        'right': navSectionPos
    });
    $('#js-nav-section').append('<a href="#" id="js-more">More </a>');
    $('.leftRightArrow').appendTo('a#js-more');
/* NB: clashes with fullscreen video button if enabled
    $('#js-nav-section').css({
        'height': dimensions.viewportHeight + 'px'
    });
	*/
    // Events
    $('a#js-more').toggle(function (e) {
        navSection.animate({
            'right': '0'
        }, 'slow');
        e.preventDefault();
    }, function () {
        navSection.animate({
            'right': navSectionPos
        }, 'slow');
        e.preventDefault();
    });
    // :hover state animation
    $('#js-nav-section .advantages a').css({
        'opacity': '0.66'
    });
    $('#js-nav-section .advantages a').hover(function () {
        $(this).stop().animate({
            'opacity': '1'
        }, 'fast');
    }, function () {
        $(this).stop().animate({
            'opacity': '0.66'
        }, 'fast');
    });
    // Liquid width media
    // 1. Give media its native width and height attributes - these attributes will be fallbacks
    // 2. CSS takes care of the liquid width
    // 3. JS removes (img) or adjusts (video) media height attributes allowing liquid layout
    // KLUDGE: 'body:not(.pageid-6)' Contact page Google map exception
    $('.entry-content img, .entry-content embed, .entry-content object, body:not(.pageid-6) .entry-content iframe, .entry-content video').removeAttr('height');
    $('.entry-content img, .entry-content embed, .entry-content object, body:not(.pageid-6) .entry-content iframe, .entry-content video').removeAttr('width');
    $('.entry-content embed, .entry-content object, body:not(.pageid-6) .entry-content iframe').height(function () {
        return $(this).width() / 16 * 9;
    });
    // Replace all form text input labels with (HTML5) placeholder attributes
    if (Modernizr.input.placeholder) {
        $('form input[type="text"]').each(function () {
            var id = $(this).attr('id');
            $(this).parents().find('label').attr('for', id).hide();
            var newLabel = $(this).parent().parent().find('label').attr('for', id).text().trim(); //TODO: .parent().parent() is messy
            $(this).attr('placeholder', newLabel);
        });
    };
    // Downloads page tabs
    // http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/
    $('ul.js-tabs').prepend('<li><a href="#">All</a></li>')
    $('ul.js-tabs li:first').click(function () {
        $('.js-tab').show(); // Hide all content
    });
    $('ul.js-tabs li:first').addClass('active').show(); // Activate first tab
    $('.js-tab:first').show(); // Show first tab content
    $('.js-tabs li a').click(function (e) {
        $('.js-tab').hide(); // Hide all content
        $('ul.js-tabs li').removeClass('active'); // Remove any 'active' class
        $(this).parent('li').addClass('active'); // Add 'active' class to selected tab
        $('.js-tab').hide(); // Hide all tab content
        var activeTab = $(this).attr('href'); // Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); // Fade in the active ID content
        e.preventDefault();
    });
    // Additional page content on hide/show
    var pages = 'body.pageid-19 .entry-content, body.pageid-21 .entry-content, body.pageid-23 .entry-content, body.pageid-15 .entry-content, body.pageid-17 .entry-content'
    var temp = $('.more');
    temp.insertAfter('#primary-wrapper').hide();
    var topic = $('.current_page_item:first').text();
    $(pages).append('<a id="js-moreTopic" href="#primary-wrapper">... read more on ' + topic + ' </a>');
    $('.rightArrow').appendTo('#js-moreTopic');
    $('#js-moreTopic').click(function () {
        $('.more').fadeIn('slow');
    });
    // .... read more link 
    $('body.parent-pageid-13 #primary-wrapper').localScroll();
    // #footer unit icon
    $('.mCubed').appendTo('#footer .branding #counter em');
});
