﻿/**
 *	@filename		otoku.js
 *	@charset		utf-8
 *	@created		October 16, 2009
 *	@lastmodified	October 16, 2009
 *	@description	おトクな情報ページフラッシュペーパー表示用
 */
(function(){

var Flyer = function(obj){
	var jButtonFace = $("li.buttonViewFace a", obj);
	var jButtonBack = $("li.buttonViewBack a", obj);
	
	this.title = obj.getElementsByTagName("h3").item(0).innerHTML;
	this.notes = $("p.note", obj).get(0).innerHTML;
	this.html = this.createHTML();
	
	this.htmlFace = this.createFlashHTMLFragment(jButtonFace.get(0).href);
	this.htmlBack = this.createFlashHTMLFragment(jButtonBack.get(0).href);
	
	jButtonFace.click(this.setHandler(true));
	jButtonBack.click(this.setHandler(false));
};

Flyer.window = null;

Flyer.isIE = document.all && !window.opera;

Flyer.WIN_WIDTH = 600;
Flyer.WIN_HEIGHT = 600;

Flyer.SWF_WIDTH = "100%";
Flyer.SWF_HEIGHT = (window.screen.height - 250);

Flyer.prototype.setHandler = function(isFace){
	var self = this;
	return function(){
		if(Flyer.window){
			Flyer.window.close();
		}
		Flyer.window = window.open("", "", "width=" + Flyer.WIN_WIDTH + ",height=" + Flyer.WIN_HEIGHT + ",resizable=yes,menubar=no,status=no,scrollbars=yes,location=no");
		var d = Flyer.window.document;
		d.open();
		d.write(self.html);
		d.close();
		var onloadHandler = function(){
			
			var button = d.getElementById("buttonView");
			var flash = d.getElementById("flash");
			
			d.body.id = isFace ? "face" : "back";
			if(isFace){
				flash.innerHTML = self.htmlFace;
			}else{
				flash.innerHTML = self.htmlBack;
			}
			
			$(button).click(function(){
				if(d.body.id === "face"){
					d.body.id = "back";
					flash.innerHTML = self.htmlBack;
				}else{
					d.body.id = "face";
					flash.innerHTML = self.htmlFace;
				}
				return false;
			});
			
			var domReady = d.getElementById("domReady");
			domReady.parentNode.removeChild(domReady);
			domReady = null;
		};
		
		//IE で Flyer.window.onloadが発火しないため以下のようにする
		(function(){
			if(Flyer.window.DOM_READY === true){
				onloadHandler();
			}else{
				window.setTimeout(arguments.callee, 0);
			}
		})();
		Flyer.window.focus();
		return false;
	};
};

Flyer.prototype.createHTML = function(){
	var html = [
		'<?xml version="1.0" encoding="utf-8"?>',
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
		'<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">',
		'<head>',
		'<meta http-equiv="X-UA-Compatible" content="IE=Emulate7" />',
		'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
		'<meta http-equiv="Content-Style-Type" content="text/css" />',
		'<meta http-equiv="Content-Script-Type" content="text/javascript" />',
		'<meta http-equiv="Imagetoolbar" content="no" />',
		'<meta name="robots" content="index,follow" />',
		'<meta name="author" content="株式会社サンデー" />',
		'<meta name="copyright" content="copyright &copy; 2009 SUNDAY co.,ltd. all rights reserved." />',
		'<link rel="stylesheet" type="text/css" href="../css/set.css" media="screen,print" />',
		'<link rel="stylesheet" type="text/css" href="../css/contents/otoku.css" media="screen,print" />',
		'<script type="text/javascript" src="../js/common/jquery-1.2.6.js"></script>',
		'<script type="text/javascript" src="../js/conmmon/lib.js"></script>',
		'<title>', this.title, ' | おトクな情報 | サンデーホームセンター</title>',
		'</head>',
		'<body class="flyer">',
			'<!-- [▼section] -->',
			'<div id="section1" class="section">',
				'<h1>', this.title, '</h1>',
				'<p class="note">', this.notes, '</p>',
				'<div id="flash"></div>',
				'<div id="buttonWrapper"><p id="buttonView"><a href="#">別面を見る</a></p></div>',
			'</div>',
			'<!-- [▲section] -->',
			'<script type="text/javascript" id="domReady">window.DOM_READY = true;</script>',
		'</body>',
		'</html>'
	].join("");
	return html;
};

Flyer.prototype.createFlashHTMLFragment = function(file){
	var html = [
		'<object width="', Flyer.SWF_WIDTH, '" height="', Flyer.SWF_HEIGHT, '" id="index"', Flyer.isIE ? '' : ('data="' + file + '"'), ' type="application/x-shockwave-flash">',
			'<param name="allowScriptAccess" value="sameDomain" />',
			'<param name="allowFullScreen" value="false" />',
			'<param name="movie" value="', file, '" />',
			'<param name="quality" value="high" />',
			'<param name="bgcolor" value="#ffffff" />',
			'<param name="menu" value="false" />',
		'</object>'
	].join("");
	return html;
};

$(function(){
	$("div.flyer").each(function(){
		new Flyer(this);
	});
});

})();