/* ---------------------------------------------
	
	site: http://kukini.honolulumarathon.jp/;

	created: 2009-09-01;
	last-modified: 2009-09-18;

--------------------------------------------- */



// popup __________________________________________________
var MacIe = navigator.userAgent.indexOf( "Mac" ) > -1 && navigator.userAgent.indexOf( "MSIE" ) > -1;
var JsWin;

function commonpop( JsUrl, JsWinName, JSWidth, JSHeight ){
	JSWidth = parseInt( JSWidth );
	JSHeight = parseInt( JSHeight );
	if( !MacIe ){ JSWidth = JSWidth + 17; }
	if( MacIe ){ JSWidth = JSWidth - 1 ; }
	JsProperty = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=" + JSWidth + ",height=" + JSHeight;
	JsWin = window.open( JsUrl, JsWinName, JsProperty );
	if( MacIe ){ return; }
	JsWin.focus();
}

function commonpop2( JsUrl, JsWinName, JSWidth, JSHeight ){
	JSWidth = parseInt( JSWidth );
	JSHeight = parseInt( JSHeight );
	JsProperty = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + JSWidth + ",height=" + JSHeight;
	JsWin = window.open( JsUrl, JsWinName, JsProperty );
	if( MacIe ){ return; }
	JsWin.focus();
}



// Rollover __________________________________________________

$(document).ready(function(){
						   
if (typeof document.body.style.maxHeight != "undefined"){/* add for kukini */

	function rollover_img(selector, suffix){
		$(selector).each(function(i){
			var defaultImg = $(this).attr("src");
			if (!defaultImg.match((suffix))) {
				var point = defaultImg.lastIndexOf(".");
				var rolloverImg = defaultImg.slice(0, point) + suffix + defaultImg.slice(point);
				var preloadImg = new Image();
				preloadImg.src = rolloverImg;
				$(this).hover(function(){
					$(this).attr("src", rolloverImg);},function(){
						$(this).attr("src", defaultImg);
						}
					)
				}
			})
		}
	rollover_img(".rollover", "_ov");

};

});




// page scroll __________________________________________________
jQuery.extend( jQuery.easing, {
	def: 'easeOutExpo',
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});
$(document).ready(function(){
	$("a[href^=#],area[href^=#]").click(function(){
	var localHash = this.hash;
	if(!localHash || localHash == "#") return false;
	$($.browser.safari ? 'body' : 'html').animate({scrollTop: $(localHash).offset().top}, 700, "easeOutExpo");
	return false;
});
});



// include __________________________________________________
var include = new Include();
include.init('resources/js/common.js');

function Include() {
	// 初期化
	this.init = function(base) {
		var scripts = document.getElementsByTagName('script');
		for (var i = 0, l = scripts.length; i < l; i++) {
			if (scripts[i].src.match(base + '([\?].*|$)')) {
				this.root = scripts[i].src.split(base)[0] + './';
				break;
			};
		};
	};

	// 出力
	this.write = function(src) {
		src = src.join('\n');
		while(src.match('__path__')) {
			src = src.replace('__path__', this.root);
		}
		document.open();
		document.write(src);
		document.close();
	};
};



// navigation add class __________________________________________________
$(document).ready(function(){
	$("ul.nav li:first-child").addClass("first");
});



// table add class __________________________________________________
$(document).ready(function(){
	$("table.history tr :nth-child(1)").addClass("date");
	$("table.history tr :nth-child(2)").addClass("distance");
	$("table.history tr :nth-child(3)").addClass("time");
	$("table.history tr :nth-child(4)").addClass("comment");
	$("table.history tr :nth-child(5)").addClass("bgm");
	$("table.history tr :nth-child(6)").addClass("btn");
	
	$("table.ranking tr th:nth-child(1)").addClass("rank");
	$("table.ranking tr td:nth-child(1)").addClass("rank");
	$("table.ranking tr th:nth-child(2)").addClass("name");
	$("table.ranking tr td:nth-child(2)").addClass("name");

	$("table.search tr :nth-child(1)").addClass("name");
});



