(function($,Machi){
	if (!$) return;

	Machi = window.Machi = $.extend({
		domready:true,
		baseUrl:'',
		imgUrl:'/img',
		useCookie:true
	},Machi);
	var BullseyePosition = 'after';

	if (Machi.domready) {
		jQuery(function($){
			initHeader();
			if ($('#lastCondition').length > 0)
				Machi.initLastCondition();
			if ($('#myFavoriteBkCnt').length > 0)
				Machi.displayFavoriteBKCount();
		});
	}
	if (Machi.useCookie) {
		initCookie();
	}
	Machi.CounselHistory = CounselHistory;
	function initCookie () {
		if (Machi.Cookie) return;
		Machi.Cookie = function(options){this.initialize(options);};
		Machi.Cookie.prototype = {
			initialize: function(options) {
				options = options || {};
				var expires = '';
				if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
					var date;
					if (typeof options.expires == 'number') {
						date = new Date();
						date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
					} else {
						date = options.expires;
					}
					expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
				}
				this.expires = expires;
				this.path = options.path ? '; path=' + options.path : '';
				this.domain = options.domain ? '; domain=' + options.domain : '';
				this.secure = options.secure ? '; secure' : '';
			}
			,cookie: function(name, value) {
				if (!value) {
					var cookieValue = null;
					if (document.cookie && document.cookie != '') {
						var cookies = document.cookie.split(';');
						for (var i = 0; i < cookies.length; i++) {
							var cookie = (cookies[i]||'').replace(/^\s+|\s+$/g, "");
							if (cookie.substring(0, name.length + 1) == (name + '=')) {
								cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
								break;
							}
						}
					}
					return cookieValue;
				} else {
					document.cookie = [name, '=', encodeURIComponent(value), this.expires, this.path, this.domain, this.secure].join('');
				}
			}
			,clear: function(name) {
				document.cookie = [name, '=Deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;', this.path, this.domain, this.secure].join('');
			}
		};
	}

	function initHeader () {
		if (!Machi.Cookie) {
			initCookie();
		}
		var A = document.getElementsByTagName('a');
		var click = (function(){
			if (document.addEventListener) {
				return function(elem,func){elem.addEventListener('click',func,false);}
			} else if (document.attachEvent) {
				return function(elem,func){elem.attachEvent('onclick',function(){func.call(elem,window.event);});}
			}
		})();


		var pathname = location.pathname;
		if (/\/detail\/[\w\-]+\.html$/.test(pathname) &&
			window.pageTracker &&
			pageTracker._trackPageview) {
			var from = pathname.match(/\/detail\/([\w\-]+)\.html$/);
		}
	}

	function CounselHistory (id) {
		if (!Machi.Cookie) {
			initCookie();
		}

	}
	Machi.setElementValueById = function(name,value){
		var el = document.getElementById(name);
		el.value = value;
	}
	Machi.submit = function(idname){
		var form = document.getElementById(idname);
		form.submit();
	}
	Machi.submit.loading = function(idname){
		var SLIDER_KEYS = ['price','space','near','newer'];
		$.each(SLIDER_KEYS, function(){
			var left = document.getElementById('s_'+this).getElementsByTagName('div')[0].style.left + '';
			if (left.charAt(0) == '0') {
				document.getElementById(this).value = 0;
				Machi.setCounselParam(this, 0);
			}
		});
		Machi.submit(idname);
		Machi.mu.showLoading();
	}
	Machi.navitimeAction = function(baseStation, traveltime, threshold, func){
		$.getJSON(Machi.baseUrl + '/api/navitime.html?baseStation='+baseStation+'&traveltime='+traveltime+'&threshold=' + threshold , null, func);
	}
	Machi.zeroFormatter = function(v,n){
	    return ('0',v + '').slice(-2);
	}
	Machi.getDateString = function(){
		var date = new Date;
		var yyyy = date.getFullYear();
		var mm = Machi.zeroFormatter((date.getMonth() + 1),2);
		var dd = Machi.zeroFormatter(date.getDate(),2);
		var HH = Machi.zeroFormatter(date.getHours(),2);
		var MM = Machi.zeroFormatter(date.getMinutes(),2);
		var SS = Machi.zeroFormatter(date.getSeconds(),2);
		return [yyyy,mm,dd,HH,MM,SS].join('');
	}


	Machi.getLastConditionURL = function(){
		if (!Machi.Cookie) {
			initCookie();
		}
		var chid = 't';
		var domain = location.host.replace(/machi\.(chintai\.net)|.*/,'$1');
		var ch = new Machi.Cookie({expires:365,path:'/',domain:domain});
		var url = ch.cookie(chid);
		return url;
	}
	Machi.isLastCondition = function(){
		var url = Machi.getLastConditionURL();
		if (url) {
			return true;
		}
		return false;
	}
	Machi.goLastCondition = function(){
		var url = Machi.getLastConditionURL();
		if (url) {
			location.href=url;
		}else{
			alert('\u524D\u56DE\u306E\u6761\u4EF6\u304C\u3042\u308A\u307E\u305B\u3093\u3002');
		}
	}
	Machi.initLastCondition = function(){
		var eid = 'lastCondition';
		var is = Machi.isLastCondition();
		if (is) {
			$('#'+eid).css("cursor","pointer"); 
			$('#'+eid).click(function(){
				Machi.goLastCondition();
			});
		} else {
			var src = $('#'+eid).attr('src');
			$('#'+eid).attr('src', src.replace('gnavi_04.gif','gnavi_04_o.gif'));
		}
	}
	Machi.getFavoriteBKCount = function(){
		if (!Machi.Cookie) {
			initCookie();
		}
		var BKS_LIMIT = 10;
		var DATE_KEY = 'date';
		var ID_KEY = 'bkid';
		var chid = 'my_favorite_bk';
		var domain = location.host.replace(/machi\.(chintai\.net)|.*/,'$1');
		var ch = new Machi.Cookie({expires:365,path:'/',domain:domain});
		var ids = ch.cookie(chid);
		if (ids) {
			var res = ids.split('&');
			return res.length;
		}
		return 0;
	}
	Machi.displayFavoriteBKCount = function(){
		var ele = $('#myFavoriteBkCnt');
		if (ele) {
			var count = Machi.getFavoriteBKCount();
			if (count > 0) {
				ele.html(count + '\u4EF6');
			} else {
				ele.html('\u672A\u767B\u9332');
			}
		}
	}
})(window.jQuery,window.Machi);

