var isReloadContents	= true;
var isReloadDetail		= true;

$(document).ready(function() {
	$("#service_contents_box").hide();
	$("#service_alike_box").hide();
	$("#servicebox").hide();
	$("#similarbox_choice").hide();
	/*
	var img = $(".imgKind");
	for ( var i = 0; i < img.length; i++ ) {
		img[i].setAttribute( "title", "" );
		var strID = img[i].id;
		var aryID = strID.split("_");
		if ( aryID[2] == strKindID ) {
			img[i].setAttribute( "title", "SELECT" );
		}
	}
	$('.imgKind').jcaption({imageAttr: 'title'});
	*/
	$('.imgContents').jcaption({imageAttr: 'title'});
	ShowService();
	$("#service_contents_box").animate( { opacity:'show'},3000 );
	$("#servicebox").animate( { opacity:'show'},3000 );
	$(".imgContents").animate( { opacity:'show'},3000 );
	$("#search_linkbox").animate( { opacity:'show'},3000 );
	$(".imgBtn").animate( { opacity:'show'},3000 );
	$("#service_alike_box").animate( { opacity:'show'},5000 );
});

function preload(imgs){
	if ( imgs == undefined ) return false;
	for(var i = 0; i < imgs.length; i++){
		var imgObj = new Image();
		imgObj.src = imgs[i];
	}
}

