﻿var speed = 1500;
var animateStartDelay = 12000;
var reAnimateDelay = 30000;
var slideTime = 12000;
var animator = null;
var slideTimer = null;
var current_index = 1;
var currentTab;
var pointerDelay = 1000;
var slideDelay = 500;
var promptDelay = 6000;
var promptAnimator = null;
var promptIndex = 0;
var promptCount = 7;
var galleryBuilt = 0;
var galleryStarted = 0;
var prompts = ["get an obligation free consultation", "we explain all of the technical stuff", "our consultants visit you in person", "we design logos and can write your content", "you don't need to be a website expert - we are!", "we'll get you onto the front page of google", "we host and maintain your new website"];
var baldGuyTimer = null;

$(document).ready(function() {

    /// Hide all animated divs
    $('div.animated-container').hide();

    // Safari rocks
    if ($.browser.safari) {
        pointerDelay = 0;
        slideDelay = 0;
    }

    $('.big-button').hover(function() {
        $(this).addClass('hover');
        $(this).find('div').addClass('hover');
    }, function() {
        $(this).removeClass('hover');
        $(this).find('div').removeClass('hover');
    });


    $('.big-button')
 .mousedown(function() {
     $(this).css('top', '1px');
 })
 .mouseup(function() {
     $(this).css('top', '0');
 });


    $('.slider-panel').hide().css({ 'position': 'absolute', 'top': '0' });
    $('.tab-pointer').show();

    // Select the appropriate first panel

    var startPanel = '#panel-1';
    var url = document.location.toString();
    if (url.match('#')) {
        startPanel = '#' + url.split('#')[1];
        animateStartDelay = 20000;
        current_index = 0;
    }
    showSlide(startPanel, false);


    $('#slider-tabs ul li a').click(function(e) {
        reStartAnimate();
        slidePointerTo(this, 0);
        $('.slider-panel').stop().hide().css({ 'zIndex': '100', 'top': '0' });
        var lastTab = currentTab;
        currentTab = $(this).attr('href');
        current_index = parseInt(currentTab.replace('#panel-', ''));
        if (currentTab == lastTab) {
            $(currentTab).show();
            return false;
        }
        $('#slider-tabs ul li').removeClass('selected');
        $(this).parent().addClass('selected');
        $(lastTab).delay(slideDelay);
        $(lastTab).show().css({ 'zIndex': '101', 'opacity': '1' }).animate({ top: '-325px' }, { duration: 1000, easing: 'easeOutQuint' });
        $(currentTab).show().css({ 'opacity': '1' });
        animate_slide(currentTab);
        e.preventDefault();
    });
    startAnimate();
});


function slidePointerTo(id, delay) {
    var elementPos = $(id).offset();
    var sliderPos = $('#slider-tabs').offset();
    var leftPos = elementPos.left - sliderPos.left;
    $('.tab-pointer').stop().delay(delay).animate({ left: leftPos }, { duration: 500, easing: 'easeOutBack' });
}

function showSlide(id, animate) {
    stop_slide(currentTab);
    $(currentTab).css({ zIndex: '102', top: '0', display: '' });
    $(id).css({ top: '0', zIndex: '101', top: '0', display: '' });
    $(currentTab).fadeOut(speed);
    $(currentTab + '-tab').removeClass('selected');
    $(id + '-tab').addClass('selected');
    slidePointerTo(id + '-tab', pointerDelay);
    currentTab = id;
    animate_slide(id);
}

function startAnimate() {
    stopAnimate();
    animator = window.setInterval(function() { changeSlide(); }, animateStartDelay);
    promptAnimator = window.setInterval(function() { changePrompt(); }, promptDelay);
}

function reStartAnimate() {
    stopAnimate();
    animator = window.setInterval(function() {
        changeSlide();
    }, reAnimateDelay);
}

function stopAnimate() {
    if (animator) {
        clearInterval(animator);
        animator = null;
    }
    if (slideTimer) {
        clearInterval(slideTimer);
        slideTimer = null;
    }
}

function changePrompt() {

    promptIndex++;
    if (promptIndex == promptCount) {
        promptIndex = 0;
    }

    if($.browser.msie) {
      $("#prompt-1").html(prompts[promptIndex]);
    } else {
      $('#prompt-button-1').addClass('button-pulse');
      $("#prompt-1").fadeOut('fast', function() { $("#prompt-1").html(prompts[promptIndex]).fadeIn(); });
      $('#prompt-button-1').animate({ opacity: 1.0 }, 500, function() {
      $('#prompt-button-1').removeClass('button-pulse')});
    }
}

