/**
 * AUDI JavaScript library: URL related Helpers
 * 
 * @projectDescription	URL related stuff 
 * @namespace			url
 *
 * @author 				$Author: jbambauer $
 * @version				$Revision: 1 $
 * @copyright			NEUE DIGITALE GmbH, Berlin
 * 
 * @jslint: 2008-10-31
 * 
 * @file:				audi.url.js
 */

/* create namespace */
audi_ngw.namespace(audi_ngw,'url');


/**
 * @method getParam
 * @description needs the name of a get parameter in the url and return its value
 * @param {string} get parameter string
 * @return {String} get parameter value
 */
audi_ngw.url.getParam = function (sParamName) {
	var sLoc = window.location.href;
	
	if (sLoc.indexOf("?") > -1) {
		var aURLParams = sLoc.split("?")[1].split("&");
		for ( var i=0; i<aURLParams.length; i++) {
			if(aURLParams[i].indexOf(sParamName) > -1) {
				return aURLParams[i].split("=")[1];
			}
		}
	}
	return null;
};

/**
 * Checks if the supplied response contains content that has to be reparsed.
 * Uses a simple chache to prevent content being checked more than once.
 * @param {XMLHTTPResponse} 
 * @return {Bool}
 */
audi_ngw.url.hasXHRContent = function (xhr,iTimeStamp) {
	// check if all parameters are present
	if (!xhr || !iTimeStamp) { return false; }
	// check if cached result can be returned 
	if (audi_ngw.url.hasXHRContentCache.split(':')[0] != iTimeStamp) {
		// test
		//var _iStatus = (/audi_content_wrapper/).test(xhr.responseText) ? 1 : 0 ;
		var _iStatus = (xhr.responseText.indexOf("audi_content_wrapper") !== -1) ? 1 : 0;
		// cache this result
		audi_ngw.url.hasXHRContentCache = iTimeStamp+':'+_iStatus;
		return !!_iStatus;
	}
	// return cached  result
	return !!parseInt(audi_ngw.url.hasXHRContentCache.split(':')[1], 10);
};

/**
 * Returns modal content id to manipulate - a.k.a clone's id
 * @return {String} DOM Id
 */
audi_ngw.url.getContentId = function ()  {
	return 'audi_modal_container';
};

/**
 * Validates supplied url, decides if we're allowed to handle xhr-calls to this location
 * @TOTO define validator
 * @method validate
 * @param {String} URL
 * @param {String} Base URL e.g. ajax calls
 * @return {Bool} 
 */
audi_ngw.url.validate = function (url) {
	
	// we just test for the host here, not for the protocol
	//var currentHost = '^' + window.location.protocol + '//' + window.location.host;
	var currentHost = '//' + window.location.host;

	
	
	// if it's a relative url, just load it
	if( -1 === url.indexOf('://') ) {
		return true;
	}
	
	if( audi_ngw.config.sHostHTTPS === audi_ngw.config.sHostHTTP ) {
		
		if( window.location.protocol === 'http:' && url.indexOf('http:') === -1 ) {
			
			return false;
			
		} else if( window.location.protocol === 'https:' && url.indexOf('https:') === -1 ) {
			
			return false;
			
		}
		
	} else {
		
		// if it doesn't contain the current host - neither http nor https, load it
		if( -1 === url.indexOf(currentHost) && -1 === url.indexOf(audi_ngw.config.sHostHTTPS) && -1 === url.indexOf(audi_ngw.config.sHostHTTP) ) {
			return false;
		}
		
	}
	
	// else 	
	return true;
};


