var socialtwist = (function() {
    'use strict';

    var theme_root, initZeroClipboard, loadHomepageVideo, initContactWidget, initHeightEqualizer, initCarousel, initPressVideo;


    // Figure out the URI we were served from
    /**
     * See http://code.google.com/apis/youtube/player_parameters.html for parameter documentation
     */
    loadHomepageVideo = function(e) {
        var target, video_url, video_id, iframe;
        e.preventDefault();

        target = jQuery(this);
        
        // isolate the video ID
        video_url = target.attr('href');
        if (video_url.indexOf('?v=') > -1) {
            video_id = video_url.replace(/.*v=/, '');
        }

        iframe = jQuery('<iframe/>');
        iframe.attr({
            width: 398,
            height: 243,
            src: 'http://www.youtube.com/embed/I0FBYl7e8sc?rel=0&autohide=1&autoplay=1&showinfo=0&modestbranding=1&fs=1',
            frameborder: 0,
            allowfullscreen: true
        });
        jQuery('#homepage-video').html(iframe);
        //swfobject.embedSWF('http://www.youtube.com/e/' + video_id + '?enablejsapi=1&autohide=1&autoplay=1&showinfo=0&rel=0&modestbranding=1&fs=1',
                           //target.parent().attr('id'), "510", "287", "8", null, null, params, attrs);
        
    };

    initContactWidget = function() {
        var submitCallback, successCallback;

        submitCallback = function(e) {
            e.preventDefault();
            
            var target, data_map, error_field, form_is_valid;

            target = jQuery(this);

            error_field = target.find('.error');

            data_map = {};

            form_is_valid = true;
            
            target.find("INPUT[type=text]").each(function() {
                var target, name;
                target = jQuery(this);
                name = target.attr('name');

                if (target.val().trim() == '') {
                    error_field.html('Please fill out all fields.');
                    form_is_valid = false;
                    return false;
                } else {
                    error_field.html('');
                }
                
                data_map[name] = target.val();
            });


            if (form_is_valid === false) return false;
            
            formalyzer_call_onclick(19753);
            
            jQuery.ajax({
                type: 'POST',
                url: target.attr('action'),
                data: data_map,
                success: successCallback
            });
        };

        successCallback = function(data) {
            jQuery('#contact-widget-form').html(data);
        }
        jQuery('#contact-widget-form').submit(submitCallback);
    };

    initHeightEqualizer = function() {
        jQuery.fn.setAllToMaxHeight = function(){
            return this.height( Math.max.apply(this, $.map( this , function(e){ return $(e).height() }) ) )
        }
        jQuery('#homepage-features LI').setAllToMaxHeight();
        jQuery('#homepage-features H2').setAllToMaxHeight();

        jQuery.fn.setAllToMaxHeight = undefined;
    };

    initCarousel = function() {
        jQuery(".scrollable").scrollable();

        jQuery(".video-thumbnail").click(function() {
            jQuery('DIV.poster A', this).triggerHandler('click');
        });

        jQuery(".video-thumbnail IMG").mouseenter(function() {
            jQuery(this).fadeTo('fast', .3, function() {
                jQuery(this).next('.play').show();
            });
        });

        jQuery(".video-thumbnail").mouseleave(function() {
            jQuery("IMG", this).fadeTo('fast', 1, function() {
                jQuery(this).next('.play').hide();
            });

        });

    };

    initPressVideo = function() {
        var triggers;

        // Create modal nodes for every play link
        triggers = jQuery('A.play');

        jQuery('.modal').detach().appendTo('body');


        // Activate the triggers
        triggers.overlay({
            mask: {
                color: '#000000',
                loadSpeed: 200,
                opacity: 0.85
            },
            onBeforeLoad: function() {
                var container = this.getOverlay().find(".container");
                //container.load(this.getTrigger().attr("href"));
            },
            onClose: function() {
                var iframe_src, container, new_iframe;
                iframe_src = this.getOverlay().find('IFRAME').attr('src');
                container = this.getOverlay().find('.container');
                container.empty();
                new_iframe = jQuery('<iframe></iframe>');
                new_iframe.attr('src', iframe_src);
                container.append(new_iframe);
            },
            onLoad: function() {
                var container, iframe, iframe_width, overlay;
                container = this.getOverlay().find('.container');
                
                overlay = this.getOverlay();
                //overlay.css('left', jQuery(document).width() - container.width()/2);
                //overlay.css('left', 0);
                container.show();

            },
            top: '10%',
            closeOnClick: true,
            closeOnEsc: true
        });



    };

    return {
        init: function() {
            initContactWidget();
            initHeightEqualizer();
            initCarousel();
            initPressVideo();
            jQuery('#homepage-video-play').click(loadHomepageVideo);
        }
    }
})();

jQuery(document).ready(socialtwist.init);

