/**
 * Initialises the detail view with 
 * extJs-functions
 * 
 * Will be called after the loading
 * of the document.
 */

function initDetailView() {
	
	/*
	 * Init mail window
	 */
	
	var mailButton = Ext.get("detail-email");
	
	if (mailButton) {
		
		mailButton.on('click', function(event) {
			
			event.stopEvent();
			
			var mailWindow = new PpsMailWindow();
			
			mailWindow.show();
			
			}
		);		
	}
	

	var phoneButton = Ext.get("detail-anrufen");
		
	if (phoneButton) {

		phoneButton.on('click', function(event) {
			
			event.stopEvent();

			var callWindow = new PpsCallWindow();
	
			callWindow.show();
		
		});
		
	}

	
	/*
	 * Add "onClick"-event to the detail tabs
	 */

	var detailTabs = Ext.fly('tab-nav');
	
	if (detailTabs) {
	
		detailTabs.on('click', tabClicked);

		var detailTab1 = Ext.get(Ext.query('div#detail-tab1'));
		
		detailTab1.setVisibilityMode(Ext.Element.DISPLAY);
		
	}

	var favorites = Ext.fly('favorites');

	if (favorites) {

		favorites.on('click', function(event, target) {

			event.stopEvent();


			if ((navigator.appVersion.indexOf("MSIE") > 0) && (parseInt(navigator.appVersion) >= 4))
				{

				window.external.AddFavorite(location.href, document.title);

				}

		}
		);

	}
	
}


/**
 * Enables the switching between the tabs on 
 * the detail view.
 * 
 * @param event			The "click"-event
 * @param target			The "target"-element which was clicked
 * @return					false to prevent links from submitting
 */

function tabClicked(event, target) {
	
	event.stopEvent();

	if (target.className == 'tabLink') {			
			
		// Transform link node to ext element 
		// and get its parent
	
		var clickedLink = Ext.get(target);
		
		var parentLINode = clickedLink.findParent('li');
		
		var parentLI = Ext.get(parentLINode);

		// Alert e-tracker	
		if (parentLI.id != '') {

			// Get the order number from the 'et_pagename'-var
			// which will be set on e-tracker initialisation

			var orderNumber = et_pagename.replace(/[A-Za-z0-9, ]*\[/g,'');
			orderNumber = orderNumber.replace(/\]/g,'');

			ET_Event.click(orderNumber+' '+parentLI.id, parentLI.id);

		}	

	
		// Get the related div-Element
		
		var clickedId = clickedLink.id;

		var tabId = clickedId.substr(5, clickedId.length);

		var selectedTabNode = Ext.query('div#'+tabId);

		var selectedTab = Ext.get(selectedTabNode);


		// First hide selected tab

		var detailTabs = Ext.query('div[id^=detail-tab][id!=detail-tabs][display!=none]');

		Ext.each(detailTabs, function(detailTabNode) {

			var tab = Ext.get(detailTabNode);
	
			tab.setVisibilityMode(Ext.Element.DISPLAY);
			
			if (tab.isVisible()) {			
			
				tab.hide();
				
				}

			},
		this);
		
		
		// Remove selected class
		
		var oldSelectedLINode = Ext.query('li.selected');
		
		var oldSelectedLI = Ext.get(oldSelectedLINode);
		
		oldSelectedLI.removeClass('selected');
	

		parentLI.addClass('selected');
		
		selectedTab.show(true);

	}
	
	return false;
}
