var status = "";
var linkstatus = "artists";
var ar = 3/2;
var scheduleArr = [];
var newsArr = [];

resize_and_crop = function() {
	// Set background div to be equal to window dimensions
	jQuery("div#uta_background").css("width", jQuery(window).width());
	jQuery("div#uta_background").css("height", jQuery(window).height());

	// TO DO Remove / clean up up background_image_aspect_ratio helper method
	var window_ar = jQuery(window).width() / jQuery(window).height();
	var crop_ar = ar;
	var image_ar = ar;

	if (crop_ar > window_ar) { // Photo is wider than the window
		var image_height = jQuery(window).height() * 2592 / 2592.0;
		var image_width = image_height * image_ar;
	}
	else { // Window is wider than the photo
		var image_width = jQuery(window).width() * 3888 / 3888.0;
		var image_height = image_width / image_ar;
	}

	// TO DO Adjust top and left offset based on window size 
	var image_top = -(0.0/2592 * image_height);
	var image_left = -(0.0/3888 * image_width);
	jQuery(".background-image").css("height", image_height);
	jQuery(".background-image").css("width", image_width);
	jQuery(".background-image").css("top", image_top);
	jQuery(".background-image").css("left", image_left);
};

setAnchor = function(anchor,fadeOut) {
	hideDetail();
	resize_and_crop();
	if( status != anchor ){
		$("div#content-wrap > *").hide();
		$("div#uta-background > img").hide();
		if( fadeOut == true ){
			$("div#uta-background > img." + status).fadeOut("slow",function(){
				$("div#uta-background > img." + anchor).fadeIn("slow",function(){
						$("div#" + anchor + "-wrap").fadeIn("slow");
						$(".trans").css("filter", "alpha(opacity=75)");
						$(".trans").css("-ms-filter", "alpha(opacity=75)");
						if( anchor == "news" ){
							$("div#social-wrap").fadeIn("slow");
						}
				});
			});
		}else{
			$("div#uta-background > img." + anchor).fadeIn(function(){
				$("div#links-wrap > div.changeartists").show();
				$("div#links-wrap > div.artists").show();
				$("div#" + anchor + "-wrap").show();
				$(".trans").css("filter", "alpha(opacity=75)");
				$(".trans").css("-ms-filter", "alpha(opacity=75)");
				if( anchor == "news" ){
					$("div#social-wrap").show("slow");
				}
				$("div#header").show();
				$("div#copyrights").show();
				$("div#musicplayer-wrap").show();
			});
		}
		status = anchor;
	}else{
		return false;
	}
}

//$(window).load(resize_and_crop);
$(window).resize(resize_and_crop);

// measures for cache
var dt = new Date();
param = "date=" + dt;

$.get("data/configxml_multi.xml");


// blogゲット
/*
$.getJSON("script/blogParse2Json.php" ,function(json){
	print_r(json);
	//$.each(json.post, function(){
	//}
});
*/

showTicket = function(venue,date){
	$("div#ticket-wrap dd.ticket-title").html(date + " : " + venue);
	$("div#ticket-wrap input.title-val").val(date + " : " + venue);
	$("div#ticket-wrap").fadeIn("slow");
}

showDetail = function(i){
	$("div#schedule-wrap > dl").fadeOut("fast", function(){
		$("div#schedule-wrap > div.detail" + i).fadeIn("fast");
	});
}

hideDetail = function(){
	$("div#ticket-wrap").fadeOut("fast");
	$(".error-message").html("");
	$(".title-val").val("");
	$(".email").val("");
	$(".formbody").val("");
	$("div#schedule-wrap > div.dtlcntt").hide(function(){
		$("div#schedule-wrap > dl").fadeIn("fast");
	});
}

postQuery = function(wrapper){
	var errArr = new Array();
	$("div#" + wrapper + "-wrap .error-message").html("");

	if( $("div#" + wrapper + "-wrap input.title-val").val() == "" ){
		errArr.push("タイトルは必須です");
	}
	if( $("div#" + wrapper + "-wrap input.email").val() == "" ){
		errArr.push("E-Mailは必須です");
	}else{
		if(!$("div#" + wrapper + "-wrap input.email").val().match(/.+@.+\..+/)){
			errArr.push("E-Mailの形式が異なります");
		}
	}
	if ( $("div#" + wrapper + "-wrap input.name").val() == "" ){
		errArr.push("お名前は必須です");
	}
	if ( wrapper == "purchase" ){
		if( $("div#purchase-wrap input.price-val").val() == "" ){
			errArr.push("購入金額は必須です");
		}
		if( $("div#purchase-wrap input.address").val() == "" ){
			errArr.push("配送先は必須です");
		}
	}else if( wrapper == "ticket" ){
		if( $("div#ticket-wrap input.ticket-type:checked").val() == "ticket" ){
			if( $("div#ticket-wrap select.number-of-ticket").val() == "") {
				errArr.push("予約枚数は必須です");
			}
		}else{
			if( $("div#ticket-wrap textarea.formbody").val() == "" ){
				errArr.push("メッセージは必須です");
			}
		}
	}else if( wrapper == "contact" ){
		if( $("div#contact-wrap select.title-val:selected").val() == "" ){
			errArr.push("タイトルは必須です");
		}
		if( $("div#contact-wrap textarea.formbody").val() == "" ){
			errArr.push("メッセージは必須です");
		}
	}else{
		return false;
	}

	if( errArr.length > 0 ){
		for(var x = 0; x < errArr.length; x++){
			$("div#" + wrapper + "-wrap .error-message").append(errArr[x] + "<br />");
		}
	}else{
		var postData = "form-type=" + wrapper;
		if( wrapper == "contact" ){
			postData+= "&title-val=" + $("div#contact-wrap select.title-val").val();
		}else{
			postData+= "&title-val=" + $("div#" + wrapper + "-wrap input.title-val").val();
		}
		postData+= "&name=" + $("div#" + wrapper + "-wrap input.name").val()
				 + "&email=" + $("div#" + wrapper + "-wrap input.email").val();
		if( wrapper == "ticket" ){
			postData += "&ticket-type=" + $("div#ticket-wrap input.ticket-type:checked").val()
						+ "&number-of-ticket=" + $("div#ticket-wrap select.number-of-ticket").val();
		}else if( wrapper == "purchase" ){
			postData += "&price_val=" + $("div#purchase-wrap input.price-val").val()
						+ "&address=" + $("div#purchase-wrap input.address").val();
		}
		postData += "&body=" + $("div#" + wrapper + "-wrap textarea.formbody").val();

		$.ajax({
			type: "POST",
			url: "script/query.php",
			data: postData,
			dataType: "json",
			success: function(json){
				if( json.status == "OK" ){
					$("div#popup-wrap > p.message").html(json.message);
					$("div#" + wrapper + "-wrap input").val("");
					$("div#" + wrapper + "-wrap textarea").val("");
					$("div#popup-wrap").show();
				}else{
					for(var j = 0; j < json.errors.length; j++){
						$("div#" + wrapper + "-wrap .error-message").append(json.errors[j]);
					}
				}
			},
			error: function(){
				$("div#" + wrapper + "-wrap .error-message").append("通信エラーが発生しました。しばらくしてからもう一度お試しください");
			}
		});
	}
}

