/*
 * JS form libraries for Piano Transport System Inc.
 *
 * Copyright (c) 2010 Baby-J Inc.
 * @company http://baby-j.com
 * @author Hayato Tomoda
 * @link http://tomodian.net/
 */

function pretty_radio (id) {
	var $dataset = $(id);
	$dataset.addClass("radiolist");
	$(".radiolist p").append('<a class="radio-select" href="#">Select</a><a class="radio-deselect" href="#">やり直す</a>');
	$(".radiolist .radio-select").click(
	  function(event) {
	    event.preventDefault();
	    var $boxes = $(this).parent().parent().children();
	    $boxes.removeClass("selected");
	    $(this).parent().addClass("selected");
	    $(this).parent().find(":radio").attr("checked","checked");
	  }
	);

	$(".radiolist .radio-deselect").click(
	  function(event) {
	    event.preventDefault();
	    $(this).parent().removeClass("selected");
	    $(this).parent().find(":radio").removeAttr("checked");
		
	  }
	);
}

function pretty_checkbox (id) {
	var $dataset = $(id);
	$dataset.addClass("checklist");
	$(".checklist p").append('<a class="checkbox-select" href="#">Select</a><a class="checkbox-deselect" href="#">外す</a>');
	
	$(".checklist .checkbox-select").click(
	  function(event) {
	    event.preventDefault();
	    $(this).parent().addClass("selected");
	    $(this).parent().find(":checkbox").attr("checked","checked");
		$.cookie($(this).parent().find(':checkbox').attr('name'), 1);
	  }
	);

	$(".checklist .checkbox-deselect").click(
	  function(event) {
	    event.preventDefault();
	    $(this).parent().removeClass("selected");
	    $(this).parent().find(":checkbox").removeAttr("checked");
		$.cookie($(this).parent().find(':checkbox').attr('name'), null);
	  }
	);
}

function restore_selected_elements () {
	// if ($.cookie('input[name="chk_cl"]')) {
	// 	alert('DSF');
	// };
}

function tab_wizard () {
	// get container for the wizard and initialize its exposing
	var wizard = $("#wizard");
	
	// enable tabs that are contained within the wizard
	$("ul.tabs", wizard).tabs("div.panes > div").history();

	// get handle to the tabs API
	var api = $("ul.tabs", wizard).tabs(0);

	// "next tab" button
	$(".next", wizard).click(function() {
		api.next();
		return false;
	});

	// "previous tab" button
	$(".prev", wizard).click(function() {
		api.prev();
		return false;
	});
}

function toggle_detail (obj) {
	var detail = obj.siblings('.detail');
	
	if (obj.attr('checked')) {
		// obj.siblings('.detail').show();
		detail.show();
	}
	else {
		// obj.siblings('.detail').hide();
		detail.hide();
		
		// when input.name matches 'txt_Grand'
		if (detail.find('input:text').attr('name') == 'txt_Grand') {	
			detail.find('input:radio').attr('checked', '');
			$('#txt_Grand').attr('value', '');
		}
		else {
			detail.find('input:text').attr('value', '');
		}
	}
}

function apply_input (id) {
	var obj = $('#' + id);
	
	if (obj.attr('checked')) {
		obj.siblings('.detail').show();
	}
}

function ajax_addresses () {
	//ADDR Ajax2
	$('#from_zip').bind('keyup', function() {
		AjaxZip2.zip2addr('txt_RecZip1','txt_RecAdd','txt_RecAdd','txt_RecZip2');
	});
	
	$('#from_has_stonesteps:radio').click(function() {
		$(this).siblings('.detail').toggle();
	});
	
	$('#from_no_stonesteps:radio').click(function() {
		$('#from_has_stonesteps:radio').siblings('.detail').attr('value', '');
		$('#from_has_stonesteps:radio').siblings('.detail').hide();
	});
	
	$('#to_zip').bind('keyup', function() {
		AjaxZip2.zip2addr('txt_SndZip1','txt_SndAdd','txt_SndAdd','txt_SndZip2');
	});
	
	$('#to_has_stonesteps:radio').click(function() {
		$(this).siblings('.detail').toggle();
	});
	
	$('#to_no_stonesteps:radio').click(function() {
		$('#to_has_stonesteps:radio').siblings('.detail').attr('value', '');
		$('#to_has_stonesteps:radio').siblings('.detail').hide();
	});
}

function autosave_form () {
	// $('form *').autosave();
	$("form *").autosave();
}

function restore_elements () {
	remember('[name=txt_Name');
}