// regist form open/close __________________________________________________
$(document).ready(function(){
	var sc = $('input:radio[name=sexCheck]');
	var yc = $('input:radio[name=yearCheck]');
	var gc = $('input:radio[name=goalCheck]')

	if(sc.length){
		sexCheck();
	};
	if(yc.length){
		yearCheck();
	};
	if(gc.length){
		goalCheck();
	};

	sc.click(function () {
		sexCheck();
	});
	yc.click(function () {
		yearCheck();
	});
	gc.click(function () {
		goalCheck();
	});
});

function sexCheck(){
	var a = $('input:radio[name=sexCheck]:checked').val();
	if(a == "women"){
		$("table.form tr.avatar ul.women").show();
		$("table.form tr.avatar ul.men").hide();
	}else{
		$("table.form tr.avatar ul.women").hide();
		$("table.form tr.avatar ul.men").show();
	}
}

function yearCheck(){
	var a = $('input:radio[name=yearCheck]:checked').val();
	if(a == "yes"){
		$("table.form tr.year").show();
	}else{
		$("table.form tr.year").hide();
	}
}

function goalCheck(){
	var a = $('input:radio[name=goalCheck]:checked').val();
	if(a == "yes"){
		$("div#goalArea table.input").show();
	}else{
		$("div#goalArea table.input").hide();
	}
}



// tab change __________________________________________________

$(function() {
	tabChange.init();
});

var tabChange = {
	
	conf: {
		containerSelector: '.tabbox', //コンテナ
		tabSelector: '.tab li a', //タブ
		sectionSelector: '.section', //コンテンツ
		currentClass: 'current', //カレント要素
		start: 1, //何番目スタート
		duration: 300, //フェードの時間
		easing: 'linear' //フェードの種類
	},
	
	item: [],
	
	init: function() {
		var self = this;
		var conf = this.conf;
		var start = conf.start - 1;
		
		$(conf.containerSelector).each(function() {
			var container = $(this);
			var index = self.item.length;
			
			var obj = {
				containerObj: container,
				current: start,
				tab: container.find(conf.tabSelector),
				section: container.find(conf.sectionSelector).css('opacity', 0)
			}
			
			self.item.push(obj);
			
			obj.section.eq(start).css({
				display:'block',
				opacity: 1
			});
			obj.tab.eq(start).addClass(conf.currentClass);
			
			obj.tab.each(function(i) {
				$(this).click(function() {
					if($(this).attr('class') != conf.currentClass) { self.change(index, i); }
				});
			});
		});
		
	},
	
	change: function(index, i) {
		var self = this;
		var conf = this.conf;
		var obj = this.item[index];
		var current = obj.current;
		var tab = obj.tab;
		var section = obj.section;
		tab.removeClass(conf.currentClass);
		tab.eq(i).addClass(conf.currentClass);
			
		section.css({
			display: 'none',
			opacity: 0
		});
		section.eq(i).css({
			display: 'block'
		}).stop().animate({
			opacity: 1
		}, {
			duration: conf.duration,
			easing: conf.easing,
			complete: function(){
				if (self.isIE) this.style.removeAttribute('filter');
			}
		});
		
		
	},
	
	// IE判定フラグ
	isIE: /msie ([0-9]+)/.test(navigator.userAgent.toLowerCase())
	
}



// load html __________________________________________________

function loadHtml(id, path) {
    div_id = "#" + id;
    $.ajaxSetup({
        cache: false
    });
    $(div_id).load(path);
}



// Fx3.6 flash 1px bug fix __________________________________________________

var isFireFoxMac=false;
if (navigator.userAgent.indexOf("Firefox")!=-1) {
    if (navigator.platform == "MacIntel" || navigator.platform == "MacPPC") {
        isFireFoxMac = true;
    }
}
function isEven(value){
    return (value%2 == 0);
}
function ensureOddWidth() {
    var width = window.innerWidth;
    if (isEven(width)) {
      self.resizeTo(width-1, window.outerHeight);
    }
}
if (isFireFoxMac) {
    window.onresize = ensureOddWidth;
    window.onload = ensureOddWidth;
}


