

function embed(color, size, funder, baseUrl){
  switch(size){
  case 'wide500':
    width=500;
    height=750;
    break;
  case 'narrowLayout':
    width=370;
    height=750;
    break;
  case 'wide250':
    width=260;
    height=750;
    break;
  default:
    width=650;
    height=750;
    break;
  }

  var header="Search for Social Entrepreneurs";
  var sub_header="Find and connect with Social Entrepreneurs.";

	var unhashedUrl = window.location.toString().split("#");
	unhashedUrl = unhashedUrl[0];
	//myUrl should just be the part before the #hash


  switch(funder){
  	case 422552095:
		header="Search for Social Innovators";
		if (unhashedUrl.indexOf('poptech')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://www.poptech.org/\">PopTech</a> Fellows.";
	  	else 
	  		sub_header="Find and connect with PopTech Fellows.";	  	
  		break;
  	case 647142596:
		if (unhashedUrl.indexOf('civicventures')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://www.civicventures.org/\">Purpose Prize</a> Fellows.";
	  	else 
	  		sub_header="Find and connect with Purpose Prize Fellows.";
  		break;
  	case 228845959:
		if (unhashedUrl.indexOf('draper')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://www.draperrichards.org/\">Draper Richards Foundation</a> Fellows.";
	  	else 
	  		sub_header="Find and connect with Draper Richards Foundation Fellows.";
  		break;
  	case 288863240:
		if (unhashedUrl.indexOf('schwab')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://www.schwabfound.org/\">Schwab Foundation</a> Fellows.";
	  	else 
	  		sub_header="Find and connect with Schwab Foundation Fellows.";
  		break;
  	case 572349203:
		if (unhashedUrl.indexOf('skoll')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://www.skollfoundation.org/\">Skoll Foundation</a> Awardees.";
	  	else 
	  		sub_header="Find and connect with Skoll Foundation Awardees.";
  		break; 
  		/* 		
  	case 647142597:
		if (unhashedUrl.indexOf('ashoka')==-1)
	  		sub_header="Find and connect with <a target=\"_blank\" href=\"http://canada.ashoka.org/\">Ashoka Canada</a> Fellows.";
	  	else 
	  		sub_header="Find and connect with Ashoka Canada Fellows.";
  		break;  		
  	case 647142598:
	  		sub_header="Find and connect with Global Social Benefit Incubator Fellows.";
  		break;  		
  */
  }
  sub_header += "  Powered by the <a class=\"searchlink\" target='_blank' href='http://socialentrepreneurapi.org/'>Social Entrepreneur Search</a>";
  

  url=baseUrl+"search/home/"+color+"/"+size+"/"+funder+"/0/0/0/"+encode64(" ")+"/"+encode64(escape(header))+"/"+encode64(escape(sub_header))+"/"+encode64(unhashedUrl)+"/destroy";
  
  code="<iframe id=\"myFrame\" frameborder=\"0\" style=\"padding:0; margin:0;\" scrolling=\"no\" src="+url+" height="+height+" width="+width+"></iframe>";

  document.write(code);	
  
  var heightVal = '#h_'+height; 
  
  var hashVal = '#nada';
  var positionVal = -1;
  var historyCount = -1; 
  var home=true;

		function checkForMessages(){

	            if(navigator.userAgent.indexOf("Safari") != -1){
                        iwin = frames["myFrame"];
                }else{
                        iwin = document.getElementById("myFrame").contentWindow;
                }

			
			if ((location.hash == '' || location.hash == '#') && !home) {
				home=true;
				url=baseUrl+"search/home/"+color+"/"+size+"/"+funder+"/0/0/0/"+encode64(" ")+"/"+encode64(escape(header))+"/"+encode64(escape(sub_header))+"/"+encode64(unhashedUrl);
                iwin.location = url;
			}
				
			if(location.hash != hashVal && location.hash != ''){	
													
				//fix for safari bug
				var hash = location.hash.replace(/%23/g, '#');
					
				var splitter = hash.split('#');
				
				for (var i in splitter) {
					if (splitter[i].length > 1) {
						if (splitter[i].indexOf('h_')>-1 && splitter[i].length >= 3) {
							heightVal = splitter[i].substr(2);
						}
						else if (splitter[i].indexOf('p_')>-1 && splitter[i].length >= 3) {
							positionVal = splitter[i].substr(2);
						}
					} 
	
				}
				
				
				
				hashVal = location.hash; 
				
				//alert(heightVal);
				
				frameHeight(heightVal); 
				
				if (positionVal == 'no') {
					//do nothing, cookies must be disabled
				}
                else if (positionVal == 'home' && !home) {
					home=true;
					historyCount=0;
				
				  url=baseUrl+"search/home/"+color+"/"+size+"/"+funder+"/0/0/0/"+encode64(" ")+"/"+encode64(escape(header))+"/"+encode64(escape(sub_header))+"/"+encode64(unhashedUrl)+"/restore";
                    iwin.location = url;
                }  
				else if (isNaN(parseInt(positionVal)) || positionVal==-1) {
					
					home=true;
					historyCount=0;
				
				  url=baseUrl+"search/home/"+color+"/"+size+"/"+funder+"/0/0/0/"+encode64(" ")+"/"+encode64(escape(header))+"/"+encode64(escape(sub_header))+"/"+encode64(unhashedUrl);
                    iwin.location = url;
                }
				else {
					positionVal = parseInt(positionVal);

					home=false;
				
					if (positionVal > historyCount) {
						historyCount = positionVal;
					} else {
						//alert('hcount: '+historyCount + ', pval:'+ positionVal);					
						//alert('historizing: '+baseUrl+"search/goToHistorySearch/"+positionVal);
						
						//screw going forward, it doesnt work anyway
						historyCount = positionVal;
					
						url=baseUrl+"search/goToHistorySearch/"+positionVal;
	
	                    iwin.location = url;
	                        
						////////////
					}						
				
				}
												
				
			} 
		}
		
				
		
		function frameHeight(h) {
			var iwin = document.getElementById("myFrame");
			iwin.style.height = h + "px";
            
            //history.back();
			//alert('h: '+h + ', iwin.height:'+iwin.style.height);
		}


  window.setInterval(checkForMessages, 200);
		
}


function exygy_embed_results(categories, funders, regions, query, layout, baseUrl){
	width=265;
    height=585;
    
	url=baseUrl+"search/entrepreneur/";
	url+=categories+"/"+funders+"/"+regions+"/"+query+"/"+layout;
	
	code="<iframe align='left' style='margin:10px' id=\"myFrame\" frameborder=\"0\" style=\"padding:0; margin:0;\" scrolling=\"no\" src="+url+" height="+height+" width="+width+"></iframe>";

  	document.write(code);	

}



// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
	var StringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var StringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}

function encode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }
   
   return output.toString();
}

function decode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	while (i < input.length) {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output.append(String.fromCharCode(chr1));

		if (enc3 != 64) {
			output.append(String.fromCharCode(chr2));
		}
		if (enc4 != 64) {
			output.append(String.fromCharCode(chr3));
		}
	}

	return output.toString();
}

