(function(){var initializing=false,fnTest=/xyz/.test(function(){xyz;})?/\b_super\b/:/.*/;this.Class=function(){};Class.extend=function(prop){var _super=this.prototype;initializing=true;var prototype=new this();initializing=false;for(var name in prop){prototype[name]=typeof prop[name]=="function"&&typeof _super[name]=="function"&&fnTest.test(prop[name])?(function(name,fn){return function(){var tmp=this._super;this._super=_super[name];var ret=fn.apply(this,arguments);this._super=tmp;return ret;};})(name,prop[name]):prop[name];}
function Class(){if(!initializing&&this.init)
this.init.apply(this,arguments);}
Class.prototype=prototype;Class.constructor=Class;Class.extend=arguments.callee;return Class;};})();
//<-- Class Initialize.


/***************************************
 * initall
 ***************************************/
var
bindClick,
$playlist, 
void0='javascript:void(0);',
gClasses = {},
// hmc = {},
spAgent={},
escapeHTML = function(str) {
  return str.replace(/[&"<>]/g, function(c) {
    return {
      "&": "&amp;",
      '"': "&quot;",
      "<": "&lt;",
      ">": "&gt;"
    }[c];
  });
}

//smartphone USER AGENT
spAgent.iPhone = navigator.userAgent.match(/iPhone/i);
spAgent.iPad = navigator.userAgent.match(/iPad/i);
bindClick = (spAgent.iPhone || spAgent.iPad) ? 'touchend':'click';

//4 chk on PC browsers.
bindClick = 'click';

//test - ALL UA is iphone
//spAgent.iPhone = true;


/* 4 iphone/ipad */

