/**
 * 
 * top.js - トップページ関連Ajax処理の実行用スクリプト
 * 
 * ○変更履歴
 * 2011.02.09 h.adachi：新規作成
 * 2011.02.15 h.adachi：オブジェクト名変更
 * 2011.03.07 h.adachi：ホット!!表示処理追加
 * 2011.09.09 h.adachi：サムネイル高さ指定削除
 *
 */

// クラス名宣言
topDisplay = function() {};
// オブジェクト生成
var top_obj = new topDisplay();

// DOMLoadイベント
$(document).ready(function($) {
	// 実行関数名リスト
	var FuncList = new Array(
		top_obj.dispHot,
		top_obj.dispMovie00,
		top_obj.dispMovie00,
		top_obj.dispFeature00,
		top_obj.dispFeature00,
		top_obj.dispReport00,
		top_obj.dispReport00,
		common.dispNews01,
		common.dispRanking
	);
	// 以下、Ajax表示処理
	var len = top_obj.xmlList.length;
	for(var i=0; i<len;i++) common.parseXML(top_obj.xmlList[i], top_obj.divList[i], FuncList[i]);
});

// ロードXMLリスト
topDisplay.prototype.xmlList = new Array(
	'/top2010/xml/index_hot.xml',
	'/common/xml/index_recommend08.xml',
	'/common/xml/index_recommend_genre08.xml',
	'/common/xml/index_specialbanner08.xml',
	'/common/xml/index_special08.xml',
	'/common/xml/index_dai208.xml',
	'/common/xml/index_dai208.xml',
	'/common/xml/index_whatsnew08.xml',
	'/common/xml/index_ranking08.xml'
);
// 出力先divリスト
topDisplay.prototype.divList = new Array(
	'hotArea_00',
	'movieArea_01',
	'movieArea_02',
	'featureArea_03',
	'featureArea_02',
	'reportArea_01',
	'reportArea_02',
	'newsContent02',
	'rankingArea01'
);
// ジャンル名リスト
topDisplay.prototype.GenreName = new Array(
	'',
	'ドラマ/映画',
	'ニュース',
	'スポーツ',
	'バラエティ',
	'情報番組',
	'音楽',
	'特別企画',
	'特別企画'
);

/* 以下、各パートの表示用関数 */

