var numArtists = 6;
var numArtistsMax = 6;
var wOpened = 570;
var wClosed = 63;

var startId_param;
var activeId;
var videoInit;
var vidIdActive;

var artists = new Array();

artists[0] = new Object();
artists[0]["name"] = "Julian Rentzsch";
artists[0]["imgPath"] = "/img/all/create_reality/img/noFlash/StartScreen_JulianRentzsch.jpg";
artists[0]["imgOffset"] = 0;
artists[0]["filename"] = "rentzsch";

artists[1] = new Object();
artists[1]["name"] = "Trust";
artists[1]["imgPath"] = "../img/all/create_reality/img/noFlash/StartScreen_Trust.jpg";
artists[1]["imgOffset"] = 0;
artists[1]["filename"] = "trust";

artists[2] = new Object();
artists[2]["name"] = "Sena";
artists[2]["imgPath"] = "../img/all/create_reality/img/noFlash/StartScreen_Sena.jpg";
artists[2]["imgOffset"] = 0;
artists[2]["filename"] = "sena";

artists[3] = new Object();
artists[3]["name"] = "McBess";
artists[3]["imgPath"] = "../img/all/create_reality/img/noFlash/StartScreen_McBess.jpg";
artists[3]["imgOffset"] = 0;
artists[3]["filename"] = "mcbess";

artists[4] = new Object();
artists[4]["name"] = "Timo Müller";
artists[4]["imgPath"] = "../img/all/create_reality/img/noFlash/StartScreen_TimoMueller.jpg";
artists[4]["imgOffset"] = 0;
artists[4]["filename"] = "timo_mueller";

artists[5] = new Object();
artists[5]["name"] = "Kätlin Kaljuvee";
artists[5]["imgPath"] = "../img/all/create_reality/img/noFlash/StartScreen_KaetlinKaljuvee.jpg";
artists[5]["imgOffset"] = 0;
artists[5]["filename"] = "kaetlin_kaljuvee";






function changeArtist(idVar){	
	$(function(){
		for(var i=0; i<numArtists; i++){
			if(i == idVar){
				$("#createReality_"+i).show();
			}else{
				$("#createReality_"+i).hide();
			}
		}
    });
}
	
