﻿//alert("ok");
/**
* ファイルパス
*
*/
var rootPath = "http://www.toyomaru.jp/themes/toyomaru/users//";

/**
* ゲストかどうか判定
*
*/
var isGuest;
if("" != ""){
 isGuest = false;
}else{
 isGuest = true;
}


/**
* 再読み込み時にだけ使用するcookie
*
*/
var s = document.cookie;
var cnslot = "prevslot=";



/**
* PHPと通信用オブジェクト生成
*
*/
var xmlReq_ext;//外部テキスト情報取得用
var xmlReq_ref;//リファラー情報取得用
var xmlReq_init;//初期化時PHPコール用
var xmlReq_movie;//閲覧済みムービー記録用
//
// 非同期通信を行うためのXMLHttpRequestオブジェクトを生成
//Win ie用
if(window.ActiveXObject){
    try {
        //MSXML2以降用
        xmlReq_ext = new ActiveXObject("Msxml2.XMLHTTP");
		xmlReq_ref = new ActiveXObject("Msxml2.XMLHTTP");
		xmlReq_init = new ActiveXObject("Msxml2.XMLHTTP");
		xmlReq_movie = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            //旧MSXML用
            xmlReq_ext = new ActiveXObject("Microsoft.XMLHTTP");
			xmlReq_ref = new ActiveXObject("Microsoft.XMLHTTP");
			xmlReq_init = new ActiveXObject("Microsoft.XMLHTTP");
			xmlReq_movie = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            
        }
     }
} else if(window.XMLHttpRequest){
    //Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
    xmlReq_ext = new XMLHttpRequest();
	xmlReq_ref = new XMLHttpRequest();
	xmlReq_init = new XMLHttpRequest();
	xmlReq_movie = new XMLHttpRequest();
} else {
    
}

/*
try {
	xmlReq_ext = new ActiveXObject("Microsoft.XMLHTTP");
	xmlReq_ref = new ActiveXObject("Microsoft.XMLHTTP");
	xmlReq_init = new ActiveXObject("Microsoft.XMLHTTP");
	xmlReq_movie = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
	xmlReq_ext = new XMLHttpRequest();
	xmlReq_ref = new XMLHttpRequest();
	xmlReq_init = new XMLHttpRequest();
	xmlReq_movie = new XMLHttpRequest();
}
*/

/**
* 起動時PHPコール
*
*/
/*window.onload = function(){
var reqestSrc = "http://www.toyomaru.jp/themes/toyomaru/users/makeinfo.php?info=&url=" + document.location.href + "&flush=" + (new Date().getTime());
xmlReq_init.open("GET",reqestSrc,false);
xmlReq_init.send(null);
}*/

$(function(){

var reqestSrc = "http://www.toyomaru.jp/themes/toyomaru/users/makeinfo.php?info=&url=" + document.location.href + "&flush=" + (new Date().getTime());
xmlReq_init.open("GET",reqestSrc,false);
xmlReq_init.send(null);

});


/**
* 再読み込み判定
*
*/
var reloaded;
//読み込み完了後処理
if (/Gecko/.test(navigator.userAgent) && /Firefox/.test(navigator.userAgent)) { 

    xmlReq_ref.onload = function() {
    	refCallBack();
    }
    
} else {

    xmlReq_ref.onreadystatechange = function() {
    	refCallBack();
    }
    
}

function refCallBack(){
	if(xmlReq_ref.readyState == 4 && xmlReq_ref.status == 200){
		if(xmlReq_ref.responseText == "true"){
			reloaded = true;
		}else{
			reloaded = false;
		}
	}
}
// サーバーとの通信を開始
var reffererURL = rootPath + "refferer.txt" + "?flush=" + (new Date().getTime());
xmlReq_ref.open("GET",reffererURL,false);
xmlReq_ref.send(null);
reloaded = false;