audi_ngw.url.validateContext = function(url)  {
	var _regexp;

	if( -1 !== url.indexOf('://') && audi_ngw.config.sHostHTTPS === audi_ngw.config.sHostHTTP ) {
		
		if( window.location.protocol === 'http:' && url.indexOf('http:') === -1 ) {
			
			return false;
			
		} else if( window.location.protocol === 'https:' && url.indexOf('https:') === -1 ) {
			
			return false;
			
		}
		
		
		
		
	} else {
		
		if ( (url.indexOf(audi_ngw.config.sHostHTTPS) !== -1 &&  window.location.protocol === 'http:') ) {

			_regexp = new RegExp(audi_ngw.config.sHostHTTP,"g");
			window.location.href = window.location.href.replace(_regexp,audi_ngw.config.sHostHTTPS);

			return false;
		}
		if ( (url.indexOf(audi_ngw.config.sHostHTTP) !== -1 &&  window.location.protocol === 'https:') ) {

			_regexp = new RegExp(audi_ngw.config.sHostHTTPS,"g");
			window.location.href = window.location.href.replace(_regexp,audi_ngw.config.sHostHTTP);

			return false;
		}
		
	}
	
	
	
	
	return true;
};


/**
 * Injects the ajax identifier (audi_ngw.fragmentSelector) into the supplied url
 * @param {String} url
 * @return  {String} Extended URL
 */
audi_ngw.url.addAjaxFragment = function(url, sFragment) {
 	sFragment = sFragment || audi_ngw.fragmentSelector;
	// insert the fragment selector before the extension
	//audi_ngw.log('url before: ' + url);
    if( sFragment && url.indexOf(sFragment) === -1 ) {
//		audi_ngw.log('add fragment: ' +sFragment +' at '+ url);
		url = url.replace(/([^?#]*)(\..*)/, "$1" + "." + sFragment + "$2");
	} 

//    if( '' !== audi_ngw.fragmentSelector ) {
//		url = url.replace(/(.*\/.*)(\.[^?#]*)/, "$1" + "." + audi_ngw.fragmentSelector + "$2");
//	} 

	//audi_ngw.log('url after: ' + url);
		
	return url;
};

/**
 * Injects the ajax identifier (audi_ngw.fragmentSelector) into the supplied url
 * @param {String} url
 * @return  {String} Extended URL
 */
audi_ngw.url.removeAjaxFragment = function(url, sFragment) {
	
 	sFragment = sFragment || audi_ngw.fragmentSelector;
	// insert the fragment selector before the extension
	//audi_ngw.log('url before: ' + url);
    if( sFragment && url.indexOf(sFragment) !== -1 ) {
		url = url.replace("." + sFragment,'');
	} 

//    if( '' !== audi_ngw.fragmentSelector ) {
//		url = url.replace(/(.*\/.*)(\.[^?#]*)/, "$1" + "." + audi_ngw.fragmentSelector + "$2");
//	} 

	//audi_ngw.log('url after: ' + url);
		
	return url;
};

/**
 * Switchs the protocol of the supplied url between https/http to ensure 
 * AJAX-loads  don't fail due to cross-domain-requests.
 * @param {String} Url
 * @return {String} Url
 */
audi_ngw.url.getContext = function(url){
	var _regexp;
	
	if ( (url.indexOf(audi_ngw.config.sHostHTTP) !== -1 &&  window.location.protocol === 'https:') ) {
		_regexp = new RegExp(audi_ngw.config.sHostHTTP,"g");
		url = url.replace(_regexp,audi_ngw.config.sHostHTTPS);
		return url;
	}
	if ( (url.indexOf(audi_ngw.config.sHostHTTPS) !== -1 &&  window.location.protocol === 'http:') ) {
		_regexp = new RegExp(audi_ngw.config.sHostHTTP,"g");
		url = url.replace(_regexp,audi_ngw.config.sHostHTTPS);
		return url;
	}
	return url;
};

audi_ngw.url.isHTTPSContext = function() {
	return (window.location.protocol === 'https:');
};

audi_ngw.url.prefixUrlWithDomain = function(url) {
	
	if( !url ) {
		return;
	}
	
	if( url.indexOf('http://') === -1 && url.indexOf('http://') === -1 ) {

		if( window.location.protocol === 'https:' ) {
			url = audi_ngw.config.sHostHTTPS + url;
		} else {
			url = audi_ngw.config.sHostHTTP + url;
		}

	}
	

	
	return url;	
};


// initial cache
audi_ngw.url.hasXHRContentCache = "0:0";


