var pgm = {
	init: function() {
		//ドラッグ機能
		this.dragFunc();
		//ドラッグ機能のON/OFF
		this.modeFunc();
	},
	dominit: function() {
		//画面に合わせ各サイズ変更
		this.resizeFunc();
	},
	resizeFunc: function() {
		winHeight = window.getHeight();
		winWidth = window.getWidth();
		blockHeight = winHeight - 52;
		// 'link' and 'nombre' = 270 + 40 = wrapper margin-top
		blockMargin = ((blockHeight - 310) / 2).toInt();

		var setMargin = 256 * 256;
		var setSize = 512 * 512;

		$('header').setStyle('width', winWidth);
		$('layer').setStyles({'height': blockHeight, 'width': winWidth});
		$('block').setStyles({'height': setSize, 'width': setSize, 'margin-top': -setMargin, 'margin-left': -setMargin});
		$('content').setStyles({'height': blockHeight, 'width': winWidth});
		$$('#content .wrapper').setStyles({'margin-top': blockMargin});
		
		limitHeight = ("-" + 0).toInt();
		limitWidth = ("-" + 0).toInt();
	},
	dragFunc: function() {
		var dragPosInit, dragPosInit2, delta = 0;
		$('content').set('morph', { duration: 360, wait: true });
			
		contentDrag = new Drag.Move('content', {
		    droppables: '#block',
			onDrop: function() {
				$('content').setStyle('cursor', 'url("http://dsn.sh/lab4/images/common/openhand.cur"), crosshair');

				dragEnd = [$('content').offsetTop, $('content').offsetLeft];
				if(dragEnd[0] > 0) dragEnd[0] = 0;
				else if(dragEnd[0] < limitHeight) dragEnd[0] = limitHeight;
				
				if(dragEnd[1] > 0) dragEnd[1] = 0;
				else if(dragEnd[1] < limitWidth) dragEnd[1] = limitWidth;

				$('content').morph({
					'top': [$('content').offsetTop, dragEnd[0]],
					'left': [$('content').offsetLeft, dragEnd[1]]
				});
			},
			onEnter: function() {
				$('content').setStyle('cursor', 'url("http://dsn.sh/lab4/images/common/closedhand.cur"), crosshair');
			},
			container: 'block'
		});

		$$('.wrapper a').addEvent('mousedown', function() {
			//if(Browser.Engine.trident) { contentDrag.detach(); contentDrag.detach(); }
			dragPosInit = this.getPosition();
		});
		$$('.wrapper a').addEvent('mouseup', function() {			
			dragPosInit2 = this.getPosition();
			deltaX = dragPosInit2.x - dragPosInit.x;
			deltaY = dragPosInit2.y - dragPosInit.y;
			delta = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
			if(delta <= 30) alert('OPEN');
		});
		$$('.wrapper').addEvent('mouseout', function() {
			//if(Browser.Engine.trident) { contentDrag.detach(); contentDrag.detach(); }
		});
	},
	modeFunc: function() {
		var dsnMode = 1;
		//モードの選択イベント
		$('modeDrag').addEvent('click', function() {
			if(dsnMode) {
				$('menuMode').setStyle('background-position', 'left -40px');
				$('content').setStyle('cursor', 'auto');
				contentDrag.detach();
				dsnMode = 0;
			} else {
				$('menuMode').setStyle('background-position', 'left top');
				$('content').setStyle('cursor', 'url("http://dsn.sh/lab4/images/common/openhand.cur"), crosshair');
				contentDrag.attach();
				dsnMode = 1;
			}
		});
	},
}

//var dsnHash = location.hash;

window.addEvent('domready', function(){ pgm.dominit() });
//window.addEvent('load', function(){ pgm.init() });　描画にずれ？
window.onresize = function() {
	pgm.resizeFunc();
};
window.onload = function() {
	pgm.init();
};
