﻿$(function () {
    initialiseCssStlyesBullets();
    initialiseTabs();
    removeEmptyTabs();
    externalLinks();
    applyTableStyles();
    addHomeTimedRotation();
});


//Adds timed rotation for home page feature rotator
function addHomeTimedRotation() {
    if ($('#homeBanner').length != 0) {
        
        var id = setInterval(function () {
            $('#hbNavigation .aRight').trigger('click');
        }, 6 * 1000); // do this every 6 seconds 

        $('#hbNavigation .aLeft').unbind("click.resetRotationLeft");
        $('#hbNavigation .aLeft').bind("click.resetRotationLeft",function () {
            clearInterval(id);
            addHomeTimedRotation();
            return false;
        });

        $('#hbNavigation .aRight').unbind("click.resetRotationRight");
        $('#hbNavigation .aRight').bind("click.resetRotationRight", function () {
            clearInterval(id);
            addHomeTimedRotation();
            return false;
        });
    }
}

//Sets the selected tab for the specified hash so that if someone bookmarks a tabbed page, the correct tab is shown
function initialiseTabs() {
    var hash = location.hash;

    $('.cbtNav li').removeClass('selected');
    if (hash == '') {
        $('.cbtNav li:first').addClass('selected');
    }
    else {
        $(".cbtNav a[href=" + hash +"]").parent().addClass('selected');
    }
}

//Apply bulleted list styles
function initialiseCssStlyesBullets() {

    $('#subpage .content ul').each(function () {
        if ($(this).attr('class') != '' || $(this).parent().hasClass('cbtNav') || $(this).parent().hasClass('contact'))
        { }
        else {
            //Set ULs class
            $(this).addClass('defaultTextpageList');

            //Now set first and last classes for LIs
            $(this).children('li:first').addClass('first');
            $(this).children('li:last').addClass('last');

            //Add clearing clear div after to fix page flow
            $(this).after('<div class=\"clear\"></div>');
        }
    });
}

//Remove empty tabs
//This cannot be done by the TabEngine as it is not aware of what
//causes a tab to be empty
function removeEmptyTabs() {

    $('div.cbTabContainer').each(function () {
        $(this).find('span.empty-tab').each(function () {
            var tabId = $(this).parent().parent().attr("id");

            //Remove tab from nav
            $(this).parent().parent().parent().parent().parent().find('.cbtNav ul li a').each(function () {
                var navItemId = $(this).attr("href").replace('#', '');
                if (tabId == navItemId) {
                    $(this).remove();
                }
            });

            //Remove tab from inner content area
            $(this).parent().parent().remove();
        });
    });
}

//Allows links to open in new tab/window whilst maintaining XHMTL validation
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

//Applies styles to tables that are from the Headscape design
function applyTableStyles() {
    $('#subpage table').each(function () {

        //Check if to apply styles
        if ($(this).hasClass('table-no-designcss')) {
        //Do nothing
        }
        else
        {
            //Calculate number of cells per row
            var cellsPerRow = $(this).children('tbody').children('tr:first-child').children('td').size();

            //Now build TableCssClass
            var tableCssClass = "Cols";
            if (cellsPerRow == 2) { tableCssClass = "half" + tableCssClass; }
            if (cellsPerRow == 3) { tableCssClass = "third" + tableCssClass; }
            if (cellsPerRow == 4) { tableCssClass = "quarter" + tableCssClass; }
            if (cellsPerRow == 5) { tableCssClass = "fift" + tableCssClass; }
            if (cellsPerRow == 6) { tableCssClass = "sixth" + tableCssClass; }
            $(this).addClass(tableCssClass);

            //Add odd row styles
            $(this).children('tbody').children('tr:odd').each(function () {
                $(this).addClass('odd');
                $(this).children('td:odd').addClass('alt');
            });

            //Add even row styles
            $(this).children('tbody').children('tr:even').each(function () {
                $(this).addClass('even');
                $(this).children('td:odd').addClass('alt');
            });

            //Add last row style
            $(this).children('tbody').children('tr:last').addClass('last');
        }
    });
}

//Method to clear textboxes - This is added as an onclick method
function clearTextboxContent(el, val) {
    if (el.value == val.toString()) {
        el.value = "";
    }
}
