/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1;}
var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}
expires='; expires='+date.toUTCString();}
var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}
return cookieValue;}};


$(function() {

	// 手配会社検索結果一覧画面用 エリア検索プルダウン
	$('div.searchPanel select[name=hanCd]').change(function() {
		var panelId = $(this).attr('class');
		var selectedArea = $(this);
		var parentTag = $(selectedArea).parent('span').siblings('ul.prefecture');

		todofukenCheckBox(selectedArea,parentTag,panelId,null);
	});

	// 海外中エリア大エリア用 エリア検索プルダウン
	$('div.arrangeObj select[name=hanCd]').change(function() {
		var panelId = '';
		var selectedArea = $(this);
		var parentTag = $(selectedArea).parent('p').parent('div.list').siblings('ul.prefecture');

		todofukenCheckBox(selectedArea,parentTag,panelId,null);

	});

	// 初回ロード時呼び出し
	if($('div.arrangeObj select[name=hanCd]').size() != 0 && $('ul.prefecture').size() == 1) {
		var hanCd = $('div.arrangeObj select[name=hanCd]').val();
		if(hanCd != '' && $('input[name=prefCd]').size() == 0) {

			var panelId = '';
			var selectedArea = $('div.arrangeObj select[name=hanCd]');
			var parentTag = $(selectedArea).parent('p').parent('div.list').siblings('ul.prefecture');
			var setCookieValue = 'setCookieValue';
		
			todofukenCheckBox(selectedArea,parentTag,panelId,setCookieValue);

		}
	}
	
	$('td.photo').mouseover(function() {
		var td = $(this).get(0);
		var div = td.getElementsByTagName('div')
		div[0].style.background = 'url(/images/abroad/frm_gallery_detail_on.gif) no-repeat left top';
		$(this).css('background', 'url(/images/abroad/bg_gallery_detail_on.gif) no-repeat left bottom');
	});
	
	$('td.photo').mouseout(function() {
		var td = $(this).get(0);
		var div = td.getElementsByTagName('div')
		div[0].style.background = 'url(/images/abroad/frm_gallery_detail.gif) no-repeat left top';
		$(this).css('background', 'url(/images/abroad/bg_gallery_detail.gif) no-repeat left bottom');
	});
	
	$('a.footerBusinessCategory').click(function() {
		$('#footerLinkForm').append(
			$('<input>').attr('type', 'hidden').attr('name', 'businessCategoryDaiCd').attr('value', $(this).attr('id').replace('fc', ''))
		);
		$('#footerLinkForm').attr('action', $(this).attr('href'));
		$('#footerLinkForm').submit();
		return false;
	});
	
	$('a.openDekaImg').click(function(){
		window.open(this.href, 'abroad_xy');
		return false;
	});
	$('a.couponTarget').click(function(){
		window.open(this.href, 'coupon');
		return false;
	});
	$('a.hpTarget').click(function(){
		window.open(this.href, 'hp');
		return false;
	});
});

// チェックボックス動的制御
function todofukenCheckBox(selectedArea,parentTag,panelId,setCookieFunc){

	selectedArea.attr('disabled', 'disabled');
	$.ajax({
		dataType:'json',
		url:XY.config.pageContext.rctx + '/abroad/searchBoxAjax/companySearchBox',
		data: {
			hanCd : selectedArea.attr('value')
		},
		success:function(todofukenList) {
			$(parentTag).empty();
			$.each(todofukenList, function() {
				var label = $('<label />');
				var input =  $("<input type='checkbox' />").attr('name', 'prefCd').val(this.value.todofukenCd);
				if(panelId === 'select-1' || panelId === ''){
					label.attr('for', 's' + this.value.todofukenCd).attr('id', 'label' + this.value.todofukenCd);
					input.attr('id', 's' + this.value.todofukenCd);
				}else if(panelId === 'select-2'){
					label.attr('for', 's' + this.value.todofukenCd + 'btm').attr('id', 'label' + this.value.todofukenCd + 'btm');
					input.attr('id', 's' + this.value.todofukenCd + 'btm');
				}

				if (this.count == 0) {
					input.attr('disabled', 'disabled');
				}

				if(setCookieFunc == 'setCookieValue'){
					setCookieValue(this,input);
				}
				label.append(this.value.todofukenNm);
				var tag = $('<li />');
				tag.append(input).append(label);
				$(parentTag).append(tag);
			});

			// ajax取得後イベント仕込み
			if(panelId != ''){
				$('.animeBtn').animationButton();
			}

			selectedArea.removeAttr('disabled');
		},
		error:function() {
			alert('error:');
			selectedArea.removeAttr('disabled');
		}
	});
}

/**
 * AbroadSelectedPrefCdクッキー情報を元にチェックBox値設定
 */
function setCookieValue(todofukenObj,input){
	var cookies = $.cookie('AbroadSelectedPrefCd');
	if (cookies) {
		var selectedPrefCd = cookies.split(',');
		for (var i = 0 ; element = selectedPrefCd[i] ; i++){
			if (element == todofukenObj.value.todofukenCd) {
				input.attr('checked', true);
			}
		}
	}
}


