$(function(){
	if($.ifm){
		getHmsBefore();
		$(".date_compare").each(function(v){
			var dd = $.mysqlTimeStampToDate($(this).text());
			$(this).text($.diffDate(dd)).show();
		});
		$(".rollover:not(.active)").rollover();
		$.externalLinks();
		$("img[src$='.png']").ifixpng();
	}
	prettyPrint();
  $("img.platform, img.genre").tooltip();
	$("#latests .brand_name").each(function(){
		var charlen = $(this).text().length;
		if(charlen >= 10) {
			$(this).css("font-size", "12px");
		} else if(charlen >= 5) {
			$(this).css("font-size", "14px");
		} else {
			$(this).css("font-size", "16px");
		}
	});
	var continue_button = $("#customer_voice .continue");
	var offset = $("#customer_voice li").size() - 1;
	continue_button.hover(
		function(){
			$(this).css("background-color", "#ccc");
		},
		function(){
			$(this).css("background-color", "white");
		}
	).on("click", function(){
		get_new_voices();
	});
	// 「SEO会社の比較」ページ
	if($("#brands_list").size() > 0){
		// 月額の区切り線の描写
		$(".datarow .cost:not(:last-child)").css({"border-bottom":"1px solid #CCC","margin-bottom":"5px"});
		// チェックボックを配信制限をもとに更新
		getStableBrands();
		// チェックされている業者数のチェック
		$("#main").submit(function(){
			if(window._gaq){
				_gaq.push(["_trackEvent", "introduce", "click", "submit"]);  // GAへ送信
			}

			if($(":checkbox:checked").size() > 8){
				alert('選択可能な業者は最大8社です。');
				return false;
			} else {
				return true;
			}
		});
	}

	function getHmsBefore(){
		$.ajax({
			url: '/json/',
			dataType: 'json',
			data: {
				'ppi': 'json_formview'
			},
			success: function(r){
				var hms_before = $("#counter_hms");
				hms_before.append(r.ymd_before);
				var dd1 = $.mysqlTimeStampToDate(r.ymd_before);
				var dd2 = $.mysqlTimeStampToDate(r.ymd_now);
				var timer = new StopWatch(diffDateEn(dd1, dd2), hms_before);
				$("#counter_today").html(r.today);
			}
		});
	}
	function get_new_voices() {
		// templateにbrand_idが宣言されている場合はそれを使う
		if(!window.brand_id){
			window.brand_id = '';
		}
		continue_button.find("span").hide().end().find("img").show();
		$.ajax({
			url: '/json/',
			dataType: 'json',
			data: {
				'ppi': 'json_newvoice',
				'offset': offset + 1,
				'brand_id': window.brand_id
			},
			success: function(r) {
				offset += 10;
				$.each(r, function(k,v) {
					var order_google = '';
					var order_yahoo = '';
					var site_genre = '';
					if(v.order_google){
						order_google = '<span>Google&nbsp;<span class="orders">'+v.order_google+'</span></span>';
					}
					if(v.order_yahoo){
						order_yahoo = '<span>Yahoo!&nbsp;<span class="orders">'+v.order_yahoo+'</span></span>';
					}
					if(v.site_genre){
						site_genre = v.site_genre;
					}
					var li = '<li><p class="caption">';
					if(!brand_id){
						li += '<a href="/introduce/'+v.brand_cd+'/"><span>'+v.brand_name+'</span></a>に';
					}
					li += '<span class="price">'+v.order_price+'</span>円/月で依頼</p><p class="status"><span>'+v.prefecture+'('+v.person+')</span>&nbsp;'+site_genre+order_google+order_yahoo+'</p><div class="comment">'+v.reason+'</div></li>';
					continue_button.before(li);
				});
				if(r.length < 10){
					// 続きがなくなったらボタンを削除
					continue_button.remove();
				}
        continue_button.find("img").hide().end().find("span").show();
			}
		});
	}
	//ストップウオッチクラス
	function StopWatch(from, target){
		var oj = {
				cnt: from || 0,		//カウンター
				sec: from || 0,		//秒
				tid: null //タイマー
		};
		start();
		//タイム出力
		function echo(htm){
			target.html(dateFormat(htm));
		}
		//スタートボタンの処理
		function start(){
			tiomeOut();
			counter();
		}
		//カウンター
		function counter(){
			oj.cnt++;
			oj.sec=oj.cnt;
			echo(oj.sec);
			tiomeOut();
			oj.tid=setTimeout(counter,1000);//再起動
		}
		//タイムアウト
		function tiomeOut(){
			if(oj.tid)clearTimeout(oj.tid);
		}
	}
	function diffDateEn(dd1, dd2) {
		var diff = 0;
		if(dd2){
			diff = dd2 - dd1;
		} else {
			var now = new Date();
			diff = now - dd1;		
		}
		if(diff <= 0){
			return 0;
		} else {
			return Math.round(diff / 1000);
		}
	}
	function dateFormat(unixtime){
		if(unixtime == 0) return "00:00:00";
		var result = "";
		var date = {d:"", h:"", m:"", s:""};
		date.d = parseInt(unixtime / 86400);
		date.h = parseInt((unixtime % 86400) / 3600);
		date.m = parseInt(((unixtime % 86400) % 3600) / 60);
		date.s = parseInt(((unixtime % 86400) % 3600) % 60);
		if(date.h){
			result += ("0" + date.h).slice(-2);
		} else {
			result += "00";
		}
		result += ":";
		if(date.m){
			result += ("0" + date.m).slice(-2);
		} else {
			result += "00";
		}
		result += ":";
		if(date.s){
			result += ("0" + date.s).slice(-2);
		} else {
			result += "00";
		}
		return result;
	}
	function getStableBrands(){
		var url = "/json/";
		var data = {
			ppi: 'json_brand',
			site_url: '',
			keywords: '',
			begginer: 0,
			judge_from: '',
			affili_id: ''
		};
		$.getJSON(url,data,refreshCheckboxes);
	}
	function refreshCheckboxes(brands){
		var brand_ids = [];
		$.each(brands, function(){
			brand_ids.push(this.brand_id);
		});
		$.each($(":checkbox"), function(){
			if($.inArray(parseInt($(this).val()), brand_ids) < 0){
				$(this).attr("disabled", "disabled");
			}
		});
	}
});