/**
* ムービーの当たりハズレ分類
*
*/
var atariArr = [];
var hazureArr = [];
for(var i=1;i<=totalMovieNum;i++){
	var targetSetName = "type" + i;
	var targetSet = eval(targetSetName);
	var targetAtari = targetSet[2];
	if(targetAtari){
		atariArr.push(targetSetName);
	}else{
		hazureArr.push(targetSetName);
	}
}


/**
* ラッキーゲージ数を取得（slot判定に使用）
*
*/
var gaugeURL = rootPath + "gauge.txt";
var luckyv;
if(isGuest){
	luckyv = 1;
}else{
	getCount(gaugeURL,"luckyv");
}


/**
* ゲージ等の表示
*
*/
//ボーナスの数を取得
var bonusv;
var bonusURL = rootPath + "bonus.txt";
if(isGuest){
	bonusv = 0;
}else{
	getCount(bonusURL,"bonusv");
}

//
//ページビュー数を取得
var pagev;
var pageURL = rootPath + "page.txt";
if(isGuest){
	pagev = 0;
}else{
	getCount(pageURL,"pagev");
}

//平均値
var averagev;
var averageURL = rootPath + "average.txt";
if(isGuest){
	averagev = 0;
}else{
	getCount(averageURL,"averagev");
}


//閲覧済みビデオ数の取得
var viewv;
var viewURL = rootPath + "view.txt"
if(isGuest){
	viewv = 0;
}else{
	getCount(viewURL,"viewv");
}

//閲覧済みページ総数の取得
var totalPageNumURL = rootPath + "total.txt"
var totalPageNum;
if(isGuest){
	totalPageNum = 0;
}else{
	getCount(totalPageNumURL,"totalPageNum");
}



/**
* 抽選
*
*/
var flvtype;
if(!reloaded){
	//スロットの当たり外れ抽選
	if(luckyv == 0){
		slot = Math.floor((Math.random()*0.999)*560+1);
		if(slot>558){
			slot = 2;
		}
	}else if(luckyv == 1){
		slot = Math.floor((Math.random()*0.999)*500+1);
		if(slot>498){
			slot = 2;
		}
	}else if(luckyv == 2){
		slot = Math.floor((Math.random()*0.999)*440+1);
		if(slot>438){
			slot = 2;
		}
	}else if(luckyv == 3){
		slot = Math.floor((Math.random()*0.999)*380+1);
		if(slot>378){
			slot = 2;
		}
	}else if(luckyv == 4){
		slot = Math.floor((Math.random()*0.999)*320+1);
		if(slot>318){
			slot = 2;
		}
	}else if(luckyv == 5){
		slot = Math.floor((Math.random()*0.999)*260+1);
		if(slot>258){
			slot = 2;
		}
	}else if(luckyv == 6){
		slot = Math.floor((Math.random()*0.999)*200+1);
		if(slot>198){
			slot = 2;
		}
	}
	//slot = Math.floor((Math.random()*0.999)*1+1);
	
	
	//ムービーの抽選
	var targetTypePosi;
	var targetTypeName;
	if(slot == 1 || slot ==4){
		targetTypePosi = Math.floor((Math.random()*0.999)*(atariArr.length)+1);
		targetTypeName = atariArr[targetTypePosi-1];
	}else{
		targetTypePosi = Math.floor((Math.random()*0.999)*(hazureArr.length)+1);
		targetTypeName = hazureArr[targetTypePosi-1];
	}
	flvtype = Number(targetTypeName.substring(4,targetTypeName.length));
	
	
	//抽選したムービー結果をPHPに送信
	if(slot == 1 || slot == 4){
		// サーバーとの通信を開始
		xmlReq_movie.open("POST","http://www.toyomaru.jp/themes/toyomaru/users/leach.php?uid=",true);
		xmlReq_movie.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
		var flvSet0 = eval("type" + flvtype);
		var flvName = flvSet0[0].substring(41,flvSet0[0].length);
		var postStr = "id=" + flvName + "&flush=" + (new Date().getTime());
		xmlReq_movie.send(postStr);
	}
}else{
	var nslot = s.indexOf(cnslot,0);
	var prevslot = getCount0(nslot,cnslot,0,0);
	slot = prevslot;
	flvtype = 1;
}

