/* POPUPS */
function popup(which, element) {
	if (popup_safety) {
		return false;
	} else {
		popup_safety = true;
	}

	if (popup_active != false) {
		popup_close(popup_active, (popup_active == which ? false : which), element);
	} else {
		popup_open(which, element);
	}
}

function popup_open(which, element) {
	popup_active = which;

	if (which.indexOf('partner') == 0) {
		num = which.substring(7);
		which = 'partner';
	}

	$(".contentwrapper .content select").css({ visibility: 'hidden' });

	switch (which) {
		case 'feedback_popup':
			$("#feedback_popup").fadeIn();
		break;	
		case 'contactinfo_popup':
			$("#contactinfo_popup").fadeIn();
		break;	
		case 'login':
//			$(".login_link").html('hide login');
			$("#login_popup").fadeIn();
		break;
		case 'search_criteria':
			var temp = findPos(document.getElementById('contentwrapper'));
			$("#search_criteria_popup").animate( {top: temp[1]} , 1000);
		break;
		case 'powerindex':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".powerindex_popup").css( {top: temp[1]} );
			$(".powerindex_background").css( {top: temp[1]} );
			$(".powerindex_background").fadeIn();
			$(".powerindex_popup").fadeIn();
		break;
		case 'feature1':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature1_popup").css( {top: temp[1]} );
			$(".feature1_background").css( {top: temp[1]} );
			$(".feature1_background").fadeIn();
			$(".feature1_popup").fadeIn();
		break;
		case 'feature2':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature2_popup").css( {top: temp[1]} );
			$(".feature2_background").css( {top: temp[1]} );
			$(".feature2_background").fadeIn();
			$(".feature2_popup").fadeIn();
		break;
		case 'feature3':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature3_popup").css( {top: temp[1]} );
			$(".feature3_background").css( {top: temp[1]} );
			$(".feature3_background").fadeIn();
			$(".feature3_popup").fadeIn();
		break;
		case 'feature4':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature4_popup").css( {top: temp[1]} );
			$(".feature4_background").css( {top: temp[1]} );
			$(".feature4_background").fadeIn();
			$(".feature4_popup").fadeIn();
		break;
		case 'feature5':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature5_popup").css( {top: temp[1]} );
			$(".feature5_background").css( {top: temp[1]} );
			$(".feature5_background").fadeIn();
			$(".feature5_popup").fadeIn();
		break;
		case 'feature6':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature6_popup").css( {top: temp[1]} );
			$(".feature6_background").css( {top: temp[1]} );
			$(".feature6_background").fadeIn();
			$(".feature6_popup").fadeIn();
		break;
		case 'feature7':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature7_popup").css( {top: temp[1]} );
			$(".feature7_background").css( {top: temp[1]} );
			$(".feature7_background").fadeIn();
			$(".feature7_popup").fadeIn();
		break;
		case 'feature8':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature8_popup").css( {top: temp[1]} );
			$(".feature8_background").css( {top: temp[1]} );
			$(".feature8_background").fadeIn();
			$(".feature8_popup").fadeIn();
		break;
		case 'feature9':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature9_popup").css( {top: temp[1]} );
			$(".feature9_background").css( {top: temp[1]} );
			$(".feature9_background").fadeIn();
			$(".feature9_popup").fadeIn();
		break;
		case 'feature10':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".feature10_popup").css( {top: temp[1]} );
			$(".feature10_background").css( {top: temp[1]} );
			$(".feature10_background").fadeIn();
			$(".feature10_popup").fadeIn();
		break;
		case 'latest':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".latest_popup").css( {top: temp[1]} );
			$(".latest_background").css( {top: temp[1]} );
			$(".latest_background").fadeIn();
			$(".latest_popup").fadeIn();
		break;
		case 'clientreview':
			var temp = findPos(document.getElementById('contentwrapper'));
			$(".clientreview_popup").css( {top: temp[1]} );
			$(".clientreview_background").css( {top: temp[1]} );
			$(".clientreview_background").fadeIn();
			$(".clientreview_popup").fadeIn();
		break;
		case 'partner':
			if (element) {
				element.getElementsByTagName('img')[0].className = 'protect';
				element.getElementsByTagName('img')[0].src = element.getElementsByTagName('img')[0].src.replace("_nm", "_hv");
			}

			var temp = findPos(document.getElementById('contentwrapper'));
			$(".partner_popup").css( {top: temp[1]} );
			$(".partner_background").css( {top: temp[1]} );

			$(".partner_popup").queue( function() {
				$.get("/modules/mod_partner.aspx", { id: num },
					function(html) {
						$(".partner_popup").html(html);
						$(".partner_background").fadeIn();
						$(".partner_popup").fadeIn();
					});
				$(this).dequeue();
			});

		break;
	}

	popup_safety = false;
}

