var step = 0;
var pxperstep = 0;
var mxsteps = 5;
var initscrollto = 0;

window.addEvent('load',function()
{
	$$("#prefbtn, #home").set("target","_top");
	$("q").set("value",term);	
	
	
	var adbox = new Element("div",{
		'class' : 'search_ad_box'
	}).inject(document.body);
	
	

	/*
	for(var i = 0, o; o = adlinks[i]; i++)
	{
		new Element("a",{
			
			'href' : o.target.replace("@@query@@",term),
			'text' : o.text,
			events : {
				click : function($e)
				{
						$e.stop();
						new Request.JSON({
							url : "/json/searchadclick/"+adlinks[this.retrieve('adidx')].id,
							adidx : this.retrieve('adidx'),
							onComplete : function($j)
							{
									var ad = adlinks[this.options.adidx];
									try{
										pageTracker._trackPageview('/search-ad-click/'+encodeURIComponent(ad.text));
									} catch($ex){}
									window.location = ad.target.replace("@@query@@",term);
							}
						}).get();
				}	
			}
		}).store("adidx",i).inject(adbox);	
	}
*/
/*
	new Element("a",{
		id : 'wixilink',
		'href' : '#',
		styles : {
			fontWeight : 'bold'
		},
		'text' : term + ' streaming',
		'target' : 'tbox',
		'events' : {
			click : function($e)
			{
				$e.stop();
				try{
					pageTracker._trackPageview('/search-ad-click/wixi');
				} catch($ex){}
					
				parent.frames[1].location = 'http://wixi.com/signup/wixi_create_first.php?ref=toto&id_subaff=18';
			}
		}
	}).inject(adbox);
*/	

	new Element("a",{
		id : 'wixilink',
		'href' : '#',
		styles : {
			fontWeight : 'bold'
		},
		'text' : '',
		'target' : 'tbox',
		'events' : {
			click : function($e)
			{
				$e.stop();
				try{
					pageTracker._trackPageview('/search-ad-click/yotta');
				} catch($ex){}
					
				parent.frames[1].location = 'http://www.newshosting.com/en/index.php?kbid=13405&m=47';
			}
		}
	}).inject(adbox);

	jsites = JSON.decode(jsites);
	sorder = JSON.decode(sorder);

/*
	sorder = sorder.slice(0,4).extend(["scrubbu"]).extend(sorder.slice(4,sorder.length));
	
	
	jsites.scrubbu = {
		"id":"100000",
		"slug":"scrubbu",
		"name":"Scrubbu",
		"url":"http:\/\/go.scrubbu.com\/",
		"smode":"get",
		"scode":"a=1618&p=t3&smessage=@@query@@",
		"surl":"http:\/\/go.scrubbu.com\/download.php?"
	};

*/	
	
	var aw = window.getSize();
	//43 on #sites chrome fix!
	//left 326
	//right 415

	var tw = aw.x - 326 - 415;
	tw = "1050";
	
	$("s").addEvent("click",function($e){
		$e.stop();
		search();
	});
	
	$("q").addEvent("keyup",function($e){
		if($e.key == "enter")
		{
					}
		else
		{
			t = this.get("value");
			$$("#container a").each(function(item)
			{
				item.set('href','/rew/'+item.get('id').substring(4) +'/'+t)	
			},this);
		
		}
	});
	


			
			

			

			//and search
			search();
			$("sites").setStyle("width",tw);
			
	
			

		
			var content = $("container").getSize().x;
			var maxx = content;
			
			if(maxx < 0)return;
			
			pxperstep = maxx / mxsteps;
			
			if(initscrollto > tw)
			{
				step = Math.ceil((initscrollto -tw) / pxperstep);
				step = step < mxsteps?step+1:step;
				$("container").set("tween",{duration:200}).tween("left",step * -pxperstep);
			}
			
		
				
	

	
	
	
	
	
});

function exit()
{
	ESCAPE = true;
	search();
	return false;
}


function search()
{
	var site = jsites[slug];
	term = $("q").get('value');
	
	var form = new Element("form",{
		'target' : ESCAPE?'_top':'tbox',
		'method' : site.smode,
		'action' : site.surl.replace("@@query@@",slug=="scrubbu"?b64e(term):term)
	});
	
	var fields = site.scode.split("&");
	
	for(var i = 0, f; f = 0; i++)
	{
		f = f.split("=");
		new Element('input',{
			
			'type' : 'hidden',
			'name' : f[0],
			'value' : f[1] == '@@query@@'?slug=="scrubbu"?b64e(term):term:f[1]
			
		})
	}

	
}



function b64e( data ) {
   
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
 
    if (!data) {
        return data;
    }
 
//    data = utf8_encode(data+'');
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}

