/* global vars */
var myScroll;

$(document).ready(function() {
	// fullscreen bg-image
	var FullscreenrOptions = {  width: 1400, height: 800, bgID: '#bgimg' };
	jQuery.fn.fullscreenr(FullscreenrOptions);
	$('#semitransbg').fadeTo(0,0.85);
	showroomList();
	ContentAnimation();
	ArtistGalleryAnimation();
	ArtistOverviewAnimation();	
	if(screen.width <= 1024){
		// myScroll = new iScroll('contentwrapper');
	}
});


function welcome(lang){
	var rebuilddelay = 5000;
	var textpart1;
	var textpart2;
	if(lang == '0'){
		textpart1 = "Willkommen bei";
		textpart2 = "Allegro Art Agency";
	} else {
		textpart1 = "Welcome to";
		textpart2 = "Allegro Art Agency";
	}

	// hide content containers
	$('#bgimg').hide();
	$('#sidebar').hide();
	$('#contentwrapper').hide();
	$('#semitransbg').css('left','-1020px');
	
	$('#bgimg').delay(500).fadeIn(1000);
	
	
	// adding contents
	$('#pseudobody').append('<h1 id="textpart1" class="introtext"><span>'+textpart1+'</span></h1>');
	$('#pseudobody').append('<h1 id="textpart2" class="introtext"><span>'+textpart2+'</span></h1>');
	$('#textpart1 span').hide();
	$('#textpart2 span').hide();
	$('#textpart1 span').delay(1500).fadeIn(1000);
	$('#textpart2 span').delay(2000).fadeIn(1000);
	$('#textpart1').delay(1500).animate({left: '300px'}, 5000,'linear');
	$('#textpart2').delay(2000).animate({left: '100px'}, 5000,'linear');
	$('#textpart1 span').delay(2700).fadeOut();
	$('#textpart2 span').delay(3000).fadeOut('slow',removeWelcomeH1);
	
	
	// build normal site
	$('#semitransbg').delay(rebuilddelay+1500).animate({left: '-100px'}, 1600, 'easeOutExpo');
	$('#sidebar').delay(rebuilddelay+1500).fadeIn();
	$('#contentwrapper').delay(rebuilddelay+2200).fadeIn(800);
	$('#navi li').each(function(i){
		$('#navi li:eq('+i+')').hide();
		$('#navi li:eq('+i+')').delay(2500+rebuilddelay+i*100).fadeIn(600);
	});
}

function removeWelcomeH1(){
	$('#textpart1').remove();
	$('#textpart2').remove();
}

function ContentAnimation(){
	$('#ContentAnimation').hide();
	$('#ContentAnimation').delay(300).fadeIn();
}

function showroomList(){
	var j=0;
	var items = new Array();
	$('#ShowroomList li').each(function(i){
		j++;
		if(j == 3){
			$('#ShowroomList li:eq('+i+')').addClass('showroomlist_li_last');
			j=0;
		}
		$('#ShowroomList li:eq('+i+')>div').hide();
		items.push(i);
	});
	
	items = shuffle(items);
	
	for(var i=0; i< items.length; i++){
		$('#ShowroomList li:eq('+i+')>div').delay(100*(items[i]+1)).fadeIn(350);
	}

}

function ArtistGalleryAnimation(){
	var items = new Array();
	$('div.artist_gallery div').each(function(i){
		$('div.artist_gallery div:eq('+i+')>img').hide();
		items.push(i);
	});
	
	items = shuffle(items);
	
	for(var i=0; i< items.length; i++){
		$('div.artist_gallery div:eq('+i+')>img').delay(100*(items[i]+1)).fadeIn(550);
	}

}

function ArtistOverviewAnimation(){
	$('div.artist_overview img').hide();
	$('div.artist_overview h2').hide();
	var items = new Array();
	$('div.artist_overview').each(function(i){
		var imgs = new Array();
		$('div.artist_overview:eq('+i+') h2').delay(100*i).fadeIn();
		$('div.artist_overview:eq('+i+') img').each(function(j){
			$('div.artist_overview:eq('+i+') img:eq('+j+')').delay(500+100*i+j*150).fadeIn();
		});	
	});
}

function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};
