var linkLeft = 0;
var linkTop = 0;
var linkWidth = 0;
var linkHeight = 0;
var contentWidth = 0;

/* parse the entered query String */
function parseSearchQuery(theForm, message) {
	var queryValue = theForm.elements["query2"].value;
	var testValue = queryValue.replace(/ /g, "");
	if (testValue.length < 3) {
		alert(message);
		return (false);
	}
	queryValue = queryValue.replace(/\+/g, "%2b");
	queryValue = queryValue.replace(/\-/g, "%2d");
	theForm.elements["query"].value = queryValue;
	return (true);
}

/* open the print version popup window */
function openPrintVersion() {
	var params = "print=true";
	if (document.location.search != "") {
		params = document.location.search + "&" + params;		
	} else {
		params = "?" + params;
	}
	window.open(document.location.pathname + params, "print", "width=670,height=750,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes");
}

/* open the imprint popup window */
function openImprint(imprintUri, pageUri, theLocale, site) {
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(imprintUri + pageUri, "imprint", "width=670,height=550,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

/* open the recommend page form popup window */
function openRecommendForm(recommendUri, pageUri, theLocale, site) {	
	if (window.location.search != "") {
		pageUri += window.location.search;		
	}
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(recommendUri + pageUri, "recommend", "width=670,height=700,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

/* open the recommend page form popup window */
function openLetterForm(letterUri, pageUri, theLocale, site) {	
	if (window.location.search != "") {
		pageUri += window.location.search;		
	}
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(letterUri + pageUri, "contact", "width=670,height=700,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

//***
//opens the social network container
//
//parameters: /
//
//return: /
//
//***   
function openSocialNetwork() {
	// set relative values
	var sociallink = $("#searchheadid");
	var offset = sociallink.offset();
	var socialContent = $("#socialnetworkcontent");
	if (linkTop == 0) {
		linkTop = offset.top;
		linkLeft = offset.left;
		linkHeight = sociallink.height();
		linkWidth = sociallink.width();
		contentWidth = socialContent.width();
	}
	$("#socialnetworkcontent").css("top", linkTop + linkHeight + 4);
	$("#socialnetworkcontent").css("left", linkLeft + linkWidth - contentWidth - 1);
	
	// show content
	$("#socialnetworkcontent").fadeIn('slow');
	
	// switch javascript click function
	document.getElementById("socialnetworklink").onclick = closeSocialNetwork;
}

//***
//closes the social network container
//
//parameters: /
//
//return: /
//
//***
function closeSocialNetwork() {
	$("#socialnetworkcontent").fadeOut('slow');
	
	// switch javascript click function
	document.getElementById("socialnetworklink").onclick = openSocialNetwork;
}

//***
//calls the Facebook login page
//
//parameters: /
//
//return: /
//
//***
function callFacebook(title) {
	// build the link
	// the title parameter in the link is not necessary, because facebook gets the title from he requested site itself 
	//var link = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(document.URL) + "&t=" + encodeURIComponent(title);
	var link = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(document.URL);
	window.open(link);
	closeSocialNetwork();
}

//***
//calls the Twitter login page
//
//parameters: /
//
//return: /
//
//***
function callTwitter(title) {
	var source = document.URL;
	// remove all before a double slash string in the URL incluwive the double slash itself
	var doubleSlashPos = source.indexOf("//");
	if (doubleSlashPos > -1) {
		source = source.substring(doubleSlashPos + 2);
	}
	// remove a leading www.
	var slashWww = source.indexOf("www.");
	if (slashWww > -1) {
		source = source.substring(4);
	}
	// remove all after the first slash inclusive the slash itself
	var slashPos = source.indexOf("/");
	if (slashPos > -1) {
		source = source.substring(0, slashPos);
	}
	// build the link
	var link = "http://twitter.com/home?source=" + encodeURIComponent(source) + "&status=" + encodeURIComponent(title) + " " + encodeURIComponent(document.URL);
	window.open(link);
	closeSocialNetwork();
}

//***
//calls the MySpace login page
//
//parameters: /
//
//return: /
//
//***
function callMySpace(title) {
	// build the link
	var link = "http://www.myspace.com/index.cfm?fuseaction=postto&t=" + encodeURIComponent(title) + "&u=" + encodeURIComponent(document.URL);
	window.open(link);
	closeSocialNetwork();
}
