/*/////////////////////////////////////////////////////////////////////////////////////////////////
	User's Guide
		SelectPrint JavaScript
 Modified : 2013-12-16
/////////////////////////////////////////////////////////////////////////////////////////////////*/

$(function(){
	
	
	$("form#selectForm :checkbox").removeAttr('checked');
	$("form#selectForm li > span").removeClass('check');
	$('#selectForm li:has(>ul)').each(function(){
		$(this).children('span').addClass('childrenToggle');
		$(this).children('span').children('input').val('');
	});
	
	$('#printButton div button#print').attr('disabled','disabled');
	
	$('body').append('<div id="nowloadingMsg"><div class="msg">'+textData.selectprintLoadingTxt+'</div><div class="btn"><button>OK</button></div></div>');
	
	$('#printButton div button').button().attr('hideFocus','true');
	if(ua._isIE6){
		$('#printButton div button').text(' ');
	}

	function winSizing(){
		if(ua._isChrome){
			window.resizeTo(800,550);
		}
		$('#selectTree').height( $(window).height() - $('#spheader').height() - $('#printButton').height() );
		$('#nowloadingMsg').css('top', ($(window).height()/2 - $('#nowloadingMsg').height()/2) +"px");
		$('#nowloadingMsg').css('left', ($(window).width()/2 - $('#nowloadingMsg').width()/2) +"px");
	}
	
	setTimeout(function(){ winSizing(); }, 0);
	$(window).resize(function(){ setTimeout(function(){ winSizing(); }, 0); });
	if(ua._isSafari){
		$(window).load(function(){ setTimeout(function(){ winSizing(); }, 0); });
	}
	
	$('#nowloadingMsg .btn button').click(function(){
		$('#nowloadingMsg').remove();
	});
	
	//スタイル用画像先読み
	function PreloadCssImage(){
		var preImg=[];
		for(var i=1; i<=8; i++){
			preImg.push('shared/img/normal_list'+i+'.gif');
		}
		for(var i=1; i<=50; i++){
			preImg.push('shared/img/number/List_Numbered_'+i+'.gif');
			preImg.push('shared/img/number/step_no_'+i+'.gif');
		}
		var doc=document;
		if(doc.images){
			if(!doc.PLCI){
				doc.PLCI = new Array();
			}
			var i, j=doc.PLCI.length, a=preImg;
			for(i=0; i<a.length; i++){
				if(a[i].indexOf("#")!=0){
					doc.PLCI[j] = new Image;
					doc.PLCI[j++].src=a[i];
				}
			}
		}
	}
	PreloadCssImage();
	
	$(window).load(function(){
		//全ロード処理
		$("body").append('<div id="contents"><div id="container" class="container"></div></div>');
		var p = [];
		$("form#selectForm input:checkbox").each(function(i){
			if(/^id/.test($(this).val())){
				p[p.length]=$(this).val();
			}
		});
		window.context = false;
		$(document).bind("contextmenu",function(e){
			return window.context;
		});
		
		$('#print').before('<span id="loadingText">'+textData.selectprintLoading+'&nbsp;<span id="par"></span></span>');
		var progressbarWidth0 = -200;
		var loadingTextWidth = $('#loadingText').width();
		/*if(ua._isChrome){
			$('#loadingText').css("marginLeft","160px");
		}*/
		if(ua._isFirefox7low){
			$('#loadingText').css({
				"margin":"-34px 0 0 90px",
				"left": ($(window).width()/2) + "px"
			});
		}
		/*if(ua._isSafari6hi && ua._isMac){
			$('#loadingText').css("marginLeft","160px");
		}*/
		var displayTable = 'display: none;';
		//var displayTable = (ua._isSafari5_1 && ua._isMac) ? 'display: table;' : 'display: block;' ;
		
		
		var pInit = p.length;
		
		var nextfunc = function(plen, f, psent){
			if(plen != 0){
				$('#par').text(psent+'%');
				$('#loadingText').css('backgroundPosition', Math.floor(progressbarWidth0 + (loadingTextWidth/100*psent)) +'px 50%');
				setTimeout(eval(f),50);
			}else{
				htmlLoadSuccess();
			}
		}
		
		var func = function(){
			psent = Math.floor( (pInit-p.length) / pInit *100 ) ;
			value = p.shift();
			$("#container").append('<div id="sp' + value + '" style="'+displayTable+'" class="pageBreak"></div>');
			//$('#sp'+value).addClass('pageBreak');//.hide();
			$('#sp'+value).load(value+'.html .page', function(text, status){
				if(status=='success'){
					$('#sp'+value+' .page *[id^="id"]').removeAttr('id');
					nextfunc(p.length, "func", psent);
				}else{
					$('#sp'+value).text('Load Error: '+value);
					nextfunc(p.length, "func", psent);
				}
			});
		}
		
		var funcChrome = function(){
			psent = Math.floor( (pInit-p.length) / pInit *100 ) ;
			value = p.shift();
			$("#container").append('<div id="sp' + value + '" style="'+displayTable+'" class="pageBreak"><div id="ddd"></div></div>');
			try{
				var variableName = value.split('-').join('_')
				loadvar = eval(variableName);
				if(loadvar){
					loadvar = loadvar.replace(/^.*?<body[^>]*?>/, "").replace(/^<\/body>.*?<\/html>/, "");
					$('#ddd').html(loadvar);
					$('#ddd .page *[id^="id"]').removeAttr('id');
					$('#ddd .page').appendTo('#sp'+value);
					$('#ddd').remove();
				}else{
					$('#sp'+value).text('Load Error: '+variableName);
				}
			}catch( err ){
				$('#sp'+value).text('Load Error: '+variableName);
			}
			nextfunc(p.length, "funcChrome", psent);
		}
		
		if(pInit){
			if( HttpReqSecure ){
				setTimeout(funcChrome, 300);
			}else{
				setTimeout(func, 300);
			}
		}else{
			alert(textData.selectPrintErrTxt);
		}
	});
	
	function htmlLoadSuccess(){
		headerTrim();//TipsなどのH5の中のスペース除去
		removeThead();//IE8のTHEAD削除
		$('#container>div').hide();
		var isFirst = true;
		$("form#selectForm :checked").each(function(i){
			if(/^id/.test($(this).val())){
				$('#sp'+$(this).val()).show();
				if(isFirst){
					$('#sp'+$(this).val()).removeClass('pageBreak');
					isFirst = false;
				}
			}
		});
		setTimeout(function(){
			window.context = true;
			$('#printButton div button#print').button( "option", "disabled",false );
			$('#loadingText').remove();
			$('#nowloadingMsg .btn button').click();
		},1000);
	}
	
	
	
	
	
	$('#printButton button#print').click(function(){
		$('form#selectForm').submit();
		return false;
	});
	$('form#selectForm').submit(function(){
		if($("form#selectForm :checked").length){
			selectP(1,1);
		}else{
			alert(textData.selectPrintErrTxt);
		}
	});
	
	function selectP(a,b){
		if(b == a){
			setTimeout(function(){
				print();
				if(ua._isSafari && !ua._isSafari5_1) setTimeout(function(){ winSizing(); }, 0);
			},500);
		}
	}
	
	
	
	
	$('#selAll').click(function(){
		$("form#selectForm :checkbox").attr('checked','checked');
		$("form#selectForm li > span").addClass('check').removeClass('childcheck');
		$('#container>div').show();
		$('#container>div').addClass('pageBreak');
		$('#container>div:first').removeClass('pageBreak');
	});
	$('#unSele').click(function(){
		$("form#selectForm :checkbox").removeAttr('checked');
		$("form#selectForm li > span").removeClass('check').removeClass('childcheck');
		$('#container>div').hide();
	});
	
	
	$(':checkbox').click(function(e){
		if($(this).parent().hasClass('childrenToggle')){//
			$(this).closest("li").attr('id','checkDummy');
			if(e.target.checked){
				$(this).closest('li').children('span').removeClass('childcheck').addClass('check');
				$('#checkDummy ul input:checkbox').each(function(i,t){
					t.checked = true;
					$(t).closest('li').children('span').removeClass('childcheck').addClass('check');
				});
				pagechmov($(this).closest('li'));
			}else{
				$(this).closest('li').children('span').removeClass('check').removeClass('childcheck');
				$('#checkDummy ul input:checkbox').each(function(i,t){
					t.checked = false;
					$(t).closest('li').children('span').removeClass('check').removeClass('childcheck');
				});
				pagechmov($(this).closest('li'));
			}
			$('#checkDummy').removeAttr('id');
		}else{
			if(e.target.checked){
				$(this).closest('li').children('span').addClass('check');
				pagechmov($(this).closest('li'));
				pagechmov($(this).closest('li').parent().closest('li'));
			}else{
				$(this).parents("li").children('span').each(function(i,t){
					$(t).children('input').removeAttr('checked')
					$(t).removeClass('check').addClass('childcheck');
				});
				$(this).closest('li').children('span').removeClass('check').removeClass('childcheck');
				pagechmov($(this).closest('li'));
				pagechmov($(this).closest('li').parent().closest('li'));
			}
		}
		
		$('#container>div').hide();
		var isFirst = true;
		$("form#selectForm :checked").each(function(i){
			if(/^id/.test($(this).val())){
				$('#sp'+$(this).val()).addClass('pageBreak').show();
				if(isFirst){
					$('#sp'+$(this).val()).removeClass('pageBreak');
					isFirst = false;
				}
			}
		});
	});
	
	function pagechmov(ta) {
		var inputNum = ta.parent("ul").children('li').length;
		var ch=0,ci=0;
		ta.parent("ul").children('li').each(function(i,t){
			if($(t).children('span').hasClass('check')){
				ch += 1;
			}else if($(t).children('span').hasClass('childcheck')){
				ci += 1;
			}
		});
		if(ch==inputNum){
			ta.parent("ul").parent("li").children('span').removeClass('childcheck').addClass('check');
			ta.parent("ul").parent("li").children('span').children('input').attr('checked','checked');
		}else if(ch==0){
			if(ci){
				ta.parent("ul").parent("li").children('span').removeClass('check').addClass('childcheck');
			}else{
				ta.parent("ul").parent("li").children('span').removeClass('childcheck').removeClass('check');
			}
		}else{
			ta.parent("ul").parent("li").children('span').removeClass('check').addClass('childcheck');
		}
	}
	
	
	
	

});