(function($){
	$.fn.imageRollover = function(options) {
		var defaults = {
			off : '_off',
			on  : '_on'
		};
		var settings = $.extend(defaults, options);
		this.each(function() {
			if ( !$(this).attr('src') || $(this).attr('src').lastIndexOf(settings.off) == -1 ) return;
			$('<img />').attr('src', $(this).attr('src').replace(settings.off, settings.on));
			$(this).hover(function() {
				$(this).attr('src',$(this).attr('src').replace(settings.off, settings.on));
			},function() {
				$(this).attr('src',$(this).attr('src').replace(settings.on, settings.off));
			});
		});
		return this;
	};
})(jQuery);

(function($){
	$.fn.pageScroller = function(options) {
		var defaults = {
			duration   : 795,
			easing     : 'easeOutExpo',
			scrollLeft : false
		},
			html = document.documentElement,
			body = document.body,
			settings = $.extend(defaults, options);
		this.each(function() {
			if (!$(this).attr('href') || $(this).attr('href').indexOf('/') != -1 || $(this).attr('href').indexOf('#') == -1) return;
			$(this).click(function(e){
				e.preventDefault();
				var target       = $($(this).attr('href')),
					targetY      = target.offset().top || 0,
					targetX      = target.offset().left || 0,
					windowHeight = html.clientHeight || body.clientHeight,
					windowWidth  = html.clientWidth || body.clientWidth,
					pageHeight   = html.scrollHeight || body.scrollHeight,
					pageWidth    = html.scrollWidth || body.scrollWidth;
				if (document.compatMode == 'BackCompat') {
					pageHeight = body.scrollHeight, pageWidth = body.scrollWidth;
				};
				if (windowHeight + targetY > pageHeight) targetY -= (windowHeight + targetY) - pageHeight;
				if (windowWidth + targetX > pageWidth) targetX -= (windowWidth + targetX) - pageWidth;
				if (! settings.scrollLeft ) {
					$('html,body').animate({
						scrollTop: targetY
					}, settings.duration, settings.easing);
				} else {
					$('html,body').animate({
						scrollTop: targetY,
						scrollLeft: targetX
					}, settings.duration, settings.easing);
				};
			});
		});
		return this;
	};
})(jQuery);

$(function(){
	$('img, input:image').imageRollover();
	$('a').pageScroller();

	/* lacalNav addClass 'last-child' for IE */
	if($('#localNav').size() && !jQuery.support.opacity){
		$('#localNav ul').each(function(){
			$(this).find('li:last').addClass('last-child');
		});
	}

	/* .news dd addClass 'last-child' for IE */
	if($('.content .news').size() && !jQuery.support.opacity){
		$('.content .news dl').each(function(){
			$(this).find('dd:last').addClass('last-child');
		});
	}

	/* #worksIndex .list addClass 'last-child' for IE */
	if($('#worksIndex').size() && !jQuery.support.opacity){
		$('#worksIndex .inner > div').each(function(){
			$(this).find('.list:last').addClass('last-child');
		});
	}

	if($("#home").size()){
		var visualCcontainer = $('#visual ul');
		visualCcontainer.cycle({
			fx: 'fade', 
			speed: 2500,
			timeout: 5000,
			before: function(){
			}
		});
	}

	if($('#homeNews').size()){
		var init = function(){
			$('#homeNews li').each(function(){
				$(this).find('img').unbind();
				$(this).find('a').click(function(e){ e.preventDefault() });
			});
			var btn = $('#homeNews li').eq(0).addClass('on');
			var img = btn.find('img');
			img.attr('src',img.attr('src').replace('_off', '_on'));
			$('#homeNews .tabs').eq(1).hide();
		}
		init();

		$('#homeNews li').click(function(){
			if(!$(this).hasClass('on')){
				$('#homeNews .tabs').hide().eq($(this).index()).show();
				$('#homeNews li').removeClass('on');
				$(this).addClass('on');
				$('#homeNews li').each(function(){
					var img = $(this).find('img');
					img.attr('src',img.attr('src').replace('_on', '_off'));
				});
				$(this).find('img').attr('src',$(this).find('img').attr('src').replace('_off', '_on'));
			}
		});
	}

});
