/* Author: Adam Jahnke - Elevate Visual Communication
*/

function slideSwitch(){
    var $active = $('#slider .slide.active');
    var $activeNav = $('#slider-container .tabs a.active');
    
    if ($active.length == 0) 
        $active = $('#slider .slide.last-item');

    if($activeNav.length == 0) 
        $activeNav = $('#slider-container .tabs a.last-item');
    
    var $next = $active.next().length ? $active.next() : $('#slider .slide.first-item');
    var $nextNav = $activeNav.next().length ? $activeNav.next() : $('#slider-container .tabs a.first-item');
    
    $active.addClass('last-active');
    $activeNav.addClass('last-active');
    
    $next.hide();
    $active.fadeOut(500).removeClass('active last-active');
    $next.addClass('active').fadeIn(500, function(){
        $('#slider .slide:not(.active)').hide();
    });
    $nextNav.addClass('active');
    $activeNav.removeClass('active last-active');
}



$('document').ready(function(){

    $('#slider .slide').each(function(){
        var slide = $(this),
        slideImg = 'url('+slide.find('img').attr('src')+')';
        slide.css('background', slideImg);
    });
    
    $('#slider .slide:first-child').show();
    $('#slider .slide:first-child a').focus();

    var playSlideshow = setInterval("slideSwitch()",6000);

    $('#slider-container').hover(function(){
            clearInterval(playSlideshow);
        },
        function(){
            clearInterval(playSlideshow);
            playSlideshow = setInterval("slideSwitch()",6000);
        }
    );

    $('#slider-container .tabs a').click(function(){
        clearInterval(playSlideshow);
        $('#slider div.slide,#slider-container .tabs a').stop();
        $('#slider div.slide.last-active').removeClass('last-active');
        $active = $('#slider div.slide.active');
        $active.addClass('last-active');
        slideId = '.'+$(this).attr('rel');
        $('#slider .slide:not(.active)').hide();
        $('#slider').find(slideId).hide().addClass('active').fadeIn(500);
        $active.removeClass('active last-active').fadeOut(500);
        $(this).addClass('active').siblings().removeClass('active');
        // playSlideshow = setInterval("slideSwitch()",6000);
        return false;
    });


    $('input[type=text],input[type=email],textarea').live('focus', function(){
        if($(this).val() === $(this).attr('title')){
          $(this).val('');
        };
    });
    
    $('input[type=text],input[type=email],textarea').live('blur', function(){
        if($(this).val() === ''){
          $(this).val($(this).attr('title'));
        };
    });

    $('#launch-interest').submit(function(e) {

        // Prevent double clicking
        $(':submit', this).click(function() {
            return false;
        });
        
        var form   = $(this);
        var email  = $("#email_lead");
        var button = $("#sign-up-button");
        if (email.val() == '' || email.val() == email.attr('title')) {
            alert("Please enter an email address");
            email.focus();
            return false;
        }
        
        button.html("Saving...");
        
        $.ajax({
            url: form.attr('action'),
            type: 'POST',
            dataType: 'json',
            data: form.serialize(),
            
            success: function(r) {
                if (r.success) {
                    button.html("Got it!");
                    email.val('');
                }
            }
        });
          
        // Cancel default form submit
        e.preventDefault();
        return false; 
    });
    
    // wire up the contact forms. 
    //
    $('#beta-interest, #contact-form').submit(function(e) {
        
        var form = $(this);
        var email = $("#email");
        var button = form.find('button');
        if (email.val() == '' || email.val() == email.attr('title')) {
            alert("Please enter an email address");
            email.focus();
            return false;
        }

        button.attr({disabled: 'disabled'});
        
        button.html("Saving...");
        
        $.ajax({
            url: form.attr('action'),
            type: 'POST',
            dataType: 'json',
            data: form.serialize(),
        
            success: function(r) {
                if (r.success) {
                    $('input', form).val('')                    
                    button.replaceWith('<p id="beta-interest-success"><span>Got it, thanks!</span> Go back to the <a href="/">main page</a>.</p>');

                    // append the retargeter pixels to the body on POST. 
                    //
                    $('body').append('<img src="http://ad.retargeter.com/seg?remove=116303&t=2" width="1" height="1" />');
                    $('body').append('<img src="http://ad.retargeter.com/px?id=12005&t=2" width="1" height="1" />');
                }
            }
        });
        
        // Cancel default form submit
        e.preventDefault();
        return false; 
    });

    /**
     * Initializes the map
     *
     */
    function rot13init() {
        var map = new Array();
        var s = "abcdefghijklmnopqrstuvwxyz";
        for (var i = 0 ; i < s.length ; i++)
            map[s.charAt(i)] = s.charAt((i+13)%26);
        for (var i = 0 ; i < s.length ; i++)
            map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
        return map;
    }
    
    /**
     * rot13's a given string. 
     */
    function str_rot13(a) {
        var map = rot13init();
        var s   = "";
        for (var i = 0 ; i < a.length ; i++) {
            var b = a.charAt(i);
            s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
        }
        return s;
    }
    
    // run through all of the anchors that have a no follow
    // 
    $('a[rel=nofollow]').each(function(i,e) {
        var a    = $(e);
        var href = a.attr('href');

        // ooh, you look suspicious. 
        //
        if (href.indexOf('/contact/')) {
            var address = href.replace(/\/rf\/company\/contact\/([a-z0-9._%-]+)\/([a-z0-9._%-]+)\/([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
            if (href != address) {
                a.attr('href','mailto:' + str_rot13(address)); 
            }
        }
    });

    // fix up the value prop links
    //
    $('div.value-prop').each(function(i, e) { 
        var container = $(e);
        var a = container.find('a');
        container.click(function(e) { 
            document.location = a.attr('href');
            return false;
        });
        container.css({cursor: 'pointer'});
    });

});

