//サイト毎固有情報定義
var per_page = 12;		//１ページあたりの表示件数

//内部利用変数定義
var movie_take_count = 0;
var movie_first = true;
var movie_loadOk = false;
var movie_safe_search = "active";		//googleリクエストのセーフサーチ：高→"active" 中→"" なし→"off"
var movie_count = 0;
var amazon_first = true;

//描画処理
function movie_dataDisplay(){
	if (movie_loadOk == false) {
		//データ読み込みが完了していなければループ
		setTimeout(movie_dataDisplay, 500);
		return false;
	}
	movie_loadOk = false;

	list = video_search.get();
	makeThumb(list);

	ret = false;
//	if(list.length < per_page && list.length != movie_take_count && movie_take_count > -1 && list.length > 0){
//		movie_take_count = (movie_take_count ? movie_take_count : per_page) - list.length;
	var is_request = true;
	if(movie_take_count < per_page ){
		is_request = video_search.is_request();
		is_poolData = video_search.is_poolData();
		if(is_request == true || is_poolData == true){
			movie_first = true;
			ret = video_search.search(word, per_page);
			if(ret == false){
				return;
			}
		}
	}
	//ページングナビ表示
//	if(movie_take_count > -1 && list.length > 0 && !ret){
	if(is_request == true || is_poolData == true){
		$j("#movie_list .more2").show();
	}
	movie_take_count = 0;
	//ローディング非表示
	$j("#movie_list div.loading").css("display", "none");
	//総件数表示
	$j("#movie_list p.count strong").text(video_search.getResultCount());

	//次ページのデータを取得
	movie_first = false;
	if(list.length > 0){
		video_search.nextpage(word);
	}
	movie_loadOk = true;

	//amazon商品取得
	if(amazon_first){
		if(typeof getAmazonData == 'function') {
			getAmazonData();
		}
		amazon_first = false;
		//ドコイク広告の表示
		if(typeof getDokoikuXML == 'function') {
			dokoikuXMLpartsHide(); //Googleのみの人だとなぜかローディング回りっぱなしになるのでとりあえず対応
			//getDokoikuXML();
		}
	}
}

//除外設定追加
function insertExcludeMovie(event){
	var pid = event.id;
	var site = event.alt;
	var vid = event.name;
	var ttl = event.value;
	var img = event.src;

	//除外リストに追加
	var $tmp = new Array();
	$tmp['site'] = site;
	$tmp['vid'] = vid;
	$tmp['ttl'] = ttl;
	$tmp['img'] = img;
	movie_excludeList.push($tmp);

	//表示上の削除
	var ul = event.parentNode.parentNode.parentNode;
	var li = event.parentNode.parentNode;
	ul.removeChild(li);

	//除外リストに追加
	$j.ajax({
		dataType: "POST",
		data: {
			"act":'api_excludemovie',
			"mode":'ins',
			"pid":pid,
			"sid":site,
			"vid":vid,
			"ttl":ttl,
			"img":img
		},
		url: "/index.php"
	});

	//除外リストへのリンク表示切替
	switchMovieExclude();

	//連番の振りなおし
	var lp = 1;
	$j(".movie .heightLine>a").each(function(){
		lp++;
		this.rel = parseInt(lp / 2);
	});

	return false;
}

//除外設定削除
function deleteExcludeMovie(event){
	var pid = event.id;
	var site = event.alt;
	var vid = event.name;
	var ttl = event.value;
	var img = event.src;

	//除外リストから削除
	var len = movie_excludeList.length - 1;
	var i;
	for(i = len; i >= 0; i--){
		if(movie_excludeList[i]['site'] == site && movie_excludeList[i]['vid'] == vid){
			movie_excludeList.splice(i,1);
		}
	}

	//表示上の削除
	var ul = event.parentNode.parentNode.parentNode;
	var li = event.parentNode.parentNode;
	ul.removeChild(li);

	//除外リストに追加
	$j.ajax({
		dataType: "POST",
		data: {
			"act":'api_excludemovie',
			"mode":'del',
			"pid":pid,
			"sid":site,
			"vid":vid,
			"ttl":ttl,
			"img":img
		},
		url: "/index.php"
	});

	//除外リストへのリンク表示切替
	switchMovieExclude();

	//連番の振りなおし
	var lp = 1;
	$j(".movie .heightLine>a").each(function(){
		lp++;
		this.rel = parseInt(lp / 2);
	});

	return false;
}

