
/*
変更点
- version 1.3.1 initAdjustHeightのinitOddとinitEvenのバグを修正。
- version 1.4.0 クラスにrolloverが付いている要素がcurrentもクラスに持っていると動作しないようにしました。

*/

var jqueryhelper = Object();

jqueryhelper.rolloverSuffix = '_on';

jqueryhelper.current = new Array();
jqueryhelper.currentSuffix = '_on';

$(document).ready(function(){

	jqueryhelper.userOS = checkOS();
	jqueryhelper.userBrowser = checkBroswer();
	
	$('body').addClass(jqueryhelper.userOS).addClass(jqueryhelper.userBrowser);
	
	initCurrent();
	initRollover();
	initRolloverGroup();
	initGoto();
	initFirstChild();
	initLastChild();
	initOdd();
	initEven();
	initAdjustHeight();
	initAdjustChildHeight();
	initPopupBtn();
});

function checkOS(){
	if(/win/i.test(navigator.userAgent)) return 'win';
	if(/mac/i.test(navigator.userAgent)) return 'mac';
	return 'other';
}

function checkBroswer(){
	if(/msie 6/i.test(navigator.userAgent)){ return 'ie6'; }
	if(/msie 7/i.test(navigator.userAgent)){ return 'ie7'; }
	if(/msie 8/i.test(navigator.userAgent)){ return 'ie8'; }
	if(/msie 9/i.test(navigator.userAgent)){ return 'ie9'; }
	if(/firefox\/2/i.test(navigator.userAgent)){ return 'ff2'; }
	if(/firefox\/3/i.test(navigator.userAgent)){ return 'ff3'; }
	if(/firefox\/4/i.test(navigator.userAgent)){ return 'ff4'; }
	if(/safari/i.test(navigator.userAgent)){
		if(/version\/2/i.test(navigator.userAgent)){ return 'safari2'; }
		if(/version\/3/i.test(navigator.userAgent)){ return 'safari3'; }
		if(/version\/4/i.test(navigator.userAgent)){ return 'safari4'; }
		if(/version\/5/i.test(navigator.userAgent)){ return 'safari5'; }
	}
	if(/chrome\/4/i.test(navigator.userAgent)){ return 'chrome4'; }
	if(/chrome\/5/i.test(navigator.userAgent)){ return 'chrome5'; }
	if(/chrome\/6/i.test(navigator.userAgent)){ return 'chrome6'; }
	if(/chrome\/7/i.test(navigator.userAgent)){ return 'chrome7'; }
	if(/opera/i.test(navigator.userAgent)){
	if(/version\/8/i.test(navigator.userAgent)){ return 'opera8'; }
		if(/version\/9/i.test(navigator.userAgent)){ return 'opera9'; }
		if(/version\/10/i.test(navigator.userAgent)){ return 'opera10'; }
	}
	return 'other';
}

function checkEngine(){
	
}

function initRollover(){
	/* オーバー時のclass名を指定 */
	$('.over').live('mouseover', function(){toggleRolloverImage($(this));}).live('mouseout', function(){toggleRolloverImage($(this));});
}

function initRolloverGroup(){
						/* オーバー時のclass名を指定 */
	$('.rolloverGroup > .over').unbind("mouseenter").unbind("mouseleave");
	$('.rolloverGroup').hover(function(){
										/* オーバー時のclass名を指定 */
		var items = $(this).children("[class*='over']");
		jQuery.each(items, function(){ toggleRolloverImage($(this)); });
	},function(){
										/* オーバー時のclass名を指定 */
		var items = $(this).children("[class*='over']");
		jQuery.each(items, function(){ toggleRolloverImage($(this)); });
	});
}

function toggleRolloverImage(btn){
	if(!btn.hasClass('current')){
		if(btn.get(0).tagName == 'IMG' || btn.attr('type') == 'image'){
			var src = btn.attr('src');
			if( src.indexOf(jqueryhelper.rolloverSuffix) == -1 ){
				btn.attr('src', src.substring(0,src.lastIndexOf('.')) + jqueryhelper.rolloverSuffix + src.substring(src.lastIndexOf('.'),src.length));
			} else {
				btn.attr('src', btn.attr('src').replace(jqueryhelper.rolloverSuffix, ''));
			}
		}
		else{
			var src = btn.css('background-image');
			if( src.indexOf(jqueryhelper.rolloverSuffix) == -1 ){
				btn.css('background-image',src.substring(0,src.lastIndexOf('.')) + jqueryhelper.rolloverSuffix + src.substring(src.lastIndexOf('.'),src.length));
			} else {
				btn.css('background-image', btn.css('background-image').replace(jqueryhelper.rolloverSuffix, ''));
			}
		}
	}
}

function initCurrent(){
	for(var i in jqueryhelper.current){
		var id = jqueryhelper.current[i];
		$(id).removeClass('rollover');
		if($(id).get(0).tagName == 'IMG'){
			var src = $(id).attr('src');
			var pos = src.lastIndexOf('.');
			$(id).attr('src', src.substring(0,src.lastIndexOf('.')) + jqueryhelper.currentSuffix + src.substring(src.lastIndexOf('.'),src.length));
			$(id).addClass('current');
		} else {
			var src = $(id).css('background-image');
			var pos = src.lastIndexOf('.');
			$(id).css('background-image',src.substring(0,src.lastIndexOf('.')) + jqueryhelper.currentSuffix + src.substring(src.lastIndexOf('.'),src.length));
			$(id).addClass('current');
		}
	}
}

function initGoto(){
	$("a[href^='#']").bind('click', function(event){
		if(event){ event.preventDefault(); }else if(window.event){ window.event.returnValue = false; }
		var targetId = $(this).attr('href');
		var pos = $(targetId).offset();
		var ty = Math.min(pos.top, ($(document).height() - $(window).height()));
		$('html,body').animate({ scrollTop: ty }, 500, 'swing');
	})
}

function initFirstChild(){
	var tmp = $("*[class*='setFirstChild']");
	if (tmp.length == 0) { return; };
	jQuery.each(tmp, function(){
		$(this).children(':first-child').addClass('firstChild');
	});
}

function initLastChild(){
	var tmp = $("*[class*='setLastChild']");
	if (tmp.length == 0) { return; };
	jQuery.each(tmp, function(){
		$(this).children(':last-child').addClass('lastChild');
	});
}

function initOdd(){
	jQuery.each($("*[class*='setOdd']"), function(){
		$(this).children(':odd').addClass('odd');
	});
}

function initEven(){
	jQuery.each($("*[class*='setEven']"), function(){
		$(this).children(':even').addClass('even');
	});
}

var groupArray = {};
function initAdjustHeight()
{
	var	tmp = $("[class*='adjustHeight']");
	if (tmp.length == 0) { return; };
	jQuery.each(tmp, function() {
		var classes = $(this).attr("class").split(" ");
		var cont = $(this);
		jQuery.each(classes, function() {
			if (this.substr(0, 12) == 'adjustHeight') {
				var	no = this.substr(13,this.length-13);
				if(!groupArray[String(no)])
					groupArray[String(no)] = new Array();
				groupArray[String(no)].push(cont);
			};
		});
	});
	adjustHeight();
}

function adjustHeight(){
	jQuery.each(groupArray, function() {
		var highestHeight;
		jQuery.each($(this), function() {
			if(!highestHeight || $(this).height() > highestHeight)
				highestHeight = $(this).height();
		});
		jQuery.each($(this), function() {
			$(this).height(highestHeight);
		});
	});
}

function initAdjustChildHeight()
{
	var tmp = $('.adjustChildHeight');
	if (tmp.length == 0) { return; };
	jQuery.each(tmp, function() {
		var highestHeight;
		var childArray = new Array();
		jQuery.each($(this).children(), function() {
			if(!highestHeight || $(this).height() > highestHeight)
				highestHeight = $(this).height();
			childArray.push($(this));
		});
		jQuery.each(childArray, function() {
			$(this).height(highestHeight);
		});
	});
}

function initPopupBtn()
{
	var tmp = $("a[class*='popup']");
	if (tmp.length == 0) { return; };
	jQuery.each(tmp, function() {
		$(this).bind("click", doPopup);
		$(this).removeAttr("target");
	});
}

function doPopup(event)
{
	if(event){
		event.preventDefault();
	}else if(window.event){
		window.event.returnValue = false;
	}
	
	var btnClasses = $(this).attr("class").split(" ");
	var size;
	var sizeArray;
	jQuery.each(btnClasses, function() {
		if(this.substr(0,5) == 'popup'){
			size = this.substr(6,this.length-6);
			sizeArray = size.split("_");
			return false;
		}
	});
	
	var wObj;
	wUrl = $(this).attr('href');
	wWidth = sizeArray[0];
	wHeight = sizeArray[1];
	scWidthCenter = screen.availWidth / 2;
	scHeightCenter = screen.availHeight / 2;
	wOption = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + wWidth + ",height=" + wHeight + ",left=" + (scWidthCenter - (wWidth / 2)) + ",top=" + (scHeightCenter - (wHeight / 2));
	wObj = window.open(wUrl,null,wOption);
	wObj.focus();
}

function preloadImage(imgs){
	if(typeof imgs == 'array'){
		jQuery.each(imgs, function(){
			jQuery("<img>").attr("src", this);
		});
	}
	else if(typeof imgs == 'string'){
		jQuery("<img>").attr("src", imgs);
	};
}

