﻿function setCornersAndTitleToolTips() {
    $(document).ready(function() {
        //Corners config
        if ($('.cornered').length > 0) {
            $('.cornered').corner("10px");
        }

        //Tooltips
        if ($.tooltip) {
            $.extend($.tooltip.defaults, {
                showBody: true,
                showURL: false,
                delay: 100,
                showBody: " - ",
                fade: 200,
                track: true,
                top: 25
            });
        }

        //Lightbox
        $('.feedback a').lightBox();

        //Tooltip for title tags - on all pages except classes page
        if ($("div.scrollable").length == 0) {
            $("*[title]").tooltip({
                left: 15
            });
        }

        //Tooltip for  class="tooltiplink"
        $(".tooltiplink").tooltip({
            bodyHandler: function() {
                return $($(this).attr("href")).html();
            },
            showURL: false,
            left: 15
        });

        //Tooltip for  class="tooltiplinkbig"
        $(".tooltiplinkbig").tooltip({
            bodyHandler: function() {
                return $($(this).attr("href")).html();
            },
            showURL: false,
            extraClass: 'tooltipbig',
            left: 40
        });

    });
}
setCornersAndTitleToolTips()

function popup(url) {
    var popup = window.open(url, 'popup', 'width=400px, height=500px, toolbars=0, scrollbars=1');
    popup.focus(); 
    return false
}

//sIFR config
var caec = { src: '/app_themes/tmdirectnew/caec.swf' };
var caecitalic = { src: '/app_themes/tmdirectnew/caecitalic.swf' };
// sIFR.useStyleCheck = true;
sIFR.activate(caec, caecitalic);
sIFR.replace(caec, {
    selector: 'h1',
    css: '.sIFR-root { color: #2eb8e7; }',
    wmode : 'transparent'
});
sIFR.replace(caecitalic, {
    selector: 'h3.quotetext',
    css: '.sIFR-root { color: #6d6d6d; }',
    wmode: 'transparent'
});
sIFR.replace(caec, {
    selector: 'h4.quotetext',
    css: '.sIFR-root { color: #388ba1; }',
    wmode: 'transparent'
});
sIFR.replace(caec, {
    selector: 'h3#step',
    wmode : 'transparent',
    css: [
        '.sIFR-root { color: #2eb8e7;  } ',
        '.span{color: #12647a; font-size:15px; padding-left:10px}']
});
sIFR.replace(caec, {
    selector: 'h3#checkout',
    wmode: 'transparent',
    css: [
        '.sIFR-root { color: #2eb8e7;  } ',
        '.span{color: #12647a; padding-left:10px}']
});