function changeSlide() {
    stopAnimate();
    current_index++;
    if (current_index > 5) {
        current_index = 1;
    }
    showSlide('#panel-' + current_index, true);
    slideTimer = window.setInterval(function() {
        changeSlide()
    }, slideTime);
}



function animate_slide(id) {
    $(id + ' div.animated-container').hide();
    $(id + ' div.progress').show();

    if (id == '#panel-1') {
        $(id).onImagesLoad({ selectorCallback: animate_ipad });
    }
    if (id == '#panel-2') {
        $(id).onImagesLoad({ selectorCallback: animate_daks });
    }
    if (id == '#panel-3') {
        $(id).onImagesLoad({ selectorCallback: animate_google });
    }
    if (id == '#panel-4') {
        $(id).onImagesLoad({ selectorCallback: animate_decisions });
    }
    if (id == '#panel-5') {
        $(id).onImagesLoad({ selectorCallback: startGallery });
    }
}

function stop_slide(id) {

    if (id == '#panel-4') {
        stopBaldGuy();
    }
    if (id == '#panel-5') {
        stopGallery();
    }
}


function animate_ipad() {
    var simple = goSimple();

    $('#home-ipad-1').stop().css({ 'left': '-385px' });
    $('#home-ipad-2').stop().css({ 'top': '-45px' });
    $('#home-ipad-3').stop().css({ 'right': '-590px' });
    $('#home-ipad-4').stop().css({ 'bottom': '-90px' });
    $('#panel-1 div.progress').fadeOut(400);
    $('#panel-1 div.animated-container').fadeIn(400);

    // ipad image
    $('#home-ipad-1').delay(1000).animate({ 'left': '10px' }, { duration: 800, easing: 'easeOutQuint' });

    // title
    $('#home-ipad-2').delay(1600).animate({ 'top': '30px' }, { duration: 800, easing: 'easeOutQuint' });

    // sub-title
    $('#home-ipad-3').delay(2000).animate({ 'right': '30px' }, { duration: 400, easing: 'easeOutCirc' });

    // text
    $('#home-ipad-4').delay(2500).animate({ 'bottom': '35px' }, { duration: 1500, easing: 'easeOutElastic' });

}

function animate_daks() {

    var simple = goSimple();

    $('#home-daks-1').stop().css({ top: '-240px' });
    $('#home-daks-3').stop().stop().stop().css({ right: '-200px', top: '57px' });
    $('#home-daks-4').stop().stop().css({ right: '-245px', top: '47px' });
    $('#panel-2 div.progress').fadeOut(400);

    if (!simple) {
        $('#home-daks-2').stop().css({ opacity: '0' });
        $('#panel-2 img.sharp').css({ opacity: '1', display: 'block' });
        $('#panel-2 img.blur').css({ opacity: '1', display: 'block' });
    } else {
        $('#home-daks-2').hide();
        $('#panel-2 img.blur').hide();
    }
    $('#panel-2 div.animated-container').fadeIn(400);

    // text
    $('#home-daks-1').delay(1000).animate({ top: '27px' }, { duration: 1600, easing: 'easeOutBack' });
    if (!simple) {
        $('#home-daks-1 img.blur').delay(2000).animate({ opacity: '0' }, 600);
    }

    // clothes line
    if (!simple) {
        $('#home-daks-2').delay(4000).animate({ opacity: '1' }, 500);
    } else {
        $('#home-daks-2').delay(4000).show();
    }

    // undies
    $('#home-daks-3').delay(4000).animate({ top: '57px', right: '-90px' }, { duration: 800, easing: 'easeInOutQuart' })
.delay(1000).animate({ top: '59px', right: '155px' }, { duration: 800, easing: 'easeInOutQuart' })
.delay(1800).animate({ top: '59px', right: '315px' }, { duration: 800, easing: 'easeInOutQuart' });

    // pants
    $('#home-daks-4').delay(6100).animate({ top: '47px', right: '-100px' }, { duration: 800, easing: 'easeInOutQuart' })
.delay(1600).animate({ top: '53px', right: '60px' }, { duration: 800, easing: 'easeInOutQuart' });
}

