/*
 flipper two object relies on jquery
 
 REQUIRES 4 or more items (no duplicate thumbs or content pages)
*/
/* initialize flipper two object */
var flipperTwo = {

	iDefaultItemsPerPage   : 3,    // items per page defaults to 3
	iItemCount: -1, // total number of items	
	iCurrentItem: -1, // the current item index
		   
	iThumbHeight : 102, // height of thumbnail
		
	iFlipTimeMillis : 689, // duration of page flip animation
	iFlipDelayMillis: 5500, // time between auto flips
	
	iFlipCompleted : 0, // counter for paging items so we only execute on last
	
	iZindexTop: 90,  // z index of top content
	iZindexBottom: 89, // z index of the rest of content (not on top)
	
	bNoFlip: false, // switch to prevent flipping during animation
	sThumbIdPrefix: 'flippertwo-page-item-', // id set for each thumbnail on init
	iAutoFlipTimeout: -1, // timeout index for autoflip
	iResetAfterFlipCount:0, // flip animation counter 
	
	/* ooo VIDEO STUFF  ooo */
	iVideoNowPlayingIndex : -1, // index of currently playing video must be kept up to date for anonymous onProgress event handler
	fVideoFlipPercent : 99, // percent of video to play before video stop and flip to next slide
	iVideoAddEventsAttemptIntervalMillis : 405, // time between attempts to attach event handlers to videos
	iVideoWidth  : 544, 
	iVideoHeight : 306,
	sVideoReplaceIdPrefix : 'video-replace-', // prefix for ids of containers replaced with video embed
	sVideoObjectIdPrefix : 'video-', // prefix for ids of video flash objects 
	aVideoSet : {},
	
	iMuteVolume: 10, 	// volume when muted
	iUnmuteVolume: 30, 	// volume when unmuted
	
	aFlashVars : {	
	 'wmode'   : 'transparent',
     'clip_id' : -1,	
	 'server': 'vimeo.com',
	 'show_title': 0,
	 'show_byline': 0,
	 'show_portrait': 0,
	 'fullscreen': 1,
	 'autoplay' :0,
	 'js_api': 1
	},
		
	aParams : {
	 'wmode'   : 'transparent',
	 'swliveconnect':true,
	 'fullscreen': 1,
	 'allowscriptaccess': 'always',
	 'allowfullscreen':true
	},
	
	aAttributes : {
	 'id':''	 
	},		
	
	
	/* ooo FUNCTIONS ooo */

	
	/* advances to next slide after video plays specified percentage */
    videoPlayingHandler: function(iElapsedTimeSeconds){
	 if(flipperTwo.iVideoNowPlayingIndex == -1) {return;} else {/**/}
	 var sNowPlayingId = flipperTwo.sVideoObjectIdPrefix + flipperTwo.iVideoNowPlayingIndex;
	 var fPercentagePlayed = iElapsedTimeSeconds/document.getElementById(sNowPlayingId).api_getDuration(); 
    
	
    // mute on 0-2% have played
    if(fPercentagePlayed*100 < .5){ 
		document.getElementById(sNowPlayingId).api_setVolume(flipperTwo.iMuteVolume); } else {}

	 // autoflip
	 if(fPercentagePlayed*100 >= flipperTwo.fVideoFlipPercent){
	   flipperTwo.resetVideo(sNowPlayingId);
	   flipperTwo.flip();
	 }else{ /**/}
	},
	
	// resets the given video
	resetVideo: function(sVideoId){
		 // pause the video, reset to start, and flip		
		if(document.getElementById(sVideoId)!=null){
			document.getElementById(sVideoId).api_pause();
			//flipperTwo.flip();		 UNCOMMENT!!!!!
			document.getElementById(sVideoId).api_seekTo(0);
			//$('#'+sVideoId).css('display','none');
		}else{/*no flash support*/}
	},
	
	
	/* loops through video set and attempts to add event handlers */
	attachEventsToVideos: function(){
	    var bAllEventsSet = true; // assume all events handlers are set successfully		
		for(var aVid in flipperTwo.aVideoSet){			
			if(flipperTwo.aVideoSet[aVid]['bEventsAdded'] === true){continue;}else{/**/} // skip video if it already has event handlers
			var sVideoObjectId = flipperTwo.sVideoObjectIdPrefix + aVid;
			
	    
		
			// see if api_addEventListener is available
			
			if(document.getElementById(sVideoObjectId) !=null  && typeof document.getElementById(sVideoObjectId).api_addEventListener == 'function'){    
			 document.getElementById(sVideoObjectId).api_addEventListener("onProgress","flipperTwo.videoPlayingHandler");    
			} else { bAllEventsSet = false; } // at least one event wasn't set, so we need to try again after a delay
			
		}
		if(!bAllEventsSet){ // if there was at least one video without event handerls set, we must try again
			setTimeout('flipperTwo.attachEventsToVideos()', flipperTwo.iVideoAddEventsAttemptIntervalMillis);
		} else {/**/}			
	},
	

		
	/* unused stub for video embed complete events */
	onEmbedComplete : function(e){},
   
	/* embeds videos */
	embedVideos: function(){
	    
		
		// loop through video set and do an embed for each id
		var i = 0;
		for(var aVid in flipperTwo.aVideoSet){
		 var oFlashVars =  {	
		 'wmode'   : 'transparent',
		 'clip_id' : -1,	
		 'server': 'vimeo.com',
		 'show_title': 0,
		 'show_byline': 0,
		 'show_portrait': 0,
		 'fullscreen': 1,
		 'autoplay' :0,
		 'js_api': 1
		};
		 // add no events field to video set element
		 flipperTwo.aVideoSet[aVid]['bEventsAdded'] = false;
		 var iVideoId = flipperTwo.aVideoSet[aVid]['video_id'];		 
		 //flipperTwo.aFlashVars['clip_id'] = iVideoId;
		 oFlashVars['clip_id'] = iVideoId;
		
		 //flipperTwo.aAttributes['id']= flipperTwo.sVideoObjectIdPrefix + i;
		 // autoplay first item if it has a video embed also first item has on embed complete handler
		 var oOnEmbedComplete = false; 
		 if(1 == $('div.flipper-two div.content div.item:eq('+flipperTwo.iCurrentItem+') div#'+
			flipperTwo.sVideoReplaceIdPrefix + i).length){
			//flipperTwo.aFlashVars['autoplay'] = 1;
			oFlashVars['autoplay'] = 1;
			flipperTwo.iVideoNowPlayingIndex = i;
		 }else{
			//flipperTwo.aFlashVars['autoplay'] = 0;
			oFlashVars['autoplay'] = 0;
		 }
			
			
			
		 swfobject.embedSWF("https://secure.vimeo.com/moogaloop.swf", 
						 flipperTwo.sVideoReplaceIdPrefix + i, 
						 ''+flipperTwo.iVideoWidth, 
						 ''+flipperTwo.iVideoHeight, 
						 "9.0.28", 
						 '',
						 oFlashVars,
						 flipperTwo.aParams, 
						 {id:flipperTwo.sVideoObjectIdPrefix + i,tabIndex:'3'},/*flipperTwo.aAttributes,*/
						 oOnEmbedComplete);
		 
		 i++;
		}
		
		// add events to videos
		setTimeout('flipperTwo.attachEventsToVideos()', flipperTwo.iVideoAddEventsAttemptIntervalMillis);
		
	},
	

	
	/* shifts thumbs down, crossfades between content items */
	flip: function(iFlipTo){
	 // prevent flip?
	 if(flipperTwo.bNoFlip){return false;}else{}
	 flipperTwo.bNoFlip = true;
	 
	 // reset all videos
	 for(var i =0; i < flipperTwo.aVideoSet.length;i++){
		flipperTwo.resetVideo(flipperTwo.sVideoObjectIdPrefix + i);
		
	 }
	 
	 	 					  // else PHOTO,set flip 
					clearTimeout(flipperTwo.iAutoFlipTimeout);
	 // determine index of destination item
	 var iIndexContent = iFlipTo;	// destination index 
	 if(typeof iIndexContent == 'undefined'){ // AUTOFLIP if value wasn't passed, set to next item
		iIndexContent = flipperTwo.iCurrentItem -1; 
		if(iIndexContent < 0 ){ iIndexContent = flipperTwo.iItemCount -1;}else{}
	 }else{}
	 // determine offset
	 var iIndexOffset = -1;
	 if(iIndexContent > flipperTwo.iCurrentItem){
		iIndexOffset = Math.abs(iIndexContent - (flipperTwo.iCurrentItem + flipperTwo.iItemCount));
	 }else{ iIndexOffset = flipperTwo.iCurrentItem - iIndexContent; }
	 	 
		 
	/* ooo SHIFT THUMBS DOWN ooo */		
	 $('div.flipper-two div.pages div.item').animate({"top": '+='+(flipperTwo.iThumbHeight*iIndexOffset)+'px'}, 
		flipperTwo.iFlipTimeMillis, 
		function(){	
			if(parseInt($(this).css('top'), 10) >= flipperTwo.iThumbHeight*flipperTwo.iDefaultItemsPerPage){;
				$(this).css('top',parseInt($(this).css('top'), 10) - (flipperTwo.iItemCount * flipperTwo.iThumbHeight));							
			}else{}
			if(flipperTwo.iResetAfterFlipCount>=(flipperTwo.iItemCount-1)){
				flipperTwo.iResetAfterFlipCount=0;		
				flipperTwo.iCurrentItem=iIndexContent;
				/* ooo if the current item is a video, play it ooo */
				  
				  var iLastItemIndex = flipperTwo.iCurrentItem+1;
				  if(iLastItemIndex < 0 ){ iLastItemIndex = flipperTwo.iItemCount -1;}else{}
				  
				  var sVidId = false; 
				  clearTimeout(flipperTwo.iAutoFlipTimeout);
				  
				  if( false !== (sVidId = flipperTwo.isVideo(iLastItemIndex)) ){ // play the vid
				   flipperTwo.iVideoNowPlayingIndex = parseInt(sVidId.replace(flipperTwo.sVideoObjectIdPrefix,''), 10);	   				   
				   document.getElementById(sVidId).api_play();	
				  }else{ flipperTwo.iAutoFlipTimeout = setTimeout('flipperTwo.flip()', flipperTwo.iFlipDelayMillis); }				
				flipperTwo.bNoFlip = false;
			}else{}
			flipperTwo.iResetAfterFlipCount++;	
		}
	   );
	   
	   /* push all content to back then put next content item to top */
	   
	   $('div.flipper-two div.content div.item').css('z-index',flipperTwo.iZindexBottom);
	   $('div.flipper-two div.content div.item:eq('+iIndexContent+')').css('z-index',flipperTwo.iZindexTop);
	   
	   /* ooo CROSS FADE ooo */
	   $('div.flipper-two div.content div.item:eq('+iIndexContent+')').animate({"opacity": 1.0}, 
		flipperTwo.iFlipTimeMillis,
		function(){});		
	   
	   $('div.flipper-two div.content div.item:eq('+flipperTwo.iCurrentItem+')').animate({"opacity": 0.0}, 
		flipperTwo.iFlipTimeMillis,
		function(){});			   	   	  
	},
			
	isVideo: function(iIndex){	
		if($('div.content div.item:eq('+(iIndex-1)+') object').length !=0){
		var sRetId = $('div.content div.item:eq('+(iIndex-1)+') object').attr('id');
			return sRetId; } else { return false; }
	},
	
	/* initialization function */
	flipperTwo: function(autoFlip,thumbCount,thumbHeight,iSetWidth,iSetHeight){
	
	if(iSetWidth){flipperTwo.iVideoWidth = iSetWidth;}else{}
	if(iSetHeight){flipperTwo.iVideoHeight = iSetHeight;}else{}	
    if(autoFlip){flipperTwo.iFlipDelayMillis   = autoFlip;}else{}
    if(thumbCount){flipperTwo.iDefaultItemsPerPage   = thumbCount;}else{}
    if(thumbHeight){flipperTwo.iThumbHeight   = thumbHeight;}else{}
     /* ooo STACK THUMBS ooo */	
	 // count items
	 flipperTwo.iItemCount = parseInt($('div.flipper-two div.pages div.item').length);	 	 
	 // position stack	 
	 for(var i = 0; i < flipperTwo.iItemCount; i++){	  
	  var iTop = flipperTwo.iThumbHeight * i;         // calculate top 	

	  // revise top value if we are below last item threshhold
	  if(i > flipperTwo.iDefaultItemsPerPage - 1){ iTop =  -(flipperTwo.iThumbHeight * (flipperTwo.iItemCount - i)); }else{}
        	  
	  // apply top value	 
	  $('div.flipper-two div.pages div.item:eq('+i+')').css('top',iTop); 
	  // REMOVE!!!!
	  //$('div.flipper-two div.pages div.item:eq('+i+')').html('<strong style="color:green;text-align:right;font-size:47px;">'+i+'</strong>');
	  // END REMOVE!!!!
	  // set an id
	  $('div.flipper-two div.pages div.item:eq('+i+')').attr('id',flipperTwo.sThumbIdPrefix+i);
	  // apply flip click events
	 $('div.flipper-two div.pages div.item:eq('+i+')').bind('click', function(e) {			
			flipperTwo.flip(parseInt(e.currentTarget.id.replace(flipperTwo.sThumbIdPrefix,'')));
		});
	 }
	
	 /* ooo SHOW PROPER CONTENT ITEM ooo */	
	 flipperTwo.iCurrentItem = flipperTwo.iDefaultItemsPerPage;
	 $('div.flipper-two div.content div.item').css({opacity:0.0});
	 $('div.flipper-two div.content div.item').css('z-index',flipperTwo.iZindexBottom);
	 $('div.flipper-two div.content div.item:eq('+flipperTwo.iCurrentItem+')').css({opacity:1.0});
     $('div.flipper-two div.content div.item:eq('+flipperTwo.iCurrentItem+')').css('z-index',flipperTwo.iZindexTop);
	 
	 /* ooo Embed Videos ooo */
	 flipperTwo.embedVideos();
	 
	 //setTimeout('flipperTwo.flip()',flipperTwo.iFlipDelayMillis*2);
	}
	
}
  
  
  