//該当クッキー名から数値を取得、数値がなければ初期化
function getCount0(cposi,cname,plusNum,initNum){
	var count0;
	var m;
	if (cposi!=-1) {
	  m = s.indexOf(";",cposi+cname.length); //*3-1: m = 有効なcookieの終わりの位置
	  if (m==-1) m = s.length; //3-2: 終わりの位置が不明の場合はcookieの最後を終わりの位置にする
	  count0 = parseInt(s.substring(cposi+cname.length,m));
 	  count0 += plusNum;
	} else {
	  count0 = initNum;
	}
	return count0;	
}



/**
* 指定テキストファイルから数値を取得
*
*/
function getCount(fileURL,targetNum){
	var xmlReq;
	//
	// 非同期通信を行うためのXMLHttpRequestオブジェクトを生成
	//Win ie用
	if(window.ActiveXObject){
	    try {
	        //MSXML2以降用
	        xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	        try {
	            //旧MSXML用
	            xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e2) {
	            
	        }
	     }
	} else if(window.XMLHttpRequest){
	    //Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
	    xmlReq = new XMLHttpRequest();
	} else {
	    
	}
	//読み込み完了後処理
	 if (/Gecko/.test(navigator.userAgent) && /Firefox/.test(navigator.userAgent)) { 

    	xmlReq.onload = function() {
    		xmlReqCallBack();
    	}

  	} else {

    	xmlReq.onreadystatechange = function() {
    		xmlReqCallBack();
    	}
 	}
 	function xmlReqCallBack(){
		if(xmlReq.readyState == 4 && xmlReq.status == 200){
			var returnSrc = Number(xmlReq.responseText);
			var textSrc = targetNum + "=" + returnSrc;
			eval(textSrc);
		}
	}
	// サーバーとの通信を開始
	var fileURL0 = fileURL + "?flush=" + (new Date().getTime());
	xmlReq.open("GET",fileURL0,false);
	xmlReq.send(null);
}




/**
* クッキーに保存する
*
*/
//*:現在のスロット値を保存
document.cookie = cnslot + slot +"; path=/;";



//*ボーナスを表示する関数
function setBonusImage(){
	imgTag = setNumbersImage(bonusv);
	return imgTag;
}

//*ページを表示する関数
function setPageImage(){
	imgTag = setNumbersImage(pagev);
	return imgTag;
}

//*全ページ数を表示する関数
function setTotalPageImage(){
	imgTag = setNumbersImage(totalPageNum);
	return imgTag;
}

//*全ページ数を表示する関数
function setLeachViewImage(){
	imgTag = setNumbersImage(viewv);
	return imgTag;
}

//全リーチ動画数を表示する関数
function setTotalLeachViewImage(){
	imgTag = setNumbersImage(atariArr.length);
	return imgTag;
}


//ボーナスの数値を表示する関数
function setBonusNum(){
	document.write(bonusv);
}


//ページの数値を表示する関数
function setPageNum(){
	document.write(pagev);
}

//閲覧済みページの数値を表示する関数
function setTotalPageNum(){
	document.write(totalPageNum);
}

//当日閲覧ページの数値を表示する関数
function setViewNum(){
	document.write(viewv);
}

//全（当たり）動画を表示する関数
function setTotalMovieNum(){
	document.write(atariArr.length);
}

//当日閲覧ページの数値を表示する関数
function setAvgNum(){
	document.write(averagev);
}


