var Showcase = {
		
	activeItem : 0,
	
	numberOfItems : 0,
	
	timeoutCall : null,
	
	init : function(reallyDoIt) {
		
		if (!reallyDoIt) {
			window.setTimeout(function() { Showcase.init(true)}, 1000);
			return;
		}

		$('#sliderContent').fadeIn("slow");
		$('#sliderBackground').fadeIn("slow");

		var slider_buttons = $('#sliderPages');
		
		var items = 0;
		$('.sliderComponents .sliderComponent').each(function(key, value) {
			slider_buttons.append('<li><a href="#" style="margin-bottom: 20px;" id="slideButton_'+(key+1)+'">'+(key+1)+'</a></li>');
			items++;
		});
		Showcase.numberOfItems = items;
		
		$('#sliderPages li a').click(function(event) {
			
			var el_id = event.target.id;
			var index = parseInt(el_id.replace('slideButton_', ''));
			if (isNaN(index) || index == Showcase.activeItem) {
				return false;
			}
			
			var parent = $(this).parent();
			
			Showcase.showItem(index);
			return false;
		});
		
		if (items) {
			
			Showcase.timeoutCall = window.setTimeout(function() {
				Showcase.showNextItem();
			}, 4000);
		}	
	},
	
	showNextItem : function () {
		if (Showcase.numberOfItems > Showcase.activeItem) {
			Showcase.showItem(Showcase.activeItem+1);	
		} else {
			Showcase.showItem(1);
		}		
	},
	
	showItem : function (index) {
		
		window.clearTimeout(Showcase.timeoutCall);
		
		$('#sliderPages li').animate({
			bottom: '0'
		}, 1000, "easeOutQuad");
		
		$('#sliderPages li:nth-child('+index+')').animate({
//			opacity: 1,
			bottom: '10'
		}, 1000, "easeOutElastic");

		var slideComponent = $('.sliderComponents .sliderComponent:nth-child('+index+')');

//		Showcase.resetComponent(slideComponent);
		
		$('#sliderBackground').fadeOut(1000);
		$('#sliderContent').fadeOut(1000, function() {
            var new_bg_img = $('.slideBackground', slideComponent).css('background-image');
            var new_bg_color = $('.slideBackground', slideComponent).css('background-color');

            if (!Showcase.activeItem) {
				
                $('#sliderContent').animate({
                    top: '-300'
                }, 400);
                Showcase.activeItem = index;
            } else {
                var activeComponent = $('.sliderComponents .sliderComponent:nth-child('+Showcase.activeItem+')');
				Showcase.slideOut(activeComponent);

                Showcase.activeItem = index;
            }

            if (!new_bg_color) {
                new_bg_color = '#ccc';
            }

			window.setTimeout(function() {
	            $('#header_container').animate({
    	            backgroundColor: new_bg_color
	            }, 800, function() {
    	            Showcase.slideIn(slideComponent, new_bg_img);
        	    });
			}, 100);
			
		});		

		Showcase.timeoutCall = window.setTimeout(function() {
			Showcase.showNextItem();
		}, 7000);
	},		
	
	slideIn : function(slideComponent, new_bg_img) {
		
		if (new_bg_img) {
			$('.slideBackground', slideComponent).fadeIn(800);
		}
		
		$('.slideLeft', slideComponent).css({
			display: 'block'
		}).animate({
			top: '0'
		}, 1000);
		
		$('.slideRight', slideComponent).css({
			display: 'block'
		}).animate({
			bottom: '0'
		}, 1000);
	},

	slideOut : function(slideComponent) {

        $('.slideBackground', slideComponent).fadeOut(200);
		
		$('.slideLeft', slideComponent).animate({
			top: '-800px'
		}, 600);
		
		$('.slideRight', slideComponent).animate({
			bottom: '-800px',
		}, 600);
	},
	
	resetComponent : function(slideComponent) {

		$('.slideLeft', slideComponent).css({
			top: '-800px',
			display: 'none'
		});
			
		$('.slideRight', slideComponent).css({
			bottom: '-800px',
			display: 'none'
		});
				
		$(slideComponent).css({
			top: 0,
			display: 'block'
		});
	}
}

$(document).ready(function() {
	
	if ($('body').hasClass('frontpage')) {
		Showcase.init();
		Ticker.init();
	} else if (document.body.id == 'XMSUserPage_31') {
		var tdj = $('.XMSCrewOverview .XMSCrewItem:nth-child(10)');
		var link = $('a', tdj);
		
		$(link).addClass('inactive');
		$(link).click(function(){ return false; });
	}
	
	// initialize the slik tab
	$(".XMSPortfolioShowcase").sliktab();
	
	// fix active parent in navigation
	$("#navigation li.active").parents("li").addClass("active");
});

var Ticker = {
	items : new Array(),
	
	activeItem : null,
	
	init : function() {
		Ticker.items = $('.tickerItems li').get();
		Ticker.showNextItem();
	},
	
	showNextItem : function () {
		var msg_element = document.getElementById('messageHighlight');
		
		Ticker.activeItem += 1;
		if (Ticker.activeItem >= Ticker.items.length) {
			Ticker.activeItem = 0;
		}
		
		var next_item = Ticker.items[Ticker.activeItem];
		var message = $(next_item).html();
		Ticker.typeString(msg_element, message);
		
		window.setTimeout(function() { Ticker.chopString(msg_element) }, 1500);
	},
	
	chopString : function (element) {
		var string = element.innerHTML;
		
		if (string) {
			var str_length = string.length;
			string = string.substring(0, str_length-1);
			element.innerHTML = string;
			window.setTimeout(function(){ Ticker.chopString(element) }, 10);
		} else {
			window.setTimeout(function(){ Ticker.showNextItem()}, 50);
		}
	},
	
	typeString : function (element, pad_string) {
		var current_string = element.innerHTML;
		
		if (pad_string && pad_string.length > 0) {
			element.innerHTML = current_string + pad_string.substring(0, 1);
			window.setTimeout(function(){ Ticker.typeString(element, pad_string.substring(1)) }, 10);
		}
	}
}

$.fn.sliktab = function(options) {

	$("#content-slider-1").tinycarousel({ display: 1, duration: 1500, interval: true, intervaltime: 4000 });
	
	$(this).find(".tabs li a").click(function(e){
		
		// prevent standard behaviour to be executed
		e.preventDefault();
		
		/**
		 * Handle tab behaviours
		 */
		
		// get all tabs and remove class, afterwards set active class on clicked link
		$(this).parent().siblings().removeClass("active");
		$(this).parent().addClass("active")
		
		/**
		 * Handle tab container behaviours
		 */
		
		// hide all tabs
		$(".XMSPortfolioShowcaseItem").hide();
		
		// show the tab corresponding with the clicked link
		var tab_id = $(this).attr("href");
		$(tab_id).show();
		
		// activate the slider
		$(tab_id).find(".content-slider").each(function(){
			$(this).tinycarousel({ display: 1, duration: 1500, interval: true, intervaltime: 4000 });
		});
		
	});
	
}

