var is_portrait = false;
var screen_width, screen_height;
var cm = new CookieManager();
var lightBox;

function init() {
  lightBox = new LightBox('lightbox');

	window.addEventListener('orientationchange', orientation_changed, false);
	orientation_changed();

	document.addEventListener('touchstart', touch_start, false);
	document.addEventListener('touchmove', touch_move, false);
	document.addEventListener('touchend', touch_end, false);

	(new Image()).src = '/images/chevron_touched.png';
	(new Image()).src = '/images/item_background_touched.png';
	(new Image()).src = '/images/back_button_touched.png';

	setTimeout(hide_address_bar, 500);
};

var hold_timer = null;
var hold_sender = null;

function start_hold(sender) {
	if (hold_timer) {
		clearTimeout(hold_timer);
	}
	hold_timer = setTimeout(end_hold, 2000);
	hold_sender = sender;
};

function end_hold() {
//	alert('bang!');
};

function body_onmousedown() {
};

function body_onmouseup() {
	clearTimeout(hold_timer);
	hold_timer = hold_sender = null;
};

function body_onmousemove() {
	clearTimeout(hold_timer);
	hold_timer = hold_sender = null;
};

function nijiran() {
	var key = encodeURI($('search_key').value);
	window.open('http://eggnog.sakura.ne.jp/kuri/search.php?disp=iphone&s=0&w=' + key);
};


//================ NAVIGATION ================//
var s = {
 jumping: false,
 base: null,
 current: [],
 offset: 0,
 direction: 1,

 get_direction: function() {
		var x, y;
		x = event.touches[0].pageX - s.current[0]; 
		y = event.touches[0].pageY - s.current[1];
		var d0 = (x * x) + (y * y);
		x = event.touches[1].pageX - s.current[0];
		y = event.touches[1].pageY - s.current[1];
		var d1 = (x * x) + (y * y);
		return (d0 < d1) ? 1 : -1;
	}
};

function touch_start() {
};

function touch_end() {
	if (s.base) {
		browser.className = browser.className; // force redraw
		s.base = null;
	}
};

function touch_move() {
	if (lightBox.visible) {
		event.preventDefault();
		return;
	}

	// 常に左タップを基準にして、右タップでスクロールするように仕様変更
	if (event.touches.length == 2) {
 		event.preventDefault();
		if (s.base == null) {
			s.base = event.touches[1].pageY - event.touches[0].pageY;
			if (event.touches[1].pageX < event.touches[0].pageX) s.base *= -1;
		}
		var y = (event.touches[1].pageY - event.touches[0].pageY);
		if (event.touches[1].pageX < event.touches[0].pageX) y *= -1;
		y = (y - s.base) * 20 + s.offset;
		if (y < 0) y = 0;
		if (y > document.height - window.innerHeight) y = document.height - window.innerHeight;
		window.scrollTo(0, y);

	} else {
		s.current = [event.touches[0].pageX, event.touches[0].pageY];
		s.offset = window.pageYOffset;
		if (s.base) {
			browser.className = browser.className; // force redraw
			s.base = null;
		}
	}
};

//================ SCREEN ORIENTATION ================//

var BUTTON_MARGIN_LEFT = 5;
var BUTTON_MARGIN_RIGHT = 10;

function orientation_changed() {
	is_portrait = (window.orientation == 0 || window.orientation == null);
	document.body.className = is_portrait ? 'portrait' : 'landscape';
	screen_width = is_portrait ? 320 : 480;
	screen_height = is_portrait ? 480 : 320;
	return;

//	var title = $('first_title');
//	if (title) {
//		var width = title.offsetWidth;
//		title.style.maxWidth = to_px(screen_width);
//		title.style.marginLeft = to_px((screen_width - width) / 2);
//	}

//	var button = $('first_button');
//	if (button) {
//		button.style.marginLeft = to_px(5);
//		button.style.marginTop = to_px(5);
//	}

//	if (lightBox) lightBox.orientation_changed();
//	window.setTimeout(function() { window.scrollTo(0, 1); }, 0);
};

function to_px(value) {
	return value + 'px';
};

function hide_address_bar() {
	var offset = window.pageYOffset;
	if (offset < 100) {
		window.scrollTo(0, 1);
		setTimeout(function() { window.scrollTo(0, 0); }, 0);
	}
};

//================ COOKIE ================//

function load_cookie() {
	var pwd = cm.getCookie("pwdc");
	if (document.forms.length > 0 && document.forms[0].pwd) {
		if (!document.forms[0].pwd.value && pwd) {
			document.forms[0].pwd.value = pwd;
		} else {
			document.forms[0].pwd.value = 
				Math.floor(Math.random() * 100000000).toString();
		}
	}
};

function save_cookie(form) {
	if (document.forms.length > 0 && document.forms[0].pwd) {
		if (document.forms[0].pwd.value) {
			cm.setCookie("pwdc", document.forms[0].pwd.value);
		}
	}
};

function get_cookie(key) {
	var tmp = ' ' + document.cookie + ';';
	var left = 0;
	var length = tmp.length;
  while (left < length) {
		var right = tmp.indexOf(';', left);
		var slice = tmp.substring(left + 1, right);
		var center = slice.indexOf('=');
		if (slice.substring(0, center) == key) {
			return unescape(slice.substring(center + 1, right - left - 1));
		}
		left = right + 1;
	}
	return '';
};

