var tags, homepage, hintervalID, startC, endC, tintervalID=0, hintervalID=0, zindex=10;
var IE7 = (navigator.userAgent.toLowerCase().indexOf('msie 7') != -1) ? true : false;  
//animateHome
window.addEvent('domready', function() {	
	
	homepage	= new Home();
	tags		= new TagCloud();	
	thema		= new Thema();
	
});

/* CLASS HOME */

var Home = new Class({

initialize: function(){
	this.bwCoords		= $('basis_wrapper').getCoordinates();
	this.setSearchBox();
	this.setSearchDropDown();	
	this.getThemes();
	this.speed		= 300;
	this.amplifier	= 300;
},
 

setSearchBox: function(){
	
	$('width').set("value", this.bwCoords.width);//set the form values to the current window client coords
	$('height').set("value", this.bwCoords.height);
	
	this.randomX	= this.randN(50, 120);//this.randN($('dock_search_container').getSize().x+100, $('dock_search_container').getSize().x+(this.bwCoords.width/2-$('dock_search_container').getSize().x)); //position the searchbox randomly Math.floor(Math.random()*(this.bwCoords.width-600));
	this.randomY	= this.randN(225, 300);//(this.bwCoords.height/2)+Math.floor(Math.random()*(this.bwCoords.height/2));170, (this.bwCoords.height/2)-75
	
	//$('dock_search_container').set('morph', {duration: 1300, transition: Fx.Transitions.Expo.easeOut}).morph({top: this.randomY, left:this.randomX, opacity:[0,1]});	
	$('dock_search_box').set('morph', {duration: 1300, transition: Fx.Transitions.Expo.easeOut}).morph({top: this.randomY, left:this.randomX, opacity:[0,1]});	
	


	
		$('zoeksuggestie').addEvent('click', function(){
			tags.toggle();
		});
			
		$('dock_search_box').addEvent('click', function(){
			zindex++;
			 this.setStyle('z-index', zindex);
		}).makeDraggable({handle: $('search-move')});	
		
		/*
		$('dock_search_box').addEvents({
	    'mouseenter': function(){
		 zindex++;
		 this.setStyle('z-index', zindex);
		 
		 $('tagcloud').setStyle('z-index', zindex+1);
		 //tags.moveOut();
		
	    },
	    
	    'mouseleave': function(){
	    //tags.moveIn(550);
	    },
	    
	    'mousedown': function(){
	    //tags.moveIn(550);
	    }
	    
	}).makeDraggable({handle: $('search-move')});	
		*/
		
},

setSearchDropDown: function(){
	
	$('search_opties_home').addEvent('click', function(){
		
		dsb = $('dock_search_box').getCoordinates();
		omhoog			= 504-dsb.top;
		delay			= (omhoog<0)? 200:0;	
		//console.log(dock_search_box.top+" - "+omhoog);
		if(omhoog<0){
		$('dock_search_box').set('morph', {duration: 150, transition: Fx.Transitions.Expo.easeOut}).morph({'top': 200});
		}
		
		(function(){
		zindex++;
		//alert(zindex); 
		pos		= $('dock_search_elements_home').getPosition(); //returns {x: 100, y: 500};
		theight = ($('search_opties_extended').getStyle('height')=="0px") ? 220 : 0;
		cursor = ($('search_opties_extended').getStyle('height')=="0px") ? 'n-resize' : 's-resize';
		opacity = ($('search_opties_extended').getStyle('height')=="0px") ? 1 : 0;
		
		$('search_opties_home').setStyles({'background': (opacity==1) ? 'url(/images/dock/opties_uit.gif) no-repeat' : 'url(/images/dock/opties.gif) no-repeat', 'cursor': cursor});		
		$('search_opties_extended_line').setStyles({'opacity': opacity, 'display':'block', 'top': (pos.y-101), 'left': (pos.x+266)});	
		$('search_opties_extended').setStyles({'opacity': (opacity==1)?0:1, 'display':'block', 'width': 358, 'top': (pos.y-90), 'left': (pos.x+30)});	
		$('search_opties_extended').set('morph', {duration: 300, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity': opacity,  'height': theight});
		
		}).delay(delay); 
		
	});


},	

getThemes: function(){

	new Request.HTML({'url': '/homepage', update: 'interface',
		
		onSuccess: function(html, elements) {
			homepage.themeActions();	
		}
		
		}).post({top: this.bwCoords.top, width:this.bwCoords.width, height: this.bwCoords.height});
	
},

themeActions: function(){
	
	$('interface').getElements('.preview_link').each(function (hhref, i){
	hhref.oldhref	= hhref.href;
	hhref.href		= "#";
	})	
	

	$('interface').getElements('.preview_canvas_theme').addEvents({
	
	'click': function(){
		animateHome.stop();
		$('preloader').set('morph', {duration: 250, transition: Fx.Transitions.Expo.easeOut}).morph({opacity:[0,1]});
		this.getParent().state	= "out";
		this.bwCoords		= $('basis_wrapper').getCoordinates();
		delink = this.getElement('.preview_link').oldhref;
		
		(function(){
		document.location.href=	delink;
		//console.log(delink);
		}).delay(150); 
		
		
		this.getParent().getElements('.preview_canvas_small').each(function (preview, i){	//uiteenvliegen van previews
			duration	= 100+(i*30);	
			preview.set('morph', {duration: duration, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity': [1,0]});			
		});
		
		$('interface').getElements('.preview_canvas_theme').each(function (preview, i){
			preview.getParent().set('morph', {duration: 800, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':[1,0]});			
			});
		
		$('dock_search_box').set('morph', {duration: 800, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':0});	
		//homepage.transform(this, "huge");
		
	},		
		
	'mouseenter': function(){
		animateHome.stop();
		zindex++;		
		this.getParent().setStyle("z-index",zindex);//verhoog z-index
		//tags.Disappear();	// zoekbox verbergen
							//animatie background stoppen 
		
		homepage.transform(this, "medium");
		
		//alle andere themes verbergen
		current_id = this.id
		$('interface').getElements('.preview_canvas_theme').each(function (preview, i){
		op = (current_id==preview.id) ? 1 : 0.1;
		
		preview.getParent().set('morph', {duration: 150, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':op});			
		});
		
		
	if(this.getElement('.preview_image')!=null){
		var tmp = this.id.split('_');
		
		
		var set_id = tmp.pop();
		var preview_frames = $('preview_frames_' + set_id).innerHTML;
		var preview_frame_list = oibGetPreviewFrameUrls(preview_frames);
		//alert(preview_frame_list + " - " +preview_frames);
		
		if (preview_frame_list!="" && preview_frame_list.length > 0) {
			animeer = new Animate(this, preview_frame_list);
		}
		
	}
	/*
	this.getParent().getElements('.preview_canvas_small').each(function (preview, i){	//uiteenvliegen van previews
		duration	= 100+(i*30);	
		preview.op 	= preview.style.opacity;
		if(preview['delta_top']==null){
		randomnumber				= Math.floor(Math.random()*300)-150;
		
		preview['delta_top']		= parseInt(preview.getStyle("top"));
		//console.log(randomnumber+" - "+preview['delta_top']);
		preview['amplifier_top']	= randomnumber+preview['delta_top'];//(preview['delta_top']<0) ? -100+preview['delta_top']+randomnumber : preview['delta_top']+100+randomnumber
		}
		
		if(preview['delta_left']==null){
		randomnumber				= Math.floor(Math.random()*500)-250;
		preview['delta_left']		= parseInt(preview.getStyle("left"));
		preview['amplifier_left']	= randomnumber+preview['delta_left'];//(preview['delta_left']<0) ? -100+preview['delta_left']+randomnumber : preview['delta_left']+100+randomnumber
		}
		
		preview.set('morph', {duration: duration, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity': 1,  'left': preview['amplifier_left'], 'top':preview['amplifier_top']});	
		
	});
	*/
	
	},
	
	
	
	'mouseleave': function(){
		if(this.getParent().state!="out"){
		
		homepage.transform(this, "small");
		current_id = this.id
		
		$('interface').getElements('.preview_canvas_theme').each(function (preview, i){
			op = (current_id==preview.id) ? [1,1] : [0,1];
			preview.getParent().set('morph', {duration: 250, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':op});		
		});
		
		

		$clear(intervalID);		
		animateHome.start();
		
		}
	}
	
	});
	$('search_trefwoord_home').focus();
	
	animateHome		= new Animatehome();
	$('interface').addEvent('click', function(){
	if(tags.status==1){
	tags.moveIn();	
	}
	
	animateHome.toggle();
	});

},


transform: function(obj, to_state){
	obj['dims']		= (obj['dims']==null) ? obj.getCoordinates(obj.getParent()) :obj['dims'];
	
	switch (to_state) {
	
	case "small": 
		//this.dims		= obj.getCoordinates(obj.getParent());
		obj.set('morph', {duration: 300, transition: Fx.Transitions.Expo.easeOut}).morph({
			'top':(obj['dims'].top)+'px',
			'left':(obj['dims'].left)+'px',
			'padding':'7px',
			'background-image':'url(/images/background_preview_small.png)', 
			'height': '162px', 
			'width': '144px'});
		//'opacity': 0.9
		//obj.getElement('.preview_arrow').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'left': '154	px', 'top': '50px'});	
		obj.getElement('div.preview_white').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'background-color':['#2c2a26', '#fff'], 'padding':'7px', 'height': '133px', 'width': '130px'});//
		obj.getElement('div.preview_thema').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'font-size': '9px', 'width': '126px'});
		obj.getElement('div.preview_title').setStyles({'font-weight': 'normal'}).set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'width':'125px', 'text-align': 'center', 'padding':'0px 0px 10px','font-size': '11px', 'color':'#272624', 'font-style': 'normal'});//'font-weight': 'normal', 
		obj.getElement('div.preview_description').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':[1,0]});
		obj.getElement('.preview_image').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'height': '96px', 'width': '130px'});
		//obj.getElement('.preview_link').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'display':'none', 'opacity':[1,0]});
		break; 
	
	case "medium": 
		obj.set('morph', {duration: 300, transition: Fx.Transitions.Expo.easeOut}).morph({
			'top':(obj['dims'].top-87)+'px',
			'left':(obj['dims'].left-65)+'px',
			'padding':'10px',
			'background-image':'url(/images/background_preview_medium.png)', 
			'height': '317px', 
			'width': '270px'});
		//,'opacity': [0,1]
		//obj.getElement('.preview_arrow').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'left': '280px', 'top': '140px'});	
		obj.getElement('div.preview_white').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'padding':'10px','height': '297px', 'width': '250px', 'background-color': '#fff'});	
		obj.getElement('div.preview_thema').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'font-size': '11px', 'width': '250px'});
		obj.getElement('div.preview_title').setStyles({'font-weight': 'bold'}).set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'width':'249px', 'text-align': 'left', 'padding':'0px 0px 10px',  'font-size': '13px', 'color':'#272624'});//'font-weight': 'normal', 'font-style': 'italic'
		obj.getElement('div.preview_description').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':[0,1]});
		obj.getElement('.preview_image').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'height': '185px', 'width': '249px'});
		//obj.getElement('.preview_link').set('morph', {duration: this.speed, transition: Fx.Transitions.Expo.easeOut}).morph({'display':'block', 'opacity':[0,1]});
		break;
		
	
	}
},