//*数値部分表示用共通関数*//
function setNumbersImage(count){
	var imgTag = "";
	if(count<10){
		/*document.write("<img src=\"/themes/toyomaru/images/gauge/");
		document.write(count);
		document.write(".jpg\"/>");*/
		imgTag = "<img src=\"/themes/toyomaru/images/gauge/"+count+".jpg\" />";
		return imgTag;
	}

	if((9<count)&&(count<100)){
		/*document.write("<img src=\"/themes/toyomaru/images/gauge/");
		var tencount = count/10;
		tencount = Math.floor(tencount);
		document.write(tencount);
		document.write(".jpg\"/>");
		var onecount = count-tencount*10;
		document.write("<img src=\"/themes/toyomaru/images/gauge/");
		document.write(onecount);
		document.write(".jpg\"/>");*/
		var tencount = count/10;
		tencount = Math.floor(tencount);
		var onecount = count-tencount*10;
		imgTag = "<img src=\"/themes/toyomaru/images/gauge/"+tencount+".jpg\" />";
		imgTag = imgTag+"<img src=\"/themes/toyomaru/images/gauge/"+onecount+".jpg\" />";
		return imgTag;
	}

	if(99<count){
		/*document.write("<img src=\"/themes/toyomaru/images/gauge/");
		var hucount = count/100;
		hucount = Math.floor(hucount);
		document.write(hucount);
		document.write(".jpg\"/>");
		document.write("<img src=\"/themes/toyomaru/images/gauge/");
		var tencount = count/10-hucount*10;
		tencount = Math.floor(tencount);
		document.write(tencount);
		document.write(".jpg\"/>");
		var onecount = count-hucount*100-tencount*10;
		document.write("<img src=\"/themes/toyomaru/images/gauge/");
		document.write(onecount);
		document.write(".jpg\"/>");*/
		var hucount = count/100;
		hucount = Math.floor(hucount);
		var tencount = count/10-hucount*10;
		tencount = Math.floor(tencount);
		var onecount = count-hucount*100-tencount*10;
		imgTag = "<img src=\"/themes/toyomaru/images/gauge/"+hucount+".jpg\" />";
		imgTag = imgTag+"<img src=\"/themes/toyomaru/images/gauge/"+tencount+".jpg\" />";
		imgTag = imgTag+"<img src=\"/themes/toyomaru/images/gauge/"+onecount+".jpg\" />";
		return imgTag;
	}
}

//*7: LuckeyGaugeを表示する関数
function setLuckeyGauge(){
	var imgTag = "";
	/*document.write("<img src=\"/themes/toyomaru/images/gauge/b");
	document.write(luckyv);	
	document.write(".png\"/>");*/
	imgTag = "<img src=\"/themes/toyomaru/images/gauge/b"+luckyv+".png\" class=\"transparent\" />";
	return imgTag;
}


//*外部テキスト表示関数
function externalTxt(){
	var writeStr;
	//読み込み完了後処理
	 if (/Gecko/.test(navigator.userAgent) && /Firefox/.test(navigator.userAgent)) { 
	 
    	xmlReq_ext.onload = function() {
    		xmlReqExtCallBack();
    	}

  	} else {

    	xmlReq_ext.onreadystatechange = function() {
    		xmlReqExtCallBack();
    	}
 	}
 	function xmlReqExtCallBack(){
		if(xmlReq_ext.readyState == 4 && xmlReq_ext.status == 200){
			writeText(xmlReq_ext);
		}
	}
	// サーバーとの通信を開始
	var reqStr = "http://www.toyomaru.jp/themes/toyomaru/users/target.php?uid=&flush=" + (new Date().getTime());
	xmlReq_ext.open("GET",reqStr,false);
	xmlReq_ext.send(null);
}

//コールバック
function writeText(xmlReq0){
	var disp = document.getElementById("disp");
	disp.innerHTML = xmlReq0.responseText;
}


//*フラッシュ埋込関係

var mSecond = new Date().getTime();
var flashvars00 = {connectName:mSecond,reloadParam:reloaded};

//スロット埋込み用パラメータ
var params00 = {
	scale:'noScale',
 	salign:'lt',
  	menu:'false',
  	allowfullscreen :'true',
  	allowScriptAccess:"always",
  	wmode:"transparent",
  	name:"movie_swf"
};

