$(document).ready(function(){
    function refreshmaps() {
        if ($.browser.msie) {
            $('iframe').each(function(){
                var o = $(this).attr('src');
                var el = $(this);
                el.attr('src', '#');
                setTimeout(function(){
                    el.attr('src', o);
                }, 500);
            });
        }
    }
    refreshmaps();

    //display hashed content
    if(window.location.hash != '')    {
        $(window).load(function(e){

            e.preventDefault();

            h = window.location.hash;

            tab = $("a[href=" + h + "]");

            tab.siblings('a').removeClass('active');
            $(h).siblings('div').hide();

            tab.addClass('active');
            $(h).fadeIn('slow');

        })

    };

    //twitter feed
    $(".twitter").tweet({
        username: "sinc_innovation",
        join_text: "auto",
        avatar_size: 32,
        count: 3,
        auto_join_text_default: "we said,",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "loading tweets...",
        template: function(info) {
            var date = new Date();
            var d = parseDate(info['tweet_time']);
            var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ];
            date.setTime(d);
            var time = date.getDate() + ' ' + monthNames[date.getMonth()] + ' ' + date.getFullYear();
            return info['text'] + ' ' + time;
        }
    });
    //ri functions
    function parseDate(str) {
      var v=str.split(' ');
      return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
    }

    //carousel
    $('#page_carousel').cycle({
        timeout: 25000,
        speed: 2000
    });
    $('#carousel').cycle({
        timeout: 25000,
        speed: 2000
    });

    //equalise
    $('.double .pan').equalise();

    //center align
    $('.pagination ul').centerAlign();

    //tabbed content
    $('#secondary a').click(function(event){

        event.preventDefault();

        id = $(this).attr('href');

        $(this).siblings('a').removeClass('active');
        $(id).siblings('div').hide();

        window.location.hash = id;

        $(this).addClass('active');
        $(id).fadeIn('slow');

        refreshmaps();
    });

    $('#tags a').click(function(){

        id = $(this).attr('href');

        $(this).siblings('a').removeClass('active');
        $(id).siblings('ul').hide();

        $(this).addClass('active');
        $(id).fadeIn('slow');

        return false;

    })
});


//Center alignment
(function($) {
    $.fn.centerAlign = function()   {

        if($(this).length)    {
            //set this inline before we can get the actual width
            $(this).css('display', 'inline');

            w = $(this).width();

            $(this).width(w).css('display', 'block');
        }

    return this;
    }
}) (jQuery);

//Equalise heights
(function($){
    $.fn.equalise = function()   {
        var tallest = 0;
        if($(this).length)    {
            //loop over each element and workout which is the tallest
            $(this).each(function(){
                if ($(this).height() > tallest) {
                    tallest = $(this).height();
                }
            })

            //if using IE6 then do this
            if ($.browser.msie && $.browser.version == 6.0) {
                $(this).height(tallest);
            //otherwise for everyone else
            } else  {
                $(this).css('min-height', tallest + 'px');
            }
        }
    return this;
    }
}) (jQuery);

//Toggle display
(function($){
    $.fn.toggleHashDisplay = function(el) {

        h = window.location.hash

        if(h.length)    {

            targetMenu = $('a[href=' + h + ']');

            targetMenu.siblings().removeClass('active');
            targetMenu.addClass('active');

            $(h).siblings().hide();
            $(h).fadeIn('slow');

        }

    return false;
    }
}) (jQuery);

//facebook like widget
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s);js.id = id;
    js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

//google plus widget
window.___gcfg = {lang: 'en-GB'};

(function() {
    var po = document.createElement('script');po.type = 'text/javascript';po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(po, s);
})();