randN: function(min, max){
	
	//return ( Math.floor ( Math.random ( ) * n + 1) );
	 var argc = arguments.length;
	    if (argc === 0) {
	        min = 0;
	        max = 2147483647;    
	        }  
	
	return Math.floor(Math.random() * (max - min + 1)) + min;

		}

});



/* CLASS ANIMATEHOME */

var i=0;
Animatehome	= new Class({

	initialize: function(obj, source) {
	i=0;
	this.obj	= obj
	this.source	= source
	this.images	= new Array();
	this.films 	= new Array("prins_bernhard", "suriname", "amsterdam_bevr_feest_kl", "vanderzeijden", "brigade_flitsen");
	this.staat	= 1;
	this.start();
	this.nummer	= this.randn(this.films.length-1);		
	},

randn: function(n){
return ( Math.floor ( Math.random ( ) * n + 1 ) );
	},	

toggle: function(){
	
	if(this.staat==1){
	this.stop();	
	}else{
	this.start();	
	}	

},

stop: function(){
  $clear(hintervalID);
  this.staat	= 0;
  //$('preview_image_home').setStyle('opacity', '0.1');
},

start: function(){
	$clear(hintervalID);
	hintervalID 	= this.rotateImage.periodical(250, this);
	this.staat	= 1;
	//$('preview_image_home').setStyle('opacity', '1');
},

rotateImage: function(){
	
	i=(i==110) ? 1 : i;	
	ni = i+1
	ii = (ni<10)? "0"+ni: ni;
	ii = (ii<100)? "0"+ii: ii;	
	begin = new Date().getTime();
	this.images[i] = new Element('img', {		    
		    'src': '/images/previews/'+this.films[this.nummer]+'-'+ii+'.jpg'
		});
	//this.images[i].loaded=0;
	
	this.images[i].onLoad = this.changeSource();

	i++;

	},
	
changeSource: function(){

//this.images[i].loaded	= 1;
afbeelding 				= ($('preview_image_home')) ? $('preview_image_home') : this.obj.getElement('.preview_image_home');
afbeelding.src			= this.images[i].src;
this.images[i].destroy();
}	
	
	
});

/* CLASS TAGCLOUD */


var TagCloud = new Class({

	initialize: function(){
	bounds			= $('tagcloud').get("rel").split("|"); 
	this.min		= bounds[0];
	this.max		= bounds[1];
	this.fontmin	= 9
	this.fontmax	= 40;
	this.status		= 0;
	this.spread 	= this.max - this.min;
	this.step 		= (this.max - this.min) / (this.spread);

	//console.log($('tagcloud').getElements('a'));
	$('tagcloud').getElements('a').addEvents({
		'click': function(){
		$('formulier').submit();
		return false;
		},
	
		'mouseenter': function(){
		//$clear(tintervalID);
	    this['opacity']=this.getStyle("opacity");
		this.setStyle("opacity","1");
		$('search_trefwoord_home').value=this.innerHTML;
		},
	    
	    'mouseleave': function(){
	   // tags.moveIn(550);
	    this.setStyle("opacity",this['opacity']);
	    }
	    
		}).setStyle("opacity",0);
	},
	
moveFollow: function(){
		
		offsety	= endC.top-startC.top;//console.log();
		offsetx	= endC.left-startC.left;//console.log();
		
		$('tagcloud').getElements('a').each(function (href, i){	
		duration		= 100+(i*100);
		toOpacity		= href.getStyle('opacity');
		left			= parseInt(href.getStyle('left'))+offsetx;
		bottom			= parseInt(href.getStyle('bottom'))-offsety;
		href.set('morph', {duration: duration, transition: Fx.Transitions.Expo.easeOut}).morph({'left':left+'px', 'opacity':[0,toOpacity], 'top': bottom+"px"});	
		});
	},	

	
toggle: function(interval){
		if(this.status==0){
			this.moveOut();
			this.status=1;	
		}else{
			this.moveIn();
			this.status=0;	
		}
		},
			

moveOut: function(){
		
		
		tintervalID		= (function(){
		minop			= 0.25;//bounds[0];
		maxop			= 1;//bounds[1];
		spreadop 		= maxop - minop;
		stepop			= (maxop - minop) / 20;
		dsb				= $('dock_search_box').getCoordinates();//document.body
		dsbl			= dsb.top+dsb.height;//document.bodydsb.top+
		
		
		
		$('tagcloud').getElements('a').each(function (href, i){
		duration		= 100+(i*10);	
		
		randomb			= Math.floor(Math.random()*20)-10;
		rij				= Math.floor(i/3);
		interlinie		= 38-(rij*2)
		htop			= dsbl+(rij*interlinie);
		prefleft		= (i==0 || i%3==0) ? 0 : prefleft+prefref.getCoordinates().width;
		prefref			= href;
		randomleft		= Math.floor(Math.random()*20)-10;
		left			= parseInt(dsb.left)+(prefleft+randomleft);
		opacity			= 1-(i*stepop);
		href.set('morph', {duration: duration, transition: Fx.Transitions.Expo.easeOut}).morph({'left':left+'px', 'opacity':opacity, 'top':htop+"px"});	
		});	
		
		
		
		
		$('interface').getElements('.preview_canvas_theme').each(function (preview, i){
		//preview.getParent().set('morph', {duration: 200, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':0.1});			
		preview.getParent().setStyle("display","none");			
		});
		
		animateHome.stop();
		}).delay(250); 
		
	},


moveIn: function(interval){
		interval		= (interval=="") ? 550 : interval;
		  
		
		tintervalID		= (function(){
		
			dsb				= $('dock_search_box').getCoordinates(document.body);
			$('tagcloud').getElements('a').each(function (href, i){
			duration		= 100+(i*10);	
			left 			= dsb.left
			htop 			= dsb.top+dsb.height;
			href.set('morph', {duration: duration, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':[1,0], 'top': htop+"px"});	
			})
		
			document.body.getElements('.preview_canvas_theme').each(function (preview, i){
			//preview.getParent().set('morph', {duration: 200, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':[0.1,1]});			
				preview.getParent().setStyle("display","block");	
			});
		
			animateHome.start();	
		}).delay(interval); 
		
		
	},
	
Disappear: function(){
			//this.moveIn(0);
			$('dock_search_box').set('morph', {duration: 400, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':0.1});	
		},
		
Appear: function(){
			$('dock_search_box').set('morph', {duration: 400, transition: Fx.Transitions.Expo.easeOut}).morph({'opacity':1});			
		}


});