// ホット!!
topDisplay.prototype.dispHot = function(div, xml){
	var str = '';
	var max_len = 3, count = 0;
	var list_flg = false;
	
	str += '<ul class="img">\n';
	// ループ開始
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// オススメ動画
topDisplay.prototype.dispMovie01 = function(div, xml){
	var str = '';
	var max_len = 3, count = 0;
	var list_flg = false;
	
	str += '<ul>\n';
	// ループ開始
	$(xml).find('recommendation').each(function(){
		var title    = $(this).find('title').text();
		var desc     = $(this).find('description').text();
		var link     = $(this).find('link_www').text();
		var target   = $(this).find('target').text();
		var thumb    = $(this).find('thumbnail_url').text();
		var alt      = $(this).find('thumbnail_url')[0].attributes[0].nodeValue;
		var icon     = $(this).find('genreiconurl').text();
		var genre_no = $(this).find('genre').text();
		//var thumb_html = common.MakeImg(thumb, 210, 158, alt, 0);
		var thumb_html = common.MakeImg(thumb, null, null, alt, 0);
		var icon_html  = common.MakeImg(icon, null, null, top_obj.GenreName[genre_no], 0);
		
		list_flg = true, count++;
		str += (count < max_len)? '<li class="space10px">\n' : '<li>\n';
		str += '<a href="' + link + '" target="' + target+ '">' + thumb_html + '</a>\n';
		str += icon_html + '\n';
		str += '<h4><a href="' + link + '" target="' + target+ '">' + title + '</a></h4>\n';
		str += '<p>' + desc + '</p>\n';
		str += '</li>\n';
		if(count >= max_len) return false;
	});
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// オススメ動画(ジャンル別)
topDisplay.prototype.dispMovie02 = function(div, xml){
	var str = '';
	var max_len = (2*3), count = 0;
	var list_flg = false;
	
	str += '<ul>\n';
	// ループ開始
	$(xml).find('recommendation').each(function(){
		var desc     = $(this).find('description').text();
		var link     = $(this).find('link_www').text();
		var target   = $(this).find('target').text();
		var thumb    = $(this).find('thumbnail_url').text();
		var alt      = $(this).find('thumbnail_url')[0].attributes[0].nodeValue;
		var genre_no = $(this).find('genre').text();
		var genre_en = $(this).find('genreimgurl').text();
		var detail   = $(this).find('detail_www').text();
		//var thumb_html = common.MakeImg(thumb, 80, 60, alt, 0);
		var thumb_html = common.MakeImg(thumb, null, null, alt, 0);
		
		list_flg = true, count++;
		if(count%2==1 && count<=4){
			str += '<li class="' + genre_en + ' space10px">\n';
		}else if(count%2==1){
			str += '<li class="' + genre_en + '">\n';
		}
		if(count%2==1) str += '<h4>' + top_obj.GenreName[genre_no] + '</h4>\n';
		
		str += '<div class="category_img">\n';
		str += '<a href="' + link + '" target="' + target+ '">' + thumb_html + '</a>\n';
		str += '<p>' + desc + '</p>\n';
		str += '</div>\n';
		
		if(count%2==0){
			str += '<div class="more"><a href="' + detail + '" target=' + target+ '"></a></div>\n';
			str += '</li>\n';
		}
		if(count >= max_len) return false;
	});
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// 特集バナー
topDisplay.prototype.dispFeature01 = function(div, xml){
	var pattern = parseInt($(xml).find('pattern').text(), 10);
	var img_no  = $(xml).find('specialbanner').length;
	// パターン別使用画像数
	var use_img = new Array(4, 1, 2, 2, 3, 3, 3, 3);
	// パターン別使用画像数よりデータ内画像数が少ない場合、使用画像数をデータ内画像数に変更
	if(use_img[pattern-1] > img_no){ use_img[pattern-1] = img_no };
	
	// specialbanner_listのspecialbanner数分表示
	var str = '';
	var count = 0;
	var list_flg = false;
	
	str += '<table width="650" cellspacing="0" cellpadding="0">';
	str += '<tbody>';
	// ループ開始
	$(xml).find('specialbanner').each(function(){
		list_flg = true;
		if(count==0){
			str += '<tr>';
		}else if(count==1 && (pattern == 3 || pattern == 7)){
			str += '<tr>';
		}else if(count==2 && (pattern == 1 || pattern == 5 || pattern == 6 || pattern == 8)){
			str += '<tr>';
		}
		// バナー設定
		str += top_obj.SetBannerClass($(this), (count+1), pattern);
		if(count==use_img[pattern-1]-1){
			str += '</tr>';
		}else if(count==0 && (pattern == 3 || pattern == 7)){
			str += '</tr>';
		}else if(count==1 && (pattern == 1 || pattern == 5 || pattern == 6 || pattern == 8)){
			str += '</tr>';
		}
		count++;
		if(count >= use_img[pattern-1]) return false;
	});
	str += '</tbody>';
	str += '</table>';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// 特集動画
topDisplay.prototype.dispFeature02 = function(div, xml){
	var str = '';
	var max_len = 4, count = 0;
	var list_flg = false;
	
	str += '<ul>\n';
	// ループ開始
	$(xml).find('special').each(function(){
		var title    = $(this).find('title').text();
		var desc     = $(this).find('description').text();
		var link     = $(this).find('link_www').text();
		var target   = $(this).find('target').text();
		var thumb    = $(this).find('thumbnail_url').text();
		var alt      = $(this).find('thumbnail_url')[0].attributes[0].nodeValue;
		var icon     = $(this).find('genreiconurl').text();
		var genre_no = $(this).find('genre').text();
		//var thumb_html = common.MakeImg(thumb, 155, 116, alt, 0);
		var thumb_html = common.MakeImg(thumb, null, null, alt, 0);
		var icon_html  = common.MakeImg(icon, null, null, top_obj.GenreName[genre_no], 0);
		
		list_flg = true, count++;
		str += (count < max_len)? '<li class="space10px">\n' : '<li>\n';
		str += '<a href="' + link + '" target="' + target+ '">' + thumb_html + '</a>\n';
		str += icon_html + '\n';
		str += '<h4><a href="' + link + '" target="' + target+ '">' + title + '</a></h4>\n';
		str += '<p>' + desc + '</p>\n';
		str += '</li>\n';
		if(count >= max_len) return false;
	});
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// 第2報道部(1段目)
topDisplay.prototype.dispReport01 = function(div, xml){
	var str = '';
	var max_len = 2, count = 0;
	var list_flg = false;
	
	str += '<ul>\n';
	// ループ開始
	$(xml).find('dai2').each(function(){
		var title    = $(this).find('title').text();
		var desc     = $(this).find('description').text();
		var link     = $(this).find('link_www').text();
		var target   = $(this).find('target').text();
		var thumb    = $(this).find('thumbnail_url').text();
		var alt      = $(this).find('thumbnail_url')[0].attributes[0].nodeValue;
		var thumb_html = common.MakeImg(thumb, 120, 90, alt, 0);
	
		list_flg = true, count++;
		str += (count < max_len)? '<li class="space10px">\n' : '<li>\n';
		str += '<a href="' + link + '" target="' + target+ '">' + thumb_html + '</a>\n';
		str += '<p>' + desc + '</p>\n';
		str += '<h4>' + title + '</h4>\n';
		str += '</li>\n';
		if(count >= max_len) return false;
	});
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

// 第2報道部(2段目)
topDisplay.prototype.dispReport02 = function(div, xml){
	var str = '';
	var start_no = 3;
	var max_len = 3, count = 0;
	var list_flg = false;
	
	var len = $(xml).find('dai2').length;
	if (len < start_no) return;
	
	str += '<ul>\n';
	// ループ開始
	$(xml).find('dai2').each(function(){
		count++;
		if(count >= start_no){
			var title    = $(this).find('title').text();
			var desc     = $(this).find('description').text();
			var link     = $(this).find('link_www').text();
			var target   = $(this).find('target').text();
			var thumb    = $(this).find('thumbnail_url').text();
			var alt      = $(this).find('thumbnail_url')[0].attributes[0].nodeValue;
			var thumb_html = common.MakeImg(thumb, 210, 60, alt, 0);
			
			list_flg = true;
			str += (count < (start_no+max_len-1))? '<li class="space10px">\n' : '<li>\n';
			str += '<a href="' + link + '" target="' + target+ '">' + thumb_html + '</a>\n';
			str += '<h4>' + title + '</h4>\n';
			str += '<p>' + desc + '</p>\n';
			str += '</li>\n';
			if(count >= (start_no+max_len-1)) return false;
		}
	});
	str += '</ul>\n';
	
	// 出力処理
	if(list_flg) $('#' + div).html(str);
}

/**
 * 特集バナー設定用関数
 */
topDisplay.prototype.SetBannerClass = function(data, position, pattern) {
	var str = "";
	
	switch(pattern){
		case 1:
			str += '<td class="xs' + position + '">';
			str += top_obj.MakeBanner(data, 323, 60);
			break;
		case 2:
			str += '<td>';
			str += top_obj.MakeBanner(data, 650, 124);
			break;
		case 3:
			str += '<td class="m' + position + '">';
			str += top_obj.MakeBanner(data, 650, 60);
			break;
		case 4:
			str += '<td class="s' + position + '">';
			str += top_obj.MakeBanner(data, 323, 124);
			break;
		case 5:
			if(position == 1){
				str += '<td rowspan="2" class="s1">';
				str += top_obj.MakeBanner(data, 323, 124);
			}else if(position == 2){
				str += '<td class="xs2">';
				str += top_obj.MakeBanner(data, 323, 60);
			}else if(position == 3){
				str += '<td class="xs4">';
				str += top_obj.MakeBanner(data, 323, 60);
			}
			break;
		case 6:
			if(position == 1){
				str += '<td class="xs1">';
				str += top_obj.MakeBanner(data, 323, 60);
			}else if(position == 2){
				str += '<td rowspan="2" class="s2">';
				str += top_obj.MakeBanner(data, 323, 124);
			}else if(position == 3){
				str += '<td class="xs3">';
				str += top_obj.MakeBanner(data, 323, 60);
			}
			break;
		case 7:
			if(position == 1){
				str += '<td colspan="2" class="m1">';
				str += top_obj.MakeBanner(data, 650, 60);
			}else if(position == 2){
				str += '<td class="xs3">';
				str += top_obj.MakeBanner(data, 323, 60);
			}else if(position == 3){
				str += '<td class="xs4">';
				str += top_obj.MakeBanner(data, 323, 60);
			}
			break;
		case 8:
			if(position == 1){
				str += '<td class="xs1">';
				str += top_obj.MakeBanner(data, 323, 60);
			}else if(position == 2){
				str += '<td class="xs2">';
				str += top_obj.MakeBanner(data, 323, 60);
			}else if(position == 3){
				str += '<td colspan="2" class="m2">';
				str += top_obj.MakeBanner(data, 650, 60);
			}
			break;
		default:
			break;
	}
	str += '</td>';
	return str;
}

/**
 * 特集バナーHTML描画用関数
 */
topDisplay.prototype.MakeBanner = function(data, width, height) {
	var str = "";
	var link   = data.find('link_www').text();
	var target = data.find('target').text();
	var thumb  = data.find('thumbnail_url').text();
	var alt    = data.find('thumbnail_url')[0].attributes[0].nodeValue;
	
	if(link != undefined && link != ''){
		// リンク先
		str += '<a href="' + link + '" ';
		// target属性
		if(target != undefined && target != '') str += 'target="' + target + '" ';
		str += '>';
		
		if(thumb != undefined && thumb != ''){
			str += '<img border="0" ';
			str += 'width="' + width + '" height="' + height + '" ';
			// 代替テキスト
			if(alt != undefined && alt != '') str += 'alt="' + alt + '" ';
			// 画像ファイル
			str += 'src="' + thumb + '" ';
			str += 'style="background-position: 50%; background-repeat: no-repeat"/>';
		}else{
			if(thumb != undefined && thumb != ''){
				str += '<img border="0" ';
				str += 'width="' + width + '" height="' + height + '" ';
				// 代替テキスト
				if(alt != undefined && alt != '') str += 'alt="' + alt + '" ';
				// 画像ファイル
				str += 'src="' + thumb + '" ';
				str += 'style="background-position: 50%; background-repeat: no-repeat;"/>';
			}
		}
		str += '</a>';
	}
	return str;
}

