/*
 * cbm.de JavaScript
 */

// run jQuery in compatibility mode since the
// image slideshow runs with Prototype.
jQuery.noConflict();

jQuery(document).ready(function(){
	// track Google Analytics events for
	// a) all external links in content and marginal areas
	// b) download links: all links with domain cbm.de or christoffel-blindenmission.de and no file extension ".html"
	jQuery('#content a, #leftColumn .slots a, #rightColumn .slots a').each(function() {
		if( jQuery(this).is('a[href*="cbm.de"]') || jQuery(this).is('a[href*="christoffel-blindenmission.de"]') || jQuery(this).is('a[href*="cbmonline.org"]') ) {
			if( jQuery(this).is('a[href*="artikel/downloads"]') || jQuery(this).is('a[href*="static/medien"]') || jQuery(this).is('a[href*="movies/"]') ) {
				// contains internal hostname and paths used for downloads
				jQuery(this).click(function() {
					trackGaEvent( 'Medien', 'Dateidownload', jQuery(this).attr('href') );
				});
			}
		} else if( jQuery(this).is('a[href*="http"]') ) {
			// no internal hostname and contains http, so it has to be an external link.
			jQuery(this).click(function() {
				trackGaEvent( 'Links', 'Externe Links', jQuery(this).attr('href') );

				if( jQuery(this).attr('target') != '_blank' ) {
					// add timeout to allow request to GA to be made in the meantime
					setTimeout('window.location = "' + jQuery(this).attr('href') + '"', 150);
					return false;
				}
			});
		}
	});

	if( jQuery('#cbm-ImageTimeline').size() > 0 ) {
		jQuery('#cbm-ImageTimeline a.facebook-share-detailimage').click(function() {
			window.open(jQuery(this).attr('href'),'sharer','toolbar=0,status=0,width=626,height=436');
			return false;
		})
	}

	if( jQuery('#eventCalendar').size() > 0 ) {
		jQuery('#eventCalendar div.vevent div.event_details').hide();

		jQuery('#eventCalendar div.vevent h3 a').click(function() {
			jQuery(this).parents('div.vevent').find('div.event_details').slideToggle();
			jQuery(this).parents('h3').toggleClass('open');
			trackGaEvent( 'Veranstaltungskalender', 'Veranstaltungsdetails angesehen', jQuery(this).parents('h3').find('a.summary').text() );
			return false;
		});

		jQuery('#eventCalendar #event_filter_calendars a').click(function() {
			trackGaEvent( 'Veranstaltungskalender', 'Kalender-Links', jQuery(this).attr('title') );
			setTimeout('window.location = "' + jQuery(this).attr('href') + '"', 150);
			return false;
		});

		jQuery('#eventCalendar .event_details a.google_maps_location').click(function() {
			trackGaEvent( 'Veranstaltungskalender', 'Google-Maps-Link', jQuery(this).attr('href') );
		});

		jQuery('#eventCalendar #event_filter_form').submit(function() {
			if( typeof( eventFilterGaSubmitted ) == 'undefined' ) {
				var catVal = jQuery(this).find('select#category').val();
				if( catVal != "" ) {
					trackGaEvent( 'Veranstaltungskalender', 'Suche - Kategorie', catVal );
				}

				var postcodeVal = jQuery(this).find('input#postcode').val();
				if( postcodeVal != "" ) {
					trackGaEvent( 'Veranstaltungskalender', 'Suche - PLZ-Bereich', postcodeVal );
				}

				eventFilterGaSubmitted = true;

				setTimeout(function() {
					jQuery('#eventCalendar #event_filter_form').submit()
				}, 300);
				return false;
			}
		});



	}
});

function trackGaEvent( category, action, label, value ) {
	if( typeof( _gaq ) != 'undefined' ) {
		_gaq.push(['_trackEvent', category, action, label, value]);
	}
}

function sendToFriend( url ) {
	newWin = window.open( url, 'SendToFriend', 'height=600,width=450, scrollbars=yes, resizable=yes, dependent=yes' );
	newWin.focus();
	return false;
}

function hide_simulation( wert ) {
	window.open('../static/html/popup_spenden.htm','spenden','ontop=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,width=300,height=180');
	window.location.href = dateiselbst;
}

/**
 * Handle keypress when in donation purpose / amount field
 *
 * If used as onkeydown handler, this function tries to click
 * the donation purpose button if [enter] was pressed inside the
 * purpose amount field. Its return value should get returned to
 * the event. E.g.:
 * <code>
 * <input type="text" name="amount-123" id="amount-123" onkeydown="return handleDonationPurposeKeypress( event, this );" />
 * </code>
 *
 * @param {event} event-object
 * @param {string} element the input-dom-element
 */
function handleDonationPurposeKeypress( event, element ) {
	var key = null;
	if( ! event ) {
		event = window.event;
	}
	if( event.which ) {
		key = event.which;
	} else if( event.keyCode ) {
		key = event.keyCode;
	}
	if ( key == 13 ) {
		if( element.id
		&& document.getElementById( element.id.split('amount').join('choose') )
		) {
			document.getElementById( element.id.split('amount').join('choose') ).click();
		}
		return false;
	}
	return true;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if( typeof window.onload != 'function' ) {
		window.onload = func;
	} else {
		window.onload = function() {
			if( oldonload ) {
				oldonload();
			}
			func();
		}
	}
}
