
var CampusStyleEventHandler = function(){};
CampusStyleEventHandler.bind = function(){
	$(".changeSubmit").change(function() {
		$(this).parents("form").submit();
	}).change();
	$(".addAlbum").click(function() {
		var id = $(this).attr("id");
		var self = this;
		$.ajax({
			url: "/campusstyle/cart/add/" + id + "/",
			type: "get",
			cache: false,
			success: function(xml){
				setAlbumCount($("count", xml).text());
				clipBaloon(self, xml);
				if ($("add", xml).length > 0) {
					var album = $('#album ul');
					if (album.length > 0) {
						if (album.children().length > 0) {
							album.prepend(createAlbum(xml));
						} else {
							album.append(createAlbum(xml));
						}
					}
				}
			}
		});
	});
	$(".delAlbum").click(function() {
		var id = $(this).attr("id");
		var self = this;
		$.blockPage();
		$.ajax({
			url: "/campusstyle/cart/delete/" + id + "/",
			type: "get",
			cache: false,
			success: function(xml){
				var count = $("count", xml).text();
				setAlbumCount(count);
				$(self).parents("li").remove();
				$("#shiryoseikyuLink").attr('href', $("shiryoseikyuUrl", xml).text());
				if (count == 0) {
					$("div.used").css("display", "none");
					$("div.empty").css("display", "block");
				}
			},
			complete: function(){
				$.unblockPage();
			}
		});
	});
	// IE6ではなぜか<area>にhoverが効かなかった
	$($("#ImgMap").attr('usemap')).children('area').mouseover(
		function(){
			var rows = $(this).parent().children('area').index(this) + 1;
			var src = $("#ImgMap").attr('src').replace(/[0-9]*.gif$/,'0' + rows + '.gif');
			$("#ImgMap").attr('src', src);
		}
	);
	$($("#ImgMap").attr('usemap')).children('area').mouseout(
		function(){
			var src = $("#ImgMap").attr('src').replace(/[0-9]*.gif$/,'.gif');
			$("#ImgMap").attr('src', src);
		}
	);
	$("#tab").children().click(function() {
		$(this).siblings(".selected").removeClass("selected");
		$(this).addClass("selected");
		var viewId = $(this).attr("id").replace("tab_", "#");
		$(viewId).siblings(".tab").css("display", "none");
		$(viewId).css("display", "block");
		adjustHeight($(viewId + " .adjustHeight"));
	});
};
CampusStyleEventHandler.unbind = function(){
	$(".changeSubmit").unbind('change');
	$("form.fwsearch").unbind('submit');
	$("input.freeword").unbind('focus');
	$("input.freeword").unbind('blur');
	$(".addAlbum").unbind('click');
	$(".delAlbum").unbind('click');
	$($("#ImgMap").attr('usemap')).children('area').unbind('mouseover');
	$($("#ImgMap").attr('usemap')).children('area').unbind('mouseout');
	$("#tab").children().unbind('click');
};

$(document).ready(function(){
	CampusStyleEventHandler.bind();
	new FreewordSerch("form.gakkoSearch", '校名の一部でもOK').bind();
	new FreewordSerch("form.reportSearch", '気になるキーワードを入れてみよう！').bind();

	$(".adjustHeight").each(function(){
		adjustHeight(this);
	});
	$(".table .row,.rowLast").each(function(){
		adjustHeight(this);
	});
});

function FreewordSerch(selector, info) {
	this.form = $(selector);
	this.info = info;
}
FreewordSerch.prototype = {
	bind: function() {
		var form = this.form;
		var info = this.info;
		var text = $(":text", form);

		if (text.val() == '' || text.val() == info) text.val(info).css("color", "#a8a8a8");

		form.submit(function(){
			var val = $(":text", this).val()
			if (!val || val == info) {
				alert('検索ワードを指定してください');
				return false;
			}
		});
		text.focus(function(){
				if ($(this).val() == info) $(this).val('').css("color", "#000000");
			})
			.blur(function(){
				if ($(this).val() == '') $(this).val(info).css("color", "#a8a8a8");
			});
	},
	unbind: function() {
		this.elem.unbind('submit');
		$(":text", elem).unbind('focus').unbind('blur');
	}
}

function clipBaloon(elem, xml) {

	$(elem).parent().children("div.baloon").remove();
	$(elem).parent().append(
		$("<div/>").addClass("baloon").append(
			$("<p/>").addClass("message").append(
				$("result", xml).text()
			)
		).append(
			$("<p/>").addClass("link").append(
				$("<a/>").attr("href", "/campusstyle/cart/").append(
					$("<img/>").attr({
						src: "/images/campusstyle/common/link_album.gif",
						alt: "キャンパスアルバムへ",
						width: 126, height: 11
					})
				)
			)
		).append(
			$("<p/>").addClass("close").append(
				$("<img/>").attr({
					src: "/images/campusstyle/common/btn_close.gif",
					alt: "閉じる",
					width: 40, height: 10
				}).click(function() {
					var div = $(this).parent();
					$(this).parents("div.baloon").remove();
				})
			)
		)
	)
}

function createAlbum(xml) {

	var imageWidth = $("type", xml).text() == 'CAMPUS_LIFE' ? 35 : 70;

	var album =
		$("<li/>").append(
			$("<p/>").addClass("ph").append(
				$("<a/>").attr("href", $("url", xml).text()).append(
					$("<img/>").attr({
						src: $("image", xml).text(),
						alt: $("alt", xml).text(),
						width: imageWidth,
						height: 70
					})
				)
			)
		).append(
			$("<p/>").addClass("txt01").append($("catch", xml).text())
		);

	return album;
}

function adjustHeight(elem) {
	var maxHeight = 0;
	var adjustElem = $(elem).children();
	adjustElem.each(function(){
		var height = $(this).height();
		if (height > maxHeight) maxHeight = height;
	});
	adjustElem.height(maxHeight);
}

function setAlbumCount(count) {
	if (count) $('#albumCount').empty().html(count);
}


