/**
 * Switches between extended
 * and simple newsletter
 * subscription form
 */
function switchSubscriptionType ( d ) {
	var m = $('subscription_form').getElementsByClassName('Hidden');
	m.each(function(p){p.setStyle({'display':d});});
	m = null;
}

/**
 * Opens PopUp with given URL
 * @param String URL
 * @param String Name
 * @param Integer Width
 * @param integer Height
 * @param Integer Show location
 * @param Integer Show menu
 * @param Integer Make resizable
 * @param Integer Pos X
 * @param Integer Pos Y
 * @param Integer Show scrolls
 * @param Integer Show status
 * @param Integer Show title
 * @param Integer Show toolbar
 */
function openPopUp (url,popup_name,w,h,l,m,r,x,y,s,stat,title,tools) {
	var features = $H({
		'alwaysRaised': 1,
		'dependent': 1,
		'height': h,
		'width': w,
		'location': l,
		'menubar': m,
		'personalbar': 0,
		'resizable': r,
		'screenX': x,
		'screenY': y,
		'scrollbars': 1,  //s
		'status': stat,
		'titlebar': title,
		'toolbar': tools
	});
	var f = '';
	features.each(function(pair){
		if ( f.length > 0 ) f += ',';
		f += pair.key+'='+pair.value;
	});
	window.open(url, popup_name, '\''+f+'\'');
	features = null;
}

/**
 * Measures all three divs, and set equal height for all
 */
Event.observe(window, 'load', function(){
	resizeContentBoxes(true);
	window.setTimeout('resizeContentBoxes(true)',500);
	window.setInterval('resizeContentBoxes(false)', 5000);
});

function jumpToAnchor () {
	if ( window.location.href.indexOf('#')>-1 ) {
		var eid = window.location.href.substring(window.location.href.indexOf('#')+1);
		if ( $(eid) ) {
			try {
				Element.scrollTo(eid);
			} catch (ex) {}
		}
	}
}

var isIE6 = /msie 6/i.test(navigator.appVersion);

/**
 * 
 */
function resizeContentBoxes (jump) {
	var layout_divs = ['leftBlock','centerBlock','rightBlock'];
	var highest_inner_block = 0;
	var right_block_height = 0;
	try {
		// Search highest inner block:
		layout_divs.each(function(layout_block_id){
			if ( layout_block_id!='rightBlock' ) {
				var inner_layout_block_height = $(layout_block_id + '_inner').getHeight();
				if ( inner_layout_block_height > highest_inner_block )
					highest_inner_block = inner_layout_block_height;
			} else {
				right_block_height = ($('rightBlock').getHeight()-40-195);
			}
		});
		if (isIE6 && highest_inner_block<right_block_height) {
			highest_inner_block = right_block_height;
		}
		// Set for all equal height:
		layout_divs.each(function(layout_block_id){
			if ( layout_block_id=='rightBlock' ) {
				if (isIE6)
					$(layout_block_id).setStyle({'height' : ( highest_inner_block + 40 + 195 ) + 'px'});
				else
					$(layout_block_id).setStyle({'min-height' : ( highest_inner_block + 40 + 195 ) + 'px'});
			} else {
				if (isIE6)
					$(layout_block_id).setStyle({'height' : ( highest_inner_block + 40 ) + 'px'});
				else
					$(layout_block_id).setStyle({'min-height' : ( highest_inner_block + 40 ) + 'px'});
			}
		});
	} catch (e) {}
	if (jump)
		window.setTimeout('jumpToAnchor()',100);
}

/**
 * Jumps to given gallery
 * @param String url
 * @param String nodeName
 */
function viewGallery ( url, nodeName ) {
	var param = '';
	if ( nodeName.length > 0 ) {
		param = '&gallery=' + nodeName;
	}
	window.location = url + param;
}

/**
 * Forwards given foto via email to given recipient
 */
var photoForwarder = {};
photoForwarder.f = null;
photoForwarder.d = 'fotoForwardDialog';
photoForwarder.to = 'to';
photoForwarder.to_val = null;
photoForwarder.from = 'from_name';
photoForwarder.from_val = null;
photoForwarder.text = 'msg';
photoForwarder.text_val = null;
photoForwarder.msg = function (m) {alert(m);};
photoForwarder.check = function () {
	this.getValues();
	var r = /^([\w-~_]+\.)*[\w-~_]+@([\w-_]+\.){1,4}\w{2,4}$/;
	if ( this.to_val && this.to_val.length>0 && r.test(this.to_val) ) {
		if ( this.from && this.from_val.length>0 ) {
			return true;
		} else {
			this.msg('Bitte geben Sie Ihren Namen ein.');
		}
	} else {
		this.msg('Bitte geben Sie richtige E-Mail-Adresse ein.');
	}
	return false;
};
photoForwarder.show = function (f,e) {
	this.f = f;
	if ( $(this.d).getStyle('display')==='block' ) {
		$(this.d).hide();
	} else {
		var dim = Element.getDimensions($(this.d));
		$(this.d).setStyle({'top': (Event.pointerY(e) - dim.height) + 'px','left': (Event.pointerX(e) - dim.width) + 'px'});
		$(this.d).setStyle({'display':'block'});
	}
};
photoForwarder.send = function () {
	if ( this.check() ) {
		var _e_from = ( e_from ) ? '&from=' + e_from.replace(/[$]{3}/, '@') : '' ;
		var _e_return_path = ( e_return_path ) ? '&rp=' + e_return_path.replace(/[$]{3}/, '@') : '' ;
		var _text_val = ( this.text_val ) ? '&msg=' + this.text_val : '' ;
		var url = 'photo?do=forward&photo=' + this.f + '&to=' + this.to_val + _e_from + _e_return_path + '&from_name=' + this.from_val + _text_val;
		new Ajax.Request(url, {
			method		:	'post',
			onSuccess	:	function ( xhr ) {
										var msg = xhr.responseText.include('OK') ? 'E-Mail wurde versandt!' : 'Es ist ein Fehler aufgetreten!' ;
										alert(msg);
									}
		});
		this.cancel();
	}
};
photoForwarder.reset = function (full) {
	if ( full ) {
		this.f = null;
		$(this.to).value = '';
		$(this.from).value = '';
		$(this.text).value = '';
	}
	this.to_val = null;
	this.from_val = null;
	this.text_val = null;
};
photoForwarder.getValues = function () {
	this.reset(false);
	this.to_val = $F(this.to);
	this.from_val = $F(this.from);
	this.text_val = $F(this.text);
};
photoForwarder.cancel = function () {
	this.reset(true);
	$(this.d).hide();
};