// create the namespace
var dcms = dcms || {};


$(function(){

    $("#tt-carousel").dcms_carousel({
        interval:5000,
        width:586,
        height:306
    });

    dcms.resizeSecondaryNavigation();
    dcms.addFirstLastClasses();
    dcms.createHotTopicsColumns();
    dcms.prettifyTwitterQuotes();
    dcms.addHoverEffects();
    dcms.addSearchText();
    
});


dcms.addHoverEffects = function(){
    // Automated Navigation Hover Effects
    $(".tt-js-hover").each(function(){
        var element = $(this);
        var classes = element.attr('class').split(" ");

        $.each(classes, function(index, value){
            if (classes[index] !== 'tt-js-hover'){

                var base_class = classes[index];

                element.bind("mouseover", function(){
                    $(this).addClass(base_class + '-hover');
                });

                element.bind("mouseout", function(){
                    $(this).removeClass(base_class + '-hover');
                });


                return false; // stop the cycle
            }else{
                return true;
            }
        });
    });

    // Specific Hover Effects
    $("#tt-subnav li").each(function(){
        var element = $(this);
        element.bind("mouseover", function(){
            var $this = $(this);
            $this.addClass('tt-subnav-hover');
        });

        element.bind("mouseout", function(){
            var $this = $(this);
            $this.removeClass('tt-subnav-hover');
        });
    });

    // AO - allow click on whole of subnav tab
    $("#tt-subnav li").each(function(){
        var url = $(this).find('a').attr('href');
        $(this).click(function() { location.href = url; });
    });
    
};


dcms.addFirstLastClasses = function(){
    // Add tt-first and tt-first classes to all the lists (and tt-blocks)
    $("ul > li:first-child, ol>li:first-child").addClass('tt-first');
    $("ul > li:last-child, ol>li:last-child").addClass('tt-last');
    $("div > .tt-block:first-child").addClass('tt-first-block');
    $("div > .tt-block:last-child").addClass('tt-last-block');
};

dcms.resizeSecondaryNavigation = function(){
    var actual_width = 0;
    var $subnav_items = $("#tt-subnav li").each(function(){
        var $this = $(this);
        var this_width = $this.width();
        actual_width += this_width;
    });

    var ideal_width = $("#tt-subnav").width();

    // calculate the extra margin needed
    var extra_margin = ((ideal_width - actual_width) / $subnav_items.length / 2) + 0.5;

    // take the actual padding of an internal link
    var $sample_link = $("a", $subnav_items[0]);
    var vertical_margin = parseInt( $sample_link.css('marginTop') , 10);
    var horizontal_margin = parseInt( $sample_link.css('marginLeft') , 10) + extra_margin;

    // fix the width of the <li> element to solve an IE problem
    $subnav_items.each(function(){
        var $this = $(this);
        $this.css('width', $this.find("a").width() + ((horizontal_margin-2)*2)+3);
    });
    
    // finally apply the new margin
    $("a", $subnav_items).css('margin', vertical_margin + 'px ' + horizontal_margin + 'px');
    // $("a", $subnav_items).css('margin-right', horizontal_margin + 'px');
    //$("a", $subnav_items).css('margin-right', '0px');
    //$("a", $subnav_items).css('margin', vertical_margin + 'px ' + '0px');
};

dcms.createHotTopicsColumns = function(){

    $(".tt-hot-topics").each(function(){
        var $this = $(this);
        var $elements = $("li", this);

        // use the actual <ul> element as the first column
        $("ul", this).addClass('tt-column tt-column-first');

        // create the new column
        var $second_column = $('<ul class="tt-column tt-column-first">' + '</ul>').appendTo($this);

        // move half elements from the first column to the second column
        var first_column_elements = Math.ceil( $elements.length / 2 );
        var second_column_elements = Math.floor( $elements.length / 2 );

        for(var i = 0; i < second_column_elements; i++){
            $( $elements[first_column_elements + i] ).appendTo($second_column);
        }

    });

    $(".tt-hot-topics ul").css('display','block');
    // $(".tt-hot-topics ul").slideDown(300);
};

dcms.prettifyTwitterQuotes = function(){
    $(".tt-twitter-updates").each(function(){
        $("li", this).each(function(){
            $(this).addClass("tt-twitter-quote")
            .append('<span class="tt-left-quotes" title="left quote" />')
            .append('<span class="tt-right-quotes" title="right quote" />');
        });
    });
};


dcms.addSearchText = function(){

    var $search_field = $("#tt-search input[type=text]");
    $search_field.val("Search this site");
    textFill($search_field);

    function textFill(input){
        var originalvalue = input.val();
        input.focus( function(){
            if( $.trim(input.val()) === originalvalue ){
                input.val('');
            }
        });
        input.blur( function(){
            if( $.trim(input.val()) === '' ){
                input.val(originalvalue);
            }
        });
    }
};

function isInFilter(cats, filter, sep)  { 
    if (cats == undefined) { return true; }
    if (cats == '')  { return true; }
    if (filter == '')  { return true; }
    var isIn = false;  var i = 0;
    var catList = cats.split(sep); 
    for (i=0; i<catList.length; i++)  {
        // Unreliable - if (catList[i].toLowerCase() == filter.toLowerCase()) { isIn = true; }
        if (catList[i].toLowerCase().indexOf(filter.toLowerCase()) > -1)  { isIn = true; }
    }
    return isIn;
}