function popup_close(which, callback, element) {
	if (which.indexOf('partner') == 0) {
		num = which.substring(7);
		which = 'partner';
	}


	switch (which) {
			case 'feedback_popup':
			$("#feedback_popup").fadeOut();
		break;
		case 'contactinfo_popup':
			$("#contactinfo_popup").fadeOut();
		break;	     
		case 'login':
//			$(".login_link").html('login');
			$("#login_popup").fadeOut();
		break;
		case 'search_criteria':
			criteria(criteria_active);
			var temp = findPos(document.getElementById('contentwrapper'));
			$("#search_criteria_popup select").css({ visibility: 'hidden' });
			$("#search_criteria_popup").fadeOut("slow");
			$("#search_criteria_popup").queue( function() {
				$(this).css( {top: ''} );
				$("#search_criteria_popup select").css({ visibility: 'visible' });
				$(this).dequeue();
			});
			$("#search_criteria_popup").fadeIn();
		break;
		case 'powerindex':
			$(".powerindex_background").fadeOut();
			$(".powerindex_popup").fadeOut();
		break;
		case 'feature1':
			$(".feature1_background").fadeOut();
			$(".feature1_popup").fadeOut();
		break;
		case 'feature2':
			$(".feature2_background").fadeOut();
			$(".feature2_popup").fadeOut();
		break;
		case 'feature3':
			$(".feature3_background").fadeOut();
			$(".feature3_popup").fadeOut();
		break;
		case 'feature4':
			$(".feature4_background").fadeOut();
			$(".feature4_popup").fadeOut();
		break;
		case 'feature5':
			$(".feature5_background").fadeOut();
			$(".feature5_popup").fadeOut();
		break;
		case 'feature6':
			$(".feature6_background").fadeOut();
			$(".feature6_popup").fadeOut();
		break;
		case 'feature7':
			$(".feature7_background").fadeOut();
			$(".feature7_popup").fadeOut();
		break;
		case 'feature8':
			$(".feature8_background").fadeOut();
			$(".feature8_popup").fadeOut();
		break;
		case 'feature9':
			$(".feature9_background").fadeOut();
			$(".feature9_popup").fadeOut();
		break;
		case 'feature10':
			$(".feature10_background").fadeOut();
			$(".feature10_popup").fadeOut();
		break;
		case 'latest':
			$(".latest_background").fadeOut();
			$(".latest_popup").fadeOut();
		break;
		case 'clientreview':
			$(".clientreview_background").fadeOut();
			$(".clientreview_popup").fadeOut();
		break;
		case 'partner':
			myimages = document.getElementById('logoScroll').getElementsByTagName('img');
			for (x in myimages) {
				if (typeof(myimages[x]) == 'object') {
					myimages[x].className = '';
					myimages[x].src = myimages[x].src.replace("_hv", "_nm");
				}
			}

			$(".partner_popup").queue( function() {
				$(".partner_background").fadeOut();
				$(this).fadeOut();
				$(this).dequeue();
			});
			$(".partner_popup").queue( function() {
				$(this).html('');
				$(this).dequeue();
			});
		break;
	}

	popup_active = false;

	if (callback != false) {
		popup_open(callback, element);
	} else {
		$(".contentwrapper .content select").css({ visibility: 'visible' });
		popup_safety = false;
	}
}

/* PARTNER SCROLL */
function scrollPartners(dir) {
	var numElements = document.getElementById('logoScroll').getElementsByTagName('img').length;
	var scrollW = partners_increment * numElements * -1;
	var maxscroll = scrollW + partners_increment * partners_count;
	var curPos = document.getElementById('partnerscroll').offsetLeft;

	if (dir == 'left') {
		moveto = curPos + (partners_increment * partners_count);
		if (moveto > 0) { moveto = 0; }
	} else {
		moveto = Math.max(curPos - (partners_increment * partners_count), maxscroll);
	}

	if (!partner_safety) {
		partner_safety = true;
		
		if (moveto > 0 || moveto <= scrollW) {
			partner_safety = false;
			return;
		}

		$(".partnerscroll").animate({
			left: moveto + 'px'
		}, partners_scroll);
		$(".partnerscroll").queue(function() {
			partner_safety = false;
			$(this).dequeue();
		});
	}
}

/* SEARCH CRITERIA CHECKLIST */
function criteria(which, element) {
	if (criteria_safety) {
		return false;
	} else {
		criteria_safety = true;
	}

	$(".search_criteria_popup .body ol li a").each(function(i) {
		$(this).text('click for checklist');
	});

	if (criteria_active != false) {
		criteria_close(criteria_active, (criteria_active == which ? false : which), element);
	} else {
		criteria_open(which, element);
	}
}

function criteria_open(which, element) {
	criteria_active = which;

	if (element) { element.firstChild.nodeValue = 'click to hide checklist'; }
	$("#" + which).animate( {height: 'toggle'} , 1000);

	criteria_safety = false;
}

