/**
 * Region.js
 * $URL: https://hhoettecke@svn.pvtool.org/svn/day_audi_ngw/trunk/ngw_base/frontend/js/audi/audi.navigation.js $
 *
 * @author 		$Author: hhoettecke $
 * @version     $Revision: 5894 $
 * 
 * 
 * 
 * @copyright   NEUE DIGITALE GmbH, Berlin
 */


audi_ngw.namespace(audi_ngw, 'region');

audi_ngw.region.initiate = function() {
	
	jQuery('.template-c-1 .audi_con_com_region').addClass('audi_con_com_region_js');
	
	// get the akamai-cookie and lowercase it
	var cookie = jQuery.cookie('countryselector');
	if( null !== cookie ) {
		cookie = cookie.toLowerCase();	
	}

	jQuery('.audi_region_form button.hide').show();
	
	
	jQuery('.audi_region_form').unbind('submit').bind('submit', function(){

		var $scope = jQuery(this);
		
		// get the url
		var url = jQuery('.regions[name=audi_region]', $scope).val();

		// if the url is not 0
		if( 0 !== url && 'undefined' !== typeof(url) ) {
			var selection = jQuery('option[value=' + url + ']:first');
			
			audi_ngw.config.onClickTrack(selection.metadata());
			
			// should it open in a layer or in this window?
			if( 'layerModal' == selection.attr('rel') ) {
				var hash = 'source=' + url + '&container=layerModal';
				audi_ngw.navigation.historyLoad(hash);
			} else {
				window.open(url, 'audi');
			}
			
		}
		

		
		return false;
	});
	
	// bind the continent-menu to the change-event ...and trigger it initially
	jQuery('.audi_continent').show().unbind('change').bind('change', function(){
		
		var $scope = jQuery(this).parents('.audi_region_form');

		var $country = false;
		var $continent = false;
		
		// hide all region-selects and disable them
		jQuery('.regions', $scope).hide().attr({name: '', disabled: 'disabled'});
		
		// read the value from continent-select
		var continent = jQuery('.audi_continent', $scope).val();
		
		// if we have something from the akamai-cookie
		if( null !== cookie ) {
			
			// find the country in the select
			//$country = jQuery('.regions option[value=' + cookie + ']').attr('selected', 'selected').parent();
			$country = jQuery('.regions .iso_' + cookie, $scope).attr('selected', 'selected').parent();
			if( 1 == $country.size() ) {
				
				// get the id of the select-menu (= audi_region_CONTINENT)
				var tmpContinent = $country.attr('class');

				// get just the continent from the string
				var tmp = tmpContinent.match(/audi_region_[a-zA-z]*/g);
				if( tmp ) {
					tmpContinent = tmp[0].substr(tmp[0].lastIndexOf('_') + 1, tmp[0].length);

					$continent = jQuery('.audi_' + tmpContinent, $scope);
					jQuery('.audi_continent option[value=' + tmpContinent + ']', $scope).attr('selected', 'selected');
				}
				
			}
				
			// reset the variable to null
			cookie = null;
		} 
		
		// if we found a country and a continent with the help of the cookie, show them
		if( false !== $country && false !== $continent ) {
		
			$continent.attr('disabled', '').show();
			$country.attr('disabled', '').attr({name: 'audi_region', disabled: ''}).show();
			
		// if we didn't get any help from the cookie or this is not the initial call
		} else {
				
			// depending on the selection show the regarding region-menu or just the first
			if( 0 !== continent ) {
				jQuery('.audi_region_' + continent, $scope).attr({name: 'audi_region', disabled: ''}).show();
			} else {
				jQuery('.regions:first', $scope).attr('name', 'audi_region').show().val(0);
			}
			
		}
	
		
	}).trigger('change');	
	
};

audi_ngw.region.show = function() {
	jQuery('.audi_con_com_region').removeClass('audi_con_com_region_js').show();
};



