IncludeJavaScript('lib/rpc.js');

var promotion_actions = false;

function navigation(selector, duration) {
    $(selector).hover(function(){
        $(this).find('a').stop().animate({opacity:0},duration);
    }, function(){
        $(this).find('a').stop().animate({opacity:1},duration);
    });
}

var data = [
	{ name: "Peter Pan", to: "peter@pan.de" },
	{ name: "Molly", to: "molly@yahoo.com" },
	{ name: "Forneria Marconi", to: "live@japan.jp" },
	{ name: "Master <em>Sync</em>", to: "205bw@samsung.com" },
	{ name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" },
	{ name: "Don Corleone", to: "don@vegas.com" },
	{ name: "Mc Chick", to: "info@donalds.org" },
	{ name: "Donnie Darko", to: "dd@timeshift.info" },
	{ name: "Quake The Net", to: "webmaster@quakenet.org" },
	{ name: "Dr. Write", to: "write@writable.com" }
];

// searchList class
function searchList( params ) {
    if(!params) params = {};
	this._cols                      = params['cols'];               // lista, columny z prezentacja danych
	this._input                     = params['input']; 				// id inputa
	this._src                       = params['src'] ; 				// rpc lub tablica / jesli żrodłem nie jest string rpc, to tablica
    this._delay                     = params['delay'];              // opóźnienie po jakim ma nastąpić wyszukanie frazy
	this._gdata = true;				
	this._df1 = null;
	this._df2 = null;
	this._nfound = false;
    this._which = this._cols.split(',');
	var self = this;

    //alert(this._which[0]);
	// akcje
	this.slist = function() {
		$(this._input)
		.keydown(function(){
			if(self._gdata == false) clearTimeout(self._df1);
			self._gdata = true;
		})
		.keyup(function(){
			if(self._gdata != false) {
				
					self._df1 = setTimeout(function() {
					self.getDataList( $(self._input).val(), self._src, false );
					self.printList();
					}, self._delay);
				
			}
			self._gdata = false;
		});
	}

	// pobierz dane
	this.getDataList = function( ss, wf, def ) {
		if(wf == 'rpc') {
            $(this._input).addClass('loader');
            var rpcService = new rpc.ServiceProxy("/services/rpc-json.php", {asynchronous:false});
            var rpcResponse =  rpcService.search(ss);
            this._newData =  rpcResponse;
        }
		else {
            this._newData = wf;
        }
		
	}

    // wyświetl dane
	this.printList = function() {
		if(promotion_actions){
		    $(this._which[0]+','+this._which[1]).empty();
		    $(this._which[0]+','+this._which[1]).append('<ul></ul>');
		    for(i=0; i<self._newData.length; i++){
		       asd = '<li><span>&raquo; </span><a href="'+self._newData[i]['link']+'">'+self._newData[i]['category_name']+'</a></li>';
		       if(i<7) $(this._which[0]+' ul').append(asd);
		       else $(this._which[1]+' ul').append(asd);
		    }
		}
		else{
		    $(this._which[0]+','+this._which[1]).empty();
		    $(this._which[0]+','+this._which[1]).append('<ul></ul>');
		    for(i=0; i<self._newData.length; i++){
		       asd = '<li><span>&raquo; </span><a href="'+self._newData[i]['link']+'">'+self._newData[i]['category_name']+'</a></li>';
		       if(i<(self._newData.length/2)) $(this._which[0]+' ul').append(asd);
		       else $(this._which[1]+' ul').append(asd);
		    }
		}
		
		$(this._input).removeClass('loader');
	}
}

$(document).ready(function(){
	navigation('ul#navigation li',100);
    var d = new searchList({
        cols: '.ldl_left, .ldl_right',
        input: '#findBranch',
        src: 'rpc',
        delay: 200
    });
    d.slist();
});


