
/**
 * Navigation.js
 * $URL: https://svn.pvtool.org/svn/day_audi_ngw/trunk/ngw_base/frontend/js/audi/audi.navigation.js $
 *
 * @author 		$Author: mkrause $
 * @version     $Revision: 3804 $
 * 
 * @jslint: 2008-10-31
 * 
 * @TODO: 
 * - remove global vars: $element 499 506, elementData 500
 * - INTEGRAE rel=ajaxContentAdvice 
 * 
 * @copyright   NEUE DIGITALE GmbH, Berlin
 */

audi_ngw.namespace(audi_ngw,'navigation');
audi_ngw.namespace(audi_ngw,'navigation.prepareLinks');
audi_ngw.namespace(audi_ngw,'navigation.handleClick');
audi_ngw.namespace(audi_ngw,'navigation.parameters');
audi_ngw.namespace(audi_ngw,'navigation.parseParameters');
audi_ngw.namespace(audi_ngw,'navigation.loadPage');
audi_ngw.namespace(audi_ngw,'navigation.container');


audi_ngw.navigation.container.page = 'audi_content_wrapper';
audi_ngw.navigation.currentPagelevel = '';

/**
 * TODO: INTEGRAE rel=ajaxContentAdvice 
 * ->History
 * ->handlClick
 * ->prepareLinks
 */


/**
 * Prepare all links in the main-container for AJAX-calls
 * @method prepareLinks
 * @public
 * @return {Void}
 */
