//------- Global variables ---------------------
var entryId = '';

//------ Pager settings ------------------------
var movieMaxRows = new Number(5); //maxRow Setting
var movieCount = new Number();
var movieStart = new Number();
var moviePage = new Number(1);

//------ Array Defaults ------------------------
var titleData = new Array();
var idData = new Array();
var hrefData = new Array();
var pubData = new Array();
var textData = new Array();
var pathData = new Array();
var thumbLData = new Array();
var thumbSData = new Array();
var commentData = new Array();

//--------------------------------

function movieFlashPlayer(file,pre,note){

	var flashvars = {
		file:file, 
		autostart:"true",
		image:pre,
		skin:"http://www.monoglobe.jp/common/player/skin.swf"
	}
	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always"
	}
	var attributes = {
		id:"player1",
		name:"player1"
	}

	swfobject.embedSWF("http://www.monoglobe.jp/common/player/player.swf", "product-movie", "456","342", "9.0.115", false, flashvars, params, attributes);

	$("#movie-comment").html(note);
}

function movieListLoad(){
	var fileName = 'http://www.monoglobe.jp/movie/index_list.xml';
	var loadingImg = 'http://www.monoglobe.jp/movie/images/loading_70.gif';
	var file = '';
	var pre = '';
	var note = '';
	var listResult = '';
	var moviePager = '';

	
	var iconS = new Array('http://www.monoglobe.jp/images/blog_movie_img.png','http://www.monoglobe.jp/images/blog_voice_img.png');

	thumbNailDisplay = function(e,i){
		var thumb = thumbSData[i];
		var img = document.getElementById(e);
		imgDisplay = function(){
			img.setAttribute('src',thumb);
		}
		setTimeout(imgDisplay,1000);
	},

	listDisplay = function(Page){
		listResult = '';
		moviePager = '';

		moviePage = Number(Page);
		if(movieCount > movieMaxRows){
			movieStart = (moviePage-1)*movieMaxRows;
			if(moviePage*movieMaxRows>movieCount){
				limit = movieCount;
			}else{
				limit = movieMaxRows*moviePage;
			}
		}else{
			movieStart = 0;
			limit = movieCount;
		}

		var nextRows = ((moviePage+1)*movieMaxRows > movieCount)? movieCount-(moviePage*movieMaxRows) : (moviePage+1)*movieMaxRows-(moviePage*movieMaxRows);
		if(movieCount > movieMaxRows && moviePage == 1){
			moviePager = '<div id="next-movie"><p><a href="javascript:void(0)" onclick="listDisplay(\''+(moviePage+1)+'\');return false;">次の'+nextRows+'件を見る</a></p></div>';
		}else if(movieCount > movieMaxRows && moviePage > 1 && moviePage*movieMaxRows<movieCount){
			moviePager = '<div id="next-movie"><p><a href="javascript:void(0)" onclick="listDisplay(\''+(moviePage-1)+'\');return false;">前の'+movieMaxRows+'件を見る</a> | <a href="javascript:void(0)" onclick="listDisplay(\''+(moviePage+1)+'\');return false;">次の'+nextRows+'件を見る</p></a></div> ';
		}else if(movieCount > movieMaxRows && moviePage > 1 && moviePage*movieMaxRows>=movieCount){
			moviePager = '<div id="next-movie"><p><a href="javascript:void(0)" onclick="listDisplay(\''+(moviePage-1)+'\');return false;">前の'+movieMaxRows+'件を見る</p></a></div>';
		}

		for(i=movieStart;i<limit;i++){
			listResult += '				<div class="product-thumbnail-wrap clearfix">';
			listResult += '					<a href="'+hrefData[i]+'"><img src="'+loadingImg+'" alt="'+titleData[i]+'" width="70" height="58" id="'+idData[i]+'" onload="thumbNailDisplay(this.id,\''+i+'\');" /></a>';
			listResult += '					<p class="color02"><a href="'+hrefData[i]+'">'+titleData[i]+'</a></p>';
			listResult += '					<p>'+commentData[i]+'</p>';
			listResult += '					<p>'+pubData[i]+'</p>';
			listResult += '				</div>';
		}

		listResult = (moviePager) ? listResult += moviePager : listResult ;

		$("#product-thumbnail").html(listResult);

	},

	loadList = function(XML){

		titleData = new Array();
		idData = new Array();
		hrefData = new Array();
		pubData = new Array();
		textData = new Array();
		pathData = new Array();
		thumbLData = new Array();
		thumbSData = new Array();
		commentData = new Array();

		$(XML).find('entry').each(function(){
			if($(this).find('id').text() != entryId){
				titleData.push($(this).find('title').text());
				idData.push($(this).find('id').text());
				hrefData.push($(this).find('href').text());
				pubData.push($(this).find('pub').text());
				textData.push($(this).find('text').text());
				pathData.push($(this).find('videoPath').text());
				thumbLData.push($(this).find('smbPathA').text());
				thumbSData.push($(this).find('smbPathB').text());
				commentData.push($(this).find('comment').text());
				movieCount++;
			}else{
				file = $(this).find('videoPath').text();
				pre = $(this).find('smbPathA').text();
				note = $(this).find('comment').text();
			}
		});
		movieFlashPlayer(file,pre,note)
		listDisplay(moviePage);
	}

	$.ajax({url:fileName,type:'get',dataType:'xml',cache:false,data:{},success:loadList})
}
function listScrollerUI(num){
	var cnt = Number(num) +1;
	var resultStr = '';
	resultStr += 'Files '+cnt+' of '+all;

	document.getElementById('blog-movie-scroller-str').innerHTML = resultStr;
}

function listNoteView(note){
	var txt = (note)? note : '&nbsp;';
	document.getElementById('blog-movie-note').innerHTML = txt;
}

function getParam(){
	var sc = new Array();
	sc = document.getElementsByTagName('script');

	for(i=0;i<sc.length;i++){
		var p = sc[i].getAttribute('src');
		var pReg = new RegExp('playMovieMovieSE\.js\?.*$');
		if(p && p.match(pReg)){
			var usr = p.split('?');
			entryId = usr[1].slice(usr[1].indexOf('=')+1);
			movieListLoad();
		}
	}
}

function number_format(num){
	return num.toString().replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,' );
}

jQuery.event.add(window,"load", getParam);