// home banner image width
var banner_image_width = 493;
// banner is moving or not
var banner_moving = false;
// current display banner, default banner is 0
var current_banner = 0;
// hold the interval object for autoBanner
var intervalAutoBanner;
var timeAutoBanner = 10000;

/*	Move home banner by n number of banner
	paras:
		n - positive value, move banner to right. negative value, move banner to left
*/
function moveBanner(n) {
	clearInterval(intervalAutoBanner);
	var el = new YAHOO.util.Element('banner_home');
	
	if (((n > 0 && el.getStyle("left") != "0px") || 
		(n < 0 && el.getStyle("left") != banner_image_width * -2 + "px")) && 
		!banner_moving) {
		var attributes = { 
			left: {
				by: banner_image_width * n
			}
		}; 
		setBannerMove();
		current_banner -= n;
		var anim = new YAHOO.util.Motion('banner_home', attributes, Math.abs(n) * 1, YAHOO.util.Easing.easeOut);
		anim.onComplete.subscribe(setBannerStop);
		anim.animate(); 
	}
	intervalAutoBanner = setInterval(autoBanner,timeAutoBanner);
}

/*	Move home banner to banner number n
	paras:
		n - banner number
*/
function moveToBanner(n) {
	moveBanner(current_banner - n);
}

/* Set banner moving status to true */
function setBannerMove() {
	banner_moving = true;
}
/* Set banner moving status to false */
function setBannerStop() {
	banner_moving = false;
}
/* autoBanner */
function autoBanner() {
	var bannerCount = $('bannerCount').innerHTML;
	if(current_banner < bannerCount - 1) {
		moveBanner(-1);
	} else {
		moveToBanner(0);
	}
}

/* *** Top Navigation for IE *** */
function startList() {

	var browserVersion = 0; // Check IE 6.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		splitVsn = navigator.appVersion.split("MSIE")
		browserVersion = parseFloat(splitVsn[1])
	}
	
	if(navigator.appName == "Microsoft Internet Explorer" && browserVersion <= "7"){	 // Checking IE 7 run below;
		if (document.all && document.getElementById) {
		// for top Navigation
			navRoot = $$(".dmenu");
			var navRootNum = navRoot.length;
			for (m = 0; m < navRootNum; m++) {
				if(navRoot[m]) {
					for (i=0; i < navRoot[m].childNodes.length; i++) {
						node = navRoot[m].childNodes[i];
						if (node.nodeName=="LI") {
							node.onmouseover=function() {this.className+=" over";}
							node.onmouseout=function() {this.className=this.className.replace(" over", "");}
						}
					}
		
				// for Left Navigation 2nd level
				/*
				var getSecondClassName = $$(".leftButton");
				var secondLevelNum = getSecondClassName.length;
				for (num=0; num < secondLevelNum; num++){
				navRoot2 = document.getElementById("firstMenuUL" + num); // check id firstMenuUL
					for (i=0; i < navRoot2.childNodes.length; i++) {
							node2 = navRoot2.childNodes[i];
						if (node2.nodeName=="LI") 
						{		
							node2.onmouseover=function() {	this.className+=" over"}
							node2.onmouseout=function() { this.className=this.className.replace(" over", "")}
							
							// for Left Navigation 3th level
							for (j=0; j < node2.childNodes.length; j++) 
							{
								if ( node2.childNodes[j].nodeName == "UL" )
								{
									UL_Node = node2.childNodes[j];
									for (k=0; k < UL_Node.childNodes.length; k++) 
									{
										node3 = UL_Node.childNodes[k];
		
										if (node3.nodeName == "LI")
										{
											node3.onmouseover=function() { this.className+=" over"}
											node3.onmouseout=function() { this.className=this.className.replace(" over", "")}
										}
									}//Next k		
								}						
							} // Loop for Lv3 End
							
						} // if (node2.nodeName=="LI") 
					} // Next i
				} // Next num
				*/
				}
			}
		} //if (document.all && document.getElementById)
	
	} //if(navigator.appName == "Microsoft Internet Explorer" && browserVersion < "7")
}
/* *** end of Top Navigation for IE *** */

/*	Slide Up/Down list
	paras:
		n - list id
*/
function slide(id) {
	if ($(id).style.display == "none")
		Effect.SlideDown(id, {duration:1});
	else
		Effect.SlideUp(id, {duration:1});
}

/* Google search js */
// the cse class encapsulates a left and right search control
// both controls are driven by a shared search form
function cse() {
	var sFormDiv = $("searchForm");
	var sResultDiv = $("searchResult");

	// create a left, right search control
	// create a custom search form
	this.searchControl = new GSearchControl();
	this.searchForm = new GSearchForm(true, sFormDiv);

	// bind clear and submit functions
	this.searchForm.setOnSubmitCallback(this, cse.prototype.onSubmit);
	this.searchForm.setOnClearCallback(this, cse.prototype.onClear);

	// set up for small result sets
	this.searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);

	var searcher;
	var options;

	// configure left control
	// Site Restrict to CSE ID for reviews
	searcher = new GwebSearch();
	options = new GsearcherOptions();
	searcher.setSiteRestriction("014478557581743254179:vx0nmtaieda");
	searcher.setUserDefinedLabel("Civic Exchange");
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
	this.searchControl.addSearcher(searcher, options);
	
	searcher = new GwebSearch();
	this.searchControl.addSearcher(searcher, options);
	
	// the search control is drawn in tabbed mode
	var drawOptions = new GdrawOptions();
	drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
	
	this.searchControl.draw(sResultDiv, drawOptions);

	// execute a starter search if parameter exist
	if (searchText != null && searchText != "")
		this.searchForm.execute(searchText);
}