audi_ngw.navigation.prepareLinks = function($scope){

	
	
	
	jQuery('a', $scope).each(function(){
		
		var $link = jQuery(this);
		var rel = $link.attr('rel');

		
		
		// if the rel is "nohistory" we do nothing (-> link is handled by the browser)
		if( 'nohistory' == rel ) {
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
			
			// we need to return true (instead of false) because false stops the each-loop completely
			return true;
			
		} else if( 'print' == rel ) {

			$link.unbind('click').click(function(event){
//				return audi_ngw.print.setPreview();
				audi_ngw.navigation.popup(window.location.href, {name:'funcPrint',maximized:'true'});
				return false;
			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});

		// if the link should open in a popup, we do that
		} else if( 'popup' == rel || 'popupOnload' == rel ) {

			$link.unbind('click').click(function(event){
				
				var data = jQuery(this).metadata();
				var url = jQuery(this).attr('href');
			
				if( 'undefined' != typeof(data.popupOnload) ) {
					data.redirect = url;
					url = data.popupOnload;

				}
				return audi_ngw.navigation.popup(url, data);

			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
			
		// the small advideLayer will open
		} else if( 'layerAdvice' == rel ) {
			$link.unbind('click').click(function(event){
				audi_ngw.layer.advice.showAnimated(event);
				return false;

			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
			
		// simple layer
		} else if( 'layerSimple' == rel ) {
			
			$link.unbind('click').click(function(event){
			
				audi_ngw.layer.simple.show(event);
				return false;

			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});

		
		} else if( 'ajaxContentAdvice' == rel ) {
			
			$link.unbind('click').click(function(event){
			
				audi_ngw.layer.modal.loadAjaxContentAdvice(this.href);
				return false;

			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});		
			
		} else if( 'tabStatic' == rel ) {
			
			$link.unbind('click').click(function(event){

				audi_ngw.navigation.tab.handleClick(event);
				return false;
				
			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
			
		// some links we handle with the history plugin (container =  rel)
		} else if( 'page' == rel || 'distributor' == rel || 'tabAjax' == rel || 'layerModal' == rel || 'layerModalContent' == rel ) {
			
			$link.unbind('click').click(function(event){

				var $link = jQuery(this);
				
				// if the clicked tab-link is inside a layer, we change the rel to tabAjaxLayer
				if( 'tabAjax' == rel && 0 < $link.parents('#audi_modal_wrapper').size() ) {
					rel = 'tabAjaxLayer';
				}

				var hash = 'source=' + $link.attr('href') + '&container=' + rel;
				var result = audi_ngw.navigation.historyLoad(hash);

				if( true === result ) {
					//return true;
				}
				return false;

			});
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
		
		// if there is no rel or no rel we are looking for...
		} else {
			
			
// TODO some special cases we need to load with ajax, though they don't have a proper set rel:
// - links in #audi_content_wrapper, when the href is the same as in the pagelevel-navigation
// - links in #audi_content_wrapper, when the href is the same as in the distributor-navigation
// - links in #audi_content_wrapper, when the href is the same as in the tab-navigation
			
			$link.bind('click.track', function(event){audi_ngw.track.onClick($(event.target));});
			
			return true;
			
		}
		
		
		
	});

	
	jQuery('#audi_navigation_gallery_extended', $scope).unbind('change').change(function(){
		var url = jQuery(this).val();

		if(  url && true == audi_ngw.url.validate(url) ) {
			var hash = 'source=' + url + '&container=galleryExtended';
			return audi_ngw.navigation.historyLoad(hash);
		}
		
		return true;
	});
	
	
	
};
 

audi_ngw.navigation.onAjaxModalContentReady = function(event,xhr){
	
	// call the function to prepare the links with the correct scope
 	//audi_ngw.navigation.prepareLinks(jQuery('#' + audi_ngw.url.getContentId()));
	
	//audi_ngw.navigation.updatePage(jQuery('#' + audi_ngw.url.getContentId()))
	
	var $scope = jQuery('#'+audi_ngw.url.getContentId());

	// bind the click-event to the relevant links
	audi_ngw.navigation.prepareLinks($scope);
	
	jQuery('.audi_ajax_load').each(function(){
		var $element = jQuery(this);
		var elementData = $element.metadata();
		
		if( 'undefined' != typeof(elementData.url) && true == audi_ngw.url.validate(elementData.url) ) {
			
			$element.removeClass('audi_ajax_load');
			jQuery.get(elementData.url, null, function(data){
				$element.html(data);
				audi_ngw.navigation.updatePage($element);
			});
		
		}
		
	});
		
	// remove the audi_js class and show the pagelinks
	jQuery('.audi_js').removeClass('audi_js');
	jQuery('.func_pagelinks').show();

	// initialize the gallery
	audi_ngw.gallery.initiate();
		
	// opens an interstitial-link in a modal layer
	jQuery('#audi_content_wrapper a[@rel*=popupOnload]', $scope).click();
	
 	// trigger another event 
	audi_ngw.event.trigger('ajaxModalContentPrepared');
	
};

/**
 * Custom Event: wil be fired as soon as we injected the xhr-snippet 
 * @param {Object} event
 * @param {Object} data {sId: new HTML parent Id}
 */
audi_ngw.navigation.onAjaxSnippetReady = function(event,data){
 
	//audi_ngw.log("EVENT - ajaxSnippetPrepared: audi_ngw.navigation.onAjaxSnippetReady",arguments);
	// execute funtions on new content
 	//audi_ngw.navigation.updatePage(jQuery(data.sId));

	var $scope = jQuery(data.sId);

	// bind the click-event to the relevant links
	audi_ngw.navigation.prepareLinks($scope);
	
	jQuery('.audi_ajax_load').each(function(){
		var $element = jQuery(this);
		var elementData = $element.metadata();
		
		if( 'undefined' != typeof(elementData.url) && true == audi_ngw.url.validate(elementData.url) ) {
			
			$element.removeClass('audi_ajax_load');
			jQuery.get(elementData.url, null, function(data){
				$element.html(data);
				audi_ngw.navigation.updatePage($element);
			});
		
		}
		
	});
		
	// remove the audi_js class and show the pagelinks
	jQuery('.audi_js').removeClass('audi_js');
	jQuery('.func_pagelinks').show();
		
	// opens an interstitial-link in a modal layer
	jQuery('#audi_content_wrapper a[@rel*=popupOnload]', $scope).click();
	
	
	// trigger event 
	audi_ngw.event.trigger('ajaxSnippetPrepared',[event,data]);
};


/**
 * Wrapper for the load-function of the history-plugin
 * @method historyLoad
 * @public
 * @param {String} hash
 * @return {Void}
 */
audi_ngw.navigation.historyLoad = function(hash) {
	if( audi_ngw.animation.accordionPagelevel.getRunning() || audi_ngw.animation.slideDistributor.getRunning() ) {
		return false;
		
	}

	hash = hash.replace(/#/, '');
	hash = hash.replace(/container=breadcrumb/, 'container=distributor');

	return jQuery.history.load(hash);
	
};


/**
 * Calls the popup-function when called by audi_ngw.dom.select.initialize()
 * @method loadFromSelect
 * @public
 * @param {Object} event
 * @return {Void}
 */
audi_ngw.navigation.loadFromSelect = function(event) {

	// get the element (we receive the select, not the selected option!!!)
	var $element = jQuery(event.target) || jQuery(event);

	// get the url
	var url = $element.val();
	
	// get the selected option and receive the metadata from it
	var data = $element.find('option[value=' + url + ']').metadata();

	// if it is a popup, call the popup method
	if( 'undefined' !== typeof(data.type) && 'popup' == data.type && true === audi_ngw.url.validate(url) ) {
		return audi_ngw.navigation.popup(url, data);
	}
	
	// if we get here, it's no popup and we open the url in this window
	window.location.href = url;
	
	return false;
};


/**
 * Opens a popup with some options, saved as metadata
 * @method popup
 * @public
 * @param {String} url
 * @param {Object} data
 * @return {Void}
 */
audi_ngw.navigation.popup = function(url, data) {



	data.width = ( 'undefined' == typeof(data.width) ) ? 400 : data.width;
	data.height = ( 'undefined' == typeof(data.height) ) ? 300 : data.height;
	data.scrollbars = ( 'undefined' !== typeof(data.scrollbars) && ('true' == data.scrollbars  || true == data.scrollbars) ) ? 'yes' : 'no';
	data.resizable = ( 'undefined' !== typeof(data.resizable) && ('true' == data.resizable  || true == data.resizable) ) ? 'yes' : 'no';
	data.maximized = ( 'undefined' !== typeof(data.maximized) && ('true' == data.maximized  || true == data.maximized) ) ? 'yes' : 'no';
	data.menubar = ( 'undefined' !== typeof(data.menubar) && ('true' == data.menubar  || true == data.menubar) ) ? 'yes' : 'no';
	data.toolbar = ( 'undefined' !== typeof(data.toolbar) && ('true' == data.toolbar  || true == data.toolbar) ) ? 'yes' : 'no';
	
	

	
	// if the popup should be maximized, overwrite the given width/height with the max values
	if( 'yes' === data.maximized ) {
		data.width = screen.availWidth;
		data.height = screen.availHeight;
	}

	var params = 'width=' + data.width + ',height=' + data.height + ',scrollbars=' + data.scrollbars + ',resizable=' + data.resizable + ',menubar=' + data.menubar + ',toolbar=' + data.toolbar;


	data.name = data.name || 'audi_' + parseInt(Math.random());
	
	var PopupWindow = window.open(url, data.name, params);
	
	if( null !== PopupWindow && 'undefined' !== typeof(data.redirect) && false !== audi_ngw.url.validate(data.redirect) ) {
		window.location.href = data.redirect;
	}
	
	return false;
};



/**
 * handle AJAX-calls from the history-plugin
 * @method handleClick
 * @public
 * @param {String} hash
 * @return {Void}
 */
audi_ngw.navigation.handleClick = function(hash){

	var parameterError = false;
	
	
	// parse the parameters
	try {
		audi_ngw.navigation._parseParameters(hash);
    } catch (e) {
		//There are parameters but they don't have the right format
		parameterError = true;
    }

	// remember the first page
	if( 'undefined' === typeof(audi_ngw.navigation.entryPage) ) {
		// TODO change "window.location.pathname" to "window.location.href"
		audi_ngw.navigation.entryPage = window.location.pathname;

		if( '' == hash ) {
			return false;
		}

// TODO handle back-button back to first page in ajax-queue (= empty hash && set audi_ngw.navigation.entryPage)
	} else if( '' == hash && 'undefined' !== typeof(audi_ngw.navigation.entryPage) ) {

		audi_ngw.navigation.parameters.source = audi_ngw.navigation.entryPage;

		if( 0 != jQuery('#audi_nav_distributor').size() ) {
			audi_ngw.navigation.parameters.container = 'distributor';
		} else {
			audi_ngw.navigation.parameters.container = 'page';
		}

	}
		

// TODO when is this code executed?	
	else if( '' == hash || true == parameterError ) {

// TODO change "window.location.pathname" to "window.location.href"
		audi_ngw.navigation.parameters.source = window.location.pathname;

		if( 1 == jQuery('#audi_nav_distributor').size() ) {
			audi_ngw.navigation.parameters.container = 'distributor';
		} else {
			audi_ngw.navigation.parameters.container = 'page';
		}
		

	//This must be a normal anchor - let the browser do his thing.
	} else if( '' !== hash && true === parameterError ) {
		return true;
	}
	
	// reload whole page in case of https / http switch
	if ( (audi_ngw.navigation.parameters.source.indexOf('https:') !== -1 &&  window.location.protocol === 'http:') ) {
		window.location.href=window.location.href.replace(/http:/g,'https:');
		return false;
	}
	if ( (audi_ngw.navigation.parameters.source.indexOf('http:') !== -1 &&  window.location.protocol === 'https:') ) {
		window.location.href=window.location.href.replace(/https:/g,'http:');
		return false;
	}
		

	// check if this is a valid url, we are allowed to load (false || url)
	if( false === audi_ngw.url.validate(audi_ngw.navigation.parameters.source) ) {
		window.location.href = audi_ngw.navigation.parameters.source;
	}

	
	// remove fragment 
	var _sUrl = audi_ngw.url.removeAjaxFragment(audi_ngw.navigation.parameters.source);
	
// TYPE: page
	if( 'page' == audi_ngw.navigation.parameters.container ) {

		audi_ngw.navigation.currentPagelevel = jQuery('#audi_nav_pagelevel .current');


		// find the link in the pagelevel or return false
		var $link = jQuery('#audi_nav_pagelevel a[@href*=' + _sUrl + '], #audi_tab_navigation a[@href*=' + _sUrl + '], #audi_navigation_gallery_extended option[@value*=' + _sUrl + ']');

		if( $link.size() == 0 && audi_ngw.navigation.parameters.source != audi_ngw.navigation.entryPage ) {
			return false;		
		} 
		
		audi_ngw.layer.modal.hide();
		
		// TODO animate the pagelevel
		audi_ngw.animation.accordionPagelevel.animate($link);
		
		// it is a page, so let's load it
		audi_ngw.navigation.loadPage(audi_ngw.navigation.parameters.source, jQuery("#" + audi_ngw.navigation.container.page));		
		
		return false;


// TYPE: modal layer
	} else if( 'layerModal' == audi_ngw.navigation.parameters.container ) {

		audi_ngw.layer.modal.showHistory(audi_ngw.navigation.parameters.source + audi_ngw.navigation.parameters.get);

		return false;


// TYPE: modal layer
	} else if( 'layerModalContent' == audi_ngw.navigation.parameters.container ) {

		audi_ngw.layer.modal.showHistory(audi_ngw.navigation.parameters.source + audi_ngw.navigation.parameters.get);
		return false;


// TYPE: galleryExtended	

	} else if( 'galleryExtended' == audi_ngw.navigation.parameters.container ) {

		audi_ngw.navigation.loadPage(audi_ngw.navigation.parameters.source, jQuery("#audi_gallery_extended_container"), audi_ngw.fragmentGallerySelector);
	


// TYPE: tabAjax 
	} else if( 'tabAjax' == audi_ngw.navigation.parameters.container ) {

		// find the link
		var $link = jQuery('.audi_tab_navigation a[@href*=' + _sUrl + ']');
		var $layerLink = jQuery('#audi_modal_wrapper .audi_tab_navigation a[@href*=' + _sUrl + ']');
		
		
		if( 0 != $layerLink.size() ) {
			$link = $layerLink;
		} else {
			audi_ngw.layer.modal.hide();
		}		
		
		var id = $link.metadata().tabOptions.sSelectorRelated;
		
		// find out if we are in the context of the gallery_extended
		if( 0 != jQuery('#audi_navigation_gallery_extended_form').size() && 0 != $link.size() ) {

			audi_ngw.navigation.tab.linkHandlingAjaxClick($link, id);
			
		// or if the link is inside a layer
		} else if( 0 != $layerLink.size() ) {

			audi_ngw.navigation.tab.linkHandlingAjaxClick($link, 'audi_modal_wrapper', id);
			
		// or, last option, the link is in the content-area...just load it
		} else {

			audi_ngw.navigation.loadPage(audi_ngw.navigation.parameters.source, jQuery("#" + audi_ngw.navigation.container.page));			
			
		}
		
		return false;


// TYPE: tabAjaxLayer		
	} else if( 'tabAjaxLayer' == audi_ngw.navigation.parameters.container ) {
		
		// find the link
		var $link = jQuery('#audi_modal_wrapper .audi_tab_navigation a[@href*=' + _sUrl + ']');
		audi_ngw.navigation.tab.linkHandlingAjaxClick($link, 'audi_modal_tab_navigation_container');
		
		return false;


// TYPE: distributor
	} else if( 'distributor' == audi_ngw.navigation.parameters.container || 'breadcrumb' == audi_ngw.navigation.parameters.container ) {


		// find the link in the pagelevel or return false
		var $link = jQuery('#audi_nav_distributor a[@href*=' + audi_ngw.navigation.parameters.source + ']');
		if( 'breadcrumb' == audi_ngw.navigation.parameters.container ) {

			if( true == $link.parents('ul').hasClass('first') ) {

				audi_ngw.navigation.parameters.source = audi_ngw.navigation.entryPage;
				$link = jQuery('#audi_nav_distributor a[@href*=' +_sUrl + ']');

			} else {

				$link = $link.parents('li').children('a');

			}
		}
		


		
		if( $link.size() == 0 && audi_ngw.navigation.entryPage != audi_ngw.navigation.parameters.source ) {
			return false;		
		} 
		
		audi_ngw.layer.modal.hide();
		// TODO animate the distributor
		audi_ngw.animation.slideDistributor.animate($link.get());
		audi_ngw.breadcrumb.update($link);
		
		// it is a page, so let's load it
		audi_ngw.navigation.loadPage(audi_ngw.navigation.parameters.source, jQuery("#" + audi_ngw.navigation.container.page));


// TYPE: unknown		
	} else {
		return true;
	}
	

	return false;
	
};


/**
 * loads a page
 * @method loadPage
 * @public
 * @param {String} source
 * @return {Void}
 */
audi_ngw.navigation.loadPage = function(source, $scope, fragment, selector){

	audi_ngw.navigation._prepareEnvironment($scope);
	
	if( !fragment ) {
		var fragment = audi_ngw.fragmentSelector;
	} 
	source = audi_ngw.url.addAjaxFragment(source, fragment);

	
	
	if( !selector ) {
		var selector = audi_ngw.navigation.container.page;
	}
	
	var loadString = source + audi_ngw.navigation.parameters.get + ' #' + selector + " > *";

	
	// actually load the page thorugh an ajax-request
	$scope.load(loadString, null, function(data, status, xhr){

		// status can either be 'success' or 'error'
        // xhr.status will give us the http-code (e.g. 404)
        if ('error' != status) {
		
			// current wrapper class name should equal xhr.reponseText's wrapper class name
			jQuery("#audi_content_wrapper")
				.removeClass('template-c-2')
				.removeClass('template-c-3')
				.addClass((xhr.responseText.match( /id=\"audi_content_wrapper\".*?class=\"(.*)\"/ ) || [''])[1]);

			
//  			audi_ngw.navigation.setTitle(data);
			audi_ngw.navigation._restoreEnvironment($scope);
			audi_ngw.event.trigger('ajaxSnippetReady',[{sId: '#'+selector}]);
			//audi_ngw.navigation.updatePage($scope);
  			audi_ngw.navigation.setTitle(data);
			return false;
			
        } else {

			audi_ngw.navigation._restoreEnvironment($scope);
		 

			// TODO roll back page-levelnavigation?
			audi_ngw.animation.accordionPagelevel.animate(audi_ngw.navigation.currentPagelevel);
            

			//audi_ngw.toggleScrollbars();
			return false;
        }
		// prepare the links inside the loaded dom for ajax-calls
        
    });
	
	return false;
};


/**
 * 
 * 
 */
audi_ngw.navigation.updatePage = function($scope){

	// bind the click-event to the relevant links
	audi_ngw.navigation.prepareLinks($scope);
	
	// initialize the foldouts
	audi_ngw.animation.simpleAccordion.initialize();
	
	// initialize flash films
	audi_ngw.flash.initiate();
	
	jQuery('.audi_ajax_load').each(function(){
		var $element = jQuery(this);
		var elementData = $element.metadata();
		
		if( 'undefined' != typeof(elementData.url) && true == audi_ngw.url.validate(elementData.url) ) {
			
			$element.removeClass('audi_ajax_load');
			
			elementData.url = audi_ngw.url.getContext(elementData.url);
			
			jQuery.get(elementData.url, null, function(data){
				$element.html(data);
				audi_ngw.navigation.updatePage($element);
			});
		
		}
		
	});

	
	// sIFR-initialize
	//audi_ngw.flash.replaceHeadlines();
		
	if( 'undefined' != audi_ngw.navigation.windowLoaded ) {
		audi_ngw.flash.initialize();
	}
	
	// remove the audi_js class and show the pagelinks
	jQuery('.audi_js').removeClass('audi_js');
	jQuery('.func_pagelinks').show();
	
	// initialize the tabs
	audi_ngw.navigation.tab.initialize({});

	// initialize the gallery
	audi_ngw.gallery.initiate();

	// initialize rounded corners
	audi_ngw.dom.rounded_corners.createOn();

	// initialize advice.footnotes
	audi_ngw.layer.advice.initialize();
	
	// opens an interstitial-link in a modal layer
	jQuery('#audi_content_wrapper a[@rel*=popupOnload]', $scope).click();
	
};


/**
 * Set the title for the html-page after an ajax-load
 * @method setTitle
 * @public
 * @param {String} sHTML
 * @return {Void}
 */
audi_ngw.navigation.setTitle = function(data){

	// set the variable
	var pageTitle = '';

	// search for the title-tag in the ajax-data
	if (data.indexOf('<title>') !== -1 && data.indexOf('</title>') !== -1) {

		// extract the title from the data
		pageTitle = audi_ngw.html_entidiy_decode(data.split('title>')[1].split('<')[0].replace(/#/g,''));
		
	}
/*	
	// if we did not find a title, search for some metadata
	if( '' !== pageTitle ) {
		var metadata = jQuery(data).find('#audi_content_wrapper').metadata();

		if( 'undefined' !== typeof(metadata.pageTitle) ) {
		pageTitle = metadata.pageTitle;
		}
	}
*/	
	// if we have a title, set it
	if( '' !== pageTitle ) {
		document.title = pageTitle
		
		// for ie: set the iframe, too
		var $iframe = jQuery("#jQuery_history");
		if( 0 !== $iframe.size() ) {
				var aktiv = window.setTimeout(function(){
					$iframe[0].document.title = pageTitle;
				}, 1000);
		}
	}
	
	
	return;
	
};
 

/**
 * Splits a parameter string concatenated with '&' and stores it into audi_ngw.navigation.parameters
 * @param {String} hash
 * @throws ParameterException
 */
audi_ngw.navigation._parseParameters = function(hash){

	// reset the parameters variable
	audi_ngw.navigation.parameters = {
		source: '',
		container: '',
		get: ''
	};
	
	// split the hash
    var parts = hash.split('&');

	if( parts.length >= 2 ) {
		
		var newParts = [];
		jQuery(parts).each(function(){
			
			var firstGet = this.split('?');
			if( firstGet.length >= 2 ) {

				newParts.push(firstGet[0]);
				newParts.push(firstGet[1]);      
			} else {
				newParts.push(firstGet[0]);
			}
			
		});

		// go through every part of the hash, split it and save it into the parameters-array
	    jQuery(newParts).each(function(){
	        var keyValueSet = this.split('=');

	        if( keyValueSet.length != 2 ) {
	            throw "ParameterException";
			} else {
	            if( 'source' != keyValueSet[0] && 'container' != keyValueSet[0] ) {
					var tmp = keyValueSet[0] + '=' + keyValueSet[1];
					if( 0 != audi_ngw.navigation.parameters.get.length ) {
						tmp = audi_ngw.navigation.parameters.get + '&' + tmp;
					} else {
						tmp = '?' + tmp;
					}
					
					audi_ngw.navigation.parameters.get = tmp;
				} else {
					audi_ngw.navigation.parameters[keyValueSet[0]] = keyValueSet[1];
				}
				
			}
	    });

		if( audi_ngw.navigation.parameters.source == '' || audi_ngw.navigation.parameters.container == '' ) {

			throw "ParameterException";		
		}
	}

};


/**
 * Prepares page by hidning unnecessary elements during loading process
 * @method _prepareEnvironment
 * @public
 * @param {Object} $element
 * @return {Void}
 */
audi_ngw.navigation._prepareEnvironment = function ($element) {
	
	jQuery('#audi_container_footer').css('visibility', 'hidden');
    
	// save the height of the content-area so the page does not "hop" during load
	if (jQuery('#audi_content_wrapper').height()) {
		jQuery('#audi_content_wrapper').css('height', jQuery('#audi_content_wrapper').height() + 'px');
	}
	// set loading on $scope element 
	if ($element) {
		if ($element.outerHeight() === 0) { $element.css({'minHeight':'150px'}); }
		audi_ngw.animation.setLoading({
			sId: audi_ngw.dom.identify($element),
			sCN: 'preload'
		});
	
	}
	// default
	else {
		audi_ngw.animation.setLoading({
			sId: '#audi_content_wrapper',
			sCN: 'preload'
		});
	}

//    audi_ngw.toggleScrollbars();
 

};



/**
 * restores page by displaying unnecessary elements after loading process
 * @method _restoreEnvironment
 * @public
 * @param {Object} $element
 * @return {Void}
 */
audi_ngw.navigation._restoreEnvironment = function ($element) {

	// delete the height for the content area
	jQuery('#audi_content_wrapper').css('height','');
	jQuery('#audi_container_footer').css('visibility', '');
 
//	audi_ngw.toggleScrollbars();
    // unset loader 
	// set loading on $scope element 
	if ($element) {
		audi_ngw.animation.unsetLoading($element.attr('id'));
	}
	// default
	else {
		audi_ngw.animation.unsetLoading('#audi_content_wrapper');
	}
	audi_ngw.animation.unsetLoading('#audi_content_wrapper');


};






/**
 * Initialization
 */
jQuery(document).ready(function(){

    // IE 5.5 and lesser aren't supported
    if (jQuery.browser.msie && jQuery.browser.version < 6) {
        return false;
    }

    // call the update-function to initiate the page
	audi_ngw.navigation.updatePage(jQuery('body'));
	
	
    // Initialize history plugin.
    // The callback is called at once by present location.hash.
    jQuery.history.init(audi_ngw.navigation.handleClick);
	
	
	// bind a function to the event, firing when a modayLayer has been loaded
	audi_ngw.event.bind('ajaxModalContentReady',audi_ngw.navigation.onAjaxModalContentReady);
	audi_ngw.event.bind('ajaxSnippetReady',audi_ngw.navigation.onAjaxSnippetReady );
	
	var $interstitital = jQuery('link[@rel*=interstitial]');
	if( 0 < $interstitital.size() ) {
		audi_ngw.layer.modal.showHistory($interstitital.attr('href'));
		audi_ngw.track.onClick($interstitital);
	}
	
	/**
	 * On window.load, start sIFR. excute on pageLoad scripts inside flash movies
	 */
	jQuery(window).load(function() {	
		audi_ngw.navigation.windowLoaded = true;
		audi_ngw.flash.initialize();
	});

});


