/**
 * Glowny skrypt serwisu
 */

function wmcPage()
{
	// obecny adres strony
	this._url = null;
	
	// pozycja fotki
	this._position = 0;
	
	// tablica wiadomosci i errorow
	this._err = [];
	this._msg = [];
	
	
	// czy chmurka otwarta
	this._cloud_open = false;
	
	// init odpalany wylacznie przez domready
	this.init = function()
	{
		if( typeof(__current_page) != 'undefined' )
			if( eval('typeof wmcPagex.'+__current_page) == 'function' )
				eval('typeof wmcPagex.'+__current_page+'()');					

		this._search();
	};
	
	// binduje wyszukiwarkes
	this._search = function()
	{
		var input_value = '';
		var x = $('search_input').getCoordinates();
		
		var search_timer;
		
		$('submit_search').addEvent('click', function() {
			wmcPagex.default_search( $('search_input').value );
		});
		
		$('search_input').addEvent('keyup', function(e) {
			//if( e.key.test(/^[a-z|A-Z|0-9]$/) )
			var obj = this;
			
			if( e.key == 'enter')
				wmcPagex.default_search( this.value );
			
			clearTimeout(search_timer);
			
			search_timer = (function() {
				if( obj.value.length > 0 )
				{
					var req = new Request.JSON({
						url: __search_url+',word,'+escape( obj.value ),
						onSuccess: function(data) {
							//wmcPagex.info_cloud( data.length );
							wmcPagex._clearSearchResults();
							$('search_results').setStyle('display','block');
							for(var i in data)
							{
								console.log(data[i]);
								var copy = $('first_result').clone().inject('search_results');
								copy.setStyle('display','block');
								copy.getElements('a').setProperty('href', data[i].url.replace(/\&amp;/g,'&'));
								if( typeof(data[i].country) != 'undefined' )
									data[i].title += ' ( '+data[i].country+' )';
								copy.getElements('a').set('text', data[i].title);
							}
						},
						onRequest: function() {
							//wmcPagex.info_cloud( __lang_cloud_loading , true);
						}
					}).send();
				}
			}).delay(600);
		});
		
		
		document.addEvent('click', function(e) {
			wmcPagex._clearSearchResults();
		});
		
		
	};
	
	this.default_search = function( value )
	{
		window.location.href = __default_search_url + ',word,'+escape(value);
	};
	
	this._clearSearchResults = function()
	{
		$('search_results').getElements('div').each(function(el) {
			if( el.getProperty('id') != 'first_result' )
				el.destroy();
		});
	}
};

var wmcPagex = new wmcPage();


