var tab = {
	init: function() {
		var tabs = this.setup.tabs;
		var pages = this.setup.pages;

		for(i=0; i < pages.length; i++) {
			if(i !== 0) pages[i].style.display = 'none';
			tabs[i].onclick = function(){ tab.showpage(this); return false; };
		}
	},
	showpage: function(obj){
		var tabs = this.setup.tabs;
		var pages = this.setup.pages;
		var num;

		for(num=0; num < tabs.length; num++) {
			if(tabs[num] === obj) break;
		}

		for(var i=0; i < pages.length; i++) {
			if(i == num) {
				pages[num].style.display = 'block';
				tabs[num].className = 'present';
			}
			else{
				pages[i].style.display = 'none';
				tabs[i].className = null;
			}
		}
	}
}

tab.setup = {
	tabs: document.getElementById('news_tab').getElementsByTagName('li'),

	pages: [
		document.getElementById('news_all'),
		document.getElementById('news_international'),
		document.getElementById('news_volunteer'),
		document.getElementById('news_volunteer2'),
		document.getElementById('news_eco'),
		document.getElementById('news_un'),
		document.getElementById('news_entrepreneur')
	]
}

tab.init();

function LoadNewsBar() {
  var newsBar;
  var options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "総合",
    horizontal : false,
    autoExecuteList : {
      executeList : ["国際協力 OR ボランティア OR 環境 OR 国連 OR 社会起業家 OR ソーシャルアントレプレナー OR ソーシャルビジネス OR CSR OR (学生 AND ボランティア) OR (学生  AND 国際協力)"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_all"), options);

  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "国際協力",
    horizontal : false,
    autoExecuteList : {
      executeList : ["国際協力 OR 青年海外協力隊 OR 海外ボランティア"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_international"), options);
    
  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "ボランティア",
    horizontal : false,
    autoExecuteList : {
      executeList : ["ボランティア"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_volunteer"), options);

  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "環境",
    horizontal : false,
    autoExecuteList : {
      executeList : ["環境 OR エコ"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_eco"), options);

  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "国連",
    horizontal : false,
    autoExecuteList : {
      executeList : ["国連"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_un"), options);

  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "ソーシャルビジネス",
    horizontal : false,
    autoExecuteList : {
      executeList : ["社会起業家 OR 社会起業 OR 社会企業 OR ソーシャルアントレプレナー OR ソーシャルアントレ OR ソーシャルビジネス OR CSR"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_entrepreneur"), options);
  
  options = {
    largeResultSet : true,
    resultStyle : GSnewsBar.RESULT_STYLE_COMPRESSED,
    title : "学生ボランティア）",
    horizontal : false,
    autoExecuteList : {
      executeList : ["(学生 AND ボランティア) OR (学生  AND 国際協力)"]
    }
  }

  newsBar = new GSnewsBar(document.getElementById("news_volunteer2"), options);
    
}
GSearch.setOnLoadCallback(LoadNewsBar);



GSnewsBar.prototype.searchComplete = function(fromListItem) {
	var ns;
	var cacheResults = false;
	var currentListItem = null;
	
	// ニュース表示件数(この値を変更)
	var NUM_NEWS = 6;
	
	
	if (fromListItem) {
		currentListItem = this.executeList[this.currentIndex];
		if (currentListItem.cacheCount == 0) {
			cacheResults = true;
			currentListItem.results = new Array();
		}
		ns = this.ns;
	} else {
		ns = this.nsBypass;
	}
	
	if ( ns.results && ns.results.length > 0) {
		this.cssSetClass(this.barBox, this.getBarBoxClass(true));
		this.removeChildren(this.resultsBox);

		if (!this.verticalMode) {
			// for horizontal mode, nuke the old results
			// and reset currentResultIndex
			this.results = new Array();
			this.currentResult = 0;
		}

		// iterate over the results and capture the .html node
		// and append into the resultBox, OR just capture so that
		// we can fade it in/out
		
    // split result box
	  var elResultBox1 = document.createElement('div');
		elResultBox1.setAttribute('class', 'resultsBox1_gsnb');
		//var elResultBox2 = document.createElement('div');
		//elResultBox2.setAttribute('class', 'resultsBox2_gsnb');
		this.resultsBox.appendChild(elResultBox1);
		//this.resultsBox.appendChild(elResultBox2);
		var tmpResultBox = elResultBox1;
		
		var images = [];
		for (var i = 0; i < ns.results.length && i < NUM_NEWS; i++) {
			// if we are listItem based search, then cache results
			if (cacheResults) {
				currentListItem.cacheCount = 1;
				currentListItem.results.push(GSnewsBar.cloneObject(ns.results[i]));
				// reset error count based on sucessful search
				currentListItem.errorCount = 0;
			}

			var res = ns.results[i];
			if (this.verticalMode) {
				var resultDiv = this.createDiv(null, this.CL_RESULTDIV);
				resultDiv.className = resultDiv.className + " no-" + (i+1);
				//if(i == 4) tmpResultBox = elResultBox2;
				var node = res.html.cloneNode(true);
				//this.resultsBox.appendChild(resultDiv);
				tmpResultBox.appendChild(resultDiv);
				resultDiv.appendChild(node);
				if(images.length < 2 && res.image){
					images.push(res.image);
				}
			} else {
				this.results[i] = res;
			}
		}

		// image
		if(images){
			var elImgBox = document.createElement('div');
			elImgBox.setAttribute('class', 'resultsImageBox_gsnb');
			
			for(var i = 0; i < images.length; i++){
			  var bugfix = document.createTextNode('　');
			  elImgBox.appendChild(bugfix);
				
			  var elAnc = document.createElement('a');
			  elAnc.setAttribute('href', images[i].originalContextUrl);
			  elAnc.setAttribute('target', '_blank');
			  
			  var elImg = document.createElement('img');
			  elImg.setAttribute('src', images[i].url);
			  elImg.setAttribute('height', 75);
			  elImg.setAttribute('width', 75*images[i].tbWidth/images[i].tbHeight);
		  	
			  elAnc.appendChild(elImg);
			  elImgBox.appendChild(elAnc);
			  
			  var space = document.createTextNode('  ');
			  elImgBox.appendChild(space);
			}
			
			//this.resultsBox.parentNode.appendChild(elImgBox);
			this.resultsBox.insertBefore(elImgBox, elResultBox1);
		}

		// start the fadein, fadeout sequence
		if (!this.verticalMode) {
			this.linkContainer = this.createDiv(null, this.linkContainerClass);
			this.resultsBox.appendChild(this.linkContainer);
			this.link = document.createElement("a");
			this.link.target = this.linkTarget;
			this.snippet = this.createSpan("&nbsp;", this.CL_SNIPPET);
			this.setHorizontalResultContent(
			this.results[this.currentResult]);
			this.setOpacity(this.linkContainer, 0);
			this.linkContainer.appendChild(this.snippet);
			this.linkContainer.appendChild(this.link);
			this.fadeOpacity = 0;
			this.fadeIn();
		}
	} else {
		// no results, mark the container as empty
		this.cssSetClass(this.barBox, this.getBarBoxClass(true));
		// retry another search expression
		if ( this.executeList.length == 1 ||
			this.cycleTime == GSnewsBar.CYCLE_TIME_MANUAL ) {
			if (this.retries > 1) {
				// we failed the default query. Don't let this get caught
				// in a failure loop
				if (this.testForDefaultQuery()) {
					// stop the timers...
					this.clearCycleTimer();
					this.clearFadeTimer();

					// clear the status area
					if (this.statusRoot) {
					this.removeChildren(this.statusRoot);
					}
					return;
				} else {
					this.resetAutoExecuteListItems([GSnewsBar.DEFAULT_QUERY]);
					this.retries = 0;
				}
			} else {
				this.totalFailures++;
				this.retries++;
			}
		
			this.switchToListItem(0);
		} else {
			// this really means that this is a list item based search
			// that should have worked and if it had worked would have
			// produced results that we cache. in this case though, we
			// got no results so mark this, and if we get too many on
			// this term, throw away the search term
			if (cacheResults) {
				this.totalFailures++;
				currentListItem.errorCount++;
				// if we are getting excessive errors from this entry
				// then reset the list without this entry
				if (ns.completionStatus == 200) {
					currentListItem.errorCount = GSnewsBar.MAX_ERROR_COUNT + 1;
				}
				if (currentListItem.errorCount > GSnewsBar.MAX_ERROR_COUNT) {
					var newList = new Array();
					for (var i=0; i<this.executeList.length; i++) {
						if (this.executeList[i].errorCount <= GSnewsBar.MAX_ERROR_COUNT) {
							newList.push(this.executeList[i].query);
						}
					}
					if (newList.length == 0) {
						newList.push(GSnewsBar.DEFAULT_QUERY);
					}
					this.resetAutoExecuteListItems(newList);
				}
			}
			this.cycleTimeout();
		}
	}
} 