//on dom ready...
window.addEvent('domready', function() {

        searchInputSetup();
        categorySwitchSetup();
        locationSwitchSetup();

	/* ajax replace element text */
	$$('.ajaxLoad').each(function(el) {
            ajaxSetup(el);
        });

        $$('.sObjectName').each(function(el) {
                    el.set('opacity', '.60');
        });

        $$('.transparent').each(function(el) {
             el.set('opacity', '.80');
        });
});

function searchInputSetup() {
    if($('search-input') == null) return; // if item is not present on the page
    var value = "zadejte výraz..."
    $('search-input').set('value', value);

    $('search-input').addEvent('click', function(event) {
       if($('search-input').get("value") == value)  {
            $('search-input').set('value', '');
       }
    });

     $('search-input').addEvent('keyup', function(event) {
        event.stop();
        if (this.value.length >= 3) {
            askForSearchResults(this.value);
        } else {
            hideSearchHintWindow();
        }
    });
}

function ajaxSetup(obj) {

    obj.addEvent('click', function(event) {
		//prevent the page from changing
		event.stop();
		//make the ajax call, replace text
		var req = new Request.HTML({
			method: 'get',
			url: obj.get('href'),
			data: {'do' : '1'},
			onRequest: function() {
                            //showLoading();
                            $('info-window').set('html', 
                            '<img src="/images/loading.gif" alt="loading" \n\
                                  class="loading" title="loading" />');
                            displayInfoWindow(obj.get("rel"));
                        },
			update: $('info-window'),
			onComplete: function(response) { 
                            //obj.setStyle('background','#fff');
			}
		}).send();
	});
}

function askForSearchResults(query) {

		//event.stop();
		//make the ajax call, replace text
		var req = new Request.HTML({
			method: 'get',
			url: 'http://www.cestovatko.cz/index.php/object-search/'+query,
			data: {'do' : '1'},
			onRequest: function() {
                            //showLoading();
                            $('search-hint-window').set('html',
                            '<img src="/images/loading.gif" alt="loading" \n\
                                  class="sloading" title="loading" />');
                            
                            displaySearchHintWindow($('search-input'));
                        },
			update: $('search-hint-window'),
			onComplete: function(response) {
                            //obj.setStyle('background','#fff');
			}
		}).send();
}
