
(function($){
	Morpheo = {
                flashparams : {
                    menu: "false",
                    wmode: "opaque",
                    allowScriptAccess:"sameDomain",
                    allowFullScreen:"true",
                    scale: "exactfit",
                    bgcolor: "#000000"
                },

                flashvars : {
                    autoplay: "true",
                    autosize: "true"
                },

                videoparams : {
                    autoplay: ""
                },
        
                currentItem : 0,
                
                width : 0,
                
                height : 0,
                
                playlist : new Array(),
                
                target : 'morpheo_media',
                
                autoId : null,
                
                auto: 0,
                
		onClickMedia : function(event)
		{
			var $this = $(this);
			var src = $this.attr("href");
			var type = $this.attr("type");
			var id = $this.attr("class");
		
			var params = {
					menu: "false",
					wmode: 'transparent',
					allowScriptAccess:"sameDomain"
				};
			
			var settings = {};
			settings.src = item.src;
			settings.type = item.type;
			settings.target = Morpheo.target;
					
			this.getMedia(settings);
		
			if(id && $("#txtVideo li."+id).length)
			{
				$("#morpheo .txt").html($("#txtVideo li."+id).html());
				$('#morpheo').bind({'mouseenter.morpho': this.onMouseEnter, 'mouseleave.morpho':this.onMouseLeave});
			}
			else
			{
				$('#morpheo').unbind('.morpho');
				
				if(type == "kewego")
					this.getVideoDetail(src);
			}
				
			$("#morpheo .txt").css({'height':'0px','opacity':'0'});
			 
			return false;
		},
		
		getMedia : function(settings)
                {
                    if(settings.type == "kewego")
                    {	
                        if (MorphoPlayer.is_iphone() || MorphoPlayer.is_ipad())
                        {
                           //var url = "http://api.kewego.com/video/getThumbnail/?size=large&appToken="+APPTOKEN+"&sig="+src;
                           //MorphoPlayer.img({src:url, target:target, width:'100%', height:'100%'});
                           //$("#"+target).click(function(event){VideoToyota.getVideoStream(src, target)})

                           this.getVideoStream(settings.src, settings.target);
                       }
                       else
                           this.getVideoStream(settings.src, settings.target);
                    }
                    else
                    {
                    	settings.width = this.width;
                    	settings.height = this.height;
                    	settings.flashparams = this.flashparams;
                    	settings.videoparams = this.videoparams;
                    	settings.flashvars = this.flashvars;
                    	settings.flvplayer = './swf/flvPlayer.swf';
                    	
                        MorphoPlayer.media(settings);
                    }
		},
                
        loadPlaylist : function(list,params)
        {
              if(!list.length)
                  return;

              this.playlist = list;
              var ctlHTML = "<div class='next'></div>";
              ctlHTML += "<div class='prev'></div>";
              for(var i=this.playlist.length; i>0; i--)
              {
                  ctlHTML += "<div class='point'></div>";
              }
              $("#morpheo .ctl").html(ctlHTML);
              $('#morpheo .ctl div.next').bind('click', this.nextItem);
              $('#morpheo .ctl div.prev').bind('click', this.prevItem);

              this.currentItem = 0;
              this.getCurrentItem();
              
              if(this.auto = params.auto)
              {
                  $('#morpheo').bind({'mouseenter': this.halt, 'mouseleave': this.animate});
                  this.animate();
              }   
          },

           getCurrentItem : function(){
               if(!this.playlist.length || this.playlist.length<=this.currentItem)
                   return;

               var item = this.playlist[this.currentItem];
				
               if(item.text)
               {
                   $("#morpheo .txt").html(item.text);
                   $("#morpheo .txt").show();
               }
               else
                   $("#morpheo .txt").hide();

               this.width = item.width ? item.width : undefined;
               this.height = item.height ? item.height : undefined;

               if(item.autoplay!=undefined && item.autoplay==false)
               {
                   this.flashvars.autoplay = "false";
                   delete this.videoparams.autoplay;
               }
               else
                   this.flashvars.autoplay = this.videoparams.autoplay = "true";

               var num = this.playlist.length - this.currentItem - 1; 
               $("#morpheo .ctl div.point").removeClass('active').eq(num).addClass('active');


				var settings = {};
				settings.src = item.src;
				settings.type = item.type;
				settings.target = this.target;
				
				settings.link = item.link;
				settings.linktarget = item.linktarget;
				
				this.getMedia(settings);
           },

           nextItem : function(event){
                   Morpheo.currentItem++;

                   if(Morpheo.currentItem>=Morpheo.playlist.length)
                       Morpheo.currentItem = 0;

                   Morpheo.getCurrentItem();
                   },

           prevItem : function(event){
                   Morpheo.currentItem--;

                   if(Morpheo.currentItem<0)
                       Morpheo.currentItem = Morpheo.playlist.length-1;

                   Morpheo.getCurrentItem();
                   },
		
		getVideoDetail : function(src){
			$.ajax({
			  type: "GET",
			  url: '/webtv/get_detail.php',
			  data: "sig="+src,
			  success: function(data) {
			  	if (data) {
					$("#morpheo .txt").html(data);
					$('#morpheo').bind({'mouseenter.morpho': this.onMouseEnter, 'mouseleave.morpho':this.onMouseLeave});
				}
				else
					$('#morpheo').unbind('.morpho');
				
				$("#morpheo .txt").css({'height':'0px','opacity':'0'});
			  }
			});
		},
		
		getVideoStream : function(sig, target){
			$("#"+target).unbind("click");
			
			var format = "normal";
			var settings = {};
			settings.type = "flv";
                        
			if(MorphoPlayer.is_iphone() || MorphoPlayer.is_ipad())
			{
				format = MorphoPlayer.is_ipad() ? "ipadHQ" : "iphone";
				settings.type = "mp4";
				
				this.videoparams.poster = "http://api.kewego.com/video/getThumbnail/?size=large&appToken="+APPTOKEN+"&sig="+sig;
			}
			else if(MorphoPlayer.is_mp4())
			{
				format = "ipadHQ";
				settings.type = "mp4";
			}
			
			settings.src = "http://api.kewego.com/video/getStream/?format="+format+"&appToken="+APPTOKEN+"&sig="+sig;
			
			settings.target = target;
			settings.width = this.width;
           	settings.height = this.height;
           	settings.flashparams = this.flashparams;
           	settings.videoparams = this.videoparams;
           	settings.flashvars = this.flashvars;
           	settings.flvplayer = './swf/flvPlayer.swf';
			MorphoPlayer.media(settings);
			
			var video = $("#"+target+" video");
			if(video)
			{
				video.bind("fullscreen", function() {
					if ($('#morpheo .media').hasClass('fullscreen'))
					{
						$('#morpheo').unbind('.morpho');
						$('#morpheo').css({'z-index':'9999'});
					}
					else
					{
						$('#morpheo').bind({'mouseenter.morpho': this.onMouseEnter, 'mouseleave.morpho':this.onMouseLeave});
						$('#morpheo').css({'z-index':'1'});
					}
						
					
					$("#morpheo .txt").css({'height':'0px','opacity':'0'});
				});
			}
		},
                
                animate: function() {
                    if(!Morpheo.autoId)
                    {
                        Morpheo.autoId = setInterval( function() {
                            Morpheo.nextItem();
                        }, Morpheo.auto);
                    }  
                },
                
                halt: function() {
                    if(Morpheo.autoId)
                    {
                        clearInterval(Morpheo.autoId);
                        Morpheo.autoId = null;
                    }
                        
                },
                
		
		onMouseEnter : function(event){
			$("#morpheo .txt").stop(true).animate({'height':'271px','opacity':'1'},500);
		},
		
		onMouseLeave : function(event){
			$("#morpheo .txt").stop(true).animate({'height':'0px','opacity':'0'},500);
		}
	};
})(jQuery);