$(document).ready(function() {
	
	init();
	
	if($.flash.hasVersion(9.1)){
		initFlash();
		//initJS();
	}else{
		initJS();
	}
	
	
	function initJS(){
		// remove Fallback image
		$("#cr_va_wrapper").empty();
		
		// append divs
		xPos = 0;
		for(var i=0;i<numArtistsMax;i++){
			
			if(i<numArtists){
				// set enabled artist
				var appendStr = '<div class="cr_va_item enabled' + ((activeId == i) ? ' active' : '') + '" style="width: 570px; z-index:'+i+'; left: '+xPos+'px" >';
				appendStr += 	'	<img ' + ((i != 0) ? 'class="offset" ' : '') + 'src="'+ artists[i]["imgPath"] +'" alt="'+ artists[i]["name"] +' creates Reality" width="570" height="320">';
				appendStr += 	'	<div class="txtWrapper"'+ ((activeId != i) ? ' style="opacity:0; display: none;"' : '') +'>';
				appendStr += 	'		<h1>create reality</h1>';
				appendStr += 	'		<h4>by '+ artists[i]["name"] +'</h4>';
				appendStr += 	'		<img class="iconPlay" src="../img/all/create_reality/img/noFlash/iconPlay.png" alt="icon play" width="26" height="25">';
				appendStr += 	'		<p>Click to play</p>';
				appendStr += 	'	</div>';
				appendStr += 	'	<img class="iconPlus" '+ ((activeId == i) ? ' style="opacity:0; display: none;"' : '') +'src="../img/all/create_reality/img/noFlash/iconPlus.png" alt="plus icon" width="26" height="25">';
				appendStr += 	'</div>';
				$("#cr_va_wrapper").append(appendStr);
			}else{
				// set disabled artist
				$("#cr_va_wrapper").append('<div class="cr_va_item" style="left: '+xPos+'px; z-index:'+i+'"><img ' + ((i != 0) ? 'class="offset" ' : '') + 'src="/img/all/create_reality/img/noFlash/teaser_new_0'+i+'.jpg" alt="coming soon" width="100" height="320"><p class="mini">soon</p></div>');
			}
			
			xPos += (activeId == i) ? wOpened : wClosed;
		}
		
			
		
		$(".cr_va_item.enabled").click(function() {
			var id = $(this).index();
			if(vidIdActive != id){
				if(vidIdActive != -1){
					removeVideo(vidIdActive);	
				}
				
				if(id != activeId){
					
					xPos = 0;
					
					var oldItem = $(".cr_va_item:eq("+ activeId +")");
					$(oldItem).find("div.txtWrapper").stop().animate({opacity: 0}, {duration: 200, easing: 'easeInOutCubic', complete: function() { $(this).hide(); } });
					$(oldItem).find(".iconPlus").stop().delay(300).show().animate({opacity: 1}, {duration: 200, easing: 'easeInOutCubic'});
					
					for(i=0;i<numArtistsMax;i++){
						var currItem = $(".cr_va_item:eq("+ i +")")
						
						if(i < id){
							//einklappen
							$(currItem).stop().animate({left: xPos}, {duration: 500, easing: 'easeInOutCubic'});
							xPos += wClosed;
						}
						
						if(i == id){
							$(currItem).stop().animate({left: xPos}, {duration: 500, easing: 'easeInOutCubic'});
							$(currItem).find("div.txtWrapper").stop().delay(300).show().animate({opacity: 1}, {duration: 200, easing: 'easeInOutCubic'});
							$(currItem).find(".iconPlus").stop().animate({opacity: 0}, {duration: 200, easing: 'easeInOutCubic', complete: function() { $(this).hide(); } });
							xPos += wOpened;
						}
						
						if(i > id){
							$(currItem).stop().animate({left: xPos}, {duration: 500, easing: 'easeInOutCubic'});
							xPos += wClosed;
						}
						
						
					}
					
					activeId = id;
					changeArtist(id);
					
				
				}else{
					
					if(isHome){
						
						if(isOverallHome) {
							window.location = "/en/create_reality.php?vInit=true&startId="+id;
						}else{
							window.location = "create_reality.php?vInit=true&startId="+id;
						}
						
					}else{
						// BUILD VIDEO
						var artist = artists[id]["filename"];
						
						$("img.offset", this).hide();
						$("div.txtWrapper", this).hide();
						
						vidIdActive = id;
						$(this).append('<div class="videoWrapper"></div>');
						$(".videoWrapper", this).html('<video class="video" width="570" height="321" controls autoplay><source src="../img/all/create_reality/vid/'+artist+'/'+artist+'.mp4"  type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'><source src="../img/all/create_reality/vid/'+artist+'/'+artist+'.webm" type=\'video/webm; codecs="vp8, vorbis"\'><source src="../img/all/create_reality/vid/'+artist+'/'+artist+'.ogg"  type=\'video/ogg; codecs="theora, vorbis"\'>Your browser does not support the <code>video</code> element.</video>');
					
					}
					
				}
				
			}
		}); // end click
		
		// (videoInit) { autostart video }
		if(videoInit){
			$(".cr_va_item:eq("+activeId+")").click();
		}
	}
	
	
	function init(){
		startId_param = gup( 'startId' );
		activeId = (startId_param == "") ? Math.round ( Math.random() * (numArtists-1)) : startId_param;
		videoInit = ( gup( 'vInit' ) == "true") ? true : false;
		vidIdActive = -1;
		if(!isHome && !isOverallHome){
			for(var i=0; i<numArtists; i++){
				if(i != activeId){
					$("#createReality_" +i).hide();
				}
			}
		}
	}
	
	function initFlash(){
		//alert("initFlash");
		$("#cr_va_wrapper").flash({
			swf: '/img/all/create_reality/swf/rotring_createReality.swf',
			bgcolor: "#FFFFFF",
			width: 890,
			height: 320,
			allowFullScreen: true,
			allowscriptaccess: 'always',
			wmode: 'opaque',
			scale: 'noscale',
			flashvars: {
				xmlPath : '/img/all/create_reality/xml/data_en_createReality.xml',
				fv_lang : 'EN',
				startId : activeId,
				videoInitOnLoad : videoInit,
				isHome : isHome,
				isFirstHome : isOverallHome
			},
			attributes: {}  
		});
	}
	
	
	function removeVideo(_vidIdActive){
		$(".cr_va_item:eq("+vidIdActive+")").find("img.offset").show();

		$(".cr_va_item:eq("+vidIdActive+")").find(".videoWrapper").empty();
		$(".cr_va_item:eq("+vidIdActive+")").find(".videoWrapper").remove();
		vidIdActive = -1;
		
	}
	
	
	function gup( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return results[1];
	}

	
	
	if(!isHome && !isOverallHome){
		$("a.julianRentzsch,a.trust,a.sena,a.mcbess,a.timoMueller,a.kaetlinKaljuvee").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'overlayShow'	:	false,
			'autoScale'		:	true,
			'overlayShow'	:	true,
			'overlayOpacity':	0.7,
			'overlayColor'	:	'#333',
			'centerOnScroll':	true,
			'cyclic'		:	true,
			'transitionIn'	:	'none',
			'transitionOut'	:	'none',
			'titlePosition'	:	'inside',
			'titleFormat'	: 	function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-inside">' + title + ' (' + (currentIndex + 1) + '/' + currentArray.length + ')</span>';
			}
		});
	}
	
	
	
});
