var isReloadContents	= true;
var isReloadDetail		= true;
var isReloadArea		= true;

$(document).ready(function() {
	$("#service_contents_box").hide();
	$("#service_area_box").hide();
	$("#price_servicebox").hide();
	$("#price_orderbtnbox").hide();
	SelectKind(strKindID);
	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'});
});


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: "_service_images.php",
		data: {
			"kid" : strKindID,
			"cid" : strContentsID,
			"did" : strDetailID,
			"acd" : strAreaCD
		},
		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,
			"acd" : strAreaCD
		},
		type     : "POST",
		dataType: "json",
		cache: true,
		async: true,
		scriptCharset: "UTF-8",
		success : function(_obj, _status){
			if ( isReloadContents ) {
				// contents枠
				var div = document.getElementById( "service_contents_list" );
				DelChildDiv( "service_contents_list" );
				if ( _obj.contents != undefined ) {
					// contents
					var objContents = _obj.contents;
					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" );
						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 );
					}
				}
			}
			if ( isReloadArea ) {
				// area枠
				var div = document.getElementById( "service_area_list" );
				DelChildDiv( "service_area_list" );
				if ( _obj.area != undefined ) {
					var ol = document.createElement( 'ol' );
					ol.id = "serch_address";
					ol.setAttribute( getClassStr(), "text11" );
					div.appendChild( ol );
					for ( var i = 0; i < _obj.area.length; i++ ) {
						// li
						var li = document.createElement( 'li' );
						ol.appendChild( li );
						// a
						var a = document.createElement( 'a' );
						a.href = 'javascript:SelectArea("' + _obj.area[i].cd + '")';
						a.innerHTML = _obj.area[i].name;
						li.appendChild( a );
					}
				}
			}
			if ( isReloadDetail ) {
				// detail枠
				var div = document.getElementById( "service_detail_list" );
				DelChildDiv( "service_detail_list" );
				if ( _obj.detail != undefined ) {
					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(), "price_service_detail" );
							div.appendChild( divDetail );
							for ( var k = 0; k < objDetail[j].zengo.length; k++ ) {
								// p
								var p = document.createElement( 'p' );
								p.setAttribute( getClassStr(), "price_before" );
								divDetail.appendChild( p );
								// img
								var img = document.createElement( 'img' );
								var a = document.createElement( 'a' );
								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.href = "../service/?k=" + strKindID + "&c=" + strContentsID + "&d=" + objDetail[j].id;
								a.appendChild( img );
								p.appendChild( a );
								// p
								var p = document.createElement( 'p' );
								p.setAttribute( getClassStr(), "price_after" );
								divDetail.appendChild( p );
								// img
								var img = document.createElement( 'img' );
								var a = document.createElement( 'a' );
								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.href = "../service/?k=" + strKindID + "&c=" + strContentsID + "&d=" + objDetail[j].id;
								a.appendChild( img );
								p.appendChild( a );
							}
							// div
							var divCaption = document.createElement( 'div' );
							divCaption.setAttribute( getClassStr(), "price_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 = "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 );
						}
					}
				}
			}
		}
	});
}

function SelectKind( _id ) {
	if ( _id == "" ) {
		_id = "1";
		strKindID = "1";
	}
	$(".imgContents").animate( { height:'hide',opacity:'hide'},2500 );
	$("#price_orderbtnbox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#price_servicebox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#service_area_box").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		= "";
	strAreaCD			= "";
	isReloadContents	= true;
	isReloadDetail		= true;
	isReloadArea		= true;
	ShowService();
	$("#service_contents_box").animate( { opacity:'show'},3000 );
	$("#service_area_box").animate( { opacity:'show'},3000 );
	$("#price_servicebox").animate( { opacity:'show'},3000 );
	$("#price_orderbtnbox").animate( { opacity:'show'},3000 );
	$(".imgContents").animate( { opacity:'show'},3000 );
}
function SelectContents( _id ) {
	if ( _id == "" ) {
		_id = "1";
		strContentsID = "1";
	}
	$("#price_orderbtnbox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#price_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;
	isReloadArea		= false;
	ShowService();
	$("#price_servicebox").animate( { opacity:'show'},3000 );
	$("#price_orderbtnbox").animate( { opacity:'show'},3000 );
}
function SelectArea( _cd ) {
	$("#price_orderbtnbox").animate( { height:'hide',opacity:'hide'},2500 );
	$("#price_servicebox").animate( { height:'hide',opacity:'hide'},2500 );
	strAreaCD			= _cd;
	isReloadContents	= false;
	isReloadDetail		= true;
	isReloadArea		= false;
	ShowService();
	$("#price_servicebox").animate( { opacity:'show'},3000 );
	$("#price_orderbtnbox").animate( { opacity:'show'},3000 );
	var strUrl = $("#linkShop").attr( "href" );
	var aryUrl = strUrl.split("?a=");
	$("#linkShop").attr( "href", aryUrl[0] + "?a=" + _cd )
}


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";
	}
}

