/**
 * Greenpeace Page Setup
 */

/*-----------------------------------------------------*/
/*                                                     */
/* open links in a new window, if they have the        */
/* attribute rel="external_link"                       */
/*                                                     */
/* used to avoid the attribute target"_blank"          */
/* within xhtml strict                                 */
/*                                                     */
/*-----------------------------------------------------*/

function open_external_link() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external_link")
        anchor.target = "_blank";
    }
}



/*-----------------------------------------------------*/
/* Open Links in a popup window */
/*-----------------------------------------------------*/

function popup(url, popW, popH, myparams) {
	var w
	var h;
	
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		w = self.innerWidth;
		h = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	// var popW = 700, popH = 675;
	
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	var windowname = "gpdpopup" + Math.floor (Math.random () * 100000);
	var params = "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos;
	if (myparams) {
		params += myparams;
	} else {
		params += "toolbar=no,location=no,directories=no,scrollbars=no,status=no,menubar=no,locationbar=no,statusbar=no,resizable=yes,dependent=yes";
	}
	var windowname = "gpdpopup" + Math.floor (Math.random () * 100000);
	window.open( url,windowname,params).focus();
}

/*-----------------------------------------------------*/
/*                                                     */
/* write a value into the search box                   */
/* change the color of the fonts in the search box     */
/*                                                     */
/*-----------------------------------------------------*/

function search_box_text(FeldText,Color) {
    if(document.getElementById) {
        document.getElementById("search_query").value = FeldText;
        document.getElementById("search_query").style.color = Color;
        document.getElementById("search_submit_button").value = "go";
        if(navigator.userAgent.indexOf("KHTML") > -1) {
            document.getElementById("search_submit_button").style.color = "black";
        }
    }
}

function set_cookie(name,value,lifetime) {
    var expire_date = new Date();
    lifetime = ((lifetime && lifetime>60) ? lifetime : (expire_date.getTime() + (365 * 24 * 60 * 60 * 1000)));
    expire_date.setTime(lifetime);
    document.cookie = name + "=" + value + "; expires=" + expire_date.toGMTString() + ";path=/;";
}

function read_cookie_value(cookie_name) {
	if(document.cookie.match(cookie_name)) {
		var cookie_name_length = cookie_name.length;
		var cookie_position = document.cookie.indexOf(cookie_name);
		var start_value_reading = cookie_position + cookie_name_length + 1;
		var interim_value = document.cookie.substr(start_value_reading);
		var position_next_separator = interim_value.indexOf(";");

		if(position_next_separator != "-1") {
			var stop_value_reading = position_next_separator;
			var cookie_value = document.cookie.substr(start_value_reading,stop_value_reading);
		} else {
			var cookie_value = document.cookie.substr(start_value_reading);
		}
		return cookie_value;
    }
	return '';
}

/* the real functions are executed only with IE, here only dummies */
function setFieldActive(id) { }
function unsetFieldActive(id) { }

/**
 *
 * G-Javascript API
 * Spenden Banner-ID
 *
 */
var spendenURL = "ueber_uns/spenden/spenden_sie_online/";
(function($) {
	var maxWidth = 520;
	fixOldFlashDimensions = function(){
		$("object,embed").each(function(){
			var obj = $(this);
			var w = obj.width();
			if (w > maxWidth) {
				var h = obj.height();
				h = Math.round( ( maxWidth * h ) / w );
				w = maxWidth;
				obj.attr('width',w);
				obj.attr('height',h);
				obj.css({ 'width':w, 'height':h });
				if (obj.parent().size()>0) {
					obj.parent().css(
						obj.parent().children('object').size()==1 ?
						{ 'width':w, 'height':h } :
						{ 'width':w }
					);
				}
			}
		});
	};
	$(document).bind('ready.dom',fixOldFlashDimensions);
	$(document).ready(function(){
		fixOldFlashDimensions();
		$("#sub_navigation a[href=" + spendenURL + "]").click(function(event) {
			event.target.href += "?bannerid=030900000002000"
		});
		$("#footer a[href=" + spendenURL + "]").click(function(event) {
			event.target.href += "?bannerid=030900000002111"
		});
	});
})(jQuery);
