﻿/**
 * Wasshoy LPO
 */


/**
 * Constructor
 */
function WasshoyLPO( _dir, _lid ) {
	
	this.dir = _dir;
	this.lid = _lid;
	
	this.INDEX_SWF = this.dir + 'swf/lpo_index.swf';
	
	this.COOKIE_REPEAT_FLAG = this.lid + '_rf';
	this.COOKIE_PREV_TIME = this.lid + '_pt';
	
	this.ref = ''
	this.repeat = false;
	this.prev = null;
	this.time = null;
	
	this.params = {
		'flashvars': '',
		'play': 'true',
		'loop': 'true',
		'quality': 'high',
		'menu': 'false',
		'wmode': 'transparent'
	};
	
	this.max_date = 365;
	
	if ( ! $ || ! jQuery ) {
		return null;
	}
	else {
		var o = this;
		$(document).ready( function() {
			o.init();
		});
	}
	
	return this;
}

/**
 * 初期化
 * init
 */
WasshoyLPO.prototype.init = function() {
	
	if ( document.referrer ) {
		this.ref = document.referrer;
	}
	
	if ( this.getCookie( this.COOKIE_REPEAT_FLAG ) ) {
		this.repeat = true;
	}
	
	if ( this.getCookie( this.COOKIE_PREV_TIME ) ) {
		this.prev = this.getCookie( this.COOKIE_PREV_TIME );
	}
	
	var date = new Date();
	
	this.time =
		date.getFullYear() + '/' +
		(date.getMonth() + 1) + '/' + 
		date.getDate() + ' ' +
		date.getHours() + ':' +
		date.getMinutes() + ':' +
		date.getSeconds();
	
	// 表示
	this.attachFlash();
	
	// 保存
	this.setCookie( this.COOKIE_REPEAT_FLAG, 1 );
	this.setCookie( this.COOKIE_PREV_TIME, this.time );
};

/**
 * Cookieの値取得
 * getCookie
 */
WasshoyLPO.prototype.getCookie = function( _key ) {
	
	var cookies = document.cookie.split( ';' );
	
	for ( var i = 0; i < cookies.length ; i ++ ) {
		var index = cookies[ i ].indexOf( '=' );
		
		if ( cookies[ i ].substring(0, index) == _key
		||	cookies[ i ].substring(0, index) == " " + _key ) {
			return decodeURIComponent( cookies[ i ].substring(index + 1) );
		}
	}
	
	return null;
};

/**
 * Cookieの値保存
 * setCookie
 */
WasshoyLPO.prototype.setCookie = function( _key, _val ) {
	
	date = new Date();
	date.setTime( date.getTime() + this.max_date * 1000 * 60 * 60 * 24 );
	
	document.cookie = _key + "=" + _val + ";expires=" + date.toUTCString();
};

/**
 * Flash表示
 * attachFlash
 */
WasshoyLPO.prototype.attachFlash = function() {
	
	// 取得値
	var flashvars =
		'dir='+escape(this.dir)+'&'
	+	'ref='+escape(this.ref)+'&'
	+	'repeat='+escape(this.repeat)+'&'
	+	'prev='+escape(this.prev)+'&'
	+	'time='+escape(this.time);
	
	this.params[ 'flashvars' ] = flashvars;
	
	// HTML
	var html = '';
	html +=
		'<object type="application/x-shockwave-flash" data="'+(this.INDEX_SWF)+'"'
	+	' width="100%" height="100%" style="outline:none;">'
	+	'<param name="movie" value="'+(this.INDEX_SWF)+'" />';
	
	for ( var i in this.params ) {
		html += '<param name="'+(i)+'" value="'+(this.params[ i ])+'" />';
	}
	
	html +=
		'<embed src="'+(this.INDEX_SWF)+'" type="application/x-shockwave-flash"'
	+	' width="100%" height="100%"';
	
	for ( var i in this.params ) {
		html += ' ' + i + '="' + this.params[ i ] + '"';
	}
	
	html += ' ></embed>';
	html += '</object>';
	
	// 適用
	if ( $('#'+this.lid).length ) {
		$('#'+this.lid).eq(0).html( html );
	}
};
