var SearchBox = Class.create({
	initialize: function(div,st,sfl,rfl,sdata,pp,prf,ffunc) {
    	this.div = div;
    	this.st = st;
    	this.sfl = sfl;
    	this.rfl = rfl;
    	this.pp = pp;
    	this.prf = prf;
    	this.ffunc = ffunc;
    	this.sdata = sdata;
    	this.base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";    	
  	},
  	setData: function(sfl,sd) {
  		this.sfl = sfl;
  		this.sdata = sd;
  	},
	getResults: function(ob,od,offset) {  		
  		this.sdata.tpl = this.st;
  		this.sdata.sf = this.sfl;
  		this.sdata.rf = this.rfl;
  		this.sdata.ob = ob;
  		this.sdata.od = od;
  		this.sdata.offset = offset;
  		this.sdata.pp = this.pp;
  		var sfd = this.base64encode(Object.toJSON(this.sdata));
  		$(this.div).update(new Element('p', { style: 'text-align: center; padding: 20px;' }).update(new Element('img', { src: '/images/loader.gif' })));
  		var st = this;
  		new Ajax.Request("/_jsonsearch/"+sfd, {
  			method: "get",
  			onSuccess: function(transport) {			
  				var json = transport.responseText.evalJSON(true);
  				var list = new Element('div');
  				var df = st.rfl.split(",");
  				var oe = 0;
  				for(var i=0;i<json.results.length;i++) {
					var row;
					if(st.ffunc != undefined) {
						row = st.ffunc(json.results[i],df,oe,i);
						oe = oe+1;
						if(oe == 2) {
							oe = 0;
						}
					}
					else {
	  					row = new Element('div', { 'class': 'srrow', 'id': 'row_'+json.results[i].article_id });
	  					row.insert(new Element('div', { 'class': 'srcell image' }).update(new Element('a', { 'href': json.results[i].section_directory+json.results[i].article_id+'/'+json.results[i].href }).update(new Element('img', { src: '/siteimage/scale/90/70/'+json.results[i].picture.media_id+'.jpg' }))));
	  					for(var j=0;j<df.length;j++) {	
	  						var val = eval('json.results[i].'+df[j].toLowerCase());
	  						if(df[j] == 'article_date') {
	  							var d = new Date();
	  							d.setTime((val*1000));
	  							val = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear();
	  						}
	  						if(df[j] == 'article_title') {
	  							val = '<a href="'+json.results[i].section_directory+json.results[i].article_id+'/'+json.results[i].href+'">'+val+'</a>';
	  						}
	  						if(val == undefined) {
	  							val = '&nbsp;';
	  						}
	  						row.insert(new Element('div', { 'class': 'srcell '+df[j].toLowerCase() }).update(val));
	  					}
					}
					
  					list.insert(row);
  				}
  				$(st.div).update(list);
  				if(st.prf != undefined) {
  					st.prf();
  				}  
  				var pages = parseInt(json.total/st.pp)+1;
  				var cp = parseInt(offset/st.pp)+1;
  				var end = parseInt(json.total)-30;  				                 			 		
  				var cpbox = '<form id="jumpbox" name="jumpbox" class="jumpbox" onSubmit="changePage(\'jumpboxinput\','+st.pp+',\''+ob+'\',\''+od+'\'); return false;"><input id="jumpboxinput" name="jumpboxinput" class="jumpboxinput" type="text" value="'+cp+'"></form>';  				  				
  				var cpboxb = '<form id="jumpbox" name="jumpbox" class="jumpbox" onSubmit="changePage(\'jumpboxinputb\','+st.pp+',\''+ob+'\',\''+od+'\'); return false;"><input id="jumpboxinputb" name="jumpboxinput" class="jumpboxinput" type="text" value="'+cp+'"></form>';  				  				
  				$(st.div+'_c').update('Page '+cpbox+' of '+pages);	  
  				$(st.div+'_c_b').update('Page '+cpboxb+' of '+pages);					  				
  				if(cp >= 2) {		  				   
  					$(st.div+'_s').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',0)" }).update('&lt;&lt;'));
  					$(st.div+'_p').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+(offset-st.pp)+")" }).update('Previous'));
  					$(st.div+'_s_b').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',0)" }).update('&lt;&lt;'));
  					$(st.div+'_p_b').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+(offset-st.pp)+")" }).update('Previous'));  					
  				}
  				if(cp<pages) {
  					$(st.div+'_n').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+(offset+st.pp)+")" }).update('Next'));
  					$(st.div+'_e').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+end+")" }).update('&gt;&gt;'));
  					$(st.div+'_n_b').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+(offset+st.pp)+")" }).update('Next'));
  					$(st.div+'_e_b').update(new Element('a', { 'onclick': "sb.getResults('"+ob+"', '"+od+"',"+end+")" }).update('&gt;&gt;'));  					
  				}  				
  			},
  			onFailure: function(){ alert('Error Fetching Search Results!') }
  		});
  	},
  	base64encode: function(str) {
  	    var out, i, len;
  	    var c1, c2, c3;

  	    len = str.length;
  	    i = 0;
  	    out = "";
  	    while(i < len) {
  		c1 = str.charCodeAt(i++) & 0xff;
  		if(i == len)
  		{
  		    out += this.base64EncodeChars.charAt(c1 >> 2);
  		    out += this.base64EncodeChars.charAt((c1 & 0x3) << 4);
  		    out += "==";
  		    break;
  		}
  		c2 = str.charCodeAt(i++);
  		if(i == len)
  		{
  		    out += this.base64EncodeChars.charAt(c1 >> 2);
  		    out += this.base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  		    out += this.base64EncodeChars.charAt((c2 & 0xF) << 2);
  		    out += "=";
  		    break;
  		}
  		c3 = str.charCodeAt(i++);
  		out += this.base64EncodeChars.charAt(c1 >> 2);
  		out += this.base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  		out += this.base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
  		out += this.base64EncodeChars.charAt(c3 & 0x3F);
  	    }
  	    return out;
  	}
});

function changePage(div,pp,ob,od) {
	var page = document.getElementById(div).value;  	
	var jump = (page-1)*pp;
   if(!jump) { jump = 0; }	  				
	sb.getResults(ob,od,jump);
}
