/*=========================================================


	Site Name   www.forumeng.co.jp
	File Name   common.js

	Create Date 2010/06/7
	Update Date 2010/06/7


==========================================================*/

/*----------------------------------------------------------

	1. Common Function (require jquery.js)

-----------------------------------------------------------*/

(function($){

	$(function(){
		$.commonFunc.winopen();
		$.commonFunc.rollover();
		$.commonFunc.fadeimg();
		$.commonFunc.externallink();
		$.commonFunc.smoothscroll();
	});

	$.commonFunc = {

		//new window
		winopen : function(options) {
			var c = $.extend({
				selector:'.winopen'
			}, options);
			$(c.selector).live('click',function(){
				var url = this.href;
				var baseurl = url.substr(0, url.indexOf("?"));
				var query = url.replace(/^[^\?]+\??/,'');
				var params = $.commonFunc.parseQuery(query);

				var baseurl = baseurl;
				var name = params['name'];
				var width = params['width'];
				var height = params['height'];
				var resize = params['resize'];

				var x = (screen.availWidth - width) / 2;
				var y = (screen.availHeight - height) / 2;

				newwin = window.open(baseurl, name, "left=" + x + ", top="+ y +", width="+ width +", height="+ height +", scrollbars="+ resize +", resizable="+ resize);
				return false;
			});
		},

		//new window(full flash)
		winopenfull : function(options) {
			var c = $.extend({
				selector:'.winopenfull'
			}, options);
			$(c.selector).live('click',function(){
				var baseurl = this.href;
				var w = screen.availWidth;
				var h = screen.availHeight;

				window.open(baseurl, "_blank", "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=no, resizable=no, left=0, top=0, width="+ w +", height="+ h);
				return false;
			});
		},

		//rollover
		rollover : function(options){
			var c = $.extend({
				selector:'.btn',
				postfix: '_o'
			}, options);
			var image_cache = new Object();
			$(c.selector).each(function(i){
				var imgsrc = this.src;
				var dot = this.src.lastIndexOf(".");
				var imgsrc_on = this.src.substr(0, dot) + c.postfix + this.src.substr(dot, 4);
				image_cache[this.src] = new Image();
				image_cache[this.src].src = imgsrc_on;
				$(this).hover(
					function(){ this.src = imgsrc_on;},
					function(){ this.src = imgsrc;}
				);
			});
		},

		//fade
		fadeimg : function(options){
			var c = $.extend({
				selector:'.fade',
				active:'.active',
				opacity:'0.3'
			}, options);
			$(c.active).fadeTo(0, c.opacity);
			$(c.selector).live('mouseover',
				function(){$(this).dequeue().fadeTo(300, c.opacity);}
			);
			$(c.selector).live('mouseout',
				function(){$(this).dequeue().fadeTo(300, 1);}
			);
		},

		//external link
		externallink : function(options){
			var c = $.extend({
				selector:'.externalLink'
			}, options);
			var popupEvent = function(event){
				window.open(this.href);
				event.preventDefault();
				event.stopPropagation();
			}
			$(c.selector).each(function(i) {
				$(this).live('click',function(){popupEvent});
				$(this).keypress(popupEvent);
			});
		},

		//smooth scroll
		smoothscroll : function(options){
			var c = $.extend({
				selector:'.pageLink',
				sclpos : 0,
				scldurat : 1000
			}, options);
			$(c.selector).live('click',function(){
				if (location.pathname.replace(/^\//,"") == this.pathname.replace(/^\//,"") && location.hostname == this.hostname) {
					var target = $(this.hash);
					target = target.length && target;
					if (target.length) {
						var targetOffset = target.offset().top - c.sclpos;
						$("html,body")
							.animate({scrollTop:targetOffset},{duration:c.scldurat,easing:"easeOutExpo"});
						return false;
					}
				}
			});
		},

		parseQuery : function(query){
			var Params = {};
			if (!query){return Params;}
			var Pairs = query.split(/[;&]/);
			for(var i=0;i<Pairs.length;i++ ) {
				var KeyVal = Pairs[i].split("=");
				if(!KeyVal||KeyVal.length!=2){continue;}
				var key = unescape(KeyVal[0]);
				var val = unescape(KeyVal[1]);
				val = val.replace(/\+/g, ' ');
				Params[key] = val;
			}
			return Params;
		},

		getPageSize : function(){
			var de = document.documentElement;
			var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
			arrayPageSize = [w,h];
			return arrayPageSize;
		}

	}

})(jQuery);

/*----------------------------------------------------------

	2. Position Fixed (require jquery.js && jquery.fixed.js)

-----------------------------------------------------------*/
/*
$(function(){
	if($.browser.msie && $.browser.version < 7){
		$('#pagetop').exFixed();
	}
});
*/

/*----------------------------------------------------------

	3. BG Position (require jquery.js)

-----------------------------------------------------------*/

$(function(){
	var windowHeight = $.commonFunc.getPageSize()[1];
	var headerHeight = $('div#headerWrap').height() || $('#showcase').height();
	var contentsHeight = $('div#contents').height();
	var footerHeight = $('div#footer').height();
	
	if(windowHeight > headerHeight + contentsHeight) {
		var requiredHeight =  windowHeight - (headerHeight + contentsHeight);
		if($.browser.msie && $.browser.version < 8){
			$('div#footer').css({
				height : footerHeight + requiredHeight
			});
		}else{
			//IE BUG
			$('div#footer').css({
				height : footerHeight + requiredHeight - 10
			});
		}
	}
	/*
	$(window).resize(function(){
		var windowHeight = $.commonFunc.getPageSize()[1];
		var headerHeight = $('div#headerWrap').height() || $('#showcase').height();
		var contentsHeight = $('div#contents').height();
		var footerHeight = $('div#footer').height();
		
		if(windowHeight > headerHeight + contentsHeight) {
			var requiredHeight =  windowHeight - (headerHeight + contentsHeight);
			if($.browser.msie && $.browser.version < 8){
				$('div#footer').css({
					height : footerHeight + requiredHeight
				});
			}else{
				//IE BUG
				$('div#footer').css({
					height : footerHeight + requiredHeight - 10
				});
			}
		}else{
			$('div#footer').css({
				height : 'auto'
			});
		}
	});
	*/
});

/*----------------------------------------------------------

	4. irst-child last-child and Zebra Function (require jquery.js)

-----------------------------------------------------------*/
$(function(){
	$("body :first-child").addClass("first-child");
	$("body :last-child").addClass("last-child");
	$("body :only-child").removeClass("first-child").removeClass("last-child").addClass("only-child");
	$(".data tr:even").addClass("even");
});

/*----------------------------------------------------------

	5. wink

-----------------------------------------------------------*/
$(function(){
	$("a img.wink, input.wink").hover(function(){
		$(this).css("opacity", "0.2");
		$(this).css("filter", "alpha(opacity=20)");
		$(this).fadeTo("slow", 1.0);
	});
	
});

/*----------------------------------------------------------

	6. jquery.flatheights.js

-----------------------------------------------------------*/
$(function(){
    $('.box1').flatHeights();
});

window.onload = function (){
	var node_a = document.getElementsByTagName('a');
		for (var i in node_a) {
			if(node_a[i].className == 'popup'){
				node_a[i].onclick = function() {
					return winOpen(this.href, this.rel)
				};
			}
		}
} ;

function winOpen(url, rel) {
	var split = rel.split(',') ;
	window.open(
	url,'popup',
	'width='+ split[0] +',height='+ split[1] +',toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');

	return false;
};


