var UseCookie = false;

jQuery('document').ready(function() {
  	if (UseCookie) {
        jQuery.cookie('nipponnowaza_search_category');
        jQuery.cookie('nipponnowaza_waza_l2_list');
        jQuery.cookie('nipponnowaza_pref_list');

        if (jQuery.cookie('nipponnowaza_search_category')) {
		    switchSearchCategory(jQuery.cookie('nipponnowaza_search_category'));
	    }

	    var CookieWazaL2List = jQuery.cookie('nipponnowaza_waza_l2_list') ? jQuery.cookie('nipponnowaza_waza_l2_list') : '';
		var SelectedWazaL2List = CookieWazaL2List.split(',');
        for (var i=0; i<SelectedWazaL2List.length; i++) {
	        var SelectedWazaL2ID = SelectedWazaL2List[i];
	        jQuery('#wazal2_'+SelectedWazaL2ID).val(SelectedWazaL2ID);
	    }
   	    scanWazaL2();
        scanPref();
	}

	jQuery('.wazaL2List').hide();
	jQuery('.wazaL2List.selected').show();
	jQuery('.categoryBox').css({'min-height' : jQuery('#temp').height()});
	jQuery('#pref_list').change(function() { scanPref(); });
	
	jQuery('#searchBox_hint_detail').hide();
	jQuery('.serchBox_hint > img').click(function() {
	    jQuery('#searchBox_hint_detail').toggle(300);
    });
});



function reloadCalendar(Year, Month, CalendarID) {
	jQuery.ajax({
	    type: "GET",
	    url: "/common/calendar.html",
	    data: 'calendar_year=' + Year + '&calendar_month=' + Month + '&display=1&calendar_id=' + CalendarID,
	    success: function(Response){
		    jQuery('#Calendar' + CalendarID).html(Response);
	   }
	 });
}



function submitForm(Selector) {
	jQuery(Selector).submit();
}



function switchSearchCategory(WazaL1ID) {
	jQuery('.wazaL1List > li').removeClass('selected');
	jQuery('#wazaL1_' + WazaL1ID).addClass('selected');
    jQuery('.wazaL2List').removeClass('selected');
	jQuery('#wazaL2List_' + WazaL1ID).addClass('selected');
	jQuery('.wazaL2List').not('.selected').hide();
	jQuery('.categoryBox > .selected').show();
	jQuery.cookie('nipponnowaza_search_category', null);
    jQuery.cookie('nipponnowaza_search_category', WazaL1ID, {expires: 7, path: '/'});
}



function onClickWazaL2(ID) {
	// 対象の値をトグルで設定
    if (jQuery('#wazal2_' + ID).val() == '') {
	    jQuery('#wazal2_' + ID).val(ID);
	} else {
	    jQuery('#wazal2_' + ID).val('');
	}
	scanWazaL2();
}



function scanWazaL2() {
    var selectedWazaL2IDList = new Array();
    jQuery(".wazal2_list").each(function() {
    	// 隠し要素に設定されているID値をプッシュ
	    if (jQuery(this).val()) {
		    selectedWazaL2IDList.push(jQuery(this).val());
		}
	});
　var uniqueArray = [];
  var storage = {};
　var i,value;
　for ( i=0; i<selectedWazaL2IDList.length; i++) {
   　value = selectedWazaL2IDList[i];
      if (!(value in storage)) {
      　storage[value] = true;
         uniqueArray.push(value);
       }
   }
   selectedWazaL2IDList = uniqueArray;
	jQuery.cookie('nipponnowaza_waza_l2_list', null);
	jQuery.cookie('nipponnowaza_waza_l2_list', selectedWazaL2IDList, {expires: 7, path: '/'});
	
	if (selectedWazaL2IDList.length >= 1) {
		jQuery('.wazaL2List > li').removeClass();
		jQuery('.wazaL2List > li').addClass('no_check');
		for (var Key in selectedWazaL2IDList) {
			jQuery('#wazaL2ListItem_' + selectedWazaL2IDList[Key]).removeClass();
			jQuery('#wazaL2ListItem_' + selectedWazaL2IDList[Key]).addClass('checked');
		}
	}
	
	else {
		jQuery('.wazaL2List > li').removeClass();
		jQuery('.wazaL2List > li').addClass('float');
	}
}

function actionCrossList(WazaL1ID, WazaL2ID) {
	action = "/contents/field/waza/bunya/bunya_base.html";
	switchSearchCategory(WazaL1ID);
	switchSearchCategory(WazaL1ID);
	releaseAllWazaL2();
	onClickWazaL2(WazaL2ID);
	jQuery('#pref_list').val("");
	scanPref();
	$('#searchFormBox').attr("action", action);
	$('#searchFormBox').submit();
}

function actionWithSinglePref(prefId) {
	releaseAllWazaL2();
	jQuery("#pref_list").val(prefId);
	scanPref();
	$('#searchFormBox').submit();
}

function checkAllWazaL2() {
    jQuery('.wazal2_list').each(function() {
	    if (ID = jQuery(this).attr('id').replace(/^wazal2_/, '')) {
		    jQuery(this).val(ID);
		}
	});
	scanWazaL2();
}



function releaseAllWazaL2() {
	jQuery('.wazal2_list').val('');
	scanWazaL2();
}



function scanPref() {
    var SelectedPrefName = '未選択';
    if (jQuery('#pref_list option:selected').val()) {
	    var SelectedPrefName = jQuery('#pref_list option:selected').html();
	}
	jQuery('#searchAreaStatus').html("選択エリア &gt; " + SelectedPrefName);
	jQuery.cookie('nipponnowaza_pref_list', null);
	jQuery.cookie('nipponnowaza_pref_list', jQuery('#pref_list option:selected').val(), { expires: 7, path: '/' });
}



function toggleAnswerBox() {
	jQuery('#AnswerFormBox').toggle(300);
}



function submitSearchResultListForm(Action) {
	jQuery('#SearchResultListForm').attr('action', Action);
	jQuery('#SearchResultListForm').submit();
}




