

/*========================================
* Init
========================================*/
$(function() {
	var img = new Image();
	img.src = "images/choice-head.gif";
	
	//Thickbox
	$("div#CONTAINER").after('<div id="DetailWrapper" style="display:none"><div style="padding-top:10px;"><a href="#" class="closeTB"><img src="/images/common/tb-close.gif" width="60" height="20" alt="閉じる" /></a></div><iframe name="DetailFrame" id="DetailFrame" frameborder="0" src="" width="670" height="520"></iframe></div>');
	$("a.closeTB").css("cursor","hand	");
	$("a.closeTB").click(function() {
		tb_remove();
		return false;
	});
	
});

/*========================================
* Choice
* for ActionScript's ExternalInterface
========================================*/

var choiceCount = 0;


function choice( i_selectedData, i_otherData ) {
	choiceCount++;
	var selectedImage = i_selectedData["imgURLM"];
	var selectedLink = i_selectedData["link"];
	var selectedName = i_selectedData["name"];
	var otherImage = i_otherData["imgURLS"];
	var otherLink = i_otherData["link"];
	var otherName = i_otherData["name"];
	$("div.guide").replaceWith('<img src="images/choice-head.gif" width="580" height="90" alt="" class="choice-head" />');
	$("div.choice span").eq(0).replaceWith('<div><a href="'+selectedLink+'" id="Choice'+choiceCount+'" target="_blank" class="selected"><img src="'+selectedImage+'" width="100" height="100" alt="'+selectedName+'" /></a><a href="'+otherLink+'" id="Choice'+choiceCount+'" target="_blank" class="other"><img src="'+otherImage+'" width="30" height="30" alt="'+otherName+'" /></a></div>');
	$("div.choice a").click(function() {
		tb_show("商品詳細","#TB_inline?height=560&width=680&inlineId=DetailWrapper&modal=false","");
		$("iframe#DetailFrame")[0].src = this.href;
		$("#TB_title").hide();
		return false;
	});
	$("div.choice a.selected:last").hide();
	$("div.choice a.other:last").hide();
	$("div.choice a.selected:last").fadeIn();
	$("div.choice a.other:last").fadeIn();
	return true;
}

/*========================================
* Show Item detail by Item Id
* for ActionScript's ExternalInterface
========================================*/

function showDetail( i_link ) {
		tb_show("商品詳細","#TB_inline?height=560&width=680&inlineId=DetailWrapper&modal=false","");
		$("iframe#DetailFrame")[0].src = i_link;
		$("#TB_title").hide();
		return true;
}


/*========================================
* Result
* for ActionScript's ExternalInterface
========================================*/

function loadResult( i_gender, i_keyword, i_target, i_id ) {
	var params = [];
	var i;
	
	//Gender
	params.push(urlParam("g",i_gender));
	//Keywords
	for ( i=0; i<i_keyword.length; i++ ) params.push(urlParam("k%5b%5d",parseInt(i_keyword[i])));
	//Targets
	for ( i=0; i<i_target.length; i++ ) params.push(urlParam("t%5b%5d",parseInt(i_target[i])));
	
	//IDs
	for ( i=0; i<i_id.length; i++ ) params.push(urlParam("c",i_id[i]));
	
	//URL
	$.ajax({
		type:"GET",
		url:"result.php",
		data:params.join("&"),
		error:onAjaxError,
		success:onResultXMLLoad
	});
	
	return true;
}

function urlParam( i_key, i_val ) {
	return i_key+"="+escape(i_val);
}

function onResultXMLLoad(xml) {
	//
	$("#CONTENT-W h2").after('<div id="RECOMMEND-ITEMS" class="clearfix"><p>あなたの<em>チョイス</em>をもとにピックアップした<em>オススメアイテム</em>です！！</p></div>');
	var $div = $("div#RECOMMEND-ITEMS");
	$("item",xml).each(function(){
		var $this = $(this);
		$div.append('<a href="'+$this.attr("link")+'" id="Item'+$this.attr("id")+'"><img src="'+$this.attr("img")+'" width="125" height="125" alt="'+$this.attr("name")+'" /></a>');
	});
	//
	$("div#RECOMMEND-ITEMS a").click(function() {
		tb_show("商品詳細","#TB_inline?height=560&width=680&inlineId=DetailWrapper&modal=false","");
		$("iframe#DetailFrame")[0].src = this.href;
		$("#TB_title").hide();
		return false;
	});
	//
	$("div#RECOMMEND-ITEMS").hide();
	//
	//var swf= document.all? window["externalFlash"] : document["externalFlash"];
	var swf = $("#externalSwf").get(0);
	try{
		if ( swf ) swf.onLoadResult();
	} catch(e) {
		//alert(e);
	}
}

/*========================================
* showResult
* for ActionScript's ExternalInterface
========================================*/

function showResult() {
	$("div#RECOMMEND-ITEMS").slideDown(1000);
}

/*========================================
* scrollDown
* for ActionScript's ExternalInterface
========================================*/

function scrollDown() {
		$('html,body').animate({scrollTop: 423, scrollLeft:0}, 500);
}

function onAjaxError(i_request, i_textStatus, i_errorThrown) {
	alert("エラー：商品データを読み込めませんでした。");
}
/*========================================
* Embed Flash
========================================*/

$(function() {
	$("#HEAD").empty();
	$("#HEAD").append('<div id="externalFlash"></div>');
});

if ( swfobject.hasFlashPlayerVersion("9.0.124") ) {
	document.write('<style type="text/css">div#NOFLASH-MESSAGE {display:none;}</style>');
}
$(window).load(function() {
	swfobject.embedSWF("index.swf", "externalFlash", "100%", "423", "9.0.124", false, {}, {wmode:"transparent",allowScriptAccess:"sameDomain"}, {id:"externalSwf"});
});

