JumAnalyzer = function(){ this.constructor.apply(this, arguments); }
JumAnalyzer.prototype = {	
	constructor: function( requestUrl, containerId, formId, statusId, triggerId, countryId, regionId ){
		
		this.temp = document.title;
		
		this.requestUrl		= requestUrl;
		this.containerId	= containerId;
		
		this.statusId		= statusId;
		this.formId			= formId;
		this.triggerId		= triggerId;
		this.countryId		= countryId;
		this.regionId		= regionId;
		
		if( !$( this.containerId ) )
		{
			alert( 'masuk' );
			var c = document.createElement( "div" );
			c.id = this.containerId;
			c.style.display = "block";
			
			//document.body.appendChild( c );
			document.body.insertBefore( c, document.body.firstChild );
		}		
	},
	
	show: function(){
		var url		= this.requestUrl;
		var options	= {
				method		: "get", 
				parameters	: "task=form-search",
				onFailure	: function(){
						alert( "There was a problem with the request" );
					},
				onLoading: function(){
						document.analyzer.showStatus( "Sending request. Please wait." );
					},
				onSuccess: function( response ){
						document.analyzer.hideStatus();
						
						var result = response.responseText.evalJSON();
						
						if( result.form ){
							$( document.analyzer.containerId ).update( result.form );
							
							setTimeout(function() {
									document.analyzer.makeCountry();
								}, .5 );
						} else
							alert( "Analyzer form doesn't found." );
					}	
			}
		
		new Ajax.Request( url, options );
	},	
	
	makeCountry: function(){
		
		var c = $(document.analyzer.countryId);
		c.options.length = 0;
		c.disabled = true;
		
		var url		= this.requestUrl;
		var options	= {
				method		: "get", 
				parameters	: "task=country-list",
				onFailure	: function(){
						alert( "There was a problem with the request" );
					},
				onLoading: function(){
						Event.stopObserving( document.analyzer.triggerId, "click", document.analyzer.checkDirectory, false );
						
						document.analyzer.showStatus( "Sending request. Please wait." );
						
						$(document.analyzer.statusId).update( "<small style=\"color:#990000\">Loading country. <strong>Please wait...</strong></small>" );
						$(document.analyzer.statusId).style.display = "block";
					},
				onSuccess: function(response){
						document.analyzer.hideStatus();
						
						$(document.analyzer.statusId).update();
						$(document.analyzer.statusId).style.display = "none";
						
						var result = response.responseText.evalJSON();
						if( result.total==0 ){
							alert( "Wedding country doesn't found." );
							return false;
						}
						
						var selector = $(document.analyzer.countryId);
							
						selector.options[0] = new Option("- - select a country - -", "");					
						for(var i=0; i<result.data.length; i++)
						{
							selector.options[i+1] = new Option( result.data[i].country_name, result.data[i].country_alias );
						}
						
						setTimeout(function() {
								Event.observe( document.analyzer.countryId, "change", document.analyzer.makeRegion, false );
								Event.observe( document.analyzer.triggerId, "click", document.analyzer.checkDirectory, false );
								selector.disabled = false;							
							}, .5 );
					}
			};
		
		new Ajax.Request( url, options );
	},
	
	makeRegion: function(){
		var r = $(document.analyzer.regionId);
		
		r.options.length = 0;
		r.disabled = true;
		r.up(0).style.display = "none"
		
		
		var country = $(document.analyzer.countryId).value;		
		if( country=="" )
		{
			return false;
		}
		
		var url		= document.analyzer.requestUrl;
		var options	= {
				method		: "get", 
				parameters	: "task=region-list&country=" + country,
				onFailure	: function(){
						alert( "There was a problem with the request" );
					},
				onLoading: function(){
						Event.stopObserving( document.analyzer.triggerId, "click", document.analyzer.checkDirectory, false );
						
						document.analyzer.showStatus( "Sending request. Please wait." );
						
						$(document.analyzer.statusId).update( "<small style=\"color:#990000\">Checking region. <strong>Please wait...</strong></small>" );
						$(document.analyzer.statusId).style.display = "block";
					},
				onSuccess: function(response){					
						Event.observe( document.analyzer.triggerId, "click", document.analyzer.checkDirectory, false );
						
						document.analyzer.hideStatus();
						
						$(document.analyzer.statusId).update();
						$(document.analyzer.statusId).style.display = "none";
						
						var result = response.responseText.evalJSON();
						if( result.total==0 ){
							return false;
						}
						
						var selector = $(document.analyzer.regionId);
						selector.options[0] = new Option("- - select a region - -", "");
						
						for(var i=0; i<result.data.length; i++)
						{
							selector.options[i+1] = new Option( result.data[i].region_name, result.data[i].region_alias );
						}
						
						selector.disabled = false;
						selector.up(0).style.display = "block";
					}
			};
		
		new Ajax.Request( url, options );
	},
	
	checkDirectory: function(){
		
		var country = $(document.analyzer.countryId).value;
		var region = $(document.analyzer.regionId).value;
		
		if( (country==0) && (region==0) )
		{
			alert("Please select country or region to search directory.");
			return false;
		}
		
		//var url = "/wedding/" + country + ( region ? "/" + region + "/" : "/" ) + "index.html";
		//var url = "/wedding/" + country + ( region ? "/" + region : "" ) + ".html";
		var url = "/wedding/" + country + ( region ? "/" + region : "" ) + "/";
		window.location.href= url;
	},
	
	showStatus: function( msg ){
		if( !msg )
		{
			msg = "Please wait for loading response";
		}
		
		this.waitCursor();
		
		document.title = this.temp + " \+ " + msg;
	},
	
	hideStatus: function(){
		this.defaultCursor();
		document.title = this.temp;
	},
	
	waitCursor: function(){
		document.body.style.cursor = 'wait';
	},
	
	defaultCursor: function(){
		document.body.style.cursor = 'default';
	}
	
}

document.analyzer = null;
Event.observe( window, "load", function(){
		var analyzer = new JumAnalyzer(
				"/ajax.php?output=json&action=search", 
				"search-box", 
				"search-form", 
				"status", 
				"trigger", 
				"country", 
				"region"
			);
		document.analyzer = analyzer;
		// document.analyzer.show();
		
		Event.observe( document.analyzer.countryId, "change", document.analyzer.makeRegion, false );
		Event.observe( document.analyzer.triggerId, "click", document.analyzer.checkDirectory, false );
		$( document.analyzer.countryId ).disabled = false;
	}, false);