function initAjaxProgress()
{
    var pageHeight = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
    var bg = document.getElementById('ajaxProgressBg');
    bg.style.height = (pageHeight + 1000) + 'px';
    reposAjaxProgress();
    window.onscroll = reposAjaxProgress;
}
function reposAjaxProgress()
{
    var div = document.getElementById('ajaxProgress');
    var st = document.body.scrollTop;
    if (st == 0) {
        if (window.pageYOffset) st = window.pageYOffset;
        else st = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
    div.style.top = 150 + st + "px";
}
/* Fade script */
var currentOp = 5;
var fadeDiv;
var fades;
var previousNum = -1;
var fadeSpeed=5;
var fadeLoop = true;
function startFade(div, array, offsetLeft, offsetTop, speed, loop)
{
    fadeDiv = document.getElementById(div)
    if(fadeDiv){
        fades = array;
        fadeLoop = loop;
        if(speed) fadeSpeed=speed;
        if(offsetLeft > 0 && offsetTop > 0){
            var pWidth= window.innerWidth!=null?innerWidth:document.body.clientWidth;
            fadeDiv.style.top = offsetTop + "px";
            fadeDiv.style.left = pWidth /2 - offsetLeft + "px"
        }
        setOpacity(5);
        getFact();
    }
}
function fadeIn(){
    if(!checkForStop()) return;
    if(currentOp < 100){
        setOpacity(currentOp);
       if(currentOp >= 90)  currentOp+=fadeSpeed/10
        else currentOp+=fadeSpeed
        setTimeout("fadeIn()",150);
    }else{
        if((previosNum + 1) == fades.length && !fadeLoop){
            waitForStop()
        }else{
            fadeOut();
        }
    }
}
function fadeOut(){
    if(!checkForStop()) return;
    if(currentOp > 0){
        setOpacity(currentOp);
        if(currentOp >= 90)  currentOp-=fadeSpeed/10
        else currentOp-=fadeSpeed
        setTimeout("fadeOut()",150);
    }else{
        getFact();
    }
}
var previosNum = -1;
function getFact(){
    if(!checkForStop()) return;
    var num = previosNum + 1;
    if(num == fades.length) num = 0;
    previosNum = num;
    fadeDiv.innerHTML = fades[num];
    fadeIn();
}
function setOpacity(opacity){
    fadeDiv.style.filter = "alpha(opacity:"+opacity+")";
    fadeDiv.style.KHTMLOpacity = opacity/100;
    fadeDiv.style.MozOpacity = opacity/100;
    fadeDiv.style.opacity = opacity/100;
}
function checkForStop()
{
    var stop = document.getElementById("stopAnimation");
    if(stop!=null)
    {
        document.getElementById("factscontainer").style.display = "none";
        document.getElementById("ajxloader").style.display = "none";
        return false;
    }
    return true;
}
function waitForStop()
{
    if(!checkForStop()) return;
    setTimeout("waitForStop()",150)
}

function changeRegistry(img, registry) {
    el = document.forms[0].SearchRegistry;
    if (registry == 2) {
        el.value = "CTM";
        document.getElementById(img).src = "/app_themes/tmdirectnew/images/registry2.gif";
    } else {
        el.value = "UK";
        document.getElementById(img).src = "/app_themes/tmdirectnew/images/registry1.gif";
    }
}

/**** Script for classes page ****/
$(document).ready(function() {

    if ($("div.scrollable").length == 0) return;

    //Initialize scroller
    $("div.scrollable").scrollable({
        size: 6,
        clickable: false,
        onSeek: function(event, i) {
            //scroll event
            i++;
            $("#nowShowing").html("Showing classes " + i + " to " + (i + 5));
            if (i == 40) {
                $("#moreClasses").html("<img src='/images/arrow_small_left.gif' /> more classes <img src='/images/arrow_small_empty.gif' />");
            } else if (i == 1) {
                $("#moreClasses").html("<img src='/images/arrow_small_empty.gif' /> more classes <img src='/images/arrow_small_right.gif' />");
            } else {
                $("#moreClasses").html("<img src='/images/arrow_small_left.gif' /> more classes <img src='/images/arrow_small_right.gif' />");
            }
        }
    }).mousewheel();

    //then let's move to the end
    var api = $("div.scrollable").data("scrollable");
    api.move(39);

    //Set images and add html
    $('ul.items li').each(function() {
        var id = $(this).attr("id");
        var h4 = $(this).find("h4");
        $('<div class="add"></div>').insertBefore(h4);
        $('<div class="classImg"></div>').insertAfter(h4).css("background-image", "url(/images/icons/" + id + ".gif)");
        if (id != "class9") {
            $(this).tooltip({ left: 15 });
        } else {
            $(this).tooltip({ id: 'tooltipmedium', left: -200, extraClass: 'tooltipmedium' });
        }
    });

    //Mouseover for the classes list
    $('ul.items li').hover(function() {
        if ($(this).attr("class").indexOf("selected") > -1) return;
        $(this).addClass("hover");
    },
        function() {
            $(this).removeClass("hover");
        });

    //Click for the classes list and event for the selected list
    $('ul.items li').click(function() {
        if ($(this).attr("class").indexOf("selected") > -1) return;

        //We have a max of 5 classes!
        currentCount = $("#selectedList li").length;
        if (currentCount >= 5) {
            $("#classCountError").html('&nbsp;&nbsp;Most applications need no more than 5 classes. More can be added lated if required.');
            return;
        } else {
            $("#classCountError").html('');
        }

        $(this).removeClass("hover");
        //Get text and id
        var html = $(this).find('span').html();
        var id = $(this).attr("id");
        //Add this to the select list
        $('<li id="selected' + id + '" style="display:none"><div class="outer"><div class="classImg" style="background-image:url(/images/icons/' + id + '.gif)"></div><span>' + html + '</span><input type="hidden" name="classes" value="' + id.replace("class", "") + '" /></div></li>')
                .appendTo("#selectedList")
                .hover(function() { $(this).addClass("hover"); },
                function() {
                    $(this).removeClass("hover");
                }).click(function() {
                    //show the original
                    $('ul.items li#' + $(this).attr("id").replace("selected", "")).removeClass("selected");
                    //remove the one clicked
                    $("#classCountError").html('');
                    $(this).remove();
                })
        $("#selected" + id).effect("highlight", { color: '#ffff00' }, 2000);
        $(this).addClass("selected");
    });
});

var startVideo = true;
/**** Frontpage tabs and movie script ****/
$(document).ready(function() {
    ////

    if ($("#tabs").length == 0) return;

    $('#tabs div.tabs').hide(); // Hide all divs
    if (startVideo) {
        $('#tabs div.tabs:first').show(); // Show the first div
        //Add preload video
        $('#movieContent').flash({ src: '/tmdirect_anim_preload.swf', width: 408, height: 226, wmode: 'transparent' });
        $('#tabs ul li:first').addClass('active'); // Set the class of the first link to active
    } else {
        $('#tabs ul li').addClass('active');
        $('#tabs ul li:first').removeClass('active');
        $('#tabs div.tabs').hide();
        $('#recentNews').show();
        $('#movieContent').html("");
    }

    $('#tabs ul li a').click(function() { //When any link is clicked
        $('#tabs ul li').removeClass('active'); // Remove active class from all links
        $(this).parent().addClass('active'); //Set clicked link class to active
        var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
        $('#tabs div.tabs').hide(); // Hide all divs
        $(currentTab).show(); // Show div with id equal to variable currentTab

        if (currentTab == "#movie") {//Re-add video
            $('#movieContent').html("");
            $('#movieContent').flash({ src: '/tmdirect_anim_preload.swf', width: 408, height: 226, wmode: 'transparent' });
        } else {
            $('#movieContent').html("");
        }
        return false;
    });

    $("#closemovie").click(function() {
        $('#tabs ul li').addClass('active');
        $('#tabs ul li:first').removeClass('active');
        $('#tabs div.tabs').hide();
        $('#recentNews').show();
        $('#movieContent').html("");
    });

    // initialize scrollable
    $(".frontpagescrollable").scrollable({ size: 1 }).circular().autoscroll(8000);
    $("#recentNews").hover(
            function() {
                $("a.browse").not(".disabled").show();
            },
            function() {
                $("a.browse").hide();
            }
        );
    //Initiall hide
    $("a.browse").hide();
});



function showPanel(panel, num) {
    $(panel).show();
    if (showing != panel) $(showing).hide();
    showing = panel;
    $("#tickimage").attr("src", "/app_themes/tmdirectnew/images/tickpanelnew" + num + ".gif");
}
var showing = "#lowfixedtext";
$(document).ready(function() {
    $("#tick1").mouseover(function() {
        showPanel("#lowfixedtext", 1);
    });
    $("#tick2").mouseover(function() {
        showPanel("#moneybacktext", 2);
    });
    $("#tick3").mouseover(function() {
        showPanel("#topadvicetext", 3);
    });


});


function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}