function CGoogleSetSearch(){
	var a = cgoogle_searchbox_name;
	var b = document.getElementsByName(a);
	if(!b.length){
		var b = document.getElementsByName("q");
	}
	var i, o;
	for(i=0;i<b.length;i++){
		o = b[i];
		if(((o.type && o.type=="text") || o.getAttribute("type") == "text") && o.form.getAttribute("id") != "adminbarsearch"){
			try{
			  CGcreateOption(o);
			}
			catch(err){}
		}
	}
}
function CGcreateOption(o){
	with(o){
		value = "";
		setAttribute("autocomplete","off");
		o.parentNode.className = "CGParent";
		var d = document.createElement ("DIV");
		o.parentNode.insertBefore(d,o);
		d.innerHTML = "<div class='arrow1'></div><div class='arrow2'></div><div class=CGinner><input type=radio name=cgoogle id=cg1 value='' checked /><span>This Site</span> &nbsp; <input type=radio name=cgoogle id=cg2 value='!' /><span>www</span></div>";
		d.className = "CGoogle";
		d.style.marginTop = o.offsetHeight + "px";
		//o.parentNode.parentNode.className = "CGtest";
		if(document.getElementById("cse-search-results") && cgoogle_result_width){
			document.getElementById("cse-search-results").setAttribute('style',cgoogle_result_width);
		}
		var oldAttri = o.getAttribute('style');
		o.onblur = function(){
			if(value==""){
				o.setAttribute('style', oldAttri+';background-repeat:no-repeat !important; background-position: left center!important; background-image: url('+cg_plugin_url+'/cgoogle/google.png)!important;');
				//style.backgroundRepeat = "no-repeat";
				//style.backgroundPosition = "left center";
				//style.backgroundImage =  "url("+cg_plugin_url+"/cgoogle/google.png)";
			}
		}
		o.onfocus = function(){
			if(value==""){
				o.setAttribute('style', oldAttri+'; background-image: none!important;');
				
			}
		}
		o.form.onmouseover = function(){
			//var pos = CGgetPos(o);
			//d.style.top = pos.y + o.offsetHeight + "px";
			//d.style.left = pos.x + "px";
			
		}
		o.form.onmouseout = function(){
			
		}
		onblur();
	}
};
var CGAddEvent = function () {
	function w3c_addEvent(el, evt, fn, useCap) {
		el.addEventListener(evt, fn, useCap);
	}
	function ie_addEvent(el, evt, fn) {
		el.attachEvent('on' + evt, fn);
	}
	if (typeof window.addEventListener !== 'undefined') {
		return w3c_addEvent;
	} else if (typeof window.attachEvent !== 'undefined') {
		return ie_addEvent;
	}
} ();
function CGgetStyle(el,styleProp){
	var x = el;
	try{
		if (x.currentStyle)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle && document.defaultView.getComputedStyle(x))
			var y = document.defaultView.getComputedStyle(x).getPropertyValue(styleProp);
	}catch(e){
		return "";
	}
	return y;
}
function CGgetPos(el) {
	var pos = {x:0,y:0};
	if (document.getBoxObjectFor) {
		var bo = document.getBoxObjectFor(el);
		pos.x = bo.x;
		pos.y = bo.y;
	}
	else {
		var rect = el.getBoundingClientRect();
		pos.x = rect.left;
		pos.y = rect.top;
	} 
	var a = el;
	var x=0;y=0,absolute=false;
	while (a.parentNode) {
		y += a.scrollTop;
		x += a.scrollLeft;
		a = a.parentNode;
		if (!absolute && CGgetStyle(a,"position") == "absolute") {
			return CGgetPosX(el);
		}
	}
	
	pos.x += x;
	pos.y += y;
	return pos;
}
var CGgetPosX = function (obj) {
	var log = false;
	function w3c_getPos(obj) {
		var a, x = 0, y = 0;
		a = obj;
		x += a.offsetLeft;
		y += a.offsetTop;
		if (typeof (a.offsetParent) != "unknown" && typeof (a.offsetParent) != "undefined") {
			while (a.offsetParent) {
				a = a.offsetParent;
				if (CGgetStyle(a,"position") == "absolute") {
					break;
				}
				x += a.offsetLeft;
				y += a.offsetTop;
				if (log) $("log").innerHTML += ">>" + a.nodeName + ":" + x;
			}

			a = obj;
			while (a.parentNode) {
				a = a.parentNode;
				if (a.nodeName == "BODY") {
					break;
				}
				if (CGgetStyle(a,"position") == "absolute") {
					break;
				}
				if (parseInt(CGgetStyle(a,"margin-top"))) {
					y += parseInt(CGgetStyle(a,"margin-top")); 
				}
				y -= a.scrollTop;
				if (log) $("log").innerHTML += ">>" + a.nodeName + ":" + x;
			}

		}
		return { x: x, y: y };
	}
	function ie_getPos(obj) {
		var a, x = 0, y = 0;

		a = obj;
		x += a.offsetLeft;
		y += a.offsetTop;
		while (a.offsetParent) {
			a = a.offsetParent;
			if (CGgetStyle(a,"position") == "absolute") {
				break;
			}
			x += a.offsetLeft;
			y += a.offsetTop - a.scrollTop;
		}
		return { x: x, y: y };
	}
	if (typeof window.addEventListener !== 'undefined') {
		return w3c_getPos;
	} else if (typeof window.attachEvent !== 'undefined') {
		if (navigator.appVersion.indexOf("MSIE 8")) {
			return w3c_getPos;
		} else {
			return ie_getPos;
		}
	}
} ();
CGAddEvent(window,"load",CGoogleSetSearch);

