$(document).ready(function() {

    $("div.prefSelectDiv #prefectures, div.prefSelectDiv #prefCities").bind('change', null, function() {
        var eTarget = this.getAttribute('id');
        if (eTarget == 'prefCities' || this.selectedIndex > 0) {
            if (eTarget == 'prefectures') {
                $("#prefCities").val(0);
            }
            this.form.submit();
        }
    });

    if ($("#prefCities").length) {
        $("#prefCities").val(prefCit);
    }

});

var img = new Image(16, 16);
img.src="/images/majax-loader.gif";
					
function formatSAItem(row, pos, max, value) {
	if (1 == pos && 0 == row[0]) {
		$("#area_search").val('');
		//$("#selPrefCity").val('');
		return "<em>" + row[1] + "!</em>";
	} else {
		return row[0] + ' <span class="roleName">(' + row[1] + ')</span>';
	}
}
function formatSAResult(row, pos, max) {
	if (0 != row[0])
		return row[0];
	else
		return '';
}

function checkAreaS(sender) {
	if ('' == sender.area_search.value) 
		return false;
	else
		return true;
}

function initAreaSearch() {
  $("#area_search").autocomplete($("#area_search").attr('rel'), 
		{
			minChars: 2,
			max: 10,
			autoFill: true,
			selectFirst: true,
			formatItem: formatSAItem,
			formatResult: formatSAResult
		}
	).result(function(event, data, formatted) {
				if (typeof(data) == "object") {
						if ('' == $("#area_search").val() || '0' == $("#area_search").val()) {
							$("#area_search").val('');
							$("#selPrefCity").val('');
						} else
							$("#selPrefCity").val(data[2]+'|'+data[3]);
				}
			});
}

function initRadioSelect() {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;

    var trigger = $('#radioTrigger');
    var popup = $('#radioSelPopup').css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        var pos = trigger.offset();
        var width = trigger.outerWidth();

        // reset position of popup box
        popup.css({
          /*top: pos.top - 38 + 'px', */
          top: pos.top - 62 + 'px',
          left: width - 1 + 'px',
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          left: '+=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
}

function launchRadioSt(sender) {
    //Re-use same window Bloody IE doesn't like spaces in window name!!!!!!!!!!!!!!!!!'
    window.open(sender.href,"rdWin","toolbar=0,resizable=0,scrollbars=no,menubar=0,location=no,status=no,top=50,left=50,height=300,width=300");
    if (window.focus) {
        window.focus();
    }

    return false;
}

function openWindow(sender) {
    window.open(sender.href, "Tutorial", "toolbar=0,resizable=0,scrollbars=no,menubar=0,location=no,status=no,top=50,left=50,height=670,width=820");
    return false;
}

function link_to_self(trg) {
    window.location.href = trg;
}

function link_to_blank(trg) {
    window.open (trg, "View", "");
}