//================ LIGHTBOX ================//

var LightBox = function (target) {
	var t = $(target);
	var img, width, height;
	var numTouch, startX, startY, startD, xOffset, yOffset;
	var x, y, scale;
	var dx = 0, dy = 0, dd = 1.0;

	this.visible = false;

	this.orientation_changed = function() {
		this.hide();
	};

	this.fadeIn = function(url) {
		t.className = "show";
	};

	this.show = function(url) {
//		alert(url);
		x = 0;
		y = 0;
		scale = 1.0;
		numTouch = 0;
		t.style.top = to_px(window.pageYOffset);
		t.style.width = to_px(window.innerWidth);
		t.style.height = to_px(window.innerHeight);
		t.style.display = "block";
		setTimeout(this.fadeIn, 0);
				
		img = document.createElement("img");
		img.onload = function () {
			var w = window.innerWidth - 10;
			var h = window.innerHeight - 45;
			width = img.width;
			height = img.height;
			rotation = 0;
			if (width > w) {
				height *= w / width;
				width = w;				
			}
			if (height > h) {
				width *= h / height;
				height = h;
			}
			img.className = "img"
			img.width = width;
			img.height = height;
			img.style.top = to_px(5 + (h - height) / 2);
			img.style.left = to_px(5 + (w - width) / 2);
			img.style.padding = to_px(10);
			t.appendChild(img);
		};
		img.onerror = function() {
			alert('画像をロードできません');
			lightBox.hide();
		};

		if (!this.visible) {
			t.addEventListener("touchstart", this.touch_start, false);
			t.addEventListener("touchmove", this.touch_move, false);
			t.addEventListener("touchend", this.touch_end, false);
			this.visible = true;
		}
		img.src = url;
	};

	this.hide = function() {
		if (img) {
			if (img.parentNode) img.parentNode.removeChild(img);
			img = null;
		}
		if (this.visible) {
			t.removeEventListener("touchstart", this.touch_start, false);
			t.removeEventListener("touchmove", this.touch_move, false);
			t.removeEventListener("touchend", this.touch_end, false);
			this.visible = false;
		}
		if (t) {
			t.style.display = "none";
			t.className = "";
		}
	};

	this.reset = function() {
		scale = 1.0;
		x = y = 0;
		setTimeout(function() {
				img.style.webkitTransform = "translate(0px, 0px) scale(1)";}, 0);
	};

	this.open = function() {
		var w = window.open(img.src);
	};

	var initTouch = function(e) {
		numTouch = e.touches.length;
		startD = 1;
		startX = e.touches[0].clientX;
		startY = e.touches[0].clientY;

		if (numTouch > 1) {
			var x = e.touches[1].clientX;
			var y = e.touches[1].clientY;
			startD = Math.sqrt(Math.pow(startX - x, 2) + Math.pow(startY - y, 2));
			startX = (startX + x) / 2;
			startY = (startY + y) / 2;
		}

		xOffset = startX - window.innerWidth / 2;
		yOffset = startY - window.pageYOffset - (window.innerHeight - 35) / 2;
		dx = dy = 0;
		dd = 1.0;
	};

	var currentX = function(e) {
		var x = e.touches[0].clientX;
		if (numTouch > 1) x = (x + e.touches[1].clientX) / 2;
		return x;
	};

	var currentY = function(e) {
		var y = e.touches[0].clientY;
		if (numTouch > 1) y = (y + e.touches[1].clientY) / 2;
		return y;
	};

	var currentD = function(e) {
		if (numTouch > 1) {
			var dx = e.touches[0].clientX - e.touches[1].clientX;
			var dy = e.touches[0].clientY - e.touches[1].clientY;
			return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
		} else return startD;
	};

	this.dragged = false;
	this.touched = false;

	this.touch_start = function(e) {
		if (e.touches.length <= 2) {
			initTouch(e);
			if (!(e.touches.length == 1 && e.touches[0].target == img)) e.preventDefault();
		}
		if (e.touches.length == 1) {
			if (e.touches[0].target == $('lightbox_close')) {
				lightBox.hide();
			} else if (e.touches[0].target == $('lightbox_reset')) {
//			lightBox.reset();
				lightBox.open();
			}
		}
	};

	this.touch_move = function(e) {
		if (e.touches.length <= 2) {
			dd = currentD(e) / startD;
			dx = currentX(e) - startX + (x - xOffset) * (dd - 1.0);
			dy = currentY(e) - startY + (y - yOffset) * (dd - 1.0);

			var transform = "translate(" + (x + dx) + "px, " + (y + dy) + "px)";
			if (numTouch >= 1) transform += " scale(" + (dd * scale) + ")";
			img.style.webkitTransform = transform;
			e.preventDefault();
		}
	};

	this.touch_end = function(e) {
		x += dx;
		y += dy;
		scale *= dd;
		if (e.touches.length > 0) initTouch(e);
	};
};

//================ INIT ================//

window.addEventListener('load', init, false);