$j(document).ready(function($){

	/***************************************
	 * scope
	 ***************************************/
	$.scope = function(target,func){ return function() { func.apply(target,arguments);}};
	$.log = function(t){try{console.log(t);}catch(e){}};
	
	/***************************************
	 * object clone
	 ***************************************/
	$.cloneObject = function(source,isDeep) {
		if(isDeep){
			return $.extend(true,{},source);
		}
		return $.extend({},source);
	}

	/// arg.fnc variables.
	var vView,vControl,plist;
	
	
	/***************************************
	 * Class::VideoView
	 ***************************************/
	gClasses.VideoView = Class.extend({
		carouselId :'',
		$stars : null,
		thumbWidth : 0,
		thumbDefX : 0,
		thumbPerPage : 6,
		thumbCnt : 0,
		thumbNow : 0,
		getPageNow : function /*get*/(){return Math.floor(this.thumbNow/this.thumbPerPage)},
		getPageMax : function /*get*/(){return Math.ceil(this.thumbCnt/this.thumbPerPage)},
		getPageWidth : function /*get*/(){return this.thumbWidth*this.thumbPerPage},
		tmpw : 0,
		caluselMotionFlg : false,
		
		eOpt : {
			'duration':660,
			'easing':'easeOutCubic',
			'complete':function(){}
		},
		
		everyMotionCompleteFnc : function(){
			this.caluselMotionFlg = false;
		},
		
		//// set stageWidth //////////////////////
		fixWidth : function (){
			var tmpw = $('#'+this.carouselId).width();
			$('#'+this.carouselId + ' ul li').each($.scope(this,function(i,elem){
				this.thumbCnt++
				if(i>=this.thumbPerPage)tmpw += this.thumbWidth;
			}));
			this.$stars.css('width',tmpw)
		},
		
		gotoIndex : function(idx){
			if(this.caluselMotionFlg)return;
			var eop = $.cloneObject(this.eOpt);
			eop.duration = 390;
			
			eop.complete = $.scope(this,function(){
				this.thumbNow=idx;
				this.caluselMotionFlg = false;
				this.everyMotionCompleteFnc()
			});
			this.$stars.animate(
				{left:-idx*this.thumbWidth},eop
			);
				
			this.caluselMotionFlg = true;
		},
		gotoPage : function(pageNext){
			if(this.caluselMotionFlg)return;
			
			var nPage = this.getPageNow();
			var surplus = this.thumbNow - nPage*this.thumbPerPage;
			var pageDiff = (surplus != 0 && pageNext - nPage <= 0) ? 0 : pageNext - nPage;
			var eop = $.cloneObject(this.eOpt);
			
			
			//this.thumbNow += pageDiff*this.thumbPerPage;
			this.thumbNow += pageDiff*this.thumbPerPage - surplus;
			
			if(this.thumbNow<0){this.thumbNow = ((this.getPageMax()-1)*this.thumbPerPage)}
			else if(this.thumbNow/this.thumbPerPage>=this.getPageMax()){this.thumbNow = 0};
			
			eop.complete = $.scope(this,function(){this.everyMotionCompleteFnc()});
			var xp = this.thumbDefX - this.getPageWidth()*(this.getPageNow());;
			
			//console.log("現在"+nPage+"ページ(+"+surplus+"thumb)です。"+pageNext+"ページに移動を試みます。\nページ差分："+pageDiff+"\n移動left : "+xp+" nextThumbIndex:"+this.thumbNow)
			
			this.$stars.animate( {"left":xp},eop);
			//pageNext
			this.caluselMotionFlg = true;
		},
		
		/// Events
		
		eNextClick : function(e){this.gotoPage(this.getPageNow()+1); e.preventDefault();},
		ePreviousClick : function(e){this.gotoPage(this.getPageNow()-1); e.preventDefault();},
		eMouseScroll : function(e,delta){
			//alert(e+","+delta)
			
			//NO SCROLL!
			if(e){
				if (e.preventDefault) {
					e.preventDefault();
				}
				e.returnValue = false;
			}
			
			if(this.caluselMotionFlg)return;
			
			//>>
			if((delta<0 && this.thumbNow >= this.thumbCnt-1 ) ||
			//<<
			(delta>0 && this.thumbNow<=0)) return;
			
			delta = delta>0 ? 1:-1;
			
			var eop = $.cloneObject(this.eOpt);
			eop.duration = 200;
			
			eop.complete = $.scope(this,function(){
				this.thumbNow-=delta;
				this.caluselMotionFlg = false;
				this.everyMotionCompleteFnc()
			});
			this.$stars.animate(
				{left:this.$stars.position().left+delta*this.thumbWidth},eop
			);
				
			this.caluselMotionFlg = true;
		},
		
		select : function(id){
			if(this.getPageMax()>1 && ((this.getPageNow()*this.thumbPerPage)-1 > id || (this.getPageNow()-1)*this.thumbPerPage < id) ){
				//スクロール可で選択idがインデックス外であるとき（自動送りのとき）、ページを自動スクロール
				this.gotoPage(Math.floor(id/this.thumbPerPage));
			}
			this.$stars.find('li > a').removeClass('now');
			this.$stars.find('li:eq('+id+') > a').addClass('now');
			//thumbs fit to left.
			//if(this.thumbNow -id != 0)this.gotoIndex(id);
		},
		
		/// Constractor
		init:function(targetId,playlist){
			
			
			var carouselId = 'hm_carousel';
			var carListId = 'hm_carList';
			
			$('#'+targetId).html('<div id="hm_video">'
+'				</div>'
+'				<div id="hm_info_wrapper">'
+'					<div id="hm_info">'
+'						<h4 id="hm_title"></h4>'
+'						<p id="hm_description"></p>'
+'						<ul id="hm_relbtns" class="cfx">'
+'						</ul>'
+'					</div>'
+'					<div id="hm_sharebtns">'
+'						<span id="hm_btn_share_bg"></span>'
+'						<span id="hm_btn_embeds_bg"></span>'
+'						<ul id="hm_sharebtns" class="cfx clearfloat">'
// +'							<li class="hm_btn_share"><a href="#">シェアする</a></li>'
// +'							<li class="hm_btn_embeds"><a href="#">埋め込みコード</a></li>'
+'							<li class="hm_btn_related"><a href="#">関連動画を見る</a></li>'
+'						</ul>'
+'					</div>'
+'					<div id="'+carouselId+'" class="clearfloat">'
+'						<div id="hm_carousel_previous"><a href="#">&laquo;</a></div>'
+'						<div id="hm_carList_wrapper">'
+'							<ul id="'+carListId+'"></ul>'
+'						</div>'
+'						<div id="hm_carousel_next"><a href="#">&raquo;</a></div>'
+'					</div>'
+'				</div>');
			
			
			//// make thumbs
			playlist.find('item > core > thumbnailImage').each(function(n,o){
				$('#'+carListId).append('<li><a href="'+void0+'"><img src="'+$(o).attr('url')+'" alt="" /></a></li>');
			});
			
			this.carouselId = carouselId;
			this.$stars = $('#'+this.carouselId + ' ul').css('position','absolute');
			this.thumbWidth = $('#'+this.carouselId + ' ul li:first').outerWidth();
			this.thumbDefX = 0;//$('#'+this.carouselId + '_previous a').outerWidth();
			//this.tmpw = $('#'+this.carouselId + '').width()
			this.fixWidth();
			this.enables = this.thumbCnt > this.thumbPerPage;
			
			//サムネイルがはみ出さない場合、スクロールしない
			if(!this.enables){
				$('#'+carouselId + '_previous a').attr('href',void0).addClass('off').bind(bindClick,function(e){return false;});
				$('#'+carouselId + '_next a').attr('href',void0).addClass('off').bind(bindClick,function(e){return false;});
				//console.log('videoのカルーセルをスクロールさせない thumbCnt:'+this.thumbCnt);
				return;
			}
			
			$('#'+carouselId + '_next a').attr('href',void0).bind(bindClick,$.scope(this,this.eNextClick));
			$('#'+carouselId + '_previous a').attr('href',void0).bind(bindClick,$.scope(this,this.ePreviousClick));
			$('#'+carouselId).bind("mousewheel",$.scope(this,this.eMouseScroll));
		}
	});
	
	
	/***************************************
	 * Class::VideoController
	 ***************************************/
	gClasses.VideoController = Class.extend({
		dat : null,
		now : 0,
		carousel : null,
		tracking: {},
		sharebtnEvFnc:[],
		
		
		/// Constractor
		init : function(playlist_xml_parsed_by_jquery, carousel){
			this.dat = playlist_xml_parsed_by_jquery;
			this.carousel = carousel;
			this.reflesh(0);
			this.carousel.$stars.find('li > a').each($.scope(this,function(n,o){
				$(o).bind(bindClick,$.scope(this,function(e){
					e.preventDefault();
					this.reflesh(n);
				}));
			}));
			
		},
		
		///// video周りの表示を更新します。
		reflesh : function(id){
			
			
			//videoインデックスのはみ出し禁止
			if(id>=this.dat.find('item').length || id<0) id = 0;
			//console.log(id)
			
			var item = this.dat.find('item:eq('+id+')')
			,trackingPrefix = this.dat.find('config > tracking > version').text() + '_' + this.dat.find('config > tracking > category').text() + '_'
			,placementCode = item.find('trackingSub > placementcode').text()
			;
			
			this.now = id;
			
			this.carousel.select(id);
			
			//表示の初期化
			$('#hm_video').html('');
			$('#hm_relbtns').html('');
			// hmc.$w1.relTabWindow.close(1)
			// hmc.$w2.relTabWindow.close(2)
			
			$('#hm_title').text(item.find('copy > displayName').text())
			$('#hm_description').html(item.find('copy > description').text())
				
			item.find('relatedLinks > link').each($.scope(this,function(n,o){
				$('#hm_relbtns').append('<li><a href="'+$(o).attr('url')+'" target="'+$(o).attr('target')+'" name="'+trackingPrefix+placementCode+'-link'+(n+1)+'" type="custom" onclick="cto.tracklink(this)"><span class="btntick"></span><span class="btntext">'+$(o).attr('title')+'</span><span class="btnright"></span></a></li>');
			}));
			
			/// videoタグ
			var visEnabled,visJpOnly, //vdat = item.find('video'),
			vurl = item.find('stream').attr('url'),
			caution = '<div class="caution">この動画はipad/iphoneでご覧頂くことはできません。</div>';
			
			
			visJpOnly = (/\/\/cp96449\.edgefcs\.net\/ondemand\//).test(vurl);
			visEnabled = (/\/\/cp91605\.edgefcs\.net\/ondemand\//).test(vurl);
			vurl = visJpOnly ? null : 'http://progressivevideo.disney.co.jp/' + vurl.substr(vurl.indexOf('/ondemand/')+10);
			
			//$.log('video url : '+vurl+'\nis JP only? : '+visJpOnly);
			
			if(!visEnabled || !vurl){
				//NG
				$('#hm_video').append(caution)
				return false;
			}else{
				//OK
				//$('#hm_video').append('<video poster="'+vdat.attr('poster')+'" src="'+vdat.attr('src')+'" preload="auto" controls="controls"><source src="'+vdat.attr('src')+'" type=\''+vdat.attr('type')+'\' /></video>');
				$('#hm_video').append('<video src="'+vurl+'" preload="auto" controls="controls"><source src="'+vurl+'" /></video>')
			}
			
			
			
			///////////// videoイベント+（トラッキング） /////////////
			
			var $video =$('#hm_video video');
			
			//トラッキング初期設定
			if(!this.tracking.trackPath){
				this.tracking.trackPath = "vid:"
					+ this.dat.find('config > tracking > version').text() + '/'
					+ this.dat.find('config > tracking > category').text() + '/';
				this.tracking.movieId = this.dat.find('config > tracking > movieID').text();
			}
				
			$video.data('firstDetect',true);
			this.tracking.mediaFileName =  vurl.substr(vurl.lastIndexOf("/")+1);
			this.tracking.mediaName =  this.tracking.trackPath + this.tracking.mediaFileName;
			
			$video.bind('ended',$.scope(this,function(e){
				//console.log('movie再生終了！次の動画にインデックスを移動します。 now:'+this.now)
				cto.MediaClose(this.tracking.mediaName);
				this.reflesh(this.now+1);
			})).bind('error',$.scope(this,function(e){
				//再生不能
				$('#hm_video').append(caution);
			}));
			
			$video.bind('durationchange',$.scope(this,function(e){
				if($video.data('firstDetect')==true){
					/// Openは初回のみ。
					//$.log(this.tracking.mediaName+'\n'+e.target.duration+'\n'+this.tracking.movieId)
					cto.MediaOpen(this.tracking.mediaName, e.target.duration, this.tracking.movieId);
					$video.data('firstDetect',false);
				}
				this.tracking.duration = e.target.duration;
				//$.log('-------------------- video.durationchange --------------------');
				//$.log('duration : '+e.target.duration);
				//$.log('currentTime : '+e.target.currentTime);
			}));
			
			$video.bind('play',$.scope(this,function(e){
				$.log('-------------------- video.play --------------------');
				//$.log('currentTime : '+e.target.currentTime);
				//digMediaPlay(mediaName, mediaOffset, contentType, propertyName)
				$video.data('isPlaying',true);
				cto.MediaPlay(this.tracking.mediaName, e.target.currentTime);
			}));
			
			//seeked・seekingイベントはiphoneのsafariでは発生しない。
			$video.bind('seeking',$.scope(this,function(e){
				$.log('-------------------- video.seek... --------------------');
				//$.log('currentTime : '+e.target.currentTime);
			}));
			
			$video.bind('pause',$.scope(this,function(e){
				$.log('-------------------- video.pause --------------------');
				//$.log('currentTime : '+e.target.currentTime);
				//digMediaPlay(mediaName, mediaOffset, contentType);
				$video.data('isPlaying',false);
				cto.MediaStop(this.tracking.mediaName, e.target.currentTime);
			}));
			
			//ページ推移イベントはunbindする
			/* ページ推移のイベントも取れない。
			if(this.tracking.beforeUnloadFnc){
				$(window).unbind('beforeunload',this.tracking.beforeUnloadFnc);
			}
			
			this.tracking.beforeUnloadFnc = $.scope(this, function(e){
				if($video.data('isPlaying')==true){
					cto.MediaStop(this.tracking.mediaName, $video.get(0).currentTime);
				}
				//alert('ページ推移します'+$video.get(0).currentTime)
				cto.MediaClose(this.tracking.mediaName);
			});
			
			$(window).bind('beforeunload',this.tracking.beforeUnloadFnc);
			*/
			
			///////////// シェアボタンイベント /////////////
			
			$('.hm_btn_related a')
			
			//前のイベント殺す
			if(this.sharebtnEvFnc.length){
				$('.hm_btn_embeds a').unbind(bindClick,this.sharebtnEvFnc[0]);
				$('.hm_btn_share a').unbind(bindClick,this.sharebtnEvFnc[1]);
				$('.hm_btn_related a').unbind(bindClick,this.sharebtnEvFnc[2]);
			}
			
			this.sharebtnEvFnc = 
			[
				$.scope(this,function(e){
					e.preventDefault();
					// hmc.$w2.relTabWindow.close(2);
					// hmc.$w1.relTabWindow.open(1,
						// item.find('copy > displayName').text(),
						// item.find('share > movieId').text(),
						// item.find('share > relatedVideo').text(),
						// $('#hm_carousel').position().top-2
					// )
					// $('#hm_btn_embeds_bg').width(480);
				}),
				$.scope(this,function(e){
					e.preventDefault();
					// hmc.$w1.relTabWindow.close(1);
					// hmc.$w2.relTabWindow.open(2,
						// item.find('copy > displayName').text(),
						// item.find('share > movieId').text(),
						// item.find('share > relatedVideo').text(),
						// $('#hm_carousel').position().top-2
					// )
					// $('#hm_btn_share_bg').width(480);
				}),
				$.scope(this,function(e){
					// alert(e.target.name);
					cto.tracklink(e.target);
				})
			]
			
			
			// $('.hm_btn_embeds a')
				// .attr('href',void0)
				// .bind(bindClick,this.sharebtnEvFnc[0]);
				
			// $('.hm_btn_share a')
				// .attr('href',void0)
				// .bind(bindClick,this.sharebtnEvFnc[1]);
			
			$('.hm_btn_related a')
				.attr('href',item.find('share > relatedVideo').text())
				.attr('type','custom')
				.attr('name',trackingPrefix+placementCode+'-relatedvideo')
				.bind('click',this.sharebtnEvFnc[2]);
			
			
			
			return true;
		}
	});
	
	
	// hmc={
		// $w1 : $('<div id="hmctw1" class="hmcWindow"></div>'),
		// $w2 : $('<div id="hmctw2" class="hmcWindow"></div>')
	// }
	// hmc.$w1.relTabWindow({id:1})
	// hmc.$w2.relTabWindow({id:2})
	
	
	
	// Add DropShadow
	$(".dropsdw2x2_484").prepend('<span class="after" />');
	
	
	
	///////////////////////////////////////////////////////////////
	//embeds movie player.
	///////////////////////////////////////////////////////////////
	
	
	var api =  '';
	var api2 = '';
	
	(function () {
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		var _d = '';
		for(var i = 0; i <hashes.length; i++) {
			var hash = hashes[i].split('=');
			if (hash[0] == '_d') _d = hash[1];
		}
		api =  '/api/?act=PR_FEATURE&ch_id=4&_d='+_d;
		api2 = '/api/?act=PR_FEATURE%26ch_id=4%26_d='+_d+'%26_t=';
	})();
	
	
	var flashvars = {
		firstPlayVideoId:"",
		confPath:(typeof(playlistXMLPath)!="undefined" && playlistXMLPath!="") ? playlistXMLPath:api //path from this html.
//		confPath:(typeof(playlistXMLPath)!="undefined" && playlistXMLPath!="") ? playlistXMLPath:"hero-module/playlist.xml" //path from this html.
		
	},
	params = {
		allowScriptAccess:'always',
		wmode:'transparent',
		play: "true",
		loop: "false",
		menu: "false",
		quality: "best",
		scale: "noscale",
		allowFullScreen: "true",
		salign: "lt"//,
		//base: "../res710/images/module/player-category/"
	},
	attributes = { };
	
	
	var $pTarg = $('#movie_player')
	,ipadInit = $.scope(this,function(d){
		
		//parseXML'n jQuery.
		$playlist = $(d);
		
		//Init Classes.
		vView = new gClasses.VideoView('movie_player',$playlist);
		vControl = new gClasses.VideoController($playlist,vView);
		
	})
	,pcInit = $.scope(this,function(d){
		
		//parseXML'n jQuery.
		$playlist = $(d);
		
		//$('#movie_player').html();
		if(chkFlaVer() == 0){
			
			$('#movie_player .noflash').css("display","block");
			
		}else{
			
			var fvPrefix = "#/?v="
			var fvid = window.location.href;
			if(fvid.lastIndexOf(fvPrefix)!=-1){
				fvid = fvid.substr(fvid.lastIndexOf(fvPrefix)+fvPrefix.length);
				if(fvid.lastIndexOf("&")!=-1) fvid = fvid.substr(0,fvid.lastIndexOf("&"));
				flashvars.firstPlayVideoId = fvid
			}
			flashvars.confPath = api2;
			swfobject.embedSWF("hero-module/hero-module-category-tvxd.swf", "movie_player", "480px", "434px", "10", "hero-module/js/expressInstall.swf", flashvars, params, attributes);
		}
	})
	;
	////////////////////////////// iPhone/iPad or PC //////////////////////////////
	if(spAgent.iPhone || spAgent.iPad){
		$pTarg.css({'height':'auto'});	
	}
	$.ajax({
		url: flashvars.confPath
		,dataType:'xml'
		,success: (spAgent.iPhone || spAgent.iPad) ? ipadInit : pcInit
		,error:function(request, status, eThrown){
			$pTarg.html(status);
		}
	});
	
	
});
