﻿
// Form Controller
var forms = {
    init    : function(){ 
    
        if($('input:text').val() != ''){
            $(this).addClass('on');
        }  
    
        $('input:text').focus(function(){
            if($(this).val() == $(this).attr('defaultValue')){
                $(this).removeClass('error').addClass('on');
            }
        });

        $('input:text').blur(function(){
            if($(this).val() == ''){
                $(this).removeClass('on');
            }
        });
    }
}


// external anchor controller
var anchors = {
    init    : function(){
        $('a[rel*=external]').click(function(){
            window.open(this.href);
            return false;
        });
        $('#header #nav h3 a:not(#header h3#register a)').click(function(){
            $('#header h3.on').removeClass('on');
            $(this).parent().addClass('on');
        });
    }
}


// Validator controller
var validator = {
    init    : function(){
        $('#submit').click(function(){
            if(!isValidEmailAddress($('#dturll-dturll').val())){
                $('#dturll-dturll').val('').removeClass('on').addClass('error');
                return false;
            }
        });
    }
}


var fonts = {
    init    : function(){
        Cufon.replace('#rCol h2, #rCol h3', {
            textShadow: '#d19938 -2px 2px'
        });
        Cufon.replace('#stage h4, dl dt, #rCol ul.speakers li strong, #rCol .speaker h5, #speakerHolder h4');
    }
}


var shareShelf = {
    shelf   : Object,
    trigger : Object,
    init    : function(){
        this.shelf = $('#shelf');
        this.trigger = $('#shelf a#share');
        this.run();
    },
    run     : function(){
        this.trigger.toggle(function(){
            $(this).addClass('on');
            shareShelf.shelf.animate({top: '0px'}, 'normal');
        }, function(){
            $(this).removeClass('on');
            shareShelf.shelf.animate({top: '-124px'}, 'normal');
        });
    }
}


var tweets = {
    total: Number,
    speakers: Object,
    current: Number,
    running: Boolean,
    init: function() {
        this.total = $('.speaker').length;
        this.speakers = $('.speaker');
        this.current = Math.floor(Math.random() * this.speakers.length);
        this.running = false;
        this.tweets();
        this.navigation();
        this.speakers.eq(tweets.current).addClass('on');
        this.current++;
    },
    tweets: function() {
        $('.speaker .timeline').each(function() {
            var username = $(this).attr('id');
            $(this).tweet({
                username: username,
                count: 1,
                loading_text: "loading tweets..."
            });
        });
    },
    navigation: function() {

        // Next Button    
        $('#nextBtn').click(function() {

            if (!tweets.running) {

                tweets.running = true;

                $('.speaker.on').fadeOut('fast', function() {
                    $(this).removeClass('on');

                    if (tweets.current < tweets.total) {
                        tweets.speakers.eq(tweets.current).fadeIn('fast', function() {
                            $(this).addClass('on');
                            tweets.running = false;
                        });
                        tweets.current++;
                    } else {
                        tweets.speakers.eq(0).fadeIn('fast', function() {
                            $(this).addClass('on');
                            tweets.running = false;
                        });
                        tweets.current = 1;
                    }
                });
            }

            return false
        });

        $('#prevBtn').click(function() {

            if (!tweets.running) {

                tweets.running = true;

                $('.speaker.on').fadeOut('fast', function() {
                    $(this).removeClass('on');
                    if (tweets.current > 1) {
                        tweets.current--;
                        tweets.speakers.eq(tweets.current - 1).fadeIn('fast', function() {
                            $(this).addClass('on');
                            tweets.running = false;
                        });
                    } else {
                        tweets.speakers.eq(tweets.total - 1).fadeIn('fast', function() {
                            $(this).addClass('on');
                            tweets.running = false;
                        });
                        tweets.current = tweets.total;
                    }
                });
            }

            return false
        });
    }
}


function initialize(){
    forms.init();
    anchors.init();
    validator.init();
    fonts.init();
    shareShelf.init();
    tweets.init();
    Shadowbox.init({
        players:    ["iframe"]
    });
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}


$(document).ready(function(){
    initialize();
});