clearAll = function(){
	$("input").val("");
	$("textarea").val("");
	$("div.error-message").html("");
	$("div#popup-wrap").fadeOut("fast");
	ar = 3/2;
	setAnchor("news",true);
}

showPurchase = function(title,price){
	$("div#purchase-wrap input.title-val").val(title);
	$("div#purchase-wrap input.price-val").val(price);
	$("div#purchase-wrap dd.disc-title").html(title);
	$("div#purchase-wrap dd.disc-price").html("&yen;" + price + "-");
	$("div#purchase-wrap").fadeIn("slow");
}
changeLinks = function(type){
	if(linkstatus != type) {
		$("div#links-wrap > div.change" + linkstatus).hide();
		$("div#links-wrap > div." + linkstatus).hide();
		$("div#links-wrap > div.change" + type).fadeIn("fast");
		$("div#links-wrap > div." + type).fadeIn("fast");
	}
	linkstatus = type;
}

function print_r(obj) {
	var count_obj = 0;
	function _output(str) {
		document.writeln(str + "<br/>");
	}
	function _print_r(obj, name, level) {
		var s = "";
		if (obj == undefined || level > 4) return;
		for (var i = 0; i < level; i++) { s += " | "; }
		s += " - " + name + ":" + typeof(obj) + "=" + obj;
		_output(s);
		if (name == "document" || typeof(obj) != "object") return;
		for ( key in obj ) {
			if (count_obj++ > 150) return;
			_print_r(obj[key], key, level + 1);
		}
	}
	_print_r(obj, "*", 0);
}


$(document).ready(function(){

	if(status == ""){
		// to change views by hash
		if (window.location.hash != ""){
			ar = 4/3;
			setAnchor(window.location.hash.slice(1),false);
		}else{
			// ar = 3/2;
			ar = 4/3;
			setAnchor("news",false);
		}
	}else{
		ar = 4/3;
		setAnchor(status,false);
	}

	$("div#header > ul > li.news").click(function(){
		// ar = 3/2;
		ar = 4/3;
		setAnchor("news",true);
	});
	$("div#header > ul > li.bio").click(function(){
		ar = 4/3;
		setAnchor("bio",true);
	});
	$("div#header > ul > li.music").click(function(){
		ar = 4/3;
		setAnchor("music",true);
	});
	$("div#header > ul > li.dvd").click(function(){
		ar = 4/3;
		setAnchor("dvd",true);
	});
	$("div#header > ul > li.schedule").click(function(){
		ar = 4/3;
		setAnchor("schedule",true);
	});
	$("div#header > ul > li.links").click(function(){
		ar = 4/3;
		setAnchor("links",true);
	});
	$("div#header > ul > li.contact").click(function(){
		ar = 4/3;
		setAnchor("contact",true);
	});
	$("div#header > ul > li.mailmagazine").click(function(){
		ar = 4/3;
		setAnchor("mailmagazine",true);
	});

	$("div#ticket-wrap input.ticket-type:first").click(function(){
		$("div#ticket-wrap .number-of-ticket").fadeIn("fast");
	});

	$("div#ticket-wrap input.ticket-type:last").click(function(){
		$("div#ticket-wrap .number-of-ticket").fadeOut("fast");
	});


	$(".purchase").click(function(){
		var title = $(this).parent().find(".title").text();
		var price = $(this).parent().parent().find(".image > .price").attr("title");
		showPurchase(title,price);
	});

	$(".ticket-query").click(function(){
		var venue = $(this).parent().find("a.venuelnk").text();
		var date = $(this).parent().find("div.date").attr("date");
		showTicket(venue,date);
	});

	$("div.submit").click(function(){
		$(this).parent("form").submit();
	});

	$("div.cansel").click(function(){
		$(this).parent().find("input").val("");
		$(this).parent().find("textarea").val("");
		$(this).parent().find("div.error-message").html("");
		$(this).parents().eq(2).fadeOut("fast");
	});

});