//サムネイル上ボタンイベント設定
function setMovieThumBtnEvent(){
	$j(".movie .heightLine .act_img").hide();
	$j(".movie .heightLine").unbind("mouseover");
	$j(".movie .heightLine").unbind("mouseout");
	$j(".movie .heightLine").bind("mouseover", function(){
		$j(".act_img" ,this).show();
	});
	$j(".movie .heightLine").bind("mouseout", function(){
		$j(".act_img" ,this).hide();
	});
}


//除外リスト描画処理
var is_excludeMovie = false;
function excludeMovie_Display(){

	if(is_excludeMovie == true){
		$j('#movie_list .switch a').html("&gt;  削除されている動画一覧");
		is_excludeMovie = false;
		var ul = $j('.movie ul');
		ul.empty();
		movie_index = 0;
		video_search = new cls_video_search();
		//取得データ初期化
		cls_youtube_video.youtube_data = null;
		cls_youtube_video.ameba_data = null;
		cls_youtube_video.zoome_data = null;
		cls_youtube_video.youtube_pool_data = null;
		cls_ameba_video.ameba_pool_data = null;
		cls_zoome_video.zoome_pool_data = null;
		movie_first=true;
		$j("#movie_list div.loading").css("display", "block");
		$j("#movie_list .more2").hide();
		video_search.search(word, per_page);

		return;
	}

	is_excludeMovie = true;
	$j('#movie_list .switch a').html("戻る");
	$j("#movie_list .more2").hide();

	var ul = $j('.movie ul');
	ul.empty();
	movie_index = 0;
	for(var i=0; i<movie_excludeList.length; i++){
		var id = movie_excludeList[i]['vid'];
		var site = movie_excludeList[i]['site'];
		var title = movie_excludeList[i]['ttl'];
		var image = movie_excludeList[i]['img'];

		var url = './'

		// li 要素の生成
		var li = document.createElement('li');
		li.className = "heightLine";

		// a 要素の生成
		var atag = document.createElement('a');
		var src = '';
		if(site =='zm'){
			src = 'http://zoome.jp/' +id;
		}else if(site == 'am'){
			src = 'http://vision.ameba.jp/watch.do?movie=' +id;
		}else if(site == 'yt'){
			src = 'http://www.youtube.com/watch?v=' +id;
		}

		atag.href = src;
		atag.rel = ++movie_index;
		atag.title = title;
		atag.className = 'thumb lightbox-enabled';

		// タイトル部分の a 要素の生成
		var aTitle = document.createElement('a');
		aTitle.href = src;
		aTitle.rel = movie_index;
		aTitle.title = title;
		aTitle.className = 'lightbox-enabled';

		// img 要素の生成
		var img = document.createElement('img');
		img.src = image;
		img.alt = title;
		atag.appendChild(img);

		//タイトル設定
		aTitle.appendChild(document.createTextNode(title));

		var menu_div = document.createElement('div');
		menu_div.className = 'act_img';
		//復活ボタン
		var back_atag = document.createElement('a');
		back_atag.title = '戻す';
		back_atag.innerHTML = '戻す';
		back_atag.className = 'rac';
		back_atag.id = detail_id;
		back_atag.name = id;
		back_atag.alt = site;
		back_atag.src = image;
		back_atag.value = title;
		menu_div.appendChild(back_atag);

		li.appendChild(atag);
		li.appendChild(aTitle);
		li.appendChild(menu_div);

		ul.append(li);
	}
	//LightBox配置
	$j(".movie .heightLine a").unbind("click");
	$j(".movie .heightLine a").bind("click", function(){movieClick(this); return false;});

	$j(".movie .rac").unbind("click");
	$j(".movie .rac").bind("click", function(){deleteExcludeMovie(this); return false;});

	//サムネイル上ボタン
	setMovieThumBtnEvent();

}

//以下基本的に編集の必要は無いはず-------------------------------------------

