/**********************************************/
/* commons.js (part of ControversIal Numbers) */
/* copyright 2008 - Thomas Michel Simons      */
/**********************************************/

//returns "br", "pt", "us", "en", "de", "es", or ""

function getIdiom()
{
	var navLang = ""; 
	if (navigator.language) //firefox 2.0
	{
		navLang = navigator.language.toLowerCase();
	}
	else if(navigator.userLanguage) //IE 7.0  // browserLanguage? systemLanguage?
	{
		navLang = navigator.userLanguage;
	}
	
	// alert("navLang='"+navLang+"'"); //line for debugging 
	if(navLang=="pt-br") 
		return "br";
	if(navLang.substring(0,2)=="pt") 
		return "pt";
	if(navLang=="en-us") 
		return "us";
	if(navLang.substring(0,2)=="es") 
		return "es";
	if(navLang.substring(0,2)=="de") 
		return "de";

	// non-American English is the default idiom 
	return "en";
}

function getTitle(idiom)
{
	if (idiom=="br")
		return "Números PolêmIcos";
	if (idiom=="pt") 
		return "Números PolémIcos";
	if (idiom=="es") 
		return "Números PolémIcos";
	if (idiom=="de") 
		return "PolemIsche Zahlen";
	//the english version, american or non-american, is the default one
	return "ControversIal Numbers";
}

function repassParams()
{
	var query = window.location.href;
	var index = query.indexOf('?'); 
	if (index == -1 || index == query.length - 1)
	{
//		alert("no parameter");
		return ""; 
	}
//	alert("&"+query.substring(index + 1));
  return "&"+	query.substring(index + 1);
}

//fs = font size
function openCGS(fs,locale)
{

	// alert("fs="+fs+", locale="+locale+".");
	//values for default font size of 14px
	var wWidth = 408;
	var wHeight = 327;

	switch(fs)
	{
		case 20:
			wWidth = 582;
			wHeight = 427;
			break;
		case 19: fs = 18;
		case 18:
			wWidth = 524;
			wHeight = 390;
			break;
		case 17: fs = 16;
		case 16:
			wWidth = 466;
			wHeight = 342;
			break;
		case 15: fs = 14
		case 14:
			wWidth = 408;
			wHeight = 327;
			break;
		case 13: fs = 12;
		case 12:
			wWidth = 350;
			wHeight = 290;
			break;
		case 11: fs = 10;
		case 10:
			wWidth = 292;
			wHeight = 264;
			break;
		case 9: fs = 8;
		case 8:
			wWidth = 234;
			wHeight = 238;
			break;
		default: fs = 14;
	}
	if (navigator.language) //firefox 2.0
	{
		wWidth += 4; wHeight += 4; //empirical value  
	}
	window.open("ConceptsAndGradesScale.html?fontSize="+fs+"&locale="+locale,"scale",
		"width="+wWidth+",height="+wHeight +
		",left=" + (screen.width - wWidth) / 2 + ",top=" + (screen.height - wHeight) / 2 );
}


//fs = font size
function openII(fs,locale)
{

	//values for default font size of 14px
	var wWidth = 520;
	var wHeight = 570;

	switch(fs)
	{
		case 20:
			// fs is 2pt less than the regular font, but never less than 8pt
		case 19: fs = 18;
		case 18:
			wWidth = 758;
			wHeight = 678;
			break;
		case 17: fs = 16;
		case 16:
			wWidth = 664;
			wHeight = 594;
			break;
		case 15: fs = 14
		case 14:
			wWidth = 590;
			wHeight = 570;
			break;
		case 13: fs = 12;
		case 12:
			wWidth = 506;
			wHeight = 506;
			break;
		case 11: fs = 10;
		case 10:
			wWidth = 422;
			wHeight = 462;
			break;
		case 9: fs = 8;
		case 8:
			wWidth = 338;
			wHeight = 418;
			break;
		default: fs = 14;
	}
	if (navigator.language) //firefox 2.0
	{
		wWidth += 4; wHeight += 4; //empirical value  
	}
	window.open("ImperfectionIntervals.html?fontSize="+fs+"&locale="+locale,"imperfectionIntervals",
		"width="+wWidth+",height="+wHeight +
		",left=" + (screen.width - wWidth) / 2 + ",top=" + (screen.height - wHeight) / 2 );
}

function openSE(locale)
{

	//values for default font size of 14px
	var wWidth = 436;
	var wHeight = 536;

	if (navigator.language) //firefox 2.0
	{
		wWidth += 4; wHeight += 4; //empirical value  
	}
	window.open("SelectingEmpty.html?locale="+locale,"selectingEmpty",
		"width="+wWidth+",height="+wHeight+
		",left=" + (screen.width - wWidth) / 2 + ",top=" + (screen.height - wHeight) / 2 );
}

function openTwoPodia(locale)
{
	window.open("TwoPodia.html?locale="+locale,"twoPodia");
}

