/*============================================================================ 
jquery.function.js
Copyright (C) C-brains Corporation All rights reserved.
============================================================================ */


/**********************************************************
setClassBtn
--------
指定の要素のclass属性に"btn"を設定
@param / @return
**********************************************************/
function setClassBtn() {
	$("ul#nav li img").each(function() {
		$(this).addClass("btn");
	});
}



/**********************************************************
initRollOverImages
--------
マウスオーバーで画像を切換
@param / @return
**********************************************************/
function initRollOverImages() {
	var image_cache = new Object();
	$("a img.btn,input[type=image].btn").not("[src*='_o.'],[src*='_d.']").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_o' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).hover(
			function() { this.src = imgsrc_on; },
			function() { this.src = imgsrc; }
		);
	});
}



/**********************************************************
pageScroll
--------
ページをスムーズにスクロール（ href属性値が#で始まるものが対象 ）
@param / @return
**********************************************************/
function pageScroll() {
	$("a[href*=#]").click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});
}



/**********************************************************
initPlaceholder
--------
プレースホルダ初期化
@param / @return
**********************************************************/
$.fn.placeholder = function(){
if(!$('.placeholder').length) return;
$('.placeholder').each(function ()
{
    (function ( elm )
    {
        // placeholder時の文字色を設定
        var defaultColor    = 'silver';
        
        // title属性をplaceholder用の文字列として保持
        $.data(elm, 'placeholder-string', $(elm).attr('title'));
        // 元の文字色を保持 => 入力があったら戻します
        $.data(elm, 'placeholder-color', $(elm).css('color'));
        switch ( $(elm).val() ) {
            case '' :
                $(elm).val($.data(elm, 'placeholder-string'));
            case $.data(elm, 'placeholder-string') :
                $(elm).css('color', defaultColor);
            break;
        }
        // フォーカスされたときに、placeholderを消して色を戻す
        $(elm).focus(function ( )
        {
            if ( $(this).val() == $.data(this, 'placeholder-string') ) {
                $(this).val('');
                $(this).css('color', $.data(this, 'placeholder-color'));
            }
        });
        // フォーカスが外れたときに、valueがカラならplaceholderを代入
        $(elm).blur(function ( )
        {
            if ( $(this).val() == '' ) {
                $(this).val($.data(this, 'placeholder-string'));
                $(this).css('color', defaultColor);
            }
        });
        // フォームのsubmit時に、placeholderのままなら消してから送る
        $(elm).parents().filter('form').submit(function ( )
        {
            if ( $(elm).val() == $.data(elm, 'placeholder-string') ) {
                $(elm).val('');
            }
            return true;
        });
    })( this )
});
}

function initPlaceholder() {
	$(document).placeholder();
}



/**********************************************************
画像スライド
**********************************************************/

function setSlide(){
$(function(){



/*==================
設定ここから
==================*/

//タイマー秒数（ミリ秒）
var time = 8000;
//現在の写真
var current = 1;
//写真の総数
var photoLength = 9;
//リンク先
var link = [];
link[0] = "/products/04/index.html";
link[1] = "/products/04/testo_thermo_SuperResolution.html";
link[2] = "/products/05/testo480.html";
link[3] = "/products/01/testo350.html";
link[4] = "/products/01/testo340.html";
link[5] = "/products/09/index.html";
link[6] = "/products/09/testoSaveris.html";
link[7] = "/products/08/index.html";
link[8] = "/products/10/testo270.html";

$("#mainImg>p>a").attr({ href : link[0] });

/*==================
設定ここまで
==================*/



//写真入れ替え
function changeImg(e,degree){
		var t = e;
		var img = $("#mainImg>p>a");
		//クリック中のイベント停止
		t.unbind('click');
		for(i=0;i<img.length;i++){
			if(i!=0){
				$(img[i]).remove();
			}
		}
		if(degree=="next"){
			current++;
		}else{
			current--;
		}
		if(current>photoLength){
			current = 1;
		}else if(current<1){
			current = photoLength;
		}
		
		$("#mainImg p a").before("<a href='"+ link[current-1] +"'><img src='home/img/main_"+ current +".jpg' alt='' /></a>");
		$("#mainImg p a img:last").fadeOut("slow",function(){
			$(this).parent().remove();
			t.bind('click',function(){ changeImg(t,degree)});

		});
		return false;
}
var timerID = setInterval(function(){ changeImg($("li.next"),'next') } ,time);
$("li.next").click(function(){
		changeImg($(this),'next');
});
$("li.prev").click(function(){
		changeImg($(this),'prev');
});
});
}



/**********************************************************
連絡先切り替え
**********************************************************/
function otherContact(){
	$(function(){
	
	$('div#testoWorldwide dl').each(function(){
				$(this).css({display : "none"});
	})
	$("#other_0").css({display : "block"});
	$("div#otherCountry a img#ok").click(function(){
			var num = $("#countries").val();
			if(!num) return;
			$('div#testoWorldwide dl').each(function(){
				$(this).css({display : "none"});
			})
			
			$("#other_"+num).css({display : "block"});
		});
	});
}


/**********************************************************
実行処理
**********************************************************/
$(document).ready(setClassBtn);
$(document).ready(initRollOverImages);
$(document).ready(pageScroll);
$(document).ready(setSlide);
$(document).ready(otherContact);
$(document).ready(initPlaceholder);