function criteria_close(which, callback, element) {
	criteria_active = false;

	$("#" + which).animate( {height: 'toggle'} , 1000);

	if (callback != false) {
		criteria_open(callback, element);
	} else {
		criteria_safety = false;
	}
}

/* UTIL */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

/* SEARCHING */
function execute_search(data) {
	window.location = '/search_results.aspx?' + data;
}

function checksubmit(e) {
	var key = window.event ? e.keyCode : e.which;
	if (key == 13) { execute_search($('#text_search_form input').serialize());  return false; }
	// autocomplete -- might want to add some kind of wait timer or something.
	/* Yeah, that's pissing me off. Don't enable it until it's ready w/some kind of delay.*/	
	$.get("Webservices/autocomplete.aspx?searchText="+$("#searchText").val(), { agency_name: $("#searchText").val() }, 
		function(data) {
			results = eval(data);
		}
	);
	
	return true;
}

function autcomplete_results(result, eventArgs) {
    
}

/********************************************************************************************** 

     COMPARE 

This function opens the compare page with the chosen 2 or 3 agencies

ALSO calls the Associate web service

****************************************************************************************************/

function compare_agencies(data, returl) {

	data = "&" + data;
	// there will always be a blank first element
	var ids = data.split("&compare=").slice(1).join(",");
	
	var idlist =  data.split("&compare=").slice(1).join("&id="); 
	
	var ws = "http://service.calexussolutions.com/winsper/associate";
	
	 //alert(ws);
	
    //	 var x = $.get(ws,{id:"4000",id:"4001",weight:"1.0";});


	if (ids.length > 0) {
		window.location = "/compare.aspx?compare=" + ids + "&returl=" + returl;		
	} else {
		alert('You must select at least one company to compare.');
	}
	
}

/**********************************************************************************************/


function setup_compare_checkboxes() {
	if ($("input:checked[name=compare]").length >= 3) {
		// disable all non-checked
		$("input:not([checked])[name=compare]").attr("disabled", "disabled");		
	}
	else {
		// enable all
		$("input[name=compare]").removeAttr("disabled");
	}
}

/* DASHBOARD */
function dashboardnav(element) {
	var ul = element.parentNode.parentNode;
	for (var i = 0; i< ul.childNodes.length; i++) {
		if (ul.childNodes[i].className) {
			ul.childNodes[i].className = '';
			ul.childNodes[i].getElementsByTagName('a')[0].className = ul.childNodes[i].getElementsByTagName('a')[0].className + ' a';
		}
	}
	element.parentNode.className = 'focused';
	element.className = element.className + ' a';
}

$(document).ready(function () {
	$('#searchText').autocomplete({ ajax: "/Webservices/autocomplete.aspx?searchText="+$("#searchText").val()});
});

//-------------------------------------------------------------------------------------------------------
//	Image swap
//-------------------------------------------------------------------------------------------------------
function swapImg(id,img)
{
	if( document.getElementById)
	{
		obj	= document.getElementById(id);
		obj.setAttribute( 'src', img );
	}
}

//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
function knowpopUp(url,name,sizeX,sizeY,scroll)
{
	if ( sizeX == 0 )
	{
		//	Top corner
		leftpos	= 10000;
		toppos	= 10000;
		
		//	Define the window size
		heightVar = '';
		widthVar  = '';
	}
	
	else
	{
		//	Center the window
		leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
		toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

		//	Define the window size
		widthVar  = 'width=' + sizeX + ',';
		heightVar = 'height=' + sizeY + ',';
	}

	//	Open the window
	winobject	= window.open(url,name,"menubar=0,statusbar=0,scrollbars=" + scroll + ",toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos);
	winobject.focus();
}


//---------------------------------------------------------------------------------------------------------
//	Show events (on click shows div layer)
//---------------------------------------------------------------------------------------------------------
var selectedID = 0;

function showwork(what)
{
	//	Hide existing one
	if (selectedID)
	{
		hidework();	
	}
	
	workobject							= document.getElementById('work' + what);
	workobject.style.display				= 'block'; 
	selectedID								= what;
}

function hidework()
{
	if (selectedID)
	{
		selectedobject					= document.getElementById('work' + selectedID);
		workobject.style.display		= 'none'; 
		selectedID						= 0;
	}
}

function setdisplay(id,value)
{
	if( document.getElementById)
	{
		obj	= document.getElementById(id);
		obj.style.display	= value;
	}
}
function popUp(url,name,sizeX,sizeY,scroll)
{
	if ( sizeX == 0 )
	{
		//	Top corner
		leftpos	= 10000;
		toppos	= 10000;
		
		//	Define the window size
		heightVar = '';
		widthVar  = '';
	}
	
	else
	{
		//	Center the window
		leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
		toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

		//	Define the window size
		widthVar  = 'width=' + sizeX + ',';
		heightVar = 'height=' + sizeY + ',';
	}

	//	Open the window
	winobject	= window.open(url,name,"menubar=,statusbar=0,scrollbars=0,toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos);
	winobject.focus();
}

