// JavaScript Document
// Scripts with low update frequency.

//1:jQuery
//	1-1:common
//	1-2:.toppage
//	1-3:.hikaku
//	1-4:isp/_isp/_plan.html
//2:functions
//3:Banner (bcode.js)

$.getScript( MEDIA_URL + "js/marippe.js");
						
// 1:jQuery --------------------------------------------------------------------------------------
$(document).ready(function(){

	//1-1 common
		
		// 1-1-1 tooltip
		$.getScript( MEDIA_URL + "js/jquery.qtip-1.0.0-rc3.min.js", function(){
																														 
			$.fn.qtip.styles.cp = { // Last part is the name of the style
				width: 200,
				background: '#0A5C89',
				color: '#fff',
				textAlign: 'center',
				border: { width: 7, radius: 5, color: '#0A5C89' },
				tip: 'bottomMiddle',
				name: 'light' // Inherit the rest of the attributes from the preset light style
			}
			$.fn.qtip.styles.nocp = { // Last part is the name of the style
				width: 200,
				background: '#ff3300',
				color: '#fff',
				textAlign: 'center',
				border: { width: 7, radius: 5, color: '#ff3300' },
				tip: 'bottomMiddle',
				name: 'light' // Inherit the rest of the attributes from the preset light style
			}
			$.fn.qtip.styles.notice = { // Last part is the name of the style
				width: 200,
				background: '#10A0ED',
				color: '#fff',
				textAlign: 'center',
				border: { width: 7, radius: 5, color: '#10A0ED' },
				tip: 'bottomMiddle',
				name: 'light' // Inherit the rest of the attributes from the preset light style
			}
			
			$("div.sBHType").qtip({	
					content: '集合住宅では戸建プランも利用可能な場合があります。',
					position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },	
					show: { effect:{type:'fade'}, when:{event:'mouseover'}},
					hide: 'mouseout',
					style: 'notice'
			});
			$("div.sBNTel").qtip({	
					content: 'NTTの家電は利用されますか？',
					position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },	
					show: { effect:{type:'fade'}, when:{event:'mouseover'}},
					hide: 'mouseout',
					style: 'notice'
			});
			$("div.sBLine").qtip({	
					content: 'ご利用予定の回線は？',
					position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },	
					show: { effect:{type:'fade'}, when:{event:'mouseover'}},
					hide: 'mouseout',
					style: 'notice'
			});
			$("a[href^='/'][href*='/track/']:not(:has(img))").qtip({	
					content: 'このリンクからのお申込みにはキャンペーンが適用されます。',
					position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },	
					show: { effect:{type:'fade'}, when:{event:'mouseover'}},
					hide: 'mouseout',
					style: 'cp'
			});

			$("#contents a[href^='http']:not(:has(img))").not("a[href^='http://www.all-navi.jp/b/']").qtip({	
					content: 'このリンクからのお申込みにはキャンペーンが適用されません。',
					position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },	
					show: { effect:{type:'fade'}, when:{event:'mouseover'}},
					hide: 'mouseout',
					style: 'nocp'
			});
		
		});
		//1-1-2 #searchBox 
		
			//1-1-2-1 mobilebb
			$("form div.sBCondition li.chkMob input.lType").click(function(){
				if( !$(this).attr("checked")){
					$(this).attr("checked",false);
					$(this).parent().next("ul.rTypes").find("input.rType").attr("checked",false);
				}else{
					$(this).attr("checked",true);
					$(this).parent().next("ul.rTypes").find("input.rType").attr("checked",true);
				}
			});
			$("form div.sBCondition li.chkMob input.rType").click(function(){
					var chkMobLen = $("form div.sBCondition li.chkMob ul.rTypes input:checked").length;
					if( chkMobLen == 0){
							$(this).parent().parent().parent().prev().children("input").attr("checked",false);
					}else{
							$(this).parent().parent().parent().prev().children("input").attr("checked",true);
					}
			});
		

	//1-2 .toppage
	
		//1-2-1 #nMap
		$(".toppage #contents. #nMap area").click(function(){
			var hrefUrlA = $(this).attr("href").split("/");
			var region = hrefUrlA[hrefUrlA.length-2];
			$(".toppage #contents. #formWrap form.hikakuForm").prepend('<input type="hidden" name="region" value="' + region + '" /><input type="hidden" name="regionSearch" value="1" />');
			document.hikakuForm.submit();
			return false;
		});
		
		//1-2-2 #ranking	
		
			$("#ranking div.line h3 span").click(function(){
				$("#rankAdsl,#rankMobileBB,#rankHikariM,#rankHikariH").removeClass("comCurrent");
				$("#ranking div.line h3").hide().show();
				$("#ranking .line .rankers").hide();
				$(this).parent().parent("div.line").addClass("comCurrent");
				$("#ranking .line.comCurrent .rankers").fadeIn("slow");
			});
																												
	
	//1-3 .hikaku
    
		//1-3-1 filter selectbox jump for IE
			if($.browser.msie){
					$("#contents table.hikakuTable thead th form.filterSelect select").jQselectable({
					 style: 'simple',
					 set: 'fadeIn',
					 out: 'fadeOut',
					 top: -10,
					 callback: function() {
							$(this).parent().submit();
					 }
					});
			}else{
					$("#contents table.hikakuTable thead th form.filterSelect select").change(function(){
							$(this).parent().submit();
					});
			}			
			
		//1-3-2 uchiwake
		$("table.hikakuTable tbody tr td.uchiwake a.comIco").click(function(e){
			var hikakuRow = $(this).parent().parent().attr("id");
			var tableId = $(this).parent().parent().parent().parent().attr("id");
			
			var rankTr = 'table#' + tableId + ' tr#'+ hikakuRow + ' ';
			
			var years = $(rankTr + "span.years").text();
			var usualIniCost = $( rankTr + "td.usualIniCost" ).text();
			var disIniCost = $( rankTr + "td.iniCost" ).text();
			var monCostStr = $( rankTr + "td.monCost" ).text();
			var monCostA = monCostStr.split(',');
			var monCostA = jQuery.map(monCostA, function(cost){
				return cost.substr(0 ,3);
			});
			
			var monCost = monCostA.join('');
			
			
			var cpDiscount = $( rankTr + "td.cpDiscount" ).text();
			var actCost = $( rankTr + "span.monTotal" ).text();
			var toDetail = $( rankTr + "td.toDetail a" ).attr("href");
			
			var uchDetail =  '<div id="overlay"><div id="uchDetail" class="sct"><p class="close"><span class="comBtn"><span class="icoClose">閉じる</span></span></p>';
			var uchDetail = uchDetail + '<p class="uchMain"><span class="signIniCost">（</span><span class="iniCost costBlock">初期費用<em>' + disIniCost + '</em></span>';
			var uchDetail = uchDetail + '<span class="signMonCost">＋</span><span class="monCost costBlock">月額費用<em>' + addComma(monCost * 12 * years) + '円</em><span class="codicil">（月額費用×' + 12*years + 'ヵ月）</span></span>';
			var uchDetail = uchDetail + '<span class="signCpDis">－</span><span class="cpDiscount costBlock">特典割引額<em>' + cpDiscount  + '</em></span>';
			var uchDetail = uchDetail + '<span class="signDivMon">）÷</span><span class="divideMon costBlock">' + 12*years + 'ヵ月</span>';
			var uchDetail = uchDetail + ' <span class="signActCost">＝</span><span class="actCost costBlock">' + years + '年間の実費<strong>' + actCost + '</strong></span></p>';
			var uchDetail = uchDetail + '<p>※実費とは上記計算により算出した実質費用のことであり、実際にお支払いいただく月額費用ではありません。</p>';
			var uchDetail = uchDetail + '<p class="toDetail"><a class="comBtnStr" href="' + toDetail + '">このプランの詳細を見る</a></p></div></div>';
	
	
			$("#"+tableId).after(uchDetail);	
			
			var uchiH = e.pageY + 40;
			var uchiW = e.pageX - ($("#uchDetail").width()/2);
			
			$("#overlay").css("position","absolute").css("left",0).css("top",0).css("height",$(document).height()).css("width","930px");
			
			$("#uchDetail").css("top",uchiH +"px").css("left",uchiW + "px");
			$("#uchDetail").fadeIn();
			
			$("#uchDetail p.close span.comBtn").click(function(){
				$("#overlay").fadeOut();
			});
			$("body").click(function(){
				$("#overlay").fadeOut();
			});
			
			return false;	
		
		});
		
	//1-4:isp/_isp/_plan.html
	$("a#costUchiwake").click(function(){
		$("#iniCostDetail").slideToggle("slow");
		$("#monCostDetail").slideToggle("slow");
	});
	$("a#cpUchiwake").click(function(){
		$("#cpDisDetail").slideToggle("slow");
		$("#actCostDetail").slideToggle("slow");
	});

	


});
// /jQuery -------------------------------------------------------------------------------------