function ShowService() {
	var strRet = $.ajax({
		url: "../price/_service_images.php",
		data: {
			"kid" : strKindID,
			"cid" : strContentsID,
			"did" : strDetailID
		},
		type     : "POST",
		dataType: "json",
		cache: true,
		async: true,
		scriptCharset: "UTF-8",
		success : function(_obj, _status){
			preload( _obj.thmb );
			preload( _obj.zengo );
		}
	});
	var strRet = $.ajax({
		url: "_service.php",
		data: {
			"kid" : strKindID,
			"cid" : strContentsID,
			"did" : strDetailID
		},
		type     : "POST",
		dataType: "json",
		cache: true,
		async: true,
		scriptCharset: "UTF-8",
		success : function(_obj, _status){
			if ( _obj == undefined ) return false;
			for ( var i = 0; i < _obj.contents.length; i++ ) {
				// パンくず
				if ( strContentsID == _obj.contents[i].id ) {
					var span = document.getElementById( "service_pan1" );
					if ( navigator.appName.indexOf("Microsoft") >= 0 ){
					} else {
						var spanHTML = span.innerHTML;
						spanHTML = spanHTML.replace("", "");
						DelChildDiv( "service_pan1" );
						var a = document.createElement( 'a' );
						a.innerHTML = spanHTML;
						a.href = '../service/?k=' + strKindID;
						a.id = "service_pan1_href";
						span.appendChild( a );
						var span = document.getElementById( "service_pan2" );
						DelChildDiv( "service_pan2" );
						span.innerHTML = "&nbsp;&nbsp;&gt;&nbsp;&nbsp;" + _obj.contents[i].name;
					}
				}
			}
			if ( isReloadContents ) {
				if ( _obj.contents == undefined ) return false;
				// contents
				var objContents = _obj.contents;
				// contents枠
				var div = document.getElementById( "service_contents_list" );
				DelChildDiv( "service_contents_list" );
				if ( _obj.contents.length < 1 ) {
					$("#selectBar").animate( { opacity:'show'},3000 );
					return false;
				}
				var ol = document.createElement( 'ol' );
				ol.id = "sliderContent";
				div.appendChild( ol );
				for ( var i = 0; i < objContents.length; i++ ) {
					// li
					var li = document.createElement( 'li' );
					if ( i == 0 ) {
						li.setAttribute( getClassStr(), "sleft" );
					} else {
						li.setAttribute( getClassStr(), "sright" );
					}
					ol.appendChild( li );
					// p
					var p = document.createElement( 'p' );
					li.appendChild( p );
					// image
					var img = document.createElement( 'img' );
					img.src = "../images/c" + strKindID + "_" + objContents[i].id + ".jpg";
					img.setAttribute( "width", "85px" );
					if ( navigator.appName.indexOf("Microsoft") >= 0 ){
						var nW = img.width;
						var nH = img.height;
						img.width = 85;
						img.height = nH * img.width / nW;
					}
					img.setAttribute( getClassStr(), "imgContents" );
					if ( strContentsID == objContents[i].id ) {
						img.setAttribute( "title", "SELECT" );
					}
					img.id = "img_c_" + objContents[i].id;
					// a
					var a = document.createElement( 'a' );
					a.href = 'javascript:SelectContents("' + objContents[i].id + '");';
					a.appendChild( img );
					p.appendChild( a );
					// p
					var p = document.createElement( 'p' );
					p.setAttribute( getClassStr(), "text10" );
					li.appendChild( p );
					// a
					var a = document.createElement( 'a' );
					a.href = 'javascript:SelectContents("' + objContents[i].id + '")';
					a.innerHTML = objContents[i].name;
					p.appendChild( a );
				}
				// SELECT
				$('.imgContents').jcaption({imageAttr: 'title'});
				
			}
			if ( isReloadDetail ) {
				if ( _obj.detail == undefined ) return false;
				// detail枠
				var div = document.getElementById( "service_detail_list" );
				DelChildDiv( "service_detail_list" );
				if ( _obj.detail.length < 1 ) {
					$("#selectBar").animate( { opacity:'show'},3000 );
					return false;
				}
				
				for ( var i = 0; i < _obj.detail.length; i++ ) {
					// h3
					var h3 = document.createElement( 'h3' );
					h3.setAttribute( getClassStr(), "text13" );
					h3.innerHTML = _obj.detail[i].kind + "　" + _obj.detail[i].contents + "　修理・お直しサービス";
					div.appendChild( h3 );
					
					var objDetail = _obj.detail[i].item;
					for ( var j = 0; j < objDetail.length; j++ ) {
						// h4
						var h4 = document.createElement( 'h4' );
						h4.setAttribute( getClassStr(), "text11" );
						h4.innerHTML = objDetail[j].name;
						div.appendChild( h4 );
						// div
						var divDetail = document.createElement( 'div' );
						divDetail.setAttribute( getClassStr(), "service_detail" );
						div.appendChild( divDetail );
						for ( var k = 0; k < objDetail[j].zengo.length; k++ ) {
							// p
							var p = document.createElement( 'p' );
							p.setAttribute( getClassStr(), "service_before" );
							divDetail.appendChild( p );
							// img
							var img = document.createElement( 'img' );
							var a = document.createElement( 'a' );
							a.href = "../service/?k=" + strKindID + "&c=" + strContentsID + "&d=" + objDetail[j].id;
							img.src = "../image.php?cd=" + objDetail[j].zengo[k].before_image_cd + "&w=130";
							img.setAttribute( "width", "130px" );
							//if ( navigator.appName.indexOf("Microsoft") >= 0 ){
							//	var nW = img.width;
							//	var nH = img.height;
							//	img.width = 130;
							//	img.height = nH * img.width / nW;
							//}
							a.appendChild( img );
							p.appendChild( a );
							// p
							var p = document.createElement( 'p' );
							p.setAttribute( getClassStr(), "service_after" );
							divDetail.appendChild( p );
							// img
							var img = document.createElement( 'img' );
							var a = document.createElement( 'a' );
							a.href = "../service/?k=" + strKindID + "&c=" + strContentsID + "&d=" + objDetail[j].id;
							img.src = "../image.php?cd=" + objDetail[j].zengo[k].after_image_cd + "&w=130";
							img.setAttribute( "width", "130px" );
							//if ( navigator.appName.indexOf("Microsoft") >= 0 ){
							//	var nW = img.width;
							//	var nH = img.height;
							//	img.width = 130;
							//	img.height = nH * img.width / nW;
							//}
							a.appendChild( img );
							p.appendChild( a );
						}
						// div
						var divCaption = document.createElement( 'div' );
						divCaption.setAttribute( getClassStr(), "service_caption" );
						divDetail.appendChild( divCaption );
						// p
						//var p = document.createElement( 'p' );
						//p.setAttribute( getClassStr(), "price_txt text11" );
						//p.innerHTML = objDetail[j].point;
						//divCaption.appendChild( p );
						// p
						var p = document.createElement( 'p' );
						p.setAttribute( getClassStr(), "price_schedule text11" );
						p.innerHTML = "納期の目安：" + objDetail[j].delivery_date + "日　　　　<!--li>料金の目安：</li-->" <!--li>+ objDetail[j].price + "円～"</li-->;
						divCaption.appendChild( p );
						// p
						var p = document.createElement( 'p' );
						var a = document.createElement( 'a' );
						var img = document.createElement( 'img' );
						img.src = "../price/images/price_detail_btn_off.gif";
						img.setAttribute( "width", "323px" );
						img.setAttribute( "height", "29px" );
						if ( navigator.appName.indexOf("Microsoft") >= 0 ){
							img.width = 323;
							img.height = 29;
						}
						a.href = "../service/?k=" + strKindID + "&c=" + strContentsID + "&d=" + objDetail[j].id;
						a.appendChild( img );
						p.appendChild( a );
						divCaption.appendChild( p );
					}
				}
				// 類似
				if ( _obj.alike != undefined ) {
					if ( _obj.alike.length != undefined ) {
						// contents枠
						var div = document.getElementById( "service_alike_list" );
						DelChildDiv( "service_alike_list" );
						var ol = document.createElement( 'ol' );
						ol.setAttribute( getClassStr(), "service_alike" );
						ol.id = "sliderContent";
						div.appendChild( ol );
						for ( var i = 0; i < _obj.alike.length; i++ ) {
							// li
							var li = document.createElement( 'li' );
							if ( i == 0 ) {
								li.setAttribute( getClassStr(), "sleft" );
							} else {
								li.setAttribute( getClassStr(), "sright" );
							}
							ol.appendChild( li );
							// p
							var p = document.createElement( 'p' );
							li.appendChild( p );
							// image
							var img = document.createElement( 'img' );
							img.src = "../images/c" + strKindID + "_" + _obj.alike[i].contents_id + ".jpg";
							img.setAttribute( "width", "85px" );
							img.setAttribute('style','width:85px;');
							img.setAttribute( getClassStr(), "imgContents" );
							img.id = "img_c_" + _obj.alike[i].contents_id;
							// a
							var a = document.createElement( 'a' );
							a.href = 'javascript:SelectContents("' + _obj.alike[i].contents_id + '");';
							a.appendChild( img );
							p.appendChild( a );
							// p
							var p = document.createElement( 'p' );
							p.setAttribute( getClassStr(), "text10" );
							li.appendChild( p );
							// a
							var a = document.createElement( 'a' );
							a.href = 'javascript:SelectContents("' + _obj.alike[i].contents_id + '")';
							a.innerHTML = _obj.alike[i].contents;
							p.appendChild( a );
						}
					}
				}
			}
		}
	});
}

