﻿// JavaScript Document
(function($){
	$.viewBox ={};
	(function(){
		$.viewBox.current = -1;
		$.viewBox.data =[];
		$.viewBox.targetData = {};
		$.viewBox.show = function(ids,skip){
				if($.viewBox.data.length>0){
					$.each($.viewBox.data,function(){
						if(this.id == ids){
							$.viewBox.targetData = this;
							return false
						}
					});
					$("body").append($.viewBox.html.overlay_html);
					$("body").append($.viewBox.html.bg_html);
					if(skip){
						$.viewBox.current = 0;
						$("#overlay_background").fadeTo(0,0).fadeTo("slow",0.85,function(){
								$.viewBox.showform($.viewBox.current);
						});
					}else{
						$("#overlay").append($.viewBox.html.viewer_html());
						
						var attr = {
							data  : "/common/swf/viewer.swf",
							id : "external_viewer_swf",
							name : "external_viewer_swf",
							width : "470",
							height : "480"
						}
						var para = {
							menu : "false",
							quality : "high",
							scale : "noscale",
							wmode : "transparent",
							flashvars : "path="+$.viewBox.targetData.path
						}
						swfobject.createSWF(attr, para, "viewer_swf");
						
						$("#overlay_background").fadeTo(0,0).fadeTo("slow",0.85,function(){
								$("#view_box").height(0).animate({height:"520px"},"slow","");
						});
					}
					$("#overlay").bind("click",function(e){
							if(e.target == this){
								$.viewBox.close();
							}
					});
					return;
				}
		}
		$.viewBox.close = function(){
				$("#overlay > div").animate({height:"0px"},"slow","",function(){
					$("#overlay_background").fadeOut("slow",function(){
						swfobject.removeSWF("viewer_swf");
						$("#overlay").remove();
						$(this).remove();
					});
				});
				$.viewBox.targetData ={};
				$.viewBox.current = -1;
				
				$("#overlay").unbind("click");
				return;
		};
		$.viewBox.next = function(){
			$.viewBox.current++
			if($.viewBox.current>0){
				$("#overlay > div > *").remove();
				setTimeout(function(){
					$.viewBox.showform($.viewBox.current);
				},0);
			}else{
				$("#overlay > div").animate({height:"0px"},"slow","",function(){
						$(this).remove();
						setTimeout(function(){
							$.viewBox.showform($.viewBox.current);
						},0);
				});
			}
		};
		
		$.viewBox.showform = function(num){
			switch(num){
				case 0 :
					$("#overlay").load("product_form_befor.html #product_form_befor",{},function(){
						$("#product_form_befor").height(0).append($.viewBox.boxhtml.close_btn);
						$("#product_form_befor").animate({height:"526px"},"slow","");
					});
					break;
				case 1 :
						$("#overlay > div").attr("id","product_form");
						var param = $.viewBox.targetData.title
						param = param.replace(/<.*>/g," ");
						$("#product_form").append('<iframe src="https://www.imagine-inc.co.jp/product/product_form.php?title='+encodeURI(param)+'" width="944" height="526"></iframe>');
						$("#product_form").append($.viewBox.boxhtml.close_btn);
					break;
			};
		};
		
		/**
		 * html
		 *------------------------------------------*/
		$.viewBox.html ={
			viewer_html : function(){
				var html = '<div id="view_box">'
					html += '<div id="contents_viewer"><div id="viewer_swf"></div></div>'
					html += $.viewBox.boxhtml.info_html($.viewBox.targetData);
					html += $.viewBox.boxhtml.link_html($.viewBox.targetData.amazon);
					html += $.viewBox.boxhtml.close_btn;
					html += '</div>'
					return html
			},
			overlay_html : '<div id="overlay"></div>',
			bg_html : '<div id="overlay_background">&nbsp;</div>'
		}
		
		/**
		 * viewer html
		 *------------------------------------------*/
		$.viewBox.boxhtml ={
			close_btn :'<div id="close_btn"><a href="javascript:$.viewBox.close();"><img src="../common/img/parts/btn_close_off.gif" width="15" height="15" alt="close" /></a></div>',
			info_html : function(data){
				var html = "";
				html +='<div id="contents_info">';
					html += '<h3 id="contents_title">'+data.title+'</h3>';
					html += '<ul id="info_list">';
					if(data.price	&& data.price		!="undefined"){ html += '<li class="icon_yen">'+data.price+'</li>';};
					if(data.type		&& data.type		!="undefined"){ html += '<li class="icon_video">'+data.type+'</li>';};
					if(data.actors	&& data.actors		!="undefined"){ html += '<li class="icon_actor">'+data.actors+'</li>';};
					if(data.year		&& data.year		!="undefined"){html += '<li class="icon_year">'+data.year+'</li>';};
					html += '</ul>';
					html += '<div id="contents_explanation">';
						html += data.explanation;
				html += '</div></div>';
				return html
			},		
			link_html : function(data){
				var html = ""
				html +='<ul id="purchase_link">';
				if(data && data !="undefined"){ html +='<li><a href="'+data+'"><span class="icon_amazon">AMAZONで購入</span></a></li>';};
					html +='<li><a href="javascript:$.viewBox.next();"><span class="icon_bank">銀行振込みで購入</span></a></li></ul>';
				return html
			}
		};
		
	})()
	
	$.fn.viewBox = function(trigerLink){
			$(this).each(function(){
				$(this).find(trigerLink.useView).attr("href","javascript:$.viewBox.show('"+this.id+"',0)");
				$(this).find(trigerLink.noUseView).attr("href","javascript:$.viewBox.show('"+this.id+"',1)");
			});
			
		return this;
	}	
	
	$(document).ready(function(){
			$.getScript("assets/js/itemdata.js",function(){
				$.viewBox.data = itemdata;
				var now = new Date().getTime();
				now = now - 7*86400000;
				$.each(itemdata,function(){
					var t = this.date;
					t = t.split(".");
					var date = new Date(t[0],t[1]-1,t[2]);
					date = Date.parse(date);
					if(date >now){
						$("#"+this.id+" .thumb a span").addClass("new_item");
					}
				});
				$.ifixpng('/common/js/blank.gif');
				$("span.new_item").ifixpng();
				
				if(document.location.hash){
					var id = document.location.hash.replace("#",""); 
					$.viewBox.show(id,0);
				}
				
				delete itemdata
			});
		});
	
})(jQuery)
