/****
 * Binds click event with function navigateBackTo to given elements 
 * and sets minus cursor for this elements.
 * @elements	elements which should be used for back navigation
 * @page			page to navigate back to, when click event is triggerd.
 */ 
function bindMouseNavMinusTo(elements, backToPage, backToId) {
	backToId = backToId || $.bbq.getState('id');
//	if (backToPage != 'case' || backToPage != 'service') { //!$.hasEventListener(elements, [0], 'click')) {
		undbindMouseNavMinus();
		elements.bind('click', function(event) { navigateBackTo(event, backToPage, backToId); });
		elements.addClass('cursorclose');
/*	}
	else { // back from case
		undbindMouseNavMinus();
		elements.bind('click', function(event) { navigateBackTo(event, 'case', backToId); });
		elements.addClass('cursorclose');
	}
*/	
}


/****
 * Remove the bonded event hanlder from all elements with class cursorminus
 */ 
function undbindMouseNavMinus() {
	if (true ) { //$.hasEventListener($('.cursorminus'), [0], 'click')) {
		$('.cursorminus').unbind('click');
		$('.cursorclose').unbind('click');
	}
	$('.cursorminus').removeClass('cursorminus');
	$('.cursorclose').removeClass('cursorclose');
	$('.cursorstandard').removeClass('cursorstandard');	
}


/****
 * Event handler: navigates the user back to the overview (case studies, services).
 * Removes bindings of this event handler
 * @event			the event which caused this action (click)
 * @page			page to navigate back to
 * @bindings	bindings from bindMouseNav* which should be removed
 */
function navigateBackTo(event, page, id) {
	event = event || window.event; // IE
	// don't stop bubbling, this would disable menu navigation
	
	var target = getEventTarget(event);
	// prevent from handling events caused by menu items.
	if (target.getAttribute('load') != null) { return; }

	// test if target is one of these elements and only if it's not, navigate back
	var dont = $.merge($('#spacer, .menu, .submenu, .submenu_iten, .menu_item, .video_item, h1, .footer_item, #header, #ajaxcontent'), $('#ajaxcontent').find('*'));
	var result = $.grep(dont, function(value, i) { // value is same array as dont
					//return (value.isSameNode(target));		 
					return (value === target);
				});
	// if result is empty, mouse clicked outside the content and menu areas
	if(result.length == 0) {
		undbindMouseNavMinus();
		// animate back to service overview or case studies
		if (page == "case_studies" || page == "services") {
			//		animateBackToOverview(event, page); // located in hid.case_studies_services.js
			loadContent(page, id); 
		}
		// switch back to whatever else
		else { 
			loadContent(page, id); 
		}
	}
}

