var undefined;


var OPTIONS = new Object();
$("script").each(function (i){
	var query = this.src.match(/site.js\?(.*)/);
	if (query){
		var queries = query[1].split("&");
		for (var i=0; i < queries.length; i++){
			var parts = queries[i].split("=");
			if (parts[1] == undefined){
				OPTIONS[parts[0]] = "";
			} else {
				OPTIONS[parts[0]] = parts[1];
			}
		}
	}
});


String.prototype.zfill = function (width){
	var str = this.toString();
	while (str.length < width) str = '0' + str;
	return str;
}


Date.prototype.parseISO = function (str){
	var re = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/;
	try {
		var date_array = re.exec(str).slice(1);
	} catch (e){
		throw '"' + str + '" is not a valid ISO date';
	}
	this.setUTCFullYear(date_array[0]);
	// Specify base 10, otherwise "08" & "09" incorrectly converted as octal
	this.setUTCMonth(parseInt(date_array[1], 10) - 1);
	this.setUTCDate(date_array[2]);
	this.setUTCHours(date_array[3]);
	this.setUTCMinutes(date_array[4]);
	this.setUTCSeconds(date_array[5]);
	return this;
}


Date.prototype.strfdate = function (format, local){
	var dt = this;
	if (local){
		var year = dt.getFullYear();
		var month = dt.getMonth();
		var date = dt.getDate();
		var hours = dt.getHours();
		var minutes = dt.getMinutes();
		var seconds = dt.getSeconds();
		
		var day = dt.getDay();
		var offset = dt.getTimezoneOffset();
	} else {
		var year = dt.getUTCFullYear();
		var month = dt.getUTCMonth();
		var date = dt.getUTCDate();
		var hours = dt.getUTCHours();
		var minutes = dt.getUTCMinutes();
		var seconds = dt.getUTCSeconds();
		
		var day = dt.getUTCDay();
		var offset = 0;
	}
	
	var pieces = new Array();
	var piece;
	for (var c=0; c < format.length; c++){
		if (format[c] == '\\'){
			c += 1;
			piece = format[c];
		} else {
			try {
				formatter = 'format_'+format[c]+'()';
				piece = eval(formatter);
			} catch (e){
				piece = format[c];
			}
		}
		pieces.push(piece);
	}
	return pieces.join('');
	
	function format_a(){
		if (hours > 11) return 'p.m.';
		return 'a.m.';
	}
	function format_A(){
		if (hours > 11) return 'PM';
		return 'AM';
	}
	function format_b(){
		return format_M().toLowerCase();
	}
	/* function format_B(){ alert('error: not implemented'); } */
	function format_d(){
		return date.toString().zfill(2);
	}
	function format_D(){
		return format_l().slice(0, 3);
	}
	function format_f(){
		if (minutes == 0) return format_g();
		return format_g() + ':' + format_i();
	}
	function format_F(){
		var NAMES = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		return NAMES[month];
	}
	function format_g(){
		if (hours == 0) return '12';
		if (hours > 12) return (hours - 12).toString();
		return hours.toString();
	}
	function format_G(){
		return hours.toString();
	}
	function format_h(){
		return format_g().zfill(2);
	}
	function format_H(){
		return format_G().zfill(2);
	}
	function format_i(){
		return minutes.toString().zfill(2);
	}
	/* function format_I(){ // TODO: to be implemented } */
	function format_j(){
		return date.toString();
	}
	function format_l(){
		var NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
		return NAMES[format_w()];
	}
	function format_L(mod){
		var y = (mod == undefined) ? year : year + mod;
		return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
	}
	function format_m(){
		return format_n().zfill(2);
	}
	function format_M(){
		return format_F().slice(0, 3);
	}
	function format_n(){
		return (month + 1).toString();
	}
	function format_N(){
		var NAMES = ['Jan.','Feb.','March','April','May','June','July','Aug.','Sept.','Oct.','Nov.','Dec.'];
		return NAMES[month];
	}
	function format_O(){
		var offset_hours = parseInt(offset / 60).toString().zfill(2);
		var offset_minutes = (offset % 60).toString().zfill(2);
		var sign = (offset >= 0) ? '+' : '-';
		return (sign + offset_hours + offset_minutes);
	}
	function format_P(){
		if (minutes == 0 && hours == 0) return 'midnight';
		if (minutes == 0 && hours == 12) return 'noon';
		return format_f() + ' ' + format_a();
	}
	function format_r(){
		return dt.strfdate('D, j M Y H:i:s O', local);
	}
	function format_s(){
		return seconds.toString().zfill(2);
	}
	function format_S(){
		if (date == 11 || date == 12 || date == 13) return 'th';
		var digit = date % 10;
		if (digit == 1) return 'st';
		if (digit == 2) return 'nd';
		if (digit == 3) return 'rd';
		return 'th';
	}
	function format_t(){
		var DAYS = [31, 28+format_L(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		return DAYS[month].toString();
	}
	/* function format_T(){ // TODO: to be implemented } */
	function format_U(){
		return (Math.round(dt.getTime() / 1000) + (offset * 60)).toString();
	}
	function format_w(){
		return day.toString();
	}
	function format_W(){
		var week_number;
		var jan1 = new Date();
		if (local){
			jan1.setDate(1);
			jan1.setMonth(0);
			jan1.setFullYear(year);
			var jan1_weekday = convertday(jan1.getDay()) + 1;
		} else {
			jan1.setUTCDate(1);
			jan1.setUTCMonth(0);
			jan1.setUTCFullYear(year);
			var jan1_weekday = convertday(jan1.getUTCDay()) + 1;
		}
		var weekday = convertday(day) + 1;
		var day_of_year = parseInt(format_z());
		if (day_of_year <= (8 - jan1_weekday) && jan1_weekday > 4){
			if (jan1_weekday == 5 || (jan1_weekday == 6 && format_L(-1))){
				week_number = 53;
			} else {
				week_number = 52;
			}
		} else {
			if (format_L()){
				var i = 366;
			} else {
				var i = 365;
			}
			if ((i - day_of_year) < (4 - weekday)){
				week_number = 1;
			} else {
				var j = day_of_year + (7 - weekday) + (jan1_weekday - 1);
				week_number = parseInt(j / 7);
				if (jan1_weekday > 4){
					week_number -= 1;
				}
			}
		}
		return week_number.toString();
		
		function convertday(d){
			return (7 + d - 1) % 7;
		}
	}
	function format_y(){
		return format_Y().slice(-2);
	}
	function format_Y(){
		return year.toString();
	}
	function format_z(){
		var DAYS = [0, 31, 59+format_L(), 90, 120, 151, 181, 212, 243, 273, 304, 334];
		var result = DAYS[month] + date;
		return result.toString();
	}
	function format_Z(){
		return (offset * 60).toString();
	}
}


var ProgressIndicator = {
	timer: null,
	running: new Object(),
	cache: new Object(),
	
	init: function(size){
		if (size == undefined){
			ProgressIndicator._cache('small');
			ProgressIndicator._cache('large');
		} else if (typeof(size) == "string"){
			ProgressIndicator._cache(size);
		} else {
			for (var i=0; i < size.length; i++){
				ProgressIndicator._cache(size[i]);
			}
		}
	},
	
	_cache: function(size){
		ProgressIndicator.cache[size] = new Array();
		for (var i=0; i < 12; i++){
			ProgressIndicator.cache[size][i] = new Image();
			ProgressIndicator.cache[size][i].src = '/media/js/progressindicator/images/indicator_' + size + '_' + i + '.png';
		}
	},
	
	start: function(id){
		ProgressIndicator.running[id] = 0;
		$('#'+id).show();
		if(ProgressIndicator.timer == null){
			ProgressIndicator._step();
			ProgressIndicator.timer = setInterval(ProgressIndicator._step, 42);
		}
	},
	
	stop: function(id){
		$('#'+id).hide();
		delete ProgressIndicator.running[id];
		var count = 0;
		for (var id in ProgressIndicator.running){
			count += 1
		}
		if(count == 0){
			clearInterval(ProgressIndicator.timer);
			ProgressIndicator.timer = null;
		}
	},
	
	_step: function(){
		for (var id in ProgressIndicator.running){
			var step = ProgressIndicator.running[id];
			var size = $('#'+id).attr('className').split('_spinner')[0];
			$('#'+id).find('img').attr('src', ProgressIndicator.cache[size][step].src);
			if(ProgressIndicator.running[id] == ProgressIndicator.cache[size].length-1){
				step = 0;
			} else {
				step += 1;
			}
			ProgressIndicator.running[id] = step;
		}
	}
}


var CookieManager = {
	read: function (name){
		var cookies = document.cookie.split('; ');
		for (var i=0; i < cookies.length; i++){
			var cookie = cookies[i].match(/(.*)=(.*)(;expires=.*)?/);
			if (cookie && cookie[1] == name){
				return unescape(cookie[2]);
			}
		}
		return null;
	},
	write: function (name, value, days){
		var expires = '';
		if (days != undefined){
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			expires = '; expires=' + date.toGMTString();
		}
		document.cookie = name + '=' + escape(value) + expires + '; path=/';
	},
	del: function (name){
		CookieManager.write(name, '', -1);
	}
};


// force external links open in new windows

$(document).ready(function (){
	$("a[@href^=http://]").attr("target", "_blank");
	$("a[@href^=https://]").attr("target", "_blank");
});


// equalise column heights & roll-up any jp language boxes

$(document).ready(function (){
	function equalise_columns(){
		if ($("div#body.single").length){
			// Page is a single column layout so no adjustment necessary
			return false;
		}
		var right_column = $("div.column:eq(1)");
		var left_column = $("div.column:eq(0)");
		var left_column_height = left_column.height();
		if (right_column.height() < left_column_height){
			// Reset left column height to work around dimension rounding bug in Firefox
			left_column.css("min-height", left_column_height);
			right_column.css("min-height", left_column_height);
		}
		if (right_column.height() < left_column_height){
			// Browser didn't respect "min-height" attribute so set "height" as well
			left_column.css("height", left_column_height);
			right_column.css("height", left_column_height);
		}
		return true;
	}
	
	$("div.multi_lang").each(function (i){
		var block = this;
		$(this).find('div[@lang="jp"]').removeClass("open").addClass("closed");
		$(this).find('div[@lang="en"]').find("p:last").append('&nbsp;<span class="separated"><a href="#" rel="show">日本語</a></span>');
		$(this).find('a[@rel="show"]').one("click", function (e){
			$(block).find('div[@lang="jp"]').removeClass("closed").addClass("open");
			$(this).parent().remove();
			equalise_columns();
			return false;
		});
	});
	equalise_columns();
});


// admin autofill trickery

if (OPTIONS["admin"] != undefined){
	if (document.location.search != ""){
		$(document).ready(function (){
			var queries = document.location.search.slice(1).split("&");
			for (var i=0; i < queries.length; i++){
				var parts = queries[i].split("=");
				if (parts[1] != undefined){
					// $("#"+parts[0]).value = parts[1];
					document.getElementById(parts[0]).value = parts[1];
				}
			}
		});
	}
}


/*
$(document).ready(function(){
	var speed = "fast";
	
	$('[@id^="switch_"]').each(function(i){
		var target_id = "#" + this.id.split('_')[1];
		
		$("a", this).one("click", function(){
			$(target_id + ":hidden").slideDown(speed);
			$(this).hide();
			return false;
		});
	});
	$('[@id^="toggle_"]').each(function(i){
		var target_id = "#" + this.id.split('_')[1];
		
		$("a", this).toggle(function(){
			$(target_id + ":hidden").slideDown(speed);
			return false;
		}, function(){
			$(target_id + ":visible").slideUp(speed);
			return false;
		});
	});
	
	$('[@id^="toggleMore_"]').each(function(i){
		var target_id = "#" + this.id.split('_')[1];
		
		$("a", this).toggle(function(){
			$(target_id + ":hidden").slideDown(speed);
			$("a", this).removeClass("more");
			return false;
		}, function(){
			$(target_id + ":visible").slideUp(speed);
			$("a", this).addClass("more");
			return false;
		});		
	});
});
*/
