        var VimeoEmbed = {popup:false,active_group:null,only_popup:1,open_on_load:1};
        
        VimeoEmbed.get_iframe=function(vimeo_id,autostart,loop){
        	
        	var rhytech_date=new Date();
        	return ('<iframe id="player_1" src="http://player.vimeo.com/video/'+vimeo_id+'?title=0&byline=0&portrait=0&color=ffffff&autoplay='+autostart+'&loop='+loop+'&js_api=1&js_swf_id=player_1&time='+rhytech_date.getTime()+'" frameborder="0"></iframe>');
        	
        }
	
        VimeoEmbed.init = function()
        {
        	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
					$("body").addClass("mozzila");
				}
 			//Set Active Element
        	if(VimeoEmbed.popup) { VimeoEmbed.active_element=$("#rhytech-popup-video-gallery-wrapper"); VimeoEmbed.passive_element=$("#rhytech-video-gallery-wrapper"); }
        	else { VimeoEmbed.active_element=$("#rhytech-video-gallery-wrapper"); VimeoEmbed.passive_element=$("#rhytech-popup-video-gallery-wrapper"); }
        	
			// Get total number of videos
        	VimeoEmbed.total_videos=VimeoPlaylist.videos.length; 
            VimeoEmbed.all_videos_id=VimeoEmbed.total_videos-1;
        	//generate controls
        	
        	var controls_html='<a href="#" class="rhytech-switch" ></a><ul>';
        	var j=0;
        	for(i=0;i<VimeoEmbed.total_videos-1;i++){
        		if(VimeoPlaylist.videos[i].type=="video"){
        			controls_html+="<li><a href='javascript:VimeoEmbed.play("+VimeoPlaylist.videos[i].video_id+","+VimeoPlaylist.videos[i].autostart+","+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].loop+")' class='rhytech_video_"+VimeoPlaylist.videos[i].video_id+"' >"+VimeoPlaylist.videos[i].video_title+"</a></li>";
        		}
        		else {
        			controls_html+="<li><a href='javascript:VimeoEmbed.showGroup("+j+")' class='rhytech_video_group_"+j+"' >"+VimeoPlaylist.videos[i].video_title+"</a></li>";
        			j+=1;
        		}
        	}
        	
        	controls_html+="</ul><a href='javascript:VimeoEmbed.play("+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].video_id+","+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].autostart+","+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].loop+")'  class='rhytech_video_"+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].video_id+" rhytech-last-video-control'>"+VimeoPlaylist.videos[VimeoEmbed.all_videos_id].video_title+"</a>";
        	$(".rhytech-video-controls",VimeoEmbed.active_element).html(controls_html);
            // Set events
		   
		   var control_class=$(VimeoEmbed.active_element).find(".rhytech-switch");

		    if(VimeoEmbed.only_popup==1) control_class=$("a.rhytech-popup-button").add($(VimeoEmbed.active_element).find(".rhytech-switch"));

		    if(VimeoEmbed.popup)
		    	 $(control_class).unbind("click").bind("click",function(){
		    	 	
		    	 	$.fancybox.close();
		    	 	
		    	 });
		    else 
			     $(control_class).fancybox({
			    	content:"<div id='rhytech-popup-video-gallery-wrapper'>"+$("#rhytech-video-gallery-wrapper").html()+"</div>",
			    	padding:0,
			    	autoScale:false,
			    	overlayOpacity:0.7,
			    	onComplete:function(){
			    		
			    		VimeoEmbed.popup=true;
			    		VimeoEmbed.init();
			    		if(VimeoEmbed.active_group!=null){
			    			
			    			if(VimeoEmbed.currently_playing==null)
			    				VimeoEmbed.showGroup(VimeoEmbed.active_group);
			    			else
			    				VimeoEmbed.playGroupVideo(VimeoEmbed.currently_playing[0],VimeoEmbed.currently_playing[1],VimeoEmbed.currently_playing[2],VimeoEmbed.active_group);
			    			
			    		}
			    		else {
			    			VimeoEmbed.play(VimeoEmbed.currently_playing[0],VimeoEmbed.currently_playing[1],VimeoEmbed.currently_playing[2]);
			    			
			    		}
			    	},
			    	onClosed:function(){
			    		
			    		VimeoEmbed.popup=false;
			    		VimeoEmbed.init();
			    		if(VimeoEmbed.active_group!=null){
			    			
			    			if(VimeoEmbed.currently_playing==null)
			    				VimeoEmbed.showGroup(VimeoEmbed.active_group);
			    			else
			    				VimeoEmbed.playGroupVideo(VimeoEmbed.currently_playing[0],VimeoEmbed.currently_playing[1],VimeoEmbed.currently_playing[2],VimeoEmbed.active_group);
			    			
			    		}
			    		else {
			    			VimeoEmbed.play(VimeoEmbed.currently_playing[0],VimeoEmbed.currently_playing[1],VimeoEmbed.currently_playing[2]);
			    			
			    		}
			    	}
			    	
			    });
        };
        
 		VimeoEmbed.play = function(video_id,autostart,loop)
        {
            var new_iframe=VimeoEmbed.get_iframe(video_id,autostart,loop);
            $(".rhytech-videos-wrapper").empty();
            $(".rhytech-videos-wrapper",VimeoEmbed.active_element).html(new_iframe);
        	Froogaloop.init();                
        	$('.rhytech-videos-wrapper iframe',VimeoEmbed.active_element).each(function(index, iframe){
                    iframe.addEvent('onLoad',  function(){VimeoEmbed.normal_video_loaded(loop)});
                });
		    $(".rhytech-video-controls li a.rhytech_control_active").removeClass("rhytech_control_active");
		    $(".rhytech_video_"+video_id,VimeoEmbed.active_element).addClass("rhytech_control_active");
		    $(".rhytech_video_"+video_id,VimeoEmbed.passive_element).addClass("rhytech_control_active");
        	VimeoEmbed.currently_playing=[video_id,autostart,loop];
        	VimeoEmbed.active_group=null;

        };                
      	
        VimeoEmbed.showGroup=function(group_index){
        	$(".rhytech-videos-wrapper").empty();
        	if(VimeoEmbed.popup)
        		$(".rhytech-videos-wrapper",VimeoEmbed.active_element).html("<div class='rhytech-grouped-videos-list' >"+$(".rhytech-video-group").eq(group_index).find(".rhytech-large-grouped-videos").html()+"</div>");
        	else
        		$(".rhytech-videos-wrapper",VimeoEmbed.active_element).html("<div class='rhytech-grouped-videos-list' >"+$(".rhytech-video-group").eq(group_index).find(".rhytech-small-grouped-videos").html()+"</div>");
        	
        	VimeoEmbed.current_group=VimeoEmbed.get_current_group(group_index);	
        		
        	var l=0;
        	
        	$(".rhytech-videos-wrapper",VimeoEmbed.active_element).find("area").each(function(i){
        		
        		if($(this).attr("shape")!="default"){
        			$(this).attr("href","javascript:VimeoEmbed.playGroupVideo("+VimeoEmbed.current_group[l].video_id+","+VimeoEmbed.current_group[l].autostart+","+VimeoEmbed.current_group[l].loop+","+group_index+")");
        			l+=1;
        		}
        	})
        	
        	$(".rhytech-videos-wrapper",VimeoEmbed.active_element).find("map").attr("id","video_group_"+group_index).attr("name","video_group_"+group_index);
        	$(".rhytech-videos-wrapper",VimeoEmbed.active_element).find("img").attr("usemap","#video_group_"+group_index);
		    $(".rhytech-video-controls li a.rhytech_control_active").removeClass("rhytech_control_active");
		    $(".rhytech_video_group_"+group_index,VimeoEmbed.active_element).addClass("rhytech_control_active");
		    $(".rhytech_video_group_"+group_index,VimeoEmbed.passive_element).addClass("rhytech_control_active");
        	VimeoEmbed.active_group=group_index;
        	VimeoEmbed.currently_playing=null;
        	
        }
        
        VimeoEmbed.playGroupVideo=function(video_id,autostart,loop,group_index){
            var new_iframe=VimeoEmbed.get_iframe(video_id,autostart,loop);
            $(".rhytech-videos-wrapper").empty(); 
            $(".rhytech-videos-wrapper",VimeoEmbed.active_element).html(new_iframe); 
        	Froogaloop.init();                
        	$('.rhytech-videos-wrapper iframe',VimeoEmbed.active_element).each(function(index, iframe){
                iframe.addEvent('onLoad', function(){VimeoEmbed.group_video_loaded(loop)});
            });     	
        	$(".rhytech-video-controls li a.rhytech_control_active").removeClass("rhytech_control_active");
		    $(".rhytech_video_group_"+group_index,VimeoEmbed.active_element).addClass("rhytech_control_active");
		    $(".rhytech_video_group_"+group_index,VimeoEmbed.passive_element).addClass("rhytech_control_active");
        	VimeoEmbed.currently_playing=[video_id,autostart,loop];
        }
        
        VimeoEmbed.get_current_group=function(group_index){

        	var k=-1;
        	
        	for(i=0;i<VimeoEmbed.total_videos;i++){
        		
        		if(VimeoPlaylist.videos[i].type=="video_group"){
        			
        			k+=1;
        			
	        		if(k==group_index){
	        			
	        			return VimeoPlaylist.videos[i].videos;
	        		}
	        		
        		}
        		
        	}        	
        	
        }
        
        VimeoEmbed.normal_video_loaded = function(loop)
        {
        	if(loop==0 && VimeoEmbed.currently_playing[0]!=VimeoPlaylist.videos[0].video_id){
        		$("#player_1").get(0).addEvent('onFinish',function(){VimeoEmbed.play(VimeoPlaylist.videos[0].video_id,VimeoPlaylist.videos[0].autostart,VimeoPlaylist.videos[0].loop)});
        	}
        }
        
        VimeoEmbed.group_video_loaded = function(loop)
        {
        	if(loop==0){
            	$("#player_1").get(0).addEvent('onFinish',function(){VimeoEmbed.showGroup(VimeoEmbed.active_group);setTimeout(function(){VimeoEmbed.showGroup(VimeoEmbed.active_group)},100);});
            }
        }
      //On document ready
      $(document).ready(function(){VimeoEmbed.init();if( VimeoEmbed.total_videos>0) VimeoEmbed.play(VimeoPlaylist.videos[0].video_id,VimeoPlaylist.videos[0].autostart,VimeoPlaylist.videos[0].loop);if(VimeoEmbed.open_on_load==1) $("a.rhytech-popup-button").eq(0).trigger("click")});
