/* -------------------------------------------

	* init
	
------------------------------------------- */
$(function () {

	/* Rollover */
	new RolloverImages("rollover", "on");

	/* adjustHeight */
	var fSWatcher = new fontSizeWatcher();
	if(!$(CLASS_TARGET_ELEMENTS).length == 0) fSWatcher.start();
	$( CLASS_TARGET_ELEMENTS ).initAdjustHeight();
	
});

/* -------------------------------------------

	* @function
	
------------------------------------------- */
(function ($) {
	
	/* ---------------------
		adjustHeight Setup
	--------------------- */
	/* class */
	CLASS_TARGET_ELEMENTS = ".adjustHeight";
	
	/* CLASS_TARGET_ELEMENTS */
	CLASS_TARGET_FIGURE_ELEMENTS = "";

	fontSizeWatcher = function(){
		this.elm = $('<div id="fontSizeWatcher">&nbsp;</div>').css({
			display: 'block',
			visibility: 'hidden',
			position: 'absolute',
			padding: '0',
			top: '0'
		});
		if(!$(CLASS_TARGET_ELEMENTS).length == 0) this.elm.appendTo('body');
		this.lastHeight = this.elm.height(); //.offsetHeight
	}
	
	fontSizeWatcher.prototype = {
		elm: null,
		lastHeight: 0,
		timer: null,
		test: function(){
			if( this.elm.height() !== this.lastHeight ){
				this.lastHeight = this.elm.height();
				this.elm.trigger("fontSizeChange");
			}
		},
		start: function(){
			var that = this;
			this.timer = setInterval( function(){that.test()}, 200 );
		},
		toString: function(){ return "fontSizeWatcher" }
	}

	/*  */
	$.fn.adjustHeight = function(){
		var targetColumn = this.find("> *");
		var targetNum = targetColumn.length; // Box
		
		/* 2 */
		if(targetNum < 2) {
			return;
		}
	
		var ROW_MAX_HEIGHT, HEADING_MAX_HEIGHT, ELEMENTS;
		
		targetColumn.each( function(i) {
			ROW_MAX_HEIGHT = 0;
			HEADING_MAX_HEIGHT = 0;
			
			/*  */
			for( var j=0; j<targetNum && i*targetNum+j < targetColumn.length; j++ ){
				ELEMENTS = $(CLASS_TARGET_FIGURE_ELEMENTS,targetColumn.get(i*targetNum+j));
				HEADING_MAX_HEIGHT = Math.max( ELEMENTS.css("height","auto").height(), HEADING_MAX_HEIGHT );
			}
			for( var j=0; j<targetNum && i*targetNum+j < targetColumn.length; j++ ){
				ELEMENTS = $(CLASS_TARGET_FIGURE_ELEMENTS,targetColumn.get(i*targetNum+j));
				ELEMENTS.height(HEADING_MAX_HEIGHT);
			}
			
			/* BOX */
			for( var j=0; j<targetNum && i*targetNum+j < targetColumn.length; j++ ){
				ELEMENTS = $(targetColumn.get(i*targetNum+j));
				ROW_MAX_HEIGHT = Math.max( ELEMENTS.css("height","auto").height(), ROW_MAX_HEIGHT );
			}
			for( var j=0; j<targetNum && i*targetNum+j < targetColumn.length; j++ ){
				ELEMENTS = $(targetColumn.get(i*targetNum+j));
				ELEMENTS.height(ROW_MAX_HEIGHT);
			}
		});
		return this;
	}

	/*  */
	$.fn.initAdjustHeight = function(){
		return this.each(function(){
			var that = $(this);
			$("#fontSizeWatcher").bind("fontSizeChange",function(){that.adjustHeight()});
			$(this).adjustHeight();
		});
	}
	
})(jQuery)

/* ------------------------------------------
  Rollover Setup
------------------------------------------ */

function RolloverImages(className, onSuffix, aSuffix) {
	this.init(className, onSuffix, aSuffix);
}

