



var ctem = function(){

	var workspaceBind = function(scroller){
		if (typeof scroller !== 'undefined'){
			// Backend setup
			$('#cms-workspace').bind('editing-enabled', function(){
				scroller.disable();
			}).bind('editing-disabled', function(){
				scroller.enable();
			});
		}
	}
	/**
	 * Make the a-feature scrolly
	 */
	var afeature = function(){
		$('div.afeature').scrolly({
			selectors: {
				container: '.image'
			},
			minimumElements:2,
			tag:'a-feature',

			// Add real widths to each span
			before:function(){
				$('.image-description ul li').css('display', 'inline-block').each(function(){
					$(this).css('width', $(this).width() + 10);
				});
			},
			autoscroll: 5000,
			after: function(){
				this.bindNext($('p.right-arrow a', this.el));
				this.bindPrev($('p.left-arrow a', this.el));
			},
			moved: function (){
				var active = this.current(),
				speed  = 400;

				// Update the text bar by sliding and fading in new text
				var el = $('.image-description ul').children().stop(true,true).hide().eq(active - 1).fadeIn(speed);
				$('.image-description').stop(true,true).animate({
					width: el.width() > 700 ? 700 : el.width()
				},speed);

				// Update the progress bar x/n
				$('.image-status').html(active + ' / ' + this.total());
			}
		});
	}

	/**
	 * Setup a scroller on the lateststats div
	 */
	var latestStats = function(){
		$('div.lateststats').scrolly({
			minimumElements:2,
			selectors: {
				container: '.item-list'
			},			
			after: function(){
				this.bindNext($('a.right-arrow', this.el));
				this.bindPrev($('a.left-arrow', this.el));
			}
		});
	}

	var latestUpdates = function(){

		var scroller;

		$('div.latestupdates-container').scrolly({
			selectors: {
				container: '.cms-anchor',
				items: '.cms-component'
			},
			minimumElements:2,
			after: function(){
				this.bindNext($('a.right-arrow', this.el));
				this.bindPrev($('a.left-arrow', this.el));

				scroller = this;
			},
			autoscroll:false,
			autohide:false,

			/**
			 * We dont want empty spaces
			 */
			willWrapNext: function(){
				return (this.current() > this.total() - 3);
			},

			/**
			 * Value if we wrap around prev
			 */
			valuePrevWrap: function(){
				return this.total() - 2;
			},

			/**
			 * Show an element of a container
			 */
			show: function(item){
				var pos = $(item).prevAll().length * -289;

				$(item).addClass(this.classes.active).parent().parent().stop(true, true).animate({
					left: pos
				}, 400, 'swing');
			},

			/**
			 * Hide an element of a container
			 */
			hide: function(item){
				$(item).removeClass(this.classes.active);
			}
		});

		workspaceBind(scroller);
	}

	var infoblocks = function(){
		var scroller;

		$('div.infoblocks-container').scrolly({
			selectors: {
				container: '.cms-anchor',
				items: '.cms-component'
			},
			minimumElements:5,
			after: function(){
				this.bindNext($('a.right-arrow', this.el));
				this.bindPrev($('a.left-arrow', this.el));

				scroller = this;
			},
			autoscroll:4000,
			autohide:false,

			/**
			 * We dont want empty spaces
			 */
			willWrapNext: function(){
				return (this.current() > this.total() - 4);
			},

			/**
			 * Value if we wrap around prev
			 */
			valuePrevWrap: function(){
				return this.total() - 3;
			},

			/**
			 * Show an element of a container
			 */
			show: function(item){
				var pos = $(item).prevAll().length * -240;

				$(item).addClass(this.classes.active).parent().parent().stop(true, true).animate({
					left: pos
				}, 400, 'swing');
			},

			/**
			 * Hide an element of a container
			 */
			hide: function(item){
				$(item).removeClass(this.classes.active);
			}
		});
		
		workspaceBind(scroller);
	}

	var gallery = function(){
		$('.gallery_unstyled').addClass('gallery').removeClass('.gallery_unstyled').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : false, // helper for making the image
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

				// fade in the image &amp; caption
				image.css('display','none').fadeIn(1000);
				//caption.css('display','none').fadeIn(1000);

				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.5);

				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');


				// add a title for the image
				//image.attr('title','Next image >>');
				$('.caption').html($('.gallery li.active .imagecaption').html());

				var position = $('.gallery li.active').prevAll().length * 111 - 382;
				$('#imagemarker').animate({
					backgroundPosition: position+'px 0px'
				}, {
					duration: 'slow',
					easing: 'easeOutQuint'
				});



			},
			onThumb : function(thumb) { // thumbnail effects goes here

				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.5';

				// fade in the thumbnail when finnished loading
				thumb.css({
					display:'none',
					opacity:_fadeTo
				}).fadeIn(1500);

				// hover effects
				thumb.hover(
				function() {
					thumb.fadeTo('fast',1);
				},
				function() {
					_li.not('.active').children('img').fadeTo('fast',0.5);
				} // don't fade out if the parent is active
			)

			}
		});
	}


	return {
		afeature:       afeature,
		latestStats:    latestStats,
		latestUpdates:  latestUpdates,
		infoblocks:     infoblocks,
		loaded:         false,
		gallery: gallery
	}
}();

$(function(){
	ctem.loaded = true;
	ctem.afeature();
	ctem.latestStats();
	ctem.latestUpdates();
	ctem.infoblocks();
	ctem.gallery();

	$('#cms-workspace').bind('block-updated', function(e, selector){
		if (selector.e.children('.afeature').length){
			ctem.afeature();
		}

		if (selector.e.children('.pages-image-gallery').length){
			ctem.gallery();
		}
	
		sifrUpdate();
	});



	// Make divs clickable
	$('.clickable:has(a[href])').click(function()
	{
		var anchor   = $('a[href]', this).eq(0),
		location = anchor.attr('href');

		// Found a location
		if (location !== undefined)
		{
			// Open a new window
			if (anchor.attr('target') === '_blank')
			{
				window.open(location, '_blank').focus();
			}
			// Forward the browser
			else
			{
				window.location = location;
			}
			
			return false;
		}

	}).bind('mouseenter mouseleave', function()
	{
		$(this).toggleClass('rollover');
	});
});

var sifrComplete = function(){
	var id,
	callback = function(){},
	setCallback = function(pcallback){
		callback = pcallback;
	},
	startTimeout = function(){
		if (id){
			clearTimeout(id);
		}

		id = setTimeout(callback, 300);
	};

	return {
		setCallback: setCallback,
		startTimeout: startTimeout
	}
}();

/**
 * Be lazy and to the fix once
 */
sifrComplete.setCallback(function(){
	if (typeof Workspace !== 'undefined'){
		Workspace.overlayComponents();
	}
});
