var MF_Cookie = {

    setCookie : function(c_name, value, expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + ';domain=.mobilefun.co.uk;path=/;';
    },

    getCookie : function(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");

            if (c_start!=-1)
            {
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    },

    clearCookie : function(c_name)
    {
        var exdate=new Date(1970, 1, 1);
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + ';domain=.mobilefun.co.uk;path=/;';
    },

    setCheckoutCookie : function(value)
    {
        if(value)
        {
            MF_Cookie.setCookie('abtest', true, 365);
        }
        else
        {
            MF_Cookie.setCookie('abtest', true, -365);
        }
    },

    hasCheckoutCookie : function()
    {
        if(MF_Cookie.getCookie('abtest') != '')
        {
            return true;
        }

        return false;
    }

};

var Product_Tabs = {

    activateTab : function(name){

        //Check tab exists
        if($('#product-tabs a[tab='+name+']').size() > 0)
        {
            // Deactivate all tabs
            $('#product-tabs .nav-active').removeClass('nav-active').addClass('nav-inactive');
            $('#product-tabs .tab-active').removeClass('tab-active').addClass('tab-inactive');
            $('#product-tabs .tab-tr-active').removeClass('tab-tr-active').addClass('tab-tr-inactive');
            $('#product-tabs .tab-tl-active').removeClass('tab-tl-active').addClass('tab-tl-inactive');

            // Hide all content
            $('.tabbed-content').hide();

            // Activate tab
            var tab = $('#product-tabs a[tab='+name+']').parent();

            tab.removeClass('nav-inactive').addClass('nav-active');
            tab.find('.tab-inactive').removeClass('tab-inactive').addClass('tab-active');
            tab.find('.tab-tr-inactive').removeClass('tab-tr-inactive').addClass('tab-tr-active');
            tab.find('.tab-tl-inactive').removeClass('tab-tl-inactive').addClass('tab-tl-active');

            // Show tab content
            $('.tabbed-content[tab='+name+']').show();
        } 

        this.current_tab = $('#product-tabs .nav-active a[tab]').attr('tab');
    },

    highlightTab : function(name, highlight){

        var tab = $('#product-tabs a[tab='+name+']').parent();

        if(tab.size() > 0)
        {
            if(highlight)
            {
                tab.removeClass('nav-inactive').addClass('nav-active');
                tab.find('.tab-inactive').removeClass('tab-inactive').addClass('tab-active');
                tab.find('.tab-tr-inactive').removeClass('tab-tr-inactive').addClass('tab-tr-active');
                tab.find('.tab-tl-inactive').removeClass('tab-tl-inactive').addClass('tab-tl-active');
            }
            else
            {
                if(name != this.current_tab)
                {
                    tab.removeClass('nav-active').addClass('nav-inactive');
                    tab.find('.tab-active').removeClass('tab-active').addClass('tab-inactive');
                    tab.find('.tab-tr-active').removeClass('tab-tr-active').addClass('tab-tr-inactive');
                    tab.find('.tab-tl-active').removeClass('tab-tl-active').addClass('tab-tl-inactive');
                }
            }
        }
    },

    setup : function(){

        this.current_tab = $('#product-tabs .nav-active a[tab]').attr('tab');
    
        if(location.href.match(/[a-zA-Z0-9-]+-p\d+.htm/))
        {
            section = location.hash;
            if(section)
            {
                section = section.substring(1, section.length);

                this.activateTab(section);
            }
        }

        $('#product-tabs a[tab]').hover(
            function(){
                var tab = $(this).attr('tab');
                Product_Tabs.highlightTab(tab, true);
            },
            function(){
                var tab = $(this).attr('tab');
                Product_Tabs.highlightTab(tab, false);
            }
        );

        $('#product-tabs a[tab]').click(function(){

            if($(this).attr('href') == '#')
            {
                var tab = $(this).attr('tab');
                Product_Tabs.activateTab(tab);

                return false;
            }

        });
    }

}

$(document).ready(function(){

    $('.reveal').click(function(){
        reveal = $(this).attr('reveal');

        if(reveal != '')
        {
                state = $('#' + reveal).css('display');

                if(state == 'none')
                {
                    $('#' + reveal).slideDown(400);
                }
            }

            return false;
    });

    $('.toggle').click(function(){
        reveal = $(this).attr('reveal');


        if(reveal != '')
        {
            state = $('#' + reveal).css('display');

            if(state == 'none')
            {
                $('#' + reveal).slideDown(400);
            }
            else
            {
                $('#' + reveal).slideUp(400);

            }
        }

        return false;
    });

    $('.selected').click(function(){
            $('body').css("cursor", "wait");
            $('.selected').css("cursor", "wait");
            $('.deselected').css("cursor", "wait");
            });

    $('.deselected').click(function(){
            $('body').css("cursor", "wait");
            $('.selected').css("cursor", "wait");
            $('.deselected').css("cursor", "wait");
            });

    $('.filtertoggle').click(function(){
        var obj = $(this);
        var reveal = $(this).attr('reveal');

        if(reveal != '')
        {
            state = $('#' + reveal).css('display');

            if(state == 'none')
            {
                $(this).html('Less<span class="selected"></span>');
                $('#' + reveal).slideDown(400);
                $('.' + reveal + '-hide').css('display', 'none');
            }
            else
            {
                var anchorObj = $('#' + reveal);
                var docViewTop = $(window).scrollTop();
                var docViewBottom = docViewTop + $(window).height();

                var elemTop = anchorObj.offset().top;
                var elemBottom = elemTop + $(anchorObj).height();

                if ((elemTop <= docViewTop) || (elemTop >= docViewBottom))
                {
                    $('html').scrollTop(elemTop);
                    $('body').scrollTop(elemTop);
                }
                $(this).html('More<span></span>');
                $('#' + reveal).slideUp(400);
                $('.' + reveal + '-hide').css('display', 'inline');
            }
        }

        return false;
    });

    $('.blog_popup').each(function(){
        $(this).fancybox({
            'onStart' : function(){$('embed, object, select').css('visibility', 'hidden');},
            'href' : '/content_blog.php?name=' + $(this).attr('blog_article'),
            'centerOnScroll' : true,
            'titleShow' : false,
            'onClosed' : function() {$('embed, object, select').css('visibility', 'visible');}
        });
    });

    Product_Tabs.setup();

    $('.layout2 .series_links a').each(function(){
        var link = $(this).attr('href');

        $(this).attr('href', link + '.');
    });

    $('.layout2 .series_link').each(function(){
        var link = $(this).attr('name');

        $(this).attr('name', link + '.');
    });

    $('.business-account-popup').fancybox({
        'type' : 'iframe',
        'width' : 676,
        'height' : 680,
        'titleShow' : false,
        'autoScale' : false,
        'showCloseButton' : true,
        'autoDimensions' : false,
        'centerOnScroll' : false
    });

    $('.corporate-ask-a-question').fancybox({
        'type' : 'iframe',
        'width' : 676,
        'height' : 605,
        'titleShow' : false,
        'autoScale' : false,
        'showCloseButton' : true,
        'autoDimensions' : false,
        'centerOnScroll' : false
    });


    $('.ask-a-question, .price-match, .send-feedback').fancybox({
        'type' : 'iframe',
        'width' : 800,
        'height' : 600,
        'titleShow' : false,
        'autoScale' : false,
        'showCloseButton' : true,
        'autoDimensions' : false,
        'centerOnScroll' : false
    });

    $('.preorder-popup').fancybox({ 
        'type' : 'iframe',
        'titleShow' : false,
        'autoScale' : false,
        'showCloseButton' : true,
        'autoDimensions' : true,
        'centerOnScroll' : false
    });

    /* JS FOR STANDARD BUTTONS */

    $('a.button').mousedown(function(){
        $(this).addClass('down');
    }).mouseup(function(){
        $(this).removeClass('down');
    }).mouseout(function(){
        $(this).removeClass('down');
    });

    $('#feedback .feedback-yes, #toplinks .feedback').live('click', function() {
        $.fancybox.close();

        var w = 800,
            h = 800;

        var l = ($(window).width() / 2) - (w / 2),
            t = ($(window).height() / 2) - (h / 2);

        var popup = window.open(
            'https://mobilefun.wufoo.com/forms/mobile-fun-website-feedback/',
            'feedback',
            'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t
        );

        return false;
    });
});

function sleep(ms){
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while(new Date().getTime() < dt.getTime());
}

function toProperCase(s) {
    return s.toLowerCase().replace(/^(.)|\s(.)/g,
        function($1) {
            return $1.toUpperCase();
        }
    );
}


/*
 * Automate the reload of the reviews page when the user
 * makes a selection in 'Sort By' or 'Show Star Ratings'.
 *
 */

 $(document).ready(function(){
    $('.review-filter-form').change(function() {
        $('.review-filter-form').submit();
    });

    $('.currency').change(function() {
        if($('.currency option:selected').val() == '1') {
            window.location = 'http://james.secure.mobilefun.co.uk/checkout/basket/?currency_id=1';
        }
        else if($('.currency option:selected').val() == '2') {
            window.location = 'http://james.secure.mobilefun.co.uk/checkout/basket/?currency_id=2';
        }
        else if($('.currency option:selected').val() == '3') {
            window.location = 'http://james.secure.mobilefun.co.uk/checkout/basket/?currency_id=3';
        }
    });
})

