$(function(){

	_dialogs._init();
	
	$('#mask').click(function(){
		$(this).hide();
		$('.window').dialog('close');
	});	
	
	//top rightbanテキストリンク用
	$('.eki_button').click(function(){
		//_obj.dialog('close');
		//_obj.dialog('destroy');
		_dialogs._dialog_99($(this).attr('name'));
	});
	
	//loading
	$("#loading").bind("ajaxSend", function(){
		$(this).fadeIn();
	}).bind("ajaxComplete", function(){
	  	$(this).fadeOut();
	});
});


//obj dialogs
var _dialogs = {
	
	_init : function(){
		
		_dialogs._rosen();
		_dialogs._jusho();
		_dialogs._edit_rosen();
		_dialogs._kodawari();
		
		$('#mask').fadeTo(0,0.7);
		
	},
	
	_rosen : function(){
		
		$('.rosen_btn').click(function(){

			var sendArr = new Array();
			
			var filter = $(this).attr('name');
			
			var _obj = $('#dialog_rosen');
			_obj.dialog('close');
			_obj.remove();
			_obj.dialog({
			
				autoOpen: false,
				resizable:false,
				draggable:true,
				width: 600,
				height:550,
				modal:false,
				//title:'路線を選ぶ',
				position:['center',100],
				open:function(){
														 
					_obj.load("dialog.php?mode=rosen",null,function(){
						//路線のphpが開いたら
						
						_dialogs._checkboxEnable();
						
						//aタグからの遷移
						$('a.rosen_link').click( 
							function(){
								_obj.dialog('close');
								_obj.dialog('destroy');
								sendArr.push(this.id)
								_dialogs._dialog_99(sendArr, filter);
							}
						);
						
						//送信ボタンからの遷移
						$(".send").click(		
							function(){
								if(!$(this).hasClass('die')){
									_obj.dialog('close');
									_obj.dialog('destroy');
									$("input:checked").each(function(){
										sendArr.push(this.value);
									});
									_dialogs._dialog_99(sendArr, filter);
								}
							}
						);
						$(".finish").click(		
							function(){
								if(!$(this).hasClass('die')){
									_obj.dialog('close');
									_obj.dialog('destroy');
									$("input:checked").each(function(){
										sendArr.push(this.value);
									});
									_dialogs._gotoSearchPage(sendArr, 'rosen', filter);
								}
							}
						);
						
						//checkboxの状態変化イベント
						$(':input[type="checkbox"]').change(function(){
							_dialogs._checkboxEnable();
						});
						
					});

				},
				beforeclose: function(){
					$('#mask').hide();
				}
				
			});
			_obj.dialog('open');
			

			var maskWidth = $(document).width();
			var maskHeight = $(document).height();
			$('#mask').css({'width':maskWidth, 'height':maskHeight});
			$('#mask').show();

		});
		
	},
	
	_jusho : function(){
		
		$('.jusho_btn').click(function(){
		
			var sendArr = new Array();
			
			var filter = $(this).attr('name');
		
			var _obj = $('#dialog_jusho');
			_obj.dialog('close');
			_obj.remove();
			_obj.dialog({
			
				autoOpen: false,
				resizable:false,
				draggable:true,
				width: 600,
				height:500,
				modal:false,
				position:['center',100],
				open:function(){
					_obj.load("dialog.php?mode=jusho",null,function(){
						
						_dialogs._checkboxEnable();
						
						//aタグからの遷移
						$('a.jusho_link').click( 
							function(){
								_obj.dialog('close');
								_obj.dialog('destroy');
								sendArr.push(this.id)
								_dialogs._gotoSearchPage(sendArr,'jusho', filter);
							}
						);
						
						//送信ボタンからの遷移
						$(".finish").click(		
							function(){
								if(!$(this).hasClass('die')){
									_obj.dialog('close');
									_obj.dialog('destroy');
									$('input[name="jusho"]:checked').each(function(){
										sendArr.push(this.value);
									});
									_dialogs._gotoSearchPage(sendArr,'jusho', filter);
								}
							}
						);
						
						$(':input[name="checkall"]').change(function(){
							var flag = $(this).attr('checked');
							if(flag){
								$(this).parents('form').find(':input[name="jusho"]').attr('checked',true);
								_dialogs._checkboxEnable();
							}else{
								$(this).parents('form').find(':input[name="jusho"]').attr('checked',false);
								_dialogs._checkboxEnable();
							}
						});
						
						//checkboxの状態変化イベント
						$(':input[type="checkbox"]').change(function(){
							_dialogs._checkboxEnable();
						});
						
					});
				},
				beforeclose: function(){
					$('#mask').hide();
				}
				
			});
			
			_obj.dialog('open');

			var maskWidth = $(document).width();
			var maskHeight = $(document).height();
			$('#mask').css({'width':maskWidth, 'height':maskHeight});
			$('#mask').show();
			
		});
		
	},
	
	_edit_rosen : function(){
		
		$('.edit_rosen_btn').click(function(){
			
			var sendArr = new Array();
			
			var filter = $(this).attr('name');
			
			var _obj = $('#dialog_edit_rosen');
			_obj.dialog('close');
			_obj.remove();
			_obj.dialog({
			
				autoOpen: false,
				resizable:false,
				draggable:true,
				width: 600,
				height:300,
				modal:false,
				//title:'路線を選ぶ',
				position:['center',100],
				open:function(){
														 
					_obj.load("dialog.php?mode=edit_rosen",null,function(){
						//路線のphpが開いた
						
						//送信ボタンからの遷移
						$(".finish").click(	
							function(){
								$('input[name=rosenmei_v]').val($('select[name="edit_rosen_select_1"] option:selected').text());
								$('input[name=ekimei_v]').val($('select[name="edit_rosen_select_2"] option:selected').text());
								$('input[name=trans1_1]').val($('select[name="edit_rosen_select_1"] option:selected').val());
								$('input[name=trans1_2]').val($('select[name="edit_rosen_select_2"] option:selected').val());
								_obj.dialog('close');
								_obj.dialog('destroy');
							}
						);
						
						$('select[name=edit_rosen_select_1]').click(function(){
								_dialogs._createEkiOption($(this).val());
						});						
					});
				},
				beforeclose: function(){
					$('#mask').hide();
				}
				
			});
			_obj.dialog('open');
			

			var maskWidth = $(document).width();
			var maskHeight = $(document).height();
			$('#mask').css({'width':maskWidth, 'height':maskHeight});
			$('#mask').show();

		});
		
	},
	
	_kodawari : function(){
		
		$('.kodawari_btn').click(function(){
			
			var sendArr = {};
			
			var filter = $(this).attr('name');
			
			var _obj = $('#dialog_kodawari');
			_obj.dialog('close');
			_obj.remove();
			_obj.dialog({
			
				autoOpen: false,
				resizable:false,
				draggable:true,
				width: 900,
				height:650,
				modal:false,
				title:'こだわり検索',
				position:['center',50],
				open:function(){
														 
					_obj.load("dialog.php?mode=kodawari",null,function(){
						//コダワリのphpが開いた
						$('.addBt').click(function(){
							$('.add_kodawari').toggle();
						});
						//送信ボタンからの遷移
						$(".finish").click(	
							function(){								
								_obj.dialog('close');
								_obj.dialog('destroy');
								$("input:checked").each(function(){
									sendArr[this.name] = this.value;
								});
								$("select").each(function(){
									sendArr[this.name] = this.value;
								});
								_dialogs._gotoSearchPage2(sendArr, filter);
							}
						);
						
						/*$('select[name=edit_rosen_select_1]').click(function(){
								_dialogs._createEkiOption($(this).val());
						});*/						
					});
				},
				beforeclose: function(){
					$('#mask').hide();
				}
				
			});
			_obj.dialog('open');
			

			var maskWidth = $(document).width();
			var maskHeight = $(document).height();
			$('#mask').css({'width':maskWidth, 'height':maskHeight});
			$('#mask').show();

		});
		
	},
	
	_dialog_99 : function(_arr, filter){
		
		var sendArr = new Array();
	
		var _obj = $('#dialog_99');
		_obj.dialog('close');
		_obj.remove();
		_obj.dialog({
				resizable:false,
				autoOpen:false,
				draggable:true,
				width:600,
				//height:600,
				modal:false,
				title:'駅から選ぶ',
				position:['center',100],
				open:function(){
					var txt = "";
					for(var i = 0; i<_arr.length; i++){
						txt += _arr[i] + ",";
					}
					_obj.load('dialog.php?mode=eki&trans1_1='+_arr,null,function(){
												
						//aタグからの遷移
						$('a.eki_link').click( 
							function(){
								_obj.dialog('close');
								_obj.dialog('destroy');
								sendArr.push(this.id)
								_dialogs._gotoSearchPage(sendArr,'eki',filter);
							}
						);
						
						//送信ボタンからの遷移
						$(".finish").click(		
							function(){
								if(!$(this).hasClass('die')){
									_obj.dialog('close');
									_obj.dialog('destroy');
									$('input[name="eki"]:checked').each(function(){
										sendArr.push(this.value);
									});
									_dialogs._gotoSearchPage(sendArr,'eki',filter);
								}
							}
						);
						
						$(':input[name="checkall"]').change(
							function(){
								var flag = $(this).attr('checked');
								if(flag){
									$(this).parents('form').find(':input[name="eki"]').attr('checked',true);
									_dialogs._checkboxEnable();
								}else{
									$(this).parents('form').find(':input[name="eki"]').attr('checked',false);
									_dialogs._checkboxEnable();
								}
							}
						);
						
						//checkboxの状態変化イベント
						$(':input[type="checkbox"]').change(function(){
							_dialogs._checkboxEnable();
						});
						
					});
					
					
					
				},
				beforeclose: function(){
					$('#mask').hide();
				}
		});
		
		_obj.dialog('open');
		
		var maskWidth = $(document).width();
		var maskHeight = $(document).height();
		$('#mask').css({'width':maskWidth, 'height':maskHeight});
		$('#mask').show();

	},
	
	//検索ボタン有効化・無効化
	_checkboxEnable : function(){
		
		var ckd = $('input:checked').length;

		if(ckd > 0){
			
			$('.send').attr('src','img/button/bt_dialog_eki_e.gif');
			$('.finish').attr('src','img/button/bt_dialog_search_e.gif');
			$('.send').removeClass('die');
			$('.finish').removeClass('die');
			
		
		}else{
			
			$('.send').attr('src','img/button/bt_dialog_eki_d.gif');
			$('.finish').attr('src','img/button/bt_dialog_search_d.gif');
			$('.send').addClass('die');
			$('.finish').addClass('die');

		}
		
	},
	
	//search.phpへ飛ばす
	
	_gotoSearchPage : function(_arr, _mode, filter){

		var r = "";
		var Href = "";
		var key = "";
				
		if(_mode == "rosen") key = "trans1_1";
		else if(_mode == "jusho") key = "syozai";
		else if(_mode == "eki") key = "trans1_2";
		
		for(var i = 0; i<_arr.length; i++){
			if(i > 0) r += ","; //%2F
			r += _arr[i];
		}
		
		(_arr.length > 0) ? Href = "search.php?" + key + "=" + r : Href = "search.php" ;

		if(filter) Href = Href + "&shubetu1="+ filter;
		
		location.href = Href;
		
	},
	_gotoSearchPage2 : function(_arr,filter){

		var r = "";
		var Href = "search.php?";
		var key = "";
		
		for(var i in _arr){
			Href += i + '=' + _arr[i] + '&';
		}		
		
		if(filter == 'student') Href += 'j1070=2';
		if(filter == 'couple') Href += 'j1030=1';
		
		location.href = Href;
		
	},
	
	_createEkiOption : function(val){

		$('select[name="edit_rosen_select_2"] option').remove();
		
		var data = 'trans1_1=' + val;
		
		sendRequest('POST','edit_rosen_res.php',data,true,function(xmlhttp){
		//callBack
			
			redrawOption(xmlhttp.responseText);
			
		});
	}
}

function redrawOption(restxt){
	var splitData = restxt.split('/');
	var codeArr = splitData[0].split(',');
	var stationArr = splitData[1].split(',');
	
	for(var i in codeArr){
		var e_opt = document.createElement('option');
		var e_txt = document.createTextNode(stationArr[i]);
		e_opt.appendChild(e_txt);
		//e_opt.value = codeArr[i];
		e_opt.value = codeArr[i];
		$('select[name="edit_rosen_select_2"]').append(e_opt);
	}
}

function createHttpRequest()
{
  var xmlhttp = null;
  if(window.ActiveXObject){
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {

      }
    }
  } else if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  } else {

  }
  if (xmlhttp == null) {
    alert("Can not create an XMLHTTPRequest instance");
  }
  return xmlhttp;
} 

function sendRequest (method, url, data, async, callback)
{
    var xmlhttp = createHttpRequest();
    
    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) { 
            callback(xmlhttp);
        }
    }
    xmlhttp.open(method, url, async);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send(data);
}
