/**
 * @required jQuery 1.4.3 <http://jquery.com/>
 * @version 1.1.0
 * @author 
 */

var UI = window.UI || {};


UI.Util = {

	
	imgcache: {},
	
	init: function () {
		$('a[href*=#]').click(UI.Util.scrollTo);
		this.setSwapimage();
	},
	
	setSwapimage: function () {
		$('img.hover').each(function () {
            var imgsrc = this.src;
            var ext = imgsrc.lastIndexOf('.');
            var imgsrc_hover = imgsrc.substr(0, ext) + '_hover' + imgsrc.substr(ext, 4);
            var cache = UI.Util.imgcache;
            cache[imgsrc] = new Image();
            cache[imgsrc].src = imgsrc_hover;
            $(this).hover(
				function () { this.src = imgsrc_hover; },
				function () { this.src = imgsrc; }
			);
        });
	},
	
	scrollTo: function (e) {
		if (!$(this)[0].hash || $(this)[0].hash == "#") {
			return false;
		}
		return $(this).each(function () {
			var $target = $($(this)[0].hash).offset().top;
			$('html,body').animate({scrollTop: $target}, 1000, 'quart');
		});
	}
	

};


// Easing
$.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};



UI.Mailzine = {
	
	// Propaties
	form_url: 'http://tour.his-usa.com/tmp_top/magazine/index.php',


	init: function () {
		$('#subnavi_mailzine a').click(function () {
			UI.Mailzine.open();
			return false;
		});
	},
	
	
	open: function () {
		$('body').append('<div id="mask"></div>');
		$('#mask').css('opacity', 0.8);
		$('#mask').click(UI.Mailzine.close);
		
		$('body').append('<div id="mailzine"><iframe id="mailzine_frame" src="'+ this.form_url +'" frameborder="0"></iframe></div>');
		$(window).resize(UI.Mailzine._resizeWindow);
		UI.Mailzine._resizeWindow();
	},
	
	
	close: function () {
		$('#mailzine').remove();
		$('#mask').fadeOut('500', function () {
			$(this).remove();
		});
		
		return false;
	},
	
	
	_setForm: function () {
		$('#mailzine_form').submit(function () {
			if (confirm('入力された情報でメルマガを登録します。')) {
				UI.Mailzine._submitForm();
				return false;
			}
			else {
				return false;
			}
		});
	},
	
	
	_resizeWindow: function () {
		var w = $(window).width();
		var h = $(window).height();
		var container_left = Math.max((w / 2 - $('#mailzine').width() / 2), 5);
		var container_top = Math.max((h / 2 - $('#mailzine').height() / 2 - 30), 20);
		
		$('#mailzine').css({
			left: container_left,
			top: container_top
		});
		
		$('#mask').height(Math.max(h, $(document).height()));
	}
	
	
	
	


};


$(function () {
	UI.Util.init();
	UI.Mailzine.init();
});