// 2:functions -----------------------------------------------------------------------------

	// 2-1 addComma
	function addComma(str) {
		var num = new String(str).replace(/,/g, "");
		while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
		return num;
	}
// /functions -----------------------------------------------------------------------------


// 3:Banner (bcode.js) -----------------------------------------------------------------------------
function AllnaviBanner( tmode, baseurl ){
    this.tmode      = tmode;
    this.baseurl    = baseurl;
}

AllnaviBanner.prototype = {
    
    dispScTag: function( url ){
        document.write('<sc' + 'ript language="JavaScript" type="text/javascript" src="' + url + '">');
        document.write('</sc' + 'ript>');
    },
    
    getAdditionalParam: function(){
        var p = '?code=utf8';
        if( this.tmode ){
            p = '?code=utf8&t=1';
        }
        return p;
    },
    
    dispPlain: function( bid ){
        this.dispScTag( this.baseurl + 'viewjspl-' + bid + this.getAdditionalParam() );
    },
    
    disp: function( bid ){
        this.dispScTag( this.baseurl + 'viewjs-' + bid + this.getAdditionalParam() );
    }
    
};

var anb = new AllnaviBanner( 0, 'http://www.all-navi.jp/b/' );
// /Banner (bcode.js) ----------------------------------------------------------------------------