// ============================================================ 
// jQuery と prototype コンフリクト対策
// ============================================================ 
jQuery.noConflict();
var $j = jQuery;

// ============================================================ 
// ロールオーバー
// ============================================================ 
var rollover = {
	swap:function(){
		$j("img.swap").each(function(i) {
			var imgsrc = this.src;
			var dot = imgsrc.lastIndexOf(".");
			var imgsrc_on = imgsrc.substr(0, dot) + '_on' + imgsrc.substr(dot, 4);
			$j("<img>").attr("src", imgsrc_on);
			
			if(!imgsrc.match("_on")) {
				$j(this).hover(
					function() { this.src = imgsrc_on; },
					function() { this.src = imgsrc; }
				);
			}
		});
	},
	
	bright:function(){
		$j("img.bright").mouseover(function(){
			$j(this).css({opacity: 0.25}).fadeTo(300, 1);
		});
	},
	
	alpha:function(){
		$j("img.alpha").hover(
			function() { $j(this).stop().fadeTo(250, 0.6); },
			function() { $j(this).stop().fadeTo(250, 1); }
		);
	}
}

$j(function() {
	rollover.swap();
	rollover.bright();
	rollover.alpha();
});


// ============================================================ 
// Droppy 0.1.2
// ============================================================ 

$j(function() {
	$j('#gnavi-inner').droppy({speed: 150});
	
	// 最後のli要素に"no-border"追加
	var gn1_len = $j("ul#gnavi-inner li ul#id1 li").length;
	$j("ul#gnavi-inner li ul#id1 li:eq("+(gn1_len-1)+")").addClass("no-border");
	var gn2_len = $j("ul#gnavi-inner li ul#id2 li").length;
	$j("ul#gnavi-inner li ul#id2 li:eq("+(gn2_len-1)+")").addClass("no-border");
	var gn3_len = $j("ul#gnavi-inner li ul#id3 li").length;
	$j("ul#gnavi-inner li ul#id3 li:eq("+(gn3_len-1)+")").addClass("no-border");
	var gn4_len = $j("ul#gnavi-inner li ul#id4 li").length;
	$j("ul#gnavi-inner li ul#id4 li:eq("+(gn4_len-1)+")").addClass("no-border");
	
});

/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$j.fn.droppy = function(options) {
		
	options = $j.extend({speed: 250}, options || {});
	
	this.each(function() {
		
		var root = this, zIndex = 1000;
		
		function getSubnav(ele) {
			if (ele.nodeName.toLowerCase() == 'li') {
				var subnav = $j('> ul', ele);
				return subnav.length ? subnav[0] : null;
			} else {
				return ele;
			}
		}
		
		function getActuator(ele) {
			if (ele.nodeName.toLowerCase() == 'ul') {
				return $j(ele).parents('li')[0];
			} else {
				return ele;
			}
		}
		
		
		function hide() {
			clearTimeout(dTimer);
			
			var subnav = getSubnav(this);
			if (!subnav) return;
			$j.data(subnav, 'cancelHide', false);
			setTimeout(function() {
				if (!$j.data(subnav, 'cancelHide')) {
					$j(subnav).slideUp(options.speed);
				}
			}, 100);
		}
	
		var dTimer; // ロールオーバーの遅延
		function show() {
			var subnav = getSubnav(this);
			if (this.nodeName.toLowerCase() == 'ul') {
				var li = getActuator(this);
				$j(li).addClass('hover');
				$j('> a', li).addClass('hover');
			}
			dTimer = setTimeout(function(){
				if (!subnav) return;
				$j.data(subnav, 'cancelHide', true);
				$j(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
			}, 200);
		}
		
		$j('ul, li', this).hover(show, hide);
		$j('li', this).hover(
			function() { $j(this).addClass('hover'); $j('> a', this).addClass('hover'); },
			function() { $j(this).removeClass('hover'); $j('> a', this).removeClass('hover'); }
		);
	});
};


// ============================================================ 
// スムーズスクロール
// ============================================================ 
$j(function () {
	if (! $j.browser.safari) {
		$j('#pagetop').click(function () {
			$j(this).blur();

			$j('html,body').animate({ scrollTop: 0 }, 'slow');
	
			return false;
		});
	}
});

$j(function() {
		
	$j('.pagetop').click(function(){
		if($j.browser.safari && $j.browser.version.charAt(0)<5) {
			location.href = "#header";
		} else {
			$j(this).blur();
			$j('html,body').animate({scrollTop: 0}, 'slow');
			return false;
		}
	});
});

function pageScroll(id) {
	if($j.browser.safari && $j.browser.version.charAt(0)<5) {
		location.href = id;
	} else {
		var targetOffset = $j(id).offset().top;
		$j('html,body').animate({ scrollTop: targetOffset }, 'slow');
	}
}

