/* Onload */
function OnLoad() {
  if(content_frame.location!=url&&url!=location.href) {
    content_frame.location.replace(url);
  }
}
/* Initial branding at onload. Could have put this into 
   the OnLoad function however wanted to keep it seperate
	 since it was specific to a piece of functionality */
function initLoad() {
  GSearch.getBranding(document.getElementById("branding"));
}

function delineate(str) {
  /*
   * following is required since a link from a Adwords ad will
   * add a gclid tak value for analytics. This is fine however
   * our code to ensure that home page is alway loaded must take
   * this into consideration and ignore it
   */
  if(str.indexOf("?gclid=") > 0 ) {
    return("http://www.leightonobrien.com/c_home.htm");
  }
  theleft = str.indexOf("=") + 1;
  theright = str.indexOf("&");
  return(str.substring(theleft, str.length));
}

function Execute(search) {
    var webSearch = new GwebSearch();
		
    // Restrict the search
    webSearch.setSiteRestriction( "www.leightonobrien.com" );
    webSearch.setUserDefinedLabel( "Leighton OBrien Search Results" );

    // execute an inital search
    webSearch.setSearchCompleteCallback(null, ShowResults, [webSearch]);

    webSearch.execute(document.getElementById(search).value);
		document.getElementById(search).value = "";
}

function ShowResults(webSearch) {
	var frameDocument = document.all.content_frame.contentWindow.document.open();
	var frameHTML = "<html><title><\/title>" +
	                "<link rel='stylesheet' type='text/css' href='css/drop_down_menu.css' />"+
	                "<body><div class='gs_search_heading'>"+
                  "Search Results<\/div><br>" +
									"<div id='MyGoogleResults'></div>"+
									"</body></html>";
  frameDocument.write(frameHTML);	
  // show web results
  var webResults = frameDocument.getElementById("MyGoogleResults");
  if (webSearch.results && webSearch.results.length > 0) {
    for (var i = 0; i < webSearch.results.length; i++) {
      var result = webSearch.results[i];
			var resultHTML = result.html.cloneNode(true);
			var myTitle   = getElement(resultHTML, "gs-title", "DIV")?
			                getElement(resultHTML, "gs-title", "DIV", true):
											"Could not find Title Element";
											
			var mySnippet = getElement(resultHTML, "gs-snippet", "DIV")?
			                getElement(resultHTML, "gs-snippet", "DIV", true):
											"Could not find Snippet element";
											
			var myURL     = getElement(resultHTML, "gs-visibleUrl gs-visibleUrl-long", "DIV")?
			                getElement(resultHTML, "gs-visibleUrl gs-visibleUrl-long", "DIV", true):
											"Could not find URL Element";			

      webResults.innerHTML += myTitle + mySnippet + myURL+"<br>";
			updateDynamicHTMLATags(frameDocument);
    }
  }
	//alert("FRAME BODY: ["+webResults+"]");
	webSearch.clearResults();
	/*
	
	There is a known issue with the callback to google within firefox in that 
	it will say "transferring data from www.google.com" at the bottom even
	though the transaction is finalised 
	
	I tried to stop the load however then it will not apply the styling from
	the css file ????
	
	No choice but to leave it at this time
	
	*/
	
	/*
	setTimeout(null,5000);
	try {
	   document.execCommand('Stop')
	} catch(e) {
	   try {
		    window.stop();
		 } catch(e) {
		   //do nothing
		 }
	}*/
}


function updateDynamicHTMLATags(frameDocument){
  hrefArray = frameDocument.getElementsByTagName('a');
  var cl = new Array()
  for(i=0;i<hrefArray.length;i++){
    var myAttribute = frameDocument.createAttribute("target");
    myAttribute.nodeValue = "content_frame";
		hrefArray[i].setAttributeNode(myAttribute);
  }
}


function getElement(parentElement, elementClass, elementType, wrapDiv) {

  for (var i=0; i < parentElement.childNodes.length; i++) {
    if(parentElement.childNodes[i].tagName.match(elementType) &&
		   parentElement.childNodes[i].className.match(elementClass)) {
		 
			 if(wrapDiv) {
			   return "<div class='my"+elementClass+"'>" +
					      parentElement.childNodes[i].innerHTML +
					 		  "</div>";
			 }
			 else {
        return parentElement.childNodes[i].innerHTML;
			 }
    }
  }
  return false;			
}


