﻿/**
 * common.js
 * 
 * Copyright (c) 2008-2011 Shunji Tabata. All Rights Reserved.
 * (http://gelsol.sub.jp)
 */

$(function(){
	$("body").append("<div id='pagetop'><a href='javascript:void(0)' title='ページの先頭へ移動します'>page top</a></div>");

	$("#pagetop").click(function(){
		$('html,body').animate({ scrollTop: 0 }, 800);
		return false;
	})

	var cssObj = {
		top: $(window).height()-50,
		left: $(window).width()-70
	}
	$("#pagetop").css(cssObj);

	$(window).resize(function(){
		var cssPos = {
			top: $(window).height()-50,
			left: $(window).width()-70
		}
		$("#pagetop").css(cssPos);
	});

	// for IE6
	if($.browser.msie && $.browser.version<7){
		$(window).scroll(function(){
		 $("#pagetop").get(0).style.setExpression("top","($(document).scrollTop()+$(window).height()-50)+'px'");
		})
	}

	var currentTallest = 0,
		currentRowStart = 0,
		rowDivs = new Array(),
		$el,
		topPosition = 0;
	$('#container > div').each(function() {
		$el = $(this);
		topPostion = $el.position().top;
		if (currentRowStart != topPostion) {
			for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
				rowDivs[currentDiv].height(currentTallest);
			}

			rowDivs.length = 0; // empty the array
			currentRowStart = topPostion;
			currentTallest = $el.height();
			rowDivs.push($el);
		} else {
			rowDivs.push($el);
			currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
		}

		for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
			rowDivs[currentDiv].height(currentTallest);
		}
	});
})

