/*var c = document.cookie;
c = c.split(";");
for(var i = 0; i < c.length; i++) {
	cookie = c[i].split("=");
	cookie[0] = cookie[0].replace(/ /g, "");
	if(cookie[0] == "from_iPhone") from_iPhone = Number(cookie[1]);
}*/
var ua = navigator.userAgent.toLowerCase();
if(/iphone/.test(ua) || /android/.test(ua)) {
	window.location.href = "/iphone/";
}

var AREA = ["", "sun", "mercury", "venus", "earth", "mars", "ceres", "jupiter", "saturn", "uranus", "neptune", "pluto"];
var AREA_NAME = ["", "太陽", "水星", "金星", "地球", "火星", "ケレス", "木星", "土星", "天王星", "海王星", "冥王星"];
var SHOP_CATEGORY = ["", "食べる", "お酒を飲む", "お茶をする", "そのほか", "ファッション", "家電・生活雑貨", "文具・書籍", "食料品・コンビニ", "そのほか", "カラダ・健康", "美容室・サロン", "建築・不動産", "遊ぶ", "学ぶ", "そのほか", "クリーニング", "公共施設", "パン・スイーツ・お茶"];
var INFO_CATEGORY = ["", "イベント", "まちの風景", "三鷹市から", "サイエンス・カフェ", "まちの安心・安全情報", "そのほか"];
var NO_DATA = '<p>データがありません。</p>';

var NEW_TERM = 14; // new icon を表示する期間（日数）

var DISPLAY_NUM = 30; // 一覧を1ページに表示する数

var isOpen = 0;

/**
 * リクエスト API
 * request API
 */
function request(func, args, callback) {
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "/include/functions.php?isOpen=" + isOpen,
		data: "func=" + func + "&args=" + args,
		success: callback,
		error: function(data, textStatus, errorThrown) { alert(["Opps!!", data, textStatus, errorThrown]); } //, data, textStatus, errorThrown
	});
}

/**
 * コンテンツ
 * contents
 */
var w, h, cw, sh;
var scrollpain;
var isContentsOpen = true;
var minWidth = 990;
var minHeight = 580;

$(window).resize(resetContents);

function resetContents() {
	windowResize();
	setContentsWidth();
	setScrollHeight();
}
/**
 * ウィンドウリサイズ時にコンテンツのサイズを変更
 * updata contents size when woindw resized.
 */
function windowResize() {
	w = $(window).width();
	h = $(window).height();
	w = (w < minWidth) ? minWidth : w ; 
	h = (h < minHeight) ? minHeight : h ;
	$('body').css({width:w, height:h});
	$('#global_asset_inner').css({height:h});
}

/**
 * ウィンドウリサイズ時にコンテンツの高さを変更
 * update contents horizontal size.
 */
function setContentsWidth() {
	cw = w - $('#global_asset').width() - $('#local_asset').width();
	$('#contents').css({width:cw});
	
	var mw = isContentsOpen ? cw - $('#contents_inner').width() + 16 : cw;
	$('#map').stop().css({width:mw});
	
	if(map) updataMapBounds();
}

/**
 * ウィンドウリサイズ時にスクロールコンテンツ高さを変更
 * update scroll contents horizontal size.
 */
var adjust = 0;
function setScrollHeight(a) {
	adjust = a || adjust;
	var mnh = $('#mnavi').height() ? $('#mnavi').height() : 0 ;
	var cnh = $('#cnavi').height() ? Number($('#cnavi').height()) + Number($('#cnavi').css("paddingTop").replace("px", "")) : 0 ;
	var ph = $('#paged').height() ? Number($('#paged').height()) + Number($('#paged').css("paddingTop").replace("px", "")) : 0 ;
	var upperPad = Number($('#main').css("paddingTop").replace("px", "")) + Number($('#main_upper').css("paddingBottom").replace("px", ""));
	var innerPad = Number($('#under_inner').css("paddingTop").replace("px", "")) + Number($('#under_inner').css("paddingBottom").replace("px", ""));
	sh = h - $('#main_upper').height() - upperPad - mnh - cnh - ph - innerPad;
	
	if($('#scroll_inner').height() > sh && isContentsOpen) {
		$('#slider_unit').css('visibility', 'visible');
		$('#more').css('display', 'block');
		$('#scroll').height(sh - $('#more').height());
	} else {
		$('#slider_unit').css('visibility', 'hidden');
		$('#more').css('display', 'none');
		$('#scroll').height(sh);
	}
	$('#slider_unit').height(sh + 12 - adjust);
	
	if(scrollpain) scrollpain.setPosition();
}