RolloverImages.prototype = {

	init: function (className, onSuffix, aSuffix) {
		if (!document.getElementById || !document.images || !className) return;

		// 
		this.targetClassName = className;
		this.onSuffix = onSuffix;
		this.aSuffix = aSuffix;
		//this.mdSuffix = mdSuffix;
		this.buttons = [];

		// 
		var _this = this;
		var imgs = [];
		var inputs = [];
		var rolloverObj = [];

		var selectors = ["img." + this.targetClassName, "input." + this.targetClassName];

		$(selectors.join(", ")).each(function (i) {
			var el = $(this);
			rolloverObj.push(this);
		});

		$.each(rolloverObj, function (i) {
			_this.registButton(this);
		});
		this.preloadImages();
	},


	registButton: function (el) {
		var _this = this;
		var btn = new Object();
		btn.src = el.src;
		btn.filetype = btn.src.substring(btn.src.lastIndexOf("."));
		btn.basename = btn.src.substring(0, btn.src.length - btn.filetype.length);
		btn.onsrc = btn.basename + this.onSuffix + btn.filetype;

		el.offsrc = btn.src;
		el.onsrc = btn.onsrc;
		el.lock = false;
		if (this.aSuffix) {
			btn.activesrc = btn.basename + this.aSuffix + btn.filetype;
			el.activesrc = btn.activesrc;
		}

		this.buttons.push(el);

		if (!this.aSuffix) {
			$(el).bind("mouseover", function () {
				_this.swapImage(el, "on");
			});
			$(el).bind("mouseout", function () {
				_this.swapImage(el, "off");
			});
			if (el.parentNode && el.parentNode.tagName == "A") {
				var p = el.parentNode;
				$(p).bind("focus", function () {
					_this.swapImage(el, "on");
				});
				$(p).bind("blur", function () {
					_this.swapImage(el, "off");
				});
			}
		} else {
			$(el).bind("mouseover", function () {
				if (!el.lock) _this.swapImage(el, "on");
			});
			$(el).bind("mouseout", function () {
				if (!el.lock) _this.swapImage(el, "off");
			});
			$(el).bind("mouseup", function () {
				$(_this.buttons).each(function (i) {
					_this.deactivate(this)
				});
				_this.activate(el);
			});

			if (el.parentNode && el.parentNode.tagName == "A") {
				var p = el.parentNode;
				$(p).bind("focus", function () {
					if (!el.lock) _this.swapImage(el, "on");
				});
				$(p).bind("blur", function () {
					if (!el.lock) _this.swapImage(el, "off");
				});

				$(p).bind("keypress", function (e) {
					if (jQuery.browser.msie || (!jQuery.browser.msie && e.keyCode == 13)) {
						//Event.stop(e);
						$(_this.buttons).each(function (i) {
							_this.deactivate(this)
						});
						_this.activate(el);
					}
				});
			}

		}
	},

	preloadImages: function () {
		var ret = [];
		for (var i = 0; i < this.buttons.length; i++) {
			(new Image()).src = this.buttons[i].onsrc;
			ret[ret.length] = this.buttons[i].onsrc;
			if (this.buttons[i].activesrc) {
				(new Image()).src = this.buttons[i].activesrc;
				ret[ret.length] = this.buttons[i].activesrc;
			}
		}
	},

	swapImage: function (obj, status) {
		if (!obj || !obj[status + "src"]) return;
		if (!obj.lock) obj.src = obj[status + "src"];
	},

	activate: function (el) {
		this.swapImage(el, "active");
		this.lock(el);
	},

	deactivate: function (el) {
		this.unlock(el);
		this.swapImage(el, "off");
	},

	lock: function (obj) {
		if (obj.lock == "undefined") return;
		if (!obj.lock) obj.lock = true;
	},

	unlock: function (obj) {
		if (obj.lock == "undefined") return;
		if (obj.lock) obj.lock = false;
	}

}
