jQuery.noConflict();
jQuery(document).ready(function(){
	// ここでは、$はprototypeの動作をします。
	// jQueryオブジェクトとしての$は一切使えず、その場合は$()ではなくjQuery()と表記する必要があります。
});

//Event.observe(document, "dom:loaded", initLandingPage);
jQuery(initLandingPage);

var landingForm = null;
function initLandingPage()
{
    landingForm = document.createElement("form");
    landingForm.action = "/reserve/reserve.php";
    landingForm.method = "post";
    document.body.appendChild(landingForm);
    
    landingForm.procode = document.createElement("input");
    landingForm.procode.name  = "procode";
    landingForm.procode.type  = "hidden";
    landingForm.procode.value = "";
    landingForm.appendChild(landingForm.procode);
    
    //var query = location.search.toQueryParams();
    var query = {};
	(function(){
		var params = location.search.substring(1) || "";
		if (params) {
			var pairs = params.split(/&/);
			jQuery.each( pairs, function(i, v){
				var pair = v.split(/=/);
				query[ pair[0] ] = pair[1];
			});
		}
	})();
    if (query.procode)
    {
        landingForm.procode.value = query.procode;
        sendProcodeByAjax('/js/MakeProcodeSession.php', query.procode);
    }
    
    landingForm.landing_url = document.createElement("input");
    landingForm.landing_url.name  = "landing_url";
    landingForm.landing_url.type  = "hidden";
    landingForm.landing_url.value = location.pathname;
    landingForm.appendChild(landingForm.landing_url);
    
    landingForm.request_url = document.createElement("input");
    landingForm.request_url.name  = "request_url";
    landingForm.request_url.type  = "hidden";
    landingForm.request_url.value = document.referrer;
    landingForm.appendChild(landingForm.request_url);

    landingForm.request_sid = document.createElement("input");
    landingForm.request_sid.name  = "sid";
    landingForm.request_sid.type  = "hidden";
    landingForm.request_sid.value = query.sid || "";
    landingForm.appendChild(landingForm.request_sid);
    
    /*
    $$("a").each(function(obj)
    {
        if (obj.href.match("reserve/reserve.php"))
        {
            obj.onclick = gotoForm;
        }
    });
    */
    jQuery.each( jQuery("a"), function(i, obj){
        if (/reserve\/reserve\.php/i.test( jQuery(obj).attr("href") )) {
            jQuery(obj).click(gotoForm);
        }
    })
}

/**
 * @return false 必ず false が返る
 */
function gotoForm()
{
    landingForm.submit();
    this.blur();
    return false;
}

function sendProcodeByAjax(url, procode){
	try {
		var msec = (new Date()).getTime();
		/*
		new Ajax.Request(url, {
			method: "get",
			parameters: "cache=" + msec + "&procode=" + procode,
			onSuccess: function(x){
				//alert(x.responseText);
			},
			onFailure: function(x){
				//alert("ajax error");
			}
		});
		*/
		var result = jQuery.ajax( {
			type : "get",
			url : url + "?cache=" + msec + "&procode=" + procode,
			dataType : "html",
			success : function(data){
				//alert(data);
			}
		});
	} catch (e) {}
}