/**
 * メインコンテンツの開閉
 * main contents open and close.
 */  
function contentsOpen() {
	if(!isContentsOpen) toggleContents();
	$(elem).animate({opacity:0}, 0).html('<img src="' + img + '" width="508" height="310" alt="photo" />').animate({opacity:1}, 500);
}

function toggleContents() {
	var cx = isContentsOpen ? -$('#contents_inner').width() + 16 : 0;
	var mw = isContentsOpen ? cw : cw - $('#contents_inner').width() + 16;
	isContentsOpen = !isContentsOpen;
	$('#contents_inner').animate({left:cx}, {"duration":500, "easing":"easeInOutCubic", "queue":false});
	$('#map').animate({width:mw}, {"duration":500, "easing":"easeInOutCubic", "queue":false, "complete":updataMapBounds});
	
	// ボタンラベル変更
	var labelA = !isContentsOpen ? "close" : "open";
	var labelB = isContentsOpen ? "close" : "open";
	$("#toggle_content_btn").attr("src", $("#toggle_content_btn").attr("src").replace(labelA, labelB)); 
	
	// スクロール表示/非表示
	if(isContentsOpen) {
		if($('#scroll_inner').height() > sh) $('#slider_unit').css('visibility', 'visible');
	} else {
		$('#slider_unit').css('visibility', 'hidden');
	}
}


function focusListItem(element, id) {
	element.each(function(i, e) {
		if(id == i) {
			$(e).addClass('boxlink');
			scrollpain.forcus($(e).position().top);
		} else {
			$(e).removeClass('boxlink');
		}
	});
}
function blurListItem(element, id) {
	element.each(function(i, e) { $(e).removeClass('boxlink'); });
}

function resetInterval(id) {
	//
}
function scrollListItem(element, id) {
	element.each(function(i, e) {
		if(id == i) {
			scrollpain.forcus($(e).position().top);
		}
	});
}

/* ------------------------------------------------------------------------------
　　
------------------------------------------------------------------------------ */
/**
 * 画像ボタンに rollover, rollout を設定.
 * add rollover rollout.
 */
function setRollover() {
	var n = $("a > img").length;
	for(var i = 0; i < n; i++) {
		if($($("a > img")[i]).attr("src").match("_off.")) {  
			$($("a > img")[i]).mouseover(function() {
				$(this).attr("src", $(this).attr("src").replace("_off.", "_on.")); 
			});
			$($("a > img")[i]).mouseout(function() {
				$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
			});
		}
	}
}

/**
 * set elements height
**/
function heightFix(a) {
	var leng = a.length;
	var h = 0;
	for(i = 0; i < leng; i++) {
		$(a[i]).each(function(){ h = Math.max($(this).height(), h); });
	}
	for(i = 0; i < leng; i++) {
		$(a[i]).each(function(){ $(this).height(h); });
	}
}

/**
 * 数字の桁を合わせる
 */
function figuresFormat(num, figures) {
	var sn = String(num);
	var n = figures - sn.length;
	if(n <= 0) return sn;
	var temp = "";
	for(var i = 0; i < n; i++) temp += "0";
	return temp + sn;
}


/* テスト出力 */
function print_r( theObj, outputObjId ) {
	if ( theObj.constructor == Array || theObj.constructor == Object ) {
		var superUlElement = document.createElement("UL");
		for ( var p in theObj ){
			var liElement;
			var text;
			liElement = document.createElement("LI");
			if ( theObj[p].constructor == Array || theObj[p].constructor == Object ) {
				text = document.createTextNode( "[" + p + "] => " + typeof(theObj) );
				liElement.appendChild(text);
				if ( childUlElement = print_r( theObj[p], "__super__" ) ) {
					liElement.appendChild(childUlElement);
				}
			} else {
				text = document.createTextNode( "[" + p + "] => " + theObj[p] );
				liElement.appendChild(text);
			}
			superUlElement.appendChild(liElement);
		}
		if ( outputObjId != "__super__" ) {
			document.getElementById(outputObjId).appendChild(superUlElement);
		} else {
			return superUlElement;
		}
	} else {
		return null;
	}
}


function alertObj(obj) {
	var pp ='';
	for(var p in obj) pp += p + "："　+ obj[p] + "\n";
	alert(pp);
}
