var QTOK = false;
var currentCue = null ;

document.observe("dom:loaded", function() {
	loadAccordions();
});


Event.observe(window, 'load', init);

function hideOverlay() {
	if($('bio').visible())
		new Effect.Fade('bio');			
}


function init() {
	initMusicLinks();
	initVideoLinks();
	$('bio_close_toggle').observe('click', hideOverlay);
	$('about_link').observe('click', showAbout);
}


function showAbout() {
	new Effect.Appear('bio');
}

function showOnset() {
	new Effect.Appear('onset', {
		afterFinish: function() {
			jQuery('.scrollable-content').jScrollPane({
				scrollbarMargin: 12
			});
		} 
	});	
}

function hideContact() {
	new Effect.Fade('contact');
}

function initVideoLinks() {
	var qts = $$('.qtvid');
	qts.each(function(link) {
		
		var videoThumb = link;
		var video_id = link.id;
		var $videoThumbImage = jQuery(videoThumb).find('img');
		
		jQuery.getJSON('http://vimeo.com/api/v2/video/' + video_id + '.json?callback=?', function(data) {
			$videoThumbImage.attr('src', data[0].thumbnail_medium);
		});
			
		Event.observe(link, 'click', playVideo.bindAsEventListener(this));
	});
	Event.observe('mode_close_toggle', 'click', endQTmode);
}

function initMusicLinks() {
	var mp3Links = jQuery('li.audio');
	mp3Links.each(function() {
		jQuery(this).click(load_cue);
	});
}

function setup_cue_info(cue) {
   //$('set').update(cue.set);
   //$('cue').update(cue.cue);
}


function load_cue(event){
	
	var cue = jQuery(event.target);	
    var thisCue = 'music.' + cue.attr('id');
    var url='';

	if(cue.hasClass('playing')) {
  		toggle_play();
  		cue.removeClass('playing').addClass('paused');
  		return;
  	}
  	
  	if(cue.hasClass('paused')) {
  		toggle_play();
  		cue.removeClass('paused').addClass('playing');
  		return;
  	}
  
  			thisCue = thisCue.evalJSON();
     		
			setup_cue_info(thisCue);
     		
			url = "portfolio/" + thisCue.file;
			
			jQuery('#qta').jPlayer("setMedia", {
                mp3: url
            }).jPlayer("play");
  
	/*
  	new Effect.Fade('now_playing', { 
  		afterFinish: function() {
   			//$('cue_waiting').show();
     		
			thisCue = thisCue.evalJSON();
     		
			setup_cue_info(thisCue);
     		
			url = "portfolio/" + thisCue.file;
			
			jQuery('#qta').jPlayer("setMedia", {
                mp3: url
            }).jPlayer("play");
			
			//init_mp3();		
     	}
     });
	*/
     
     $$('.paused').invoke('removeClassName', 'paused');
     $$('.playing').invoke('removeClassName', 'playing');
  
     cue.addClass('playing');

}

function toggle_play(event){	      
    var audioPlayer = jQuery('#qta');
    if (myMov.GetRate()==1) { 
		audioPlayer.jPlayer("stop");
	}
    else {
		audioPlayer.jPlayer("pause", 0);
    }
}

function loadAccordions() {
	var  winHeight=document.viewport.getDimensions().height;
	if (winHeight>620){
		topMargin = parseInt(((winHeight-620)/2)-20)+'px';
		$('content').setStyle({
			marginTop: topMargin,
			marginLeft:'auto',
			marginRight:'auto'
		});
	}
	else{
		$('content').setStyle({
		  	marginTop: '0px',
			marginLeft: 'auto',
			marginRight: 'auto'
		});
	}
    var portAccordion = new accordion('portfolio-accordion', {
    	classNames : {
    		toggle : 'accordion_toggle',
    		toggleActive : 'accordion-toggle-active',
    		content : 'accordion-content'
    	}    
    });
    
    portAccordion.activate($$('#portfolio-accordion .accordion_toggle')[0]);
    
    jQuery('.scrollable-content').jScrollPane({
		scrollbarMargin: 12,
		autoReinitialise: true
	});
    
};


// stop_music function called as accordion callback
function stop_music() {
	var audioPlayer = jQuery('#qta');
	audioPlayer.jPlayer("stop");	
}