function addToFavorites(titleNP,locale)
{
    var urlNP      = "http://numpol.com/";
    // alert("sidebar='"+window.sidebar+", opera='"+window.opera+"' , print='"+window.print+"', document.all='"+document.all+"'"); // for debugging purposes
    if (window.sidebar) //Firefox
    {
		window.sidebar.addPanel(titleNP, urlNP+"sidebar.html?locale="+locale,"")
	}
/* PS: It does not work with Opera 9.51
    else if(window.opera && window.print) //PS: couldn't make it work
    {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',urlNP);
        mbm.setAttribute('title',titleNP);
        mbm.click();
    }
*/
    else if(document.all)
	{
		if (locale=="br") {		
			alert('Para adicionar Números PolêmIcos à barra de links, mude a opção-padrão da lista "Criar em:" (Favoritos) para LINKS.');
		} else if (locale=="pt") {		
			alert('Para adicionar Números PolémIcos à barra de links, altera a opção-padrão da lista "Criar em:" (Favoritos) para LINKS.');
		} else if (locale=="es") {		
			alert('Para agregar Números PolémIcos a la barra de links, cambia la opción estándar de la lista "Criar en:" (Favoritos) para LINKS.');
		} else if (locale=="de") {		
			alert('Um PolemIsche Zahlen zu der Links-Toolbar zu speichern, ändern Sie bitte die Standardoption von "Erstellen in" (Favoriten) zu LINKS.');
		} else {
			alert('In order to add ControversIal Numbers to Bookmarks Toolbar Folder, change the "Create in:" standard option (Favorites) to LINKS.');
		}

		window.external.AddFavorite(urlNP, titleNP);
	}

	else if (navigator.language && !window.opera) //firefox 2.0
	{
		//HERE implement a similar block to Opera as soon as it addToFavorite code works for it
		if (locale=="br") {		
			alert('Para adicionar Números PolêmIcos aos seus favoritos, aperte simultaneamente a tecla CTRL e a tecla D.');
		} else if (locale=="pt") {		
			alert('Para adicionar Números PolémIcos aos teus favoritos, aperta simultaneamente a tecla CTRL e a tecla D.');
		} else if (locale=="es") {		
			alert('Para agregar Números PolémIcos a tus favoritos, presiona simultáneamente la tecla CONTROL y la tecla D.');
		} else if (locale=="de") {		
			alert('Um PolemIsche Zahlen in Ihre Favoriten zu einfügen, drücken Sie gleichzeitig die Taste CTRL und die Taste D.');
		} else {
			alert('In order to add ControversIal Numbers to your favorites, press CTRL + D');
		}
	}
}

function goBack()
{
	window.history.back();
}

function goForward()
{
	window.history.forward();
}

function openContributionWindow(locale, stringNumber)
{
	var _width = screen.width*0.9; var _height = screen.height*0.9; //_width = 780; var _height = 570;
	var cw = window.open(locale+"/essays.php?chapter="+stringNumber,"contributionWindow",
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no"
		);
	cw.focus();
}

function openEssayWindowDirectly(locale,stringNumber)
{
	var _width = screen.width*0.9; var _height = screen.height*0.75;
	window.open(locale+'/pdf/load-doc.htm?'+stringNumber+".pdf",'essay_'+removeTheComma(stringNumber),
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, status=no, titlebar=no, toolbar=no"
		);
}

function openDonationWindow(locale)
{
	if (locale=="br") {
		var _width = 620; var _height = 400;
		window.open(locale+"/payment.html","contributionWindow",
			"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no"
			);
	} else {
		toPayPal(locale);
	}
}

function aoPagSeguro() //to PagSeguro, "a kind of brazilian PayPal"
{
	var _width = screen.width*0.8; var _height = screen.height*0.9;
	var address = "https://pagseguro.uol.com.br/security/webpagamentos/webdoacao.aspx?email_cobranca=controversial.numbers@gmail.com&moeda=BRL";
	window.open(address,"pagseguro",
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, status=no, titlebar=no, toolbar=no"
		); 
}


function toPayPal(locale)
{
	var _width = screen.width*0.8; var _height = screen.height*0.9;

	//default address to payment page is in English (American). There is alternatives in German and in Spanish	
	var address = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&lc=US&hosted_button_id=4081092";
	if (locale == "de")
	{
		address = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&lc=DE&hosted_button_id=4080060";
	}
	else if (locale == "es")
	{
		address = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&lc=ES&hosted_button_id=4103905";
	}
	
	//payment page
	window.open(address,"paypal",
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, status=no, titlebar=no, toolbar=no"
		);
		
	//advertisement page (in front of payment page)
	window.open('https://www.paypal.com/'+locale+'/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=500');			
}
			
function removeTheComma(s)
{
	var i = s.indexOf(',');
	if (i < 0)
		return s;
	return s.substr(0,i) + s.substr(i+1);
}

function openEssay()
{
	var chapter = getChapter();
	var _width = screen.width*0.9; var _height = screen.height*0.8;
	window.open('pdf/load-doc.htm?'+chapter+".pdf",'essay_'+removeTheComma(chapter),
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, status=no, titlebar=no, toolbar=no"
		);
}

function openReleaseNotes()
{
	window.open("http://numpol.wordpress.com/release-notes-portugues/","releaseNotes");
}

function openBlog(locale)
{
	var blAddr = "http://controversialnumbers.wordpress.com";
	if (locale=="br"|| locale=="pt") {
		blAddr = "http://numpol.wordpress.com";
	} else if (locale == "es") {
		blAddr = "http://numerospolemicos.wordpress.com";
	} else if (locale == "de") {
		blAddr = "http://polemischezahlen.wordpress.com";
	}

	var _width = screen.width*0.9; var _height = screen.height*0.9; //_width = 780; var _height = 570;
	var bl = window.open(blAddr,"blog",
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no"
		);
	bl.focus();
}
	
function openTwitter(locale)
{
	var twAddr = "http://twitter.com/numpol_en";
	if (locale=="br"|| locale=="pt") {
		twAddr = "http://twitter.com/numpol";
	} else if (locale == "es") {
		twAddr = "http://twitter.com/numpol_es";
	} else if (locale == "de") {
		twAddr = "http://twitter.com/numpol_de";
	}

	var _width = screen.width*0.9; var _height = screen.height*0.9; //_width = 780; var _height = 570;
	var tw = window.open(twAddr,"twitter",
		"width="+_width+", height="+_height+", left="+((screen.width - _width) / 2)+", top="+((screen.height - _height) / 2)+ ", location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no"
		);
	tw.focus();
}
