/*
 * Note: This file depends on the jQuery library and the jQuery Tools Library
 * Note: Module pattern liberally used: http://yuiblog.com/blog/2007/06/12/module-pattern/
*/

if (typeof GETSMB === 'undefined') {
    GETSMB = {};
} 

GETSMB.toggleContentVisibility = function(options) {
    var config = {
        control: '.toggle_control',
        content: '.toggle_content'
    };
    
    $.extend(config, options);
    
    // Expand/Collapse
    // use live so as not to bind an event to each element
    $(config.control).live('click', function (e) {
        var $this = $(this);
        
        e.preventDefault(); //just in case we want to use a link
        
        $this.next(config.content).slideToggle('normal', function () {
            $this.toggleClass('closed').toggleClass('opened');
        });
    });
};

GETSMB.createTabs = (function() {
    var config, init;
    
    config = {
        $tabControls: $('#tabs'),
        $paneContainer: $('#panes')
    };
    
    init = function () {
        var $panes;
        
        if (!config.$tabControls.length || !$.fn.tabs) {
            return;
        }
        
        $panes = config.$paneContainer.children('div');
        config.$tabControls.tabs($panes, {
            current: 'active',
            effect: 'default'
        });
    };
    
    return {
        init: init
    }
}());

GETSMB.createTooltips = function () {
    if (!$.fn.tooltip) {
        return;
    }
    
    $('.info_trigger').tooltip({
        effect: 'toggle',
        delay: 100,
        position: 'top right',
        offset: [30, -10]
    });
};

GETSMB.createOverlay = function () {
    if (!$.fn.overlay) {
        return;
    }

    // override the default effect with this custom one
    $.tools.overlay.addEffect('simple', 

    /* 
    onLoad/onClose functions must be called otherwise none of the 
    user supplied callback methods will be called
    */
    function(pos, onLoad) {

        var conf = this.getConf(),
        w = $(window);

        if (!conf.fixed)  {
            pos.top += w.scrollTop();
            pos.left += w.scrollLeft();
        } 

        pos.position = conf.fixed ? 'fixed' : 'absolute';
        this.getOverlay().css(pos).show(); 
        onLoad.call()

        }, function(onClose) {
            this.getOverlay().hide();
            onClose.call(); 
        }
    );
    
    $('.overlay_trigger').overlay({
        effect: 'simple',
        onBeforeLoad: function (event) {
            var $overlay = this.getOverlay(),
                $content = $overlay.find('div.expanded_info_content'),
                $el = this.getTrigger(),                
                $parent = $el.parent(),
                $prevTrigger = $parent.prevAll().find('.overlay_trigger').eq(0),
                $nextTrigger = $parent.nextAll().find('.overlay_trigger').eq(0);
                
                
            function pageInfo() {
                if ($(this).is('p.next a')) {
                    $nextTrigger.trigger('click');
                } else {
                    $prevTrigger.trigger('click');
                }
            };
            
            if ($nextTrigger.length) {
               $content.append('<p class="next"><a class="ir">Next</a></p>');
            }
            
            if ($prevTrigger.length) {
                $content.prepend('<p class="previous"><a class="ir">Previous</a></p>');
            }
            
            $el.addClass('active');
            $content
                .wrap('<div class="expanded_info_wrap" />')
                .find('p.previous a, p.next a').bind('click', pageInfo);
        },
        onClose: function (event){
            var $el = this.getTrigger(),
                $overlay = this.getOverlay();
            
            $el.removeClass('active');                
            $overlay
                .find('div.expanded_info_content').unwrap()
                .find('p.previous, p.next').remove();
        }
    });
};

GETSMB.clickableContent = (function () {
    var config, make;
    
    config = {
        parent: '#homepage_hero',
        content: 'div.hero_content',
        link: 'a.more_link'
    };
        
    make = function (options) {
        var $content;
        
        // if we passed in an option object combine it with the config object
        if (options) {
            $.extend(config, options);
        }
        
        $content = $(config.parent).find(config.content);

        if ($content.length) {
            $content.bind('click', function() {
                window.location = $(this).find(config.link).attr('href');
            });
        }
    };
    
    return {
        makeClickable: make
    }
}());

GETSMB.heroSlider = function() {
    var $hero = $('#homepage_hero div.scrollable'),
        $pager = $('#hero_pager');
    
    if (!$hero.length) {
        return;
    }
    
    $hero
        .scrollable({
            circular: true,
            speed: 400
        })
        .autoscroll({ 
            interval: 10000,
            autoplay: true 
        })
        .navigator({
            navi: $pager,
            naviItem: 'li',
            activeClass: 'active'
        });
        
    // stop autoscroll on click
    $pager.delegate('li', 'click', function () {
        $hero
            .removeData('events') // remove all events so we don't start the slider on hover
            .data('scrollable').stop()
    });
};

/* For Form Normalization:
 * https://github.com/nathansmith/formalize/
 * Modified to remove the IE6 stuff
*/
GETSMB.formalize = (function($, window, document, undefined) {
	// Private constants.
	var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');
	var AUTOFOCUS_SUPPORTED = 'autofocus' in document.createElement('input');
	var WEBKIT = 'webkitAppearance' in document.createElement('select').style;
	var IE7 = !!($.browser.msie && parseInt($.browser.version, 10) === 7);

	// Expose innards of FORMALIZE.
	return {
		// FORMALIZE.go
		go: function() {
			for (var i in GETSMB.formalize.init) {
				GETSMB.formalize.init[i]();
			}
		},
		// FORMALIZE.init
		init: {
			detect_webkit: function() {
				if (!WEBKIT) {
					return;
				}

				// Tweaks for Safari + Chrome.
				$('html').addClass('is_webkit');
			},
			// FORMALIZE.init.full_input_size
			full_input_size: function() {
				if (!IE7 || !$('textarea, input.input_full').length) {
					return;
				}

				// This fixes width: 100% on <textarea> and class="input_full".
				// It ensures that form elements don't go wider than container.
				$('textarea, input.input_full').wrap('<span class="input_full_wrap"></span>');
			},
			// FORMALIZE.init.placeholder
			placeholder: function() {
				if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
					// Exit if placeholder is supported natively,
					// or if page does not have any placeholder.
					return;
				}

				$(':input[placeholder]').each(function() {
					var el = $(this);
					var text = el.attr('placeholder');

					function add_placeholder() {
						if (!el.val() || el.val() === text) {
							el.val(text).addClass('placeholder_text');
						}
					}

					add_placeholder();

					el.focus(function() {
						if (el.val() === text) {
							el.val('').removeClass('placeholder_text');
						}
					}).blur(function() {
						add_placeholder();
					});

					// Prevent <form> from accidentally
					// submitting the placeholder text.
					el.closest('form').submit(function() {
						if (el.val() === text) {
							el.val('').removeClass('placeholder_text');
						}
					}).bind('reset', function() {
						setTimeout(add_placeholder, 50);
					});
				});
			},
			// FORMALIZE.init.autofocus
			autofocus: function() {
				if (AUTOFOCUS_SUPPORTED || !$(':input[autofocus]').length) {
					return;
				}

				$(':input[autofocus]:visible:first').focus();
			}
		}
	};
// Alias jQuery, window, document.
})(jQuery, this, this.document);


// we don't need document.ready if this script is at the bottom of the page. 
GETSMB.formalize.go();
GETSMB.toggleContentVisibility();
GETSMB.heroSlider();
GETSMB.clickableContent.makeClickable();
GETSMB.createTabs.init();
GETSMB.createTooltips();
GETSMB.createOverlay();