function animate_google() {

    $('#home-google-2').stop().css({ right: '-551px' });
    $('#home-google-3 img').hide();
    $('#panel-3 div.progress').fadeOut(400);
    $('#panel-3 div.animated-container').fadeIn(400);

    $('#home-google-2').delay(1000).animate({ right: '10px' }, { duration: 800, easing: 'easeInOutQuart' });

    $('#google-feet-1').delay(3000).fadeIn(300).hide();
    $('#google-feet-2').delay(3200).fadeIn(300).hide();
    $('#google-feet-3').delay(3400).fadeIn(300).hide();
    $('#google-feet-4').delay(3600).fadeIn(300);

}


function animate_decisions() {

    var simple = goSimple();

    $('#home-decisions-5').stop().css({ right: '-365px' });
    $('#home-decisions-4').stop().css({ top: '270px' });

    $('#panel-4 div.progress').fadeOut(400);
    if (!simple) {
        $('#panel-4 img.sharp').css({ opacity: '0.01' });
        $('#panel-4 img.blur').css({ opacity: 1, display: 'block' });
    } else {
        $('#panel-4 img.sharp').css({ opacity: '1' });
        $('#panel-4 img.blur').hide();
    }
    $('#panel-4 div.animated-container').fadeIn(400);

    stopBaldGuy();
    startBaldGuy();

    $('#home-decisions-5').delay(3000).animate({ right: '20px' }, { duration: 800, easing: 'easeInOutQuart' });
    if (!simple) {
        $('#home-decisions-5 img.sharp').delay(3000).animate({ opacity: 1 }, 800);
        $('#home-decisions-5 img.blur').delay(3500).animate({ opacity: '0.01' }, 300);
    }

    $('#home-decisions-4').delay(5000).animate({ top: '130px' }, { duration: 800, easing: 'easeInOutQuart' });
    if (!simple) {
        $('#home-decisions-4 img.sharp').delay(5500).animate({ opacity: 1 }, 300);
        $('#home-decisions-4 img.blur').delay(5500).animate({ opacity: '0.01' }, 300);
    }

}

function startBaldGuy() {
    $('#home-decisions-3').moveBaldGuy({

        top: 0,
        left: 40,
        right: 150,
        bottom: -20,
        speed: 800,
        pause: 300
    });
}

function stopBaldGuy() {
    $('#home-decisions-3').stop();
    if (baldGuyTimer) {
        clearInterval(baldGuyTimer);
        baldGuyTimer = null;
    }
}

function randomIntBetween(lower, higher) {
    return parseInt(rand_no = Math.floor((higher - (lower - 1)) * Math.random()) + lower);
}

$.fn.extend({

    moveBaldGuy: function(options) {
        var options = $.extend({
            top: 50,
            left: 50,
            right: 290,
            bottom: 320,
            speed: 4000,
            pause: 0
        }, options || {});
        var el_id = $(this).attr('id');
        var t = randomIntBetween(options.top, options.bottom);
        var l = randomIntBetween(options.left, options.right);
        $('#' + el_id).animate({
            bottom: t + 'px',
            left: l + 'px'
        }, { duration: options.speed, easing: 'easeInOutExpo' });

        baldGuyTimer = window.setTimeout(function() {
            $('#' + el_id).moveBaldGuy(options);
        }, options.speed + options.pause)

        return this;
    }
});

function startGallery() {

    var reflect = 0.9;
    if (goSimple()) {
        reflect = 0;
        $('#uniquemail-gallery').css({ height: '130px' });
    }
        
    if (galleryBuilt == 0) {
        $('#uniquemail-gallery').iCarousel({
            itemWidth: 120,
            itemHeight: 80,
            itemMinWidth: 100,
            items: 'a',
            reflections: reflect,
            rotationSpeed: 1.8
        });
        galleryBuilt = 1;
    }
    if (galleryStarted == 1)
        return;
    $.startGallery();
    galleryStarted = 1;
}

function stopGallery() {
    if (galleryStarted == 0)
        return;
    $.stopGallery();
    galleryStarted = 0;
}
 

function goSimple() {
    var agent = navigator.userAgent.toLowerCase();
    var is_iphone = (agent.indexOf('iphone') != -1);
    if (is_iphone) { return true; }
    if ($.browser.msie && $.browser.version == "6.0") {
        return true;
    }
    return false;
}