function makeThumb(list){
//	var ul = $j('#movie_list ul');
	var ul = $j('.movie ul');
	for(i=0; i<list.length; i++){
		movie_take_count++;
		var image = list[i];

		// li 要素の生成
		var li = document.createElement('li');
		li.className = "heightLine";

		// a 要素の生成
		var atag = document.createElement('a');
		atag.href = image['video'];
		atag.rel = $j(".movie .heightLine .thumb").length + 1;
		atag.title = image['name'];
		atag.className = 'thumb lightbox-enabled';

		// img 要素の生成
		var img = document.createElement('img');
		img.src = image['thumb'];
		img.alt = image['name'];
		atag.appendChild(img);

		// タイトル部分の a 要素の生成
		var aTitle = document.createElement('a');
		aTitle.href = image['video'];
		aTitle.rel = $j(".movie .heightLine .thumb").length + 1;
		aTitle.title = image['name'];
		aTitle.className = 'lightbox-enabled';

		//タイトル設定
		aTitle.appendChild(document.createTextNode(image['name']));

		//除外ボタン
		var menu_div = document.createElement('div');
		menu_div.className = 'act_img';

		//除外
		var exclude_atag = document.createElement('a');
		exclude_atag.className = 'iac';
		exclude_atag.title = 'この動画を表示しない'; 
		exclude_atag.id = detail_id;
		exclude_atag.src = image['thumb'];
		exclude_atag.value = image['name']; 

		var site = "";
		var id = "";
		var re = new RegExp('^http://(www.youtube.com)', "i");
		if(image['video'].search(re) != -1){
				site = 'yt';
				id = image['id'];
		}

		if(site ==''){
			var re = new RegExp('^http://(vision.ameba.jp)', "i");
			if(image['video'].search(re) != -1){
				site = 'am';
				id = image['id'];
			}
		}
		if(site ==''){
			var re = new RegExp('^http://(zoome.jp)', "i");
			if(image['video'].search(re) != -1){
					site = 'zm';
					var ret = image['video'].match(/http:\/\/zoome\.jp\/(.*)$/i);
					if(ret[1] != ''){
						id = RegExp.$1;
					}
			}
		}
		exclude_atag.alt = site;
		exclude_atag.name = id;
		menu_div.appendChild(exclude_atag);


		li.appendChild(atag);
		li.appendChild(aTitle);
		li.appendChild(menu_div);
		ul.append(li);

	}
	//LightBox配置
	$j(".movie .heightLine a").unbind("click");
	$j(".movie .heightLine a").bind("click", function(){movieClick(this); return false;});

	$j(".movie .iac").unbind("click");
	$j(".movie .iac").bind("click", function(){insertExcludeMovie(this); return false;});

	//サムネイル上ボタン
	setMovieThumBtnEvent();

}

function movie_init(word ,yt_word ,am_word ,zm_word) {

	//除外リストへのリンク表示切替
	switchMovieExclude();

	video_search = new cls_video_search();
	video_search.word = word;
	video_search.yt_word = yt_word;
	video_search.am_word = am_word;
	video_search.zm_word = zm_word;
	movie_initAjaxStop();
	video_search.search(word, per_page);
}

function movie_initAjaxStop(){
	//ページングナビ非表示
	$j("#movie_list .more2").hide();

	//取得結果を表示
	$j("#movie_list").ajaxStop(function(){
		movie_loadOk = true;
		if(movie_first) {
			$j("#movie_list .more2 a").click();
		}
	});

	$j("#movie_list .more2 a").click(function(){
		$j("#movie_list div.loading").css("display", "block");
		$j("#movie_list .more2").hide();

		setTimeout(movie_dataDisplay, 0);

		return false;
	});
}
/*
 * 除外リストへのリンク切り替え
 */
function switchMovieExclude(){
	if(movie_excludeList.length == 0){
		//除外リンク非表示
		$j("#movie_list .switch a").hide();
		//除外リストを表示中のとき
		if($j("#movie_list .switch a").html() == "戻る"){
			//通常画面に遷移させる
			excludeMovie_Display();
		}
	}else{
		//除外リンク表示
		$j("#movie_list .switch a").show();
	}
}
