
	var page6 = Class.create(abstractOrderPage, {
		
		initialize: function($super, container, controller){
			$super(container, controller);
			this.instID = 'page6.' + parseInt(Math.random()*1000000);
			this.somethingsPlaying = false;
		},
		
		start:function(){
			var wpsRPC = new wps.rpc;
			wpsRPC.debug=true;
			wpsRPC.createCall('order', this.drawMusic.bind(this));
			wpsRPC.call('getMusic', 'orderID='+this.controller.orderID);
		},
		
		drawMusic: function(req){
			var music = req.responseJSON;
			if (!this.musicSelection){
				var player = new Element('div', {id:this.instID+"_player"});
				$('musicSelect').insert(player);
				player.hide();
				
				var flashvars = {
						width : 1,
						height : 1,
						autostart : false,
						fullscreen : false
				};
				
				var params = { allowfullscreen:'false', allowscriptaccess:'always', wmode:"transparent"};
				var attributes = { id:this.instID+"_player", name:this.instID+"_player"};
			    
				swfobject.embedSWF("/js/3rdparty/jwplayer/player4.swf", this.instID+"_player", flashvars.width, flashvars.height, "9.0.0", '/js/3rdparty/jwplayer/expressInstall.swf', flashvars, params, attributes);
			    $(this.instID+"_player").srcObj = this;
			    this.player = $(this.instID+"_player"); 
				this.musicSelection = new Element('div', {className:'musicSelection', id:this.instID+"_music"});
				$('musicSelect').insert(this.musicSelection);
				$A(music).each(
					(function(obj){
						var div = new Element('div', {className:"musicRow"});
						div.song = obj;
						div.songSelected = false;
						$('musicSelect').insert(div);
						
						var check = new Element('div', {className:'musicRowCheck'});
						check.song = obj;
						div.insert(check);
						
						var name =  new Element('div', {className:'musicRowName'}).update(obj.title);
						name.song = obj;
						div.insert(name);
						
						name.observe('click', this.selectMusic.bind(this));
						check.observe('click', this.selectMusic.bind(this));
						
						var play = new Element('div', {className:'musicPlay'}).update("[luisteren]");
						div.insert(play);
						play.playing = false;
						play.song = obj;
						play.observe('click', this.playSong.bind(this));

						if (obj.selected == "true"){
							this._selectMusic(name);
						}
						
					}).bind(this)
				)
				this.errorDiv = new Element('div', {className:'error'});
				this.musicSelection.insert(this.errorDiv);
				//this.musicSelection.update('');
			}
		},
		
		selectMusic : function(e){
			var elem = e.element();
			this._selectMusic(elem);
		},
		
		_selectMusic : function(elem){
			var song = elem.song;
			
			$$('.musicRowCheck').each(
				function(node){node.update('')}
			)
			$$('.musicRow').each(
				function(node){
					node = $(node);
					node.setStyle('background-color:#eee;color:#762137;');
					node.songSelected = false;
				}
			)
			
			var parent = $(elem.parentNode);
			parent.setStyle('background-color:#8C7A89;color:white;');
			parent.songSelected = true;
			parent.select('.musicRowCheck')[0].update('<img src="/images/check.png" class="check"/>');
			this.musicID = song.id;
		},
		
		playSong : function(e){
			var elem = e.element();
			var song = elem.song;
			
			if (this.somethingsPlaying){
				$$('.musicPlay').each(
					function(elem){
						elem.update("[luisteren]");
					}
				)
			}
			if (elem.playing){
				this.somethingsPlaying = false;
				this.player.sendEvent("PLAY", false);
				elem.playing = false;
				return;
			}
			
			var uri = 'http://' + window.location.hostname + "/upload/music/" + song.file; 
			this.player.sendEvent("LOAD", uri);
			this.player.sendEvent("PLAY", true);
			this.somethingsPlaying = true;
			elem.playing = true;
			elem.update("[stop]");
		},
		
		validate : function(){
			if (!this.musicID){
				this.errorDiv.update("Je hebt nog geen achtergrond-muziek gekozen, selecteer a.u.b. een achtergrond-muziekje");
				return false;
			}
			this.errorDiv.update("");
			return true;
		},
		
		save : function(callBack){
			var wpsRPC = new wps.rpc;
			wpsRPC.debug=true;
			wpsRPC.createCall('order', function(){});
			wpsRPC.call('updateOrder', 'orderID='+this.controller.orderID, 'ORDER_page_index='+ (this.controller.pageIdx), 'ORDER_music_id='+ this.musicID);			
			callBack();
		}
	
	})