// music portfolio objects
var music= { 			"reel" : [  
                                    { "file" : "reel/Sunset.mp3",  
                                      "set":"Film Score Reel" , 
                                      "cue":"Sunset",
                                      "format" : ".mp3",
                                      "length" : "2:21"
                                      
                                       },
                                    
                                    { "file" : "reel/Thriller-Action-Adventure.mp3",
                                      "set"   :   "Film Score Reel" , 
                                      "cue":"Thriller / Action-Adventure",
                                      "format" : ".mp3",
                                      "length" : "1:53"
                                      
                                       },
                                       
                                    { "file" : "reel/TV Theme.mp3", 
                                      "set"  : "Film Score Reel",
                                       "cue":"TV Theme",

                                      "format" : ".mp3",
                                      "length" :	"2:21"
                                      
                                       },
                                       
                                    { "file" : "reel/Romance-Tragedy.mp3", 
                                      "set"   :   "Film Score Reel" , 
                                      "cue":"Romance / Tragedy",
                                      "format" : ".mp3",
                                      "length" :	"3:16"
                                      
                                       }                                  ],                               
                        "MFTMAY": [   
                                    { "file" :   "MFTMAY/Introitus.mp3",  
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue": "Introitus",
                                      "format" : ".mp3",
                                      "length" : " 0:57"
                                      
                                       },
                                    
                                    { "file" : "MFTMAY/Pastorale.mp3",
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue": "Pastorale",
                                      "format" : ".mp3",
                                      "length" : " 0:57"
                                      
                                       },
                                       
                                    { "file" : "MFTMAY/Dusk%20to%20Night.mp3", 
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue": "Dusk to Night",
                                      "format" : ".mp3",
                                      "length" :	"1:08"
                                      
                                       },
                                       
                                    { "file" : "MFTMAY/The%20Alley.mp3", 
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue": "The Alley",
                                      "format" : ".mp3",
                                      "length" :	"2:22"
                                      
                                       },
                                    
                                    { "file"   : "MFTMAY/The%20Little%20One.mp3",
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue"  : "The Little One",
                                      "format" : ".mp3",
                                      "length" : "1:53"
                                      
                                       },
                                       
                                    { "file"   : "MFTMAY/The%20Cocktail%20Party.mp3", 
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue"  : "The Cocktail Party",
                                      "format" : ".mp3",
                                      "length" : "2:04"
                                      
                                       },
                                       
                                    { "file"   : "MFTMAY/Violence.mp3", 
                                      "set"  : "<i>My Friends Told Me About You</i> Score" ,  
                                      "cue"  : "Violence",
                                      "format" : ".mp3",
                                      "length" :	"2:28"
                                      }
                                      
                                    ],                               
             "OLTA"       : [       { "file"   : "OLTA/Introduction.mp3",  
                                      "set"  : "OLTA Installation" , 
                                      "cue"  : "Introduction",
                                      "format" : ".mp3",
                                      "length" : "0:57"
                                      
                                       },
                                    
                                    { "file"   : "OLTA/Interlude.mp3",
                                      "set"  : "OLTA Installation" , 
                                      "cue"  : "Interlude",
                                      "format" : ".mp3",
                                      "length" : "0:57"
                                      
                                       },
                                       
                                    { "file"   : "OLTA/Background.mp3", 
                                      "set"  : "OLTA Installation" , 
                                      "cue"  : "Background",
                                      "format" : ".mp3",
                                      "length" : "1:08"
                                      
                                       }
									],                               
             "Interpol"       : [   { "file"   : "music/pioneer.mp3",  
                                      "set"  : "",
                                      "cue"  : "Pioneer to the Falls (Orchestral)",
                                      "format" : ".mp3",
                                      "length" : "0:57"
                                      
                                       },
									{ "file" : "music/The Wall.mp3",
										"set" : "",
										"cue" : "The Wall",
										"format" : ".mp3",
										"length" : "5:28"
									
										}]
  
  
      } //music portfolio


//music display functions

function list_cues(proj) {
	var  myCues= music[proj];
	document.write('<ul>');
    myCues.each(function(t,r) {
    	document.write('<li class="audio" id="'+proj+'['+r+']'+'">'+t.cue+' </li>');
	});
	document.write('</ul>');
}



// video display functions
function playVideo(event) {
	var element = event.element();
	var myVid = element.up().id;
	var videoThumb = element.up();
		
	var video_id = videoThumb.id;	
	var qtVidWidth = jQuery('#' + myVid).data('qtvid-width');
	var qtVidHeight = jQuery('#' + myVid).data('qtvid-height');

	var qtWidth = '600';
	var qtHeight = '400';
	if(qtVidWidth != undefined && qtVidHeight != undefined) {
		qtWidth = qtVidWidth;
		qtHeight = qtVidHeight;
		jQuery('#qtv').css({
			width: qtVidWidth + 'px',
			height: qtVidHeight + 'px',
			marginTop: (452 - qtHeight ) / 2 
		});
	}
	
	
	var embedCode = '<iframe src="http://player.vimeo.com/video/' + video_id + '?title=0&amp;byline=0&amp;portrait=0&amp;color=7d7d7d" width="' + qtVidWidth + '" height="' + qtHeight + '" frameborder="0"></iframe>';

	new Effect.Appear('mode_overlay_1', {
		to: .6,
		duration: .5
	});
	
	new Effect.Appear('mode_overlay_2', {
		duration: .5, 
		delay: .5, 
		afterFinish:function() {
			$('qtv').update(embedCode);
		}
	});
}


function endQTmode() {
	$('qtv').update("");
	new Effect.Fade('mode_overlay_2',{duration:.15});
	new Effect.Fade('mode_overlay_1',{duration:.25, delay:.15});
}



 