function SelectKind( _id ) {
	if ( _id == "" ) {
		_id = "1";
		strKindID = "1";
	}
	$(".imgBtn").animate( { height:'hide',opacity:'hide'},500 );
	$("#search_linkbox").animate( { height:'hide',opacity:'hide'},2500 );
	$(".imgContents").animate( { height:'hide',opacity:'hide'},2500 );
	$("#servicebox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#service_contents_box").animate( { height:'hide',opacity:'hide'},2500 );
	
	var img = $(".imgKind");
	for ( var i = 0; i < img.length; i++ ) {
		img[i].setAttribute( "title", "" );
		var strID = img[i].id;
		var aryID = strID.split("_");
		if ( aryID[2] == _id ) {
			img[i].setAttribute( "title", "SELECT" );
		}
	}
	$('.imgKind').jcaption({imageAttr: 'title'});
	strKindID			= _id;
	strContentsID		= "";
	isReloadContents	= true;
	isReloadDetail		= true;
	ShowService();
	
	$("#service_contents_box").animate( { opacity:'show'},3000 );
	$("#servicebox").animate( { opacity:'show'},3000 );
	$(".imgContents").animate( { opacity:'show'},3000 );
	$("#search_linkbox").animate( { opacity:'show'},3000 );
	$(".imgBtn").animate( { opacity:'show'},3000 );
}
function SelectContents( _id ) {
	if ( _id == "" ) {
		_id = "1";
		strContentsID = "1";
	}
	$(".service_alike").animate( { height:'hide',opacity:'hide'},2500 );
	$("#service_alike_box").animate( { height:'hide',opacity:'hide'},2500 );
	$("#search_linkbox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#servicebox").animate( { height:'hide',opacity:'hide'},2500 );
	var img = $(".imgContents");
	for ( var i = 0; i < img.length; i++ ) {
		img[i].setAttribute( "title", "" );
		var strID = img[i].id;
		var aryID = strID.split("_");
		if ( aryID[2] == _id ) {
			img[i].setAttribute( "title", "SELECT" );
		}
	}
	$('.imgContents').jcaption({imageAttr: 'title'});
	strContentsID		= _id;
	isReloadContents	= false;
	isReloadDetail		= true;
	ShowService();
	$("#servicebox").animate( { opacity:'show'},3000 );
	$("#search_linkbox").animate( { opacity:'show'},3000 );
	$("#service_alike_box").animate( { opacity:'show'},3000 );
	$(".service_alike").animate( { opacity:'show'},3000 );
}
function DelChildDiv( id ) {
    var div = document.getElementById( id );
	if ( !div ) return;
	if ( !div.firstChild ) return;
    while ( div.firstChild ) { 
        div.removeChild( div.lastChild );
    }
}
function getClassStr(){
	if ( navigator.appName.indexOf("Microsoft") >= 0 ){
		return "className";
	} else {
		return "class";
	}
}

