// show hide tabs
// This is used on some of the older landing pages iwth top info 'tabs' and bottom expert 'tabs'
function getObj(id){return document.getElementById(id)}
function showHide(a,div){
				if(div=='Info'){
								numDivs=5
								linkShow='linkShowInfo-'
								divShow='divShowInfo-'
				}else{
								numDivs=4
								linkShow='linkShowTab-'
								divShow='divShowTab-'
				}
				for(i=1;i<=numDivs;i++){
								getObj(linkShow+i).className=getObj(linkShow+i).className.replace(/ enable/,'')
								getObj(divShow+i).style.display='none'
				}
				getObj(linkShow+a).className+=' enable'
				getObj(divShow+a).style.display='block'
}

// Show's elements matching show, hides elements matching hide in reverse order
function showHide2(show, hide){
	// hide all with name like hide
	$(hide).hide();
	$(show).show();
}

// adds / removes the 'enable' class from the given element(s)	
function enableDisable2(enable){
	$('.enable').removeClass('enable');
	$(enable).addClass('enable');
}

// pre-loads images w/jquery
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


// remove html text
function remove_html(string) {
	var x =  string.replace(/<\/?[^>]+>/gi,"");
	//alert("given: "+string+"\n\ntrimmed is: "+x);
	return x;
}

//sets the status text from the status id for all of the expert grids on the page
// pass it the jquery object. In this case: something like $('#mygrid_Expert_1 .contact-live')
function set_status(jqueryobj, status_texts){
	//whether your setting a 'livetext', both 'livetext' and 'awaytext', or none, it still has to be in
	//an array like so: loop('gridname', set_status,['online','offline']); || loop('gridname', set_status,[]); 
	livetext = status_texts[0];
	awaytext = status_texts[1];
	
	if(!String(livetext) || livetext == undefined)
	{
		livetext = 'Contact Live';
	}
	
	if(!String(awaytext) || awaytext == undefined)
	{
		awaytext = 'Send Email';
	}
	
	var text ='';

    switch (jQuery.trim(String(jqueryobj.find(' .contact-link').html()))) {        //cast it to a string before you party
		case ('0'): // Away
			text = awaytext;
			break;
        case ('1'):  // Online
            text = livetext;
            break;
        case ('2'): // Busy
            text = awaytext;
            break;
        case ('3'):   // Away
			text = awaytext;
            break; 
        case ('4'):  // Online
            text = livetext;
            break;
        default: //errors are boring
            break;
        }
	
	jqueryobj.find(' .contact-link').html( text );
}

//loops through each expert profile
// in this case the selector is blank or '.profile-bottom'
function loop(gridname, func, params)
{
	var x=0;
	var expert_grid_id = "#"+gridname+"_Expert_";
	var gridclass = expert_grid_id+x;
	if(x==0 && $(gridclass).length==0 ){x=1; gridclass = expert_grid_id+x;}

	while($(gridclass).length>0)
	{
		//execute the function on the grid with selector
		func( $(gridclass) ,params )
		x = x + 1;
		gridclass=expert_grid_id + x;
	}
}



//trims descriptions from the jquery object
// uses it's own selectors
function trim_description(jqueryobj, description_max_length){
	//entire expert display div
	//description text: $(".grid_Expert_1 .description ").html();
	//profile url:  $(".grid_Expert_1 #profileurl").attr("href");

		var description = jqueryobj.find(" .description ").html();
		var profileurl =  jqueryobj.find(" #profile_url").attr("href");
		var profilelink = ' <a href=" ' + profileurl + ' " target=\'_blank\'>(read more)</a>';	
		var lastspace = description.lastIndexOf(' ');
		// the last space occurs past the max length, trim the description with the next space
		if(lastspace > description_max_length)
		{
			var trimhere = description.indexOf(' ', description_max_length);
			text = description.substr(0,trimhere);
			
			jqueryobj.find(" .description").html(text);
			jqueryobj.find(" .description").append(profilelink);
		}
	
}




//removes bold from the description text
// the nasty replace is a URI encoded of the b-tags so that ems will let it get published
function remove_tags(jqueryobj){
	
	//try to remove bold via css
	$(".description").css("font-weight","normal"); // css styling is easy to fix
	
	//remove html tags
	var old_desc = String( jqueryobj.find(" .description").text() );
	var new_desc = remove_html(old_desc);
	jqueryobj.find(" .description").html(new_desc);			
}

// Do something like this
// $(document).ready(function () {
//  loop('mygrid', set_status, []); 
//  loop('mygrid', trim_description, 145 ); 
//  loop('mygrid', remove_tags, []);
//})

	function OpenWindow(url, width, height)
	{
		if (typeof (width) == "undefined")
		{
			width = 420;
		}
		if (typeof (height) == "undefined")
		{
			height = 300;
		}
		window.open(url, 'winHelp', "width=" + width + ", height=" + height + ", resizable=1, scrollbars=1");
	}
	
	
	// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
