/* -----------------------------------------------------------------------------
>> 物件比較用ＪＳ
----------------------------------------------------------------------------- */

var i,type,url;

jQuery(document).ready(function(){
	var name=window.location.pathname;
	url=jQuery('#siteurl').val()+'/js/compare.php';
	
	if     (name.match(/biz_estate_result1/)){ type='biz_house'; }
	else if(name.match(/biz_estate_result2/)){ type='biz_estate'; }
	else if(name.match(/estate_result1|estate1/)){ type='house'; }
	else if(name.match(/estate_result2|estate2/)){ type='estate'; }
	else if(name.match(/((biz_)?(rent|house|estate))/)){
		type=RegExp.$1;
		if(type=='biz_estate' && name.match(/lifestyle/)) type='biz_house';
	}
	
	if(type && !name.match(/compare/)) {
		jQuery.get(
			url,
			{'type':type, 'mode':'check'},
			function(res){
				res=eval(res);
				if(res.data.length){
					jQuery("input[name='compare']").each(function(){
						var chk=(jQuery.inArray(parseInt(jQuery(this).val()), res.data) != -1);
						jQuery(this).attr('checked',chk);
					});
				}
			}
		);
	}
});

// -----------------------------------------------------------------------------

jQuery(function(){
	jQuery('#lump').click(function(){
		window.alert("ｶﾞ━━━━(；ﾟдﾟ)━━━━━ﾝ !! \n\n現在の仕様に矛盾が発生しているため、一括選択機能の実装は確認待ちになっています。\n（一覧が最大20件なのに、比較登録は10件まで）");
		if(jQuery('#lump').attr('checked')) jQuery('#lump').attr('checked',false);
		/*
		var chk=jQuery(this).attr('checked');
		jQuery("input[name='compare']").each(function(){
			jQuery(this).attr('checked', chk);
			jQuery(this).click();
		});
		*/
	});
	
	jQuery("input[name='compare']").click(function(){
		if(jQuery(this).attr('checked')){
			jQuery.get(
				url,
				{'type':type, 'mode':'add', 'id':jQuery(this).val()},
				function(res){
					res=eval(res);
					if(!res.result){
						window.alert(res.message);
						jQuery("input[value='"+res.cancel+"']").attr('checked',false);
					}
				}
			);
		}else{
			remove(jQuery(this).val(),false);
		}
	});
});

// -----------------------------------------------------------------------------

function remove(id,flag) {
	jQuery.get(
		url,
		{'type':type, 'mode':'remove', 'id':id},
		function(res){
			res=eval(res);
			if(res.result){
				if(flag)window.location.href=window.location.href;
			}else{
				window.alert(res.message);
			}
		}
	);
}

