/**
 * The MSFlyoutTruck class extends the MSFlyout class and
 * handles the flyout menus on the truck website. It offers timeout values 
 * that can be different for each flyout type. This class depends on the 
 * daimler_basic.js, navigation.js and MSFlyoutClass.js!
 * 
 * @author pascal.erb(at)namics.com
 * @version 2.0
 * @requires daimler_basic.js
 * @requires navigation.js
 * @requires MSFlyoutClass.js
 */

// Class MSFlyoutTruck (extends MSFlyout), Constructor
function MSFlyoutTruck(/*String*/ id, /*int*/ type) {
	if(!id || id == "") {
		LOG_ERR("MSFlyoutTruck: NO ID!");
		return;
	}
		
	this.$me = jQuery('#' + id);
	LOG('MSFlyoutTruck: constructor: id: ' + id + ', type: ' + type);

	// call super constructor
	MSFlyout.call(this, id, type);
	

	MSFlyoutTruck.$getCoreNav1Containers().click(this.highlightActive);
	this.identifyPermanentFlyout(id);
}

MSFlyoutTruck.prototype = new MSFlyout(); //inherit from super

// static fields ///////////////////////////////////////////////////////////////

//Static Flyout Types (from super)
MSFlyoutTruck.TYPE_CORENAV1 = MSFlyout.TYPE_CORENAV1;

//Static Timeout Settings (overwrites parent values!)
MSFlyout.ACTIVATION_TIMEOUT[MSFlyout.TYPE_CORENAV1] = 250;
LOG('MSFlyoutTruck: MSFlyout.ACTIVATION_TIMEOUT: ' + MSFlyout.ACTIVATION_TIMEOUT[MSFlyout.TYPE_CORENAV1]);

MSFlyout.DEACTIVATION_TIMEOUT[MSFlyout.TYPE_CORENAV1] = 2000;
LOG('MSFlyoutTruck: MSFlyout.DEACTIVATION_TIMEOUT: ' + MSFlyout.DEACTIVATION_TIMEOUT[MSFlyout.TYPE_CORENAV1]);

// integer value to increase / decrease navigation z-indexes
MSFlyoutTruck.Z_DELTA = 1;
MSFlyoutTruck.Z = 40;

MSFlyoutTruck.$selected = undefined;

// static methods //////////////////////////////////////////////////////////////
/*jQuery*/ MSFlyoutTruck.$getCoreNav1Containers = function() {
	return jQuery('#ms-navi-main').find('[id*="ms-corenav"]');
};

// instance methods ////////////////////////////////////////////////////////////
/*override void*/ MSFlyoutTruck.prototype.handleActivate = function() {
	MSFlyout.prototype.handleActivate.apply(this); // calling super
	this.$me.find('div').css('display', 'block');
};

/*override void*/ MSFlyoutTruck.prototype.handleDeactivate = function() {
	MSFlyout.prototype.handleDeactivate.apply(this); // calling super
	var id = this.$me.attr('id');
	if(MSFlyoutTruck.$selected) { // don't eliminate the selected
		var selectedId = MSFlyoutTruck.$selected.attr('id');
		LOG("MSFlyoutTruck#handleDeactivate (id: " + id + "): Selected flyout found: " + selectedId);
		if(selectedId == id) {
			LOG('MSFlyoutTruck#handleDeactivate (id: ' + id + '): The selected item will not be hidden.');
			return;
		}
		MSFlyoutTruck.$selected.find('div').css('display', 'block');
	}
	LOG("MSFlyoutTruck#handleDeactivate (id: " + id + "): Hiding the flyout.");
	this.$me.find('div').css('display', 'none');
};