// when the form fires a submit, grab its
// value and call the left and right control
cse.prototype.onSubmit = function(form) {
	var q = form.input.value;
	if (q && q!= "") {
	  this.searchControl.execute(q);
	}
	return false;
}

// when the form fires a clear, call the left and right control
cse.prototype.onClear = function(form) {
	this.searchControl.clearAllResults();
	form.input.value = "";
	return false;
}

//function OnLoad() {
//	new cse();
//}
//GSearch.setOnLoadCallback(OnLoad);
/* End Google search js */

/* Body on Load function */
function bodyOnLoad() {
	startList();
	
	var screenmode = getParameter("screenmode");

	if(screenmode != null && screenmode != "") {
		if(screenmode.toLowerCase()=="print") printPage2();
	}

	if(getParameter("screenmode")=="print") {
		$$('select').each( function(node) { $(node).disabled=true; } );
		$$('input').each( function(node) { $(node).disabled=true; } );
	}
	
	// hide list 4 by default, should be done on .net side
	//if($('list4'))
	//	$('list4').style.display = "none";
		
	// init Google search if any
	if($('searchForm'))
		cse();

	$$('input.subscribeEmail').each(function(node) {
		$(node).addClassName('unfocus');
		$(node).setValue('Email address here');
		$(node).observe('focus', function() {
			$(this).removeClassName('unfocus');
			if ($F(this) == 'Email address here') {
				$(this).clear();
			}
		});
		$(node).observe('blur', function() {
			if ($F(this).strip() == '') {
				$(this).addClassName('unfocus');
				$(this).setValue('Email address here');
			}
		});
	});
	$$('.top_menu .top_nav_ul').each(function(node) {
		$(node).observe('mouseenter', function() {
			try {
				$(this).up('li').down('.topmenutitle').addClassName('active');
			} catch (Error) {
			}
		});
		$(node).observe('mouseleave', function() {
			try {
				$(this).up('li').down('.topmenutitle').removeClassName('active');
			} catch (Error) {
			}
		});
	});
	$$('.publication_thumb').each( function(node) {
		$(node).observe('mouseenter', function(e) {
			try {
				$(this).next('.publication_tips').show();
			} catch(error) {
			}
		});
		$(node).observe('mouseleave', function(e) {
			try {
				$(this).next('.publication_tips').hide();
			} catch(error) {
			}
		});
		$(node).observe('mousemove', function(e) {
			try {
				var element = $(this).next('.publication_tips');
				var left;
				var top;
				left = e.pointerX() + 20;
				top = e.pointerY() + 20;
				var width = $(element).getWidth();
				var height = $(element).getHeight();
				if(left + width + 10 > document.viewport.getWidth()) {
					left = e.pointerX() - width - 20;
					top = e.pointerY() + 20;
				}
				if(top + height + 10 > document.viewport.getHeight()) {
					top = e.pointerY() - height + 5;
				}
				if(e || (left && top)) $(element).setStyle( { left: left + 'px', top: top + 'px' } );
			} catch(error) {
			}
		});
	} );
}
window.onload = bodyOnLoad;

// Hide or Show select drop down, coz in IE6, top menu is covered by select box
function hideSearchSelect() {
  var browserVersion = 0; // Check IE 6.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		splitVsn = navigator.appVersion.split("MSIE")
		browserVersion = parseFloat(splitVsn[1])
	}
	
  if(navigator.appName == "Microsoft Internet Explorer" && browserVersion <= "7"){	 // Checking IE 7 run below;
    if($('wcEventlist_dMonth')) $('wcEventlist_dMonth').style.display="none";
    if($('wcEventlist_dYear')) $('wcEventlist_dYear').style.display="none";
    if($('wcPresslist_dMonth')) $('wcPresslist_dMonth').style.display="none";
    if($('wcPresslist_dYear')) $('wcPresslist_dYear').style.display="none";
  }
}
function showSearchSelect() {
  var browserVersion = 0; // Check IE 6.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		splitVsn = navigator.appVersion.split("MSIE")
		browserVersion = parseFloat(splitVsn[1])
	}
	
  if(navigator.appName == "Microsoft Internet Explorer" && browserVersion <= "7"){	 // Checking IE 7 run below;
    if($('wcEventlist_dMonth')) $('wcEventlist_dMonth').style.display="inline";
    if($('wcEventlist_dYear')) $('wcEventlist_dYear').style.display="inline";
    if($('wcPresslist_dMonth')) $('wcPresslist_dMonth').style.display="inline";
    if($('wcPresslist_dYear')) $('wcPresslist_dYear').style.display="inline";
  }
}
