/**
 * MB site script
 */
var Config = {
	fadeSpeed: 200,
	videoOverlayOpacity: .9,
	videoOutOpacity: .5,
	cycleSpeed: 450,
	easing: 'easeInOutExpo',
	footerSpeed: 450
};

(function($,undefined) {
var SA = window.SA = {};

SA.extend = function(obj) {
	$.extend(true, SA, obj);
};

// Initialize
SA.extend({
	init: function()
	{
		for (var i in SA) SA[i].init && SA[i].init();
	}
});

// Work Media
SA.extend({
	work:
	{
		init: function()
		{
			this.$projects = $('#projects');
			
			if ( this.$projects.length )
			{
				this.$media = $('.media');
				this.bindEvents();
			}
		},
		bindEvents: function()
		{
			
			
			var self = this;
			
			this.$projects.find('a.ready')
				.filter('.ready')
					.hover(function() {					
						$(this).find('.overlay').stop(true, true).fadeTo(Config.fadeSpeed, Config.videoOverlayOpacity); 
					}, function() {
						$(this).find('.overlay').stop(true, true).fadeTo(Config.fadeSpeed, Config.videoOutOpacity);
					})
				.end()
				.on('click', function(e) {
					var $t = $(this);
					var $box = $t.attr('box');
					e.preventDefault();
					
					
					
					
					$t.removeClass('ready');
					// $('a.project').add("<span class='overlay'></span>");					
					$t.find('.overlay').remove();
					this.req = $.ajax({
						url: $t.attr('href')
					})
					.done(function(data) {
						$('.'+$box).find('.content').html( data );
						SA.iframes.loadFrames();
					})
					.fail(function(data) {
						console.debug( arguments );
					});
				});
			
			this.$media.find('.close').on('click', function(e) {
				e.preventDefault();
				
				self.$media.slideUp(250, function() {
					self.$media.find('.content').empty();
				});
			});
			
		}
	}
});

// Iframe resizing
SA.extend({
	iframes:
	{
		init: function()
		{
			this.$media = $('.media');
			
			if ( this.$media.length )
			{
				this.bindEvents();
			}
		},
		bindEvents: function()
		{
			var self = this;
			
			if ( this.$frames == null ) this.loadFrames();
			
			$(window).on('resize.iframes', function() {
				var nW = self.$media.width();
				
				self.$frames.each(function() {
					var $t = $(this);
					
					$t.width( nW ).height( nW * $t.data('ratio') );
				});
			}).trigger('resize');
			
			this.bound = true;
		},
		loadFrames: function()
		{
			this.$frames = $('iframe', this.$media);
			
			this.$frames.each(function() {
				$(this)
					.data('ratio', this.height / this.width)
					.removeAttr('width height');
			});
			
			if ( this.bound ) $(window).trigger('resize.iframes');
		}
	}
});

// Media Cycles
SA.extend({
	cycle:
	{
		init: function()
		{
			this.$cycle = $('.ui-cycle');
			
			if ( this.$cycle.length )
			{
				this.cycle();
			}
		},
		cycle: function()
		{
			// we have to give each instance an id and nav
			this.$cycle.each(function(i) {
				$(this).attr('id', 'cycle-'+i+1);
			});
			
			this.$cycle.each(function() {
				var $t = $(this);
				
				$t.find('.items').cycle({
					fx: 'scrollHorz',
					speed: Config.cycleSpeed,
					easing: Config.easing,
					pager: '.sub_nav',
					pagerAnchorBuilder: function(idx, slide) { 
					        // return selector string for existing anchor 
					        return '.sub_nav li:eq(' + idx + ') a:not(.link)'; 
					    }
					
				})
				.cycle('pause');
			});
			
			
			
		}
	}
});

// Footer Global
SA.extend({
	footer:
	{
		init: function()
		{
			this.$footer = $('footer');
			this.$content = this.$footer.find('.content');
			this.$copyright = $('#ft_copyright');
			
			this.bindPosition('header').bindEvents();
		},
		bindEvents: function()
		{
			var self = this;
			
			// i link floating at the bottom of the page
			$('#ftcopy').on('click', function(e) {
				e.preventDefault();
				
				self.isOpen = !self.isOpen;
				
				if ( self.isOpen )
				{
					self.initIndex().open();
				}
				else
				{
					self.close();
				}
			});
			
			// menu items
			this.$content.find('.menu a').on('click', function(e) {
				var $t = $(this),
					id = $t.parent().attr('id').substr(6);
				
				e.preventDefault();
				
				if ( $t.parent().is('.currentpage') ) return;
				
				$t.parent().siblings('.currentpage').removeClass('currentpage').end().addClass('currentpage');
				
				self.$content.find('.entries').children().hide().end().find('.entry_'+id).show();
				
				self.checkLength();
			});
			
			// close box
			$('#closeFt').on('click', function(e) {
				e.preventDefault();
				self.close();
			});
			
			return this;
		},
		bindPosition: function( bindTo )
		{
			var self = this;
			
			this.$bindPositionTo = $( bindTo );
			
			$(window)
				.on('resize.footer', function() {
					var offset = self.$bindPositionTo.offset().left;
					
					self.$copyright.css('left', offset);
					
					$('.jspScrollable:visible').each(function() {
						$(this).data('jsp').reinitialise();
					})
				})
				.trigger('resize.footer');
			
			return this;
		},
		checkLength: function()
		{
			var $active = this.$content.find('.entry:visible');
			
			if ( !$active.is('.jspScrollable') )
			{
				$active.jScrollPane();
			}
			else {
				$active.data('jsp').reinitialise();
			}
			
			return this;
		},
		close: function()
		{
			this.isOpen = false;
			this.$content.stop(true, true).slideUp( Config.footerSpeed, Config.easing );
			return this;
		},
		initIndex: function()
		{
			this.$content.find('.menu li:first a').trigger('click');
			return this;
		},
		open: function()
		{
			this.isOpen = true;
			this.$content.stop(true, true).slideDown( Config.footerSpeed, Config.easing );
			
			$.scrollTo('+=350', {axis:'y', easing: Config.easing, duration: Config.footerSpeed});
			return this;
		}
	}
});

})(jQuery);

// document ready
jQuery( SA.init );