/*void*/ MSFlyoutTruck.prototype.identifyPermanentFlyout = function(id) {
	var helper = this.$me;
	var helperThis = this;
	result = jQuery(function() {

		var /*int*/ newZ = MSFlyoutTruck.Z;
		
		var makeItPermanent = function() {
			newZ = MSFlyoutTruck.Z - MSFlyoutTruck.Z_DELTA;
			MSFlyoutTruck.$selected = helper;
			MSFlyout.openFlyout[helperThis.type] = helperThis;
			var dummyEvent = new Object();
			dummyEvent.currentTarget = helper[0];
			LOG('MSFlyoutTruck: dummyEvent.currentTarget (id: ' + id + '): ' + dummyEvent.currentTarget);
			helperThis.highlightActive(dummyEvent);
		};
		
		var /*String*/ currentLoc = stripUrlToHandle(location.pathname);
		LOG("MSFlyoutTruck#isThisPagesFlyout: The current locations's handle (id: " + id + "): " + currentLoc);
		var cursor = jQuery('#' + id + ' > a');
		LOG("MSFlyoutTruck#isThisPagesFlyout: # of 'a' tags (id: " + id + "): " + cursor.length); 
		if(cursor && cursor.length > 0) {
			cursor = stripUrlToHandle(cursor[0].pathname);
			// cursor now points to the current id's handle
			LOG("MSFlyoutTruck#isThisPagesFlyout: Cursor's handle (id: " + id + "): " + cursor); 
		} else {
			LOG("MSFlyoutTruck#isThisPagesFlyout (id: " + id + "): No 'a' tags found.");
			result = false;
		}
		LOG("MSFlyoutTruck#isThisPagesFlyout: (id: " + id + "): " + cursor);
		if(currentLoc == cursor) {
			LOG("MSFlyoutTruck#isThisPagesFlyout (id: " + id + "): currentLoc == cursor");
			makeItPermanent();
		} else {
			var /*String*/ currentPrefix = (cursor.length <= currentLoc.length) ?
					currentLoc.substring(0, cursor.length) :
					currentLoc;
			var /*char*/ charAfterPrefix = (currentPrefix.length < currentLoc.length) ?
					currentLoc.charAt(currentPrefix.length) : 
					"";
			if(cursor == currentPrefix && charAfterPrefix == '/') {
				LOG("MSFlyoutTruck#isThisPagesFlyout (id: " + id + "): cursor == currentPrefix && charAfterPrefix == '/'"); 
				makeItPermanent();
			}
		}
		LOG("MSFlyoutTruck#isThisPagesFlyout (id: " + id + "): No match.");
		helper.find('div').css('z-index', newZ);
		LOG('MSFlyoutTruck: new z-index: ' + helper.find('div').css('z-index'));
	});
};


/*void*/ MSFlyoutTruck.prototype.highlightActive = function(event) {

	var /*jQuery*/ $secondLevDiv = null;
	var /*int*/ secondLevZIndex = null;
	
	//Reset:
	if(MSFlyoutTruck.$selected) {
		// increase z-index
		$secondLevDiv = MSFlyoutTruck.$selected.find('div');
		var determinedZ = $secondLevDiv.css('z-index');
		if((typeof determinedZ) == "number") {
			secondLevZIndex = parseInt(determinedZ);
		} else {
			secondLevZIndex = MSFlyoutTruck.Z;
		}
		LOG('MSFlyoutTruck: increase z-index: ' + secondLevZIndex + ' + ' + MSFlyoutTruck.Z_DELTA);
		$secondLevDiv.css('z-index', secondLevZIndex + MSFlyoutTruck.Z_DELTA);
		// remove css class
		MSFlyoutTruck.$selected.removeClass(MSFlyout.CSS_CLASS_ACTIVE);
		jQuery(MSFlyoutTruck.$selected.find('a')[0]).removeClass(MSFlyout.CSS_CLASS_ACTIVE)
	}
	// change selected nav entry
	MSFlyoutTruck.$selected = jQuery(event.currentTarget);
	
	// add css class
	MSFlyoutTruck.$selected.addClass(MSFlyout.CSS_CLASS_ACTIVE);
	jQuery(MSFlyoutTruck.$selected.find('a')[0]).addClass(MSFlyout.CSS_CLASS_ACTIVE);
	
	// decrease z-index
	$secondLevDiv = MSFlyoutTruck.$selected.find('div');
	secondLevZIndex = parseInt($secondLevDiv.css('z-index'));
	LOG('MSFlyoutTruck: decrease z-index: ' + secondLevZIndex + ' - ' + MSFlyoutTruck.Z_DELTA);
	$secondLevDiv.css('z-index', secondLevZIndex - MSFlyoutTruck.Z_DELTA);
	$secondLevDiv.show();
};