var attributes = {};


//オーバーレイ埋込み用
var flvSet = eval("type" + flvtype);

var flashvars2 = {connectName:mSecond,daiImageURL:flvSet[1][0],flvPosiX:flvSet[1][1],flvPosiY:flvSet[1][2],flvURL:flvSet[0]};

var params2 = {
	scale:'noScale',
	salign:'lt',
	menu:'false',
	allowfullscreen :'true',
	allowScriptAccess:"always",
	wmode:"transparent",
	name:"movieswfover"
};

//再生するスロットswfの決定
var slotSWFName;
var slot3Ver;
if ( slot == 1 ) {
	slotSWFName = "/themes/toyomaru/images/gauge/slot1.swf";
}else if ( slot == 2 ){
	slotSWFName = "/themes/toyomaru/images/gauge/slot2.swf";
}else if ( slot == 3 ){
	slot3Ver = Math.floor((Math.random()*0.999)*5+1);
	slotSWFName = "/themes/toyomaru/images/gauge/slot3_" + slot3Ver +".swf";
}else if ( slot == 4 ){
	slotSWFName = "/themes/toyomaru/images/gauge/slot4.swf";
}else{
	slot3Ver = Math.floor((Math.random()*0.999)*5+1);
	slotSWFName = "/themes/toyomaru/images/gauge/slot3_" + slot3Ver +".swf";
}

//再生するオーバーレイswfの決定
var overraySWFURL;
if(slot == 1 || slot == 4){
	overraySWFURL = "/themes/toyomaru/images/gauge/overray1.swf"
}else{
	overraySWFURL = "/themes/toyomaru/images/gauge/overray2.swf"
}

//スロットの埋込用関数
function setLeachSWF(){
	swfobject.embedSWF(slotSWFName, "slotswf", "118", "46", "10.0.0","/themes/toyomaru/js/swfobject/expressInstall.swf", flashvars00, params00, attributes);
}

//オーバーレイ表示関数
function setOverray(){
	showFlash();
}


function embedOverray(){
	swfobject.embedSWF(overraySWFURL, "overraySWF", "100%", "100%", "10.0.0","/themes/toyomaru/js/swfobject/expressInstall.swf", flashvars2, params2, attributes);
}

function closeOverray(){
	hideFlash();
}



//オーバーレイに関して
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}



var objOverlay;


function showFlash(){
	if(navigator.appName.indexOf("Microsoft") != -1){
		var br = "ie";
	}else{
		var br = "other";
	}
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var objBody = document.body;
	objOverlay = document.createElement("div");
	var myTag= '<div id="overraySWF"></div>';
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideFlash(); return false;}
	objOverlay.style.display = 'block';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = document.body.scrollTop;
	objOverlay.style.left = '0';
	objOverlay.style.width = '100%';
	objOverlay.style.height = '100%';
	$('overraySWF').css('width','100%');$('overraySWF').css('height','100%');
	objOverlay.style.zIndex = '200';
	objOverlay.innerHTML = myTag;
	objBody.insertBefore(objOverlay, objBody.firstChild);
	objOverlay.style.display = 'block';
	$( '#overlay' ).scrollFollow( {
		//アニメーションの速度の設定。0 にするとアニメーションなしになります。
		speed: 0,
		//ブラウザの領域とフロート要素との余白。0にするとぴったりくっつきます。
		offset: 0
	} );
	setTimeout(function(){embedOverray();},500);
	resize();
	setTimeout(function(){resize();},500);
}


function hideFlash(){
	document["slotswf"].stopSlot();
	var objBody = document.body;
	objOverlay = 0;
	objBody.removeChild(objBody.firstChild);
}




//ウィンドウリサイズ処理
$(window).resize(resize);
function resize(){
	setTimeout(function(){document["overraySWF"].callResize();},100)
}
	
