/*
-------------------------------------------------------------------------------------------------
	[common.js] JavaScript
-------------------------------------------------------------------------------------------------
	copyright          : Lampros co.,Ltd.
	author             : Atsushi Umehara
	making day         : 2010-06-23
-------------------------------------------------------------------------------------------------
	note
	2011-04-06 : close windowを追加
-------------------------------------------------------------------------------------------------*/
var overChar = '_f2';
var btnClass = 'btn';

jQuery( function( $ ) {

	// add class
	$( 'html' ).css( 'overflow-y', 'scroll' );
	$( 'body' ).addClass( $.browser.className );

	// pngfix
	$( 'img' ).pngfix();
	$( '.iepngfix' ).pngfix();

	// current hilite main
	$( '#mainMenu' ).currentHiliteMain( { overChar:overChar} );

	// current hilite sub
	$( '.subHilite' ).currentHiliteSub( { overChar:overChar} );

	// rolloveer
	$( "img." + btnClass + ", input." + btnClass, 'body' ).each( function(){
		setOverImg( $( this ) );
	} );

	//odd even
	$( 'ul, ol', 'body' ).each( function(){
		var thisDOM = $( this );
		thisDOM.find( 'li:first' ).addClass( 'first' );
		thisDOM.find( 'li:odd' ).addClass( 'odd' );
		thisDOM.find( 'li:even' ).addClass( 'even' );
		thisDOM.find( 'li:last' ).addClass( 'last' );
	} );
	$( 'table', 'body' ).each( function(){
		var thisDOM = $( this );
		thisDOM.find( 'tr:first' ).addClass( 'first' );
		thisDOM.find( 'tr:odd' ).addClass( 'odd' );
		thisDOM.find( 'tr:even' ).addClass( 'even' );
		thisDOM.find( 'tr:last' ).addClass( 'last' );
	} );

	//photoGallery
	$( '.photoGallery' ).each( function(){
		$( this ).photoGallery();
	} );
	
	//photoSlideGallery
	$( '.photoSlideGallery' ).each( function(){
		$( this ).photoSlideGallery();
	} );
		

	// #で始まるアンカーをクリックした場合に処理
	$('a[href^=#]').click(function() {
		var speed = 400;
		var href= $(this).attr("href");
		var target = $(href == "#" || href == "" ? 'html' : href);
		var position = target.offset().top;
		$($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
		return false;
	});
	
	//非表示設定　スタッフ紹介
	$( '#articleCase' ).each( function(){
		if( $( this ).find("ul").find("li:eq(0)").html() == null ) $( this ).hide();
	} );
	
	//新着2週間アイコン表示
	var currentDate = new Date();
	var timeA = currentDate.getTime();
	$( "#sec1 li" ).each( function(){
		var d = $( ".ld", this ).html();
		var spDate = d.split(".");
		var limitDate = new Date( spDate[0], eval ( spDate[1] )-1, eval ( spDate[2] )+15, 00, 00, 0 );
		var timeB = limitDate.getTime();
		var newIcon = '&nbsp;';
		if( timeA < timeB ) newIcon = '<img src="/_img/index/icon_new.jpg" alt="NEW" width="29" height="14" />';
		$( ".lm", this ).html( newIcon );
	});
	
	$( ".dck" ).each( function(){
		var d = $( this ).attr( "title" );
		var spDate = d.split(".");
		var limitDate = new Date( spDate[0], eval ( spDate[1] )-1, eval ( spDate[2] )+15, 00, 00, 0 );
		var timeB = limitDate.getTime();
		var newIcon = '&nbsp;';
		if( timeA < timeB ) newIcon = '<img src="/_img/index/icon_new.jpg" alt="NEW" width="29" height="14" /> ';
		$( ".txt", this ).prepend( newIcon );
	});
	


} );

// photoSlideGallery
( function( $ ){
	$.fn.photoSlideGallery = function( options ) {
		var defaults = {
			'overChar' : '_f2',
			'viewAreaNum' : 3,
			'photoW' : 190,
			'photoId' : 0,
			'_photoId' : 0,
			'changeSpeed' : 1500,
			'changeTime' : 5000
		};
		var setting = $.extend( defaults, options );

		var photo_n = $( 'li', this ).length;

		if( photo_n > setting.viewAreaNum ){

			var photoDOM = $( '.viewArea ul' );
			var myInterval = setInterval( scrollPhotoRotation, setting.changeTime );

			$( ".next a" ).click( function(){
				photoDOM.stop();
				if ( myInterval ) clearInterval( myInterval );
				scrollPhotoRotation();
				myInterval = setInterval( scrollPhotoRotation, setting.changeTime );
				return false;
			});
			$( ".pre a" ).click( function(){
				photoDOM.stop();
				if ( myInterval ) clearInterval( myInterval );
				scrollPhotoRotationPrev();
				myInterval = setInterval( scrollPhotoRotation, setting.changeTime );
				return false;
			});

		} else {
			$( ".pre", this ).hide();
			$( ".next", this ).hide();
		}

		function scrollPhotoRotation(){
			setting.photoId++;
			if( setting.photoId > photo_n - setting.viewAreaNum ) setting.photoId = 0;
			scrollPhotoRun( setting.photoId );
		}

		function scrollPhotoRotationPrev(){
			setting.photoId--;
			if( setting.photoId > photo_n ) setting.photoId = 0;
			if( setting.photoId < 0 ) setting.photoId = photo_n - setting.viewAreaNum;
			scrollPhotoRun( setting.photoId );
		}

		function scrollPhotoRun( viewId ){
			setting._photoId = setting.photoId;
			photoDOM.animate( { left: -( viewId * setting.photoW ) }, setting.changeSpeed );
			setting.photoId = viewId;
		}

		return this;
	};
} )( jQuery );



// photoGallery
( function( $ ){
	$.fn.photoGallery = function( options ) {
		var defaults = {
			'overChar' : '_f2',
			'changeTime' : 5000
		};
		var setting = $.extend( defaults, options );
		
		var viewArea = $( '.viewArea', this );
		var viewList = $( '.viewlist', this );
		var photo_n = $( 'li', viewList ).length;
		var pid = "0";
		var nowViewId = "0";
		
		viewArea.html("");
		
		$( 'li', viewList ).each( function(){
			var photoDom = $( "<img src=\"" + $( "a", this ).attr( 'href' ) + "\"/>" );
			viewArea.append( photoDom );
			if( pid != 0 ) photoDom.hide();
			if( pid == 0 ) $( this ).addClass("view");
			var imgDom = $( "img", this );
			var srcDom = imgDom.attr( "src" );
			$( this ).click( function () {
				changePhotoCk( $( "li", viewList ).index( this ) );
				return false;
			});
			pid++;
		} );
		
		var changeInterval = setInterval( changePhoto, setting.changeTime );

		function changePhoto(){
			$('img', viewArea ).eq( nowViewId ).hide();
			$('li', viewList ).eq( nowViewId ).removeClass("view");
			nowViewId++;
			if( nowViewId > photo_n - 1 ) nowViewId = 0;
			$('img', viewArea ).eq( nowViewId ).fadeIn( 1000 );
			$('li', viewList ).eq( nowViewId ).addClass("view");
		}

		function changePhotoCk( chId ){
			clearInterval( changeInterval );
			$('img', viewArea ).eq( nowViewId ).hide();
			$('li', viewList ).eq( nowViewId ).removeClass("view");
			$('img', viewArea ).eq( chId ).fadeIn( 1000 );
			$('li', viewList ).eq( chId ).addClass("view");
			nowViewId = chId;
			changeInterval = setInterval( changePhoto, setting.changeTime );
		}
		
		return this;
	};
} )( jQuery );


// set over img
function setOverImg( thisDOM ){
	var imgout = thisDOM.attr( "src" );
	var imgovr = imgout.replace( '.gif', overChar + '.gif' ).replace( '.jpg', overChar + '.jpg' ).replace( '.png', overChar + '.png' );
	thisDOM.hover( function(){
		thisDOM.attr( "src", imgovr );
	}, function(){
		thisDOM.attr( "src", imgout );
	} );
}

//close window
function closeWin(){
	var nvua = navigator.userAgent;
	if( nvua.indexOf( 'MSIE' ) >= 0 ){
		if( nvua.indexOf( 'MSIE 5.0' ) == -1 ) { top.opener = ''; }
	}else if( nvua.indexOf( 'Gecko' ) >= 0 ){
		top.name = 'CLOSE_WINDOW';
		wid = window.open( '','CLOSE_WINDOW' );
	}
	top.close();
}

// current hilite Main
( function( $ ){
    $.fn.currentHiliteMain = function( options ) {
        var defaults = {
            'overChar' : '_f2',
            'debugUrl' : null
        };
        var setting = $.extend( defaults, options );
				
				var rootLevel = 3;
				var path = document.URL;
				var pathArr = path.split( "/" );
				if( setting.debugUrl != null ){
					var debugUrlArr = setting.debugUrl.split( "/" );
					rootLevel = debugUrlArr.length - 1;
				}
				var currentName = pathArr[rootLevel];
				if( currentName != "order" && currentName != "skipfloor" && currentName != "used" && currentName != "reform" ) currentName = "index";
				if( currentName == ""  ) currentName = "index";
				$( ' a[class*="' + currentName + '"] > img', this ).each( function(){
					var thisDOM = $( this );
					var imgout = thisDOM.attr( "src" );
					var imgovr = imgout.replace( '.jpg', setting.overChar + '.jpg' );
					thisDOM.attr( "src", imgovr );
					thisDOM.removeClass( btnClass );
					return false;
				} );
	
        return this;
    };
} )( jQuery );

// current hilite Sub
( function( $ ){
    $.fn.currentHiliteSub = function( options ) {
        var defaults = {
            'overChar' : '_f2',
            'debugUrl' : null
        };
        var setting = $.extend( defaults, options );
				
				var rootLevel = 3;
				var path = document.URL;
				var pathArr = path.split( "/" );
				if( setting.debugUrl != null ){
					var debugUrlArr = setting.debugUrl.split( "/" );
					rootLevel = debugUrlArr.length - 1;
				}
				var currentName = pathArr[rootLevel];
				if( currentName == "order" || currentName == "skipfloor" || currentName == "used" || currentName == "reform" ) currentName = pathArr[rootLevel + 1];
				if( currentName.indexOf( "." ) != -1 ){
					var currentName = currentName.substring( 0, currentName.lastIndexOf( "." ) );
				}
				if( currentName == "" ) currentName = "index";
				$( ' a[class*="' + currentName + '"] > img', this ).each( function(){
					var thisDOM = $( this );
					var imgout = thisDOM.attr( "src" );
					var imgovr = imgout.replace( '.jpg', setting.overChar + '.jpg' );
					thisDOM.attr( "src", imgovr );
					thisDOM.removeClass( btnClass );
				} );
	
        return this;
    };
} )( jQuery );

// window center
function windowResizeCenter( target, targetW, initX ){
	windowResizeCenterRun( target, targetW, initX );
	$(window).resize(function(){
		windowResizeCenterRun( target, targetW, initX );
	});
}
function windowResizeCenterRun( target, targetW, initX ){
	var winW = $(window).width();
	var mvX = ( winW - targetW ) / 2;
	if( mvX <= initX ) mvX = initX;
	target.css("left", mvX + "px" );
}

	

