﻿
function OnSwitch(){
	// LightインテリアをONにする
	
	this.frames.LightDiv.removeNode( true );
}
function OffSwitch(){
	// LightインテリアをOFFにする
	
	if ( typeof(this.frames.LightDiv) == 'undefined' ) {
		var availW = window.screen.availWidth;
		var availH = parent.frames[0].document.body.scrollHeight + 20
		var availL = 0 - (window.screen.availWidth - 960)/2
		if (window.screen.availWidth <= 960) {
			availL = 0;
			availW = 960;		
		}
		var strHTML = '<div id=LightDiv style="width:' + availW + ';height:' + availH +';position:absolute;left:' + availL + ';top:0;z-index:1050;background-color:black;FILTER: alpha( opacity=50 );"></div>';
		AccessaryLoadArea.insertAdjacentHTML( 'beforeEnd', strHTML );
	}
}
var IntroOffFlag = 0;
function OffIntro(){
	// Introインテリアを削除
	
	if (IntroOffFlag==1)
		return;

	var oDvs = '';
	var oDvs = document.all.tags( 'DIV' );
	for( var i = 0; i < oDvs.length; i++ )
	{
		if( oDvs(i).id && oDvs(i).id.indexOf( 'AccSetDiv_' ) >= 0 && !oDvs(i).style.floatingDiv )
		{
			var strID = oDvs(i).id;
			oDvs(i).removeNode( true );
		}
	}
	IntroOffFlag = 1;
}


function AccOnBodyMouseDown()
{
	if( !arrDiv ) return;
	
	oDiv = event.srcElement;
	while( oDiv )
	{
		if( oDiv.currentStyle && oDiv.currentStyle.floatingDiv && oDiv.id && oDiv.id.indexOf('AccSetDiv') != -1 )
			break;
		
		oDiv = oDiv.parentNode;
	}

	if( oDiv && oDiv.currentStyle.zIndex >= 0 && oDiv.currentStyle.zIndex < 100 )
	{
		for( var i = 0; i < arrDiv.length; i++ )
		{
			if( arrDiv[i].currentStyle.zIndex && arrDiv[i].currentStyle.zIndex < 100 &&
				arrDiv[i].currentStyle.zIndex > oDiv.currentStyle.zIndex )
			{
				arrDiv[i].runtimeStyle.zIndex = arrDiv[i].currentStyle.zIndex - 1;
			}
		}
		oDiv.runtimeStyle.zIndex = arrDiv.length - 1;
	}
	
	if( oDiv )
	{
//		oDiv.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50);";
		document.body.onmousemove = AccOnBodyMouseMove;
		originPointX = event.screenX;
		originPointY = event.screenY;
		originDivLeft = parseInt( oDiv.currentStyle.left );
		originDivTop = parseInt( oDiv.currentStyle.top );
	}
	
	if( g_fMouseDown ) g_fMouseDown.call();
}
function AccOnBodyMouseUp()
{
//	if( oDiv )
//		oDiv.runtimeStyle.filter = "";

	//Magnet Effect
	AccCheckAdjacentDiv();
		
	document.body.onmousemove = null;
	oDiv = null;
	
	if( g_fMouseUp ) g_fMouseUp.call();
}

function AccCheckAdjacentDiv()
{
	if( !oDiv || nMagnetRange < 1 ) return;
	
	var a,b,c,d;
	for( var i = 0; i < arrDiv.length; i++ )
	{
		var rcDiv1 = oDiv.getBoundingClientRect();
		var rcDiv2 = arrDiv[i].getBoundingClientRect();
		
		a = rcDiv1.top;
		b = rcDiv2.bottom;

		if( ((rcDiv1.left < rcDiv2.left + 10 && rcDiv1.right > rcDiv2.left + 10) ||
			(rcDiv1.left < rcDiv2.right - 10 && rcDiv1.right > rcDiv2.right - 10) ) &&
			Math.abs( a - b ) < nMagnetRange )
		{
//			alert( document.body.scrollTop );
			oDiv.runtimeStyle.top = document.body.scrollTop + rcDiv2.bottom - document.body.clientTop + nGap;
			break;
		}

		a = rcDiv1.bottom;
		b = rcDiv2.top;
		
		if( ((rcDiv1.left < rcDiv2.left + 10 && rcDiv1.right > rcDiv2.left + 10) ||
				(rcDiv1.left < rcDiv2.right - 10 && rcDiv1.right > rcDiv2.right - 10) ) &&
			Math.abs( a - b ) < nMagnetRange )
		{
//			alert( document.body.scrollTop );
			oDiv.runtimeStyle.top = document.body.scrollTop + rcDiv2.top - (rcDiv1.bottom - rcDiv1.top) - document.body.clientTop - nGap;
			break;
		}
	}
}

function AccOnBodyMouseMove()
{
	if( !arrDiv ) return;
	if( !oDiv ) return;

	if( !(event.button & 0x01) )
	{
		oDiv = null;
		return;
	}

	//Grid Effect
	if( nGridSize > 1 )
	{
		oDiv.runtimeStyle.left = parseInt(( originDivLeft + event.screenX - originPointX) / nGridSize) * nGridSize;
		oDiv.runtimeStyle.top = originDivTop + event.screenY - originPointY;
//		oDiv.runtimeStyle.top = originDivTop + parseInt((event.screenY - originPointY) / nGridSize) * nGridSize;
	}
	else
	{
		oDiv.runtimeStyle.left = originDivLeft + event.screenX - originPointX;
		oDiv.runtimeStyle.top = originDivTop + event.screenY - originPointY;
	}
}

var g_fMouseDown = null;
var g_fMouseUp = null;

function AccMakeFloatingDivArray()
{

	if( arrDiv )
	{
		alert('error');
		return;
	}
	
	arrDiv = new Array();
	var nZIndex = 1;
	var colDiv = document.all.tags( 'DIV' );

		
	for( var i = 0; i < colDiv.length; i++ )
	{

		if (colDiv(i).id.indexOf('AccSetDiv') == 0) {
	
				
			if( colDiv(i).currentStyle.floatingDiv )
			{			
				arrDiv[ arrDiv.length ] = colDiv(i);
			
				if( colDiv(i).currentStyle.zIndex == null || colDiv(i).currentStyle.zIndex == 0 )
					colDiv(i).runtimeStyle.zIndex = nZIndex;
			
				nZIndex++;
			}
		}
	}

	g_fMouseDown = document.body.onmousedown;
	g_fMouseUp = document.body.onmouseup;
	document.body.onmousedown = AccOnBodyMouseDown;
	document.body.onmouseup = AccOnBodyMouseUp;
//	document.body.onselectstart = new Function( 'return false;' );
//	document.body.ondragstart = new Function( 'return false;' );
}

function PrintDivInfo()
{
	if( !arrDiv ) return;
	var strInfo = '';
	for( var i = 0; i < arrDiv.length; i++ )
	{
		strInfo += '[(|)]' + arrDiv[i].className + ';';
		strInfo += parseInt( arrDiv[i].currentStyle.left ) + ';';
		strInfo += parseInt( arrDiv[i].currentStyle.top ) + ';';
		strInfo += arrDiv[i].currentStyle.zIndex + ';';
	}
	alert( strInfo );
}


// Accessaryを動けるようにする	
var oDiv = null;
var originDivLeft = 0;
var originDivTop = 0;
var originPointX = 0;
var originPointY = 0;
var nGridSize = 3;
var nMagnetRange = 5;
var nGap = 3;
var arrDiv = null;	
AccMakeFloatingDivArray();
