/**
 * Init des lieux page index
 */
function lieuxInit()
{
	var magSel = $('#magasinSelect');
	var staSel = $('#stationSelect')[0];
	if (isset(formDefaults) && isset(formDefaults.station_id) && isset(formDefaults.magasin_id) ){
		for(var i=0; i < staSel.options.length; i++){
			if (staSel.options[i].value == formDefaults.station_id){
				staSel.options[i].selected = true;
				populateMagSelectWithDefaults(formDefaults.station_id, formDefaults.magasin_id);
				setPlan(formDefaults.station_id);
			}
			//$('#goOn').attr('src', '/medias/images/go_button.gif');
		}
		
		if (isset(formDefaults.debut) && isset(formDefaults.fin)){
			var fdebut = getMagDate(formDefaults.debut);
			var ffin   = getMagDate(formDefaults.fin);
			updateStartDate(fdebut);
			updateEndDate(ffin);
		}
	} else {
		magSel.disabled = true;
		setDatesState('off');
	}
}

/**
 * Init des lieux ( page station)
 */
function lieuxInitFromStation()
{
	var magSel = $('#magasinSelect');

	var stationId = $('#stationSelect').val();
	setPlan(stationId);
	populateMagSelectWithDefaults(stationId, 0);
	magSel.disabled = false;
	setDatesState('off');
	
}


/**
 * Init des lieux ( page magasin)
 */
function lieuxInitFromMagasin()
{
	var magSel = $('magasinSelect');
	magSel.disabled = false;
	setDatesState('on');
	
}

function populateMagSelectWithDefaults(staId, magId)
{
	var magSel = $('#magasinSelect')[0];
	magSel.disabled = false;
	var idx = 1;
	magSel.options.length = 1;
	$.each(magasinsDatas, function(key, magasin){
		if (magasin.parent == staId){
			magSel.options[idx] = new Option(unescape(magasin.nom), magasin.id);
			magSel.options[idx].selected = (magasin.id == magId) ? true : false;
			idx++;
		}
	});
	
}

function setPlan(stationId)
{
	// console.log(stationId)
	var pdatas;
    if (typeof curlanguage == 'undefined'){
        var curlanguage = 'fr';
    }	
	if(typeof plansDatas[stationId] != 'undefined'){
		pdatas = plansDatas[stationId];
		var planhref = '/index/plan/url/'+ pdatas.url + '/type/' + pdatas.type;
		if (stationId == 23){
			planhref = '/medias/images/dynamic/station/index_' + curlanguage + '.html';
		}
		$('#plandisplay').click( function(){
			var ww = 940;
				window.open(planhref,'popupswf','height=900,width=' + ww + ',status=yes,scrollbars=yes,menubar=no,resizable=yes');
		});
		$('#plandisplay').show();
	} else {
		$('#plandisplay').hide();
	}
}

function chooseStation()
{

	var magSel = $('#magasinSelect')[0];
	var stationId = $(this).val();
	
	if (stationId == 0){
		magSel.selectedIndex = 0;
		magSel.disabled = true;
		setDatesState('off');
		$('#plandisplay').hide();
		return;
	}
	
	setPlan(stationId);
		
	magSel.disabled = false;
	var idx = 1;
	magSel.options.length = 1;
	$.each(magasinsDatas, function(key, magasin){
		if (magasin.parent == stationId){
			magSel.options[idx] = new Option(unescape(magasin.nom), magasin.id);
			idx++;
		}
	});
	
	//si qu'un seul magasin
	/*if (magSel.options.length == 2){
		magSel.options[1].selected = true;
	}*/
	
	var resSel = $('#residenceSelect')[0];

	
	if (typeof residencesDatas != 'undefined' && residencesDatas[stationId]){

		var idx = 1;
		$('#residencesHolderText').show();
		$('#residencesHolder').show();
		$.each(residencesDatas[stationId], function(key, res){
			resSel.options[idx] = new Option(unescape(res.nom), res.id);
			idx++;
	});
	} else {
		/*
		resSel.selectedIndex = 0;
		$('#residencesHolderText').hide();
		$('#residencesHolder').hide();*/
	}
	
	//var pkCart = stationMapping[stationId].openmap_pk;
	//	charger(pkCart);
}

function setMagFromResidence()
{
	var resSel = $('#residenceSelect')[0];
	var magId  = resSel.options[resSel.selectedIndex].value;
	
	if (magId != 0 && typeof(magId) != 'undefined'){
		var magSel = $('#magasinSelect')[0];
		for(var i = 0; i < magSel.options.length; i++){
			if (magSel.options[i].value == magId){
				magSel.selectedIndex = i;
				setDatesState('on');
				return;
			}
		}
	}
}


function setDatesState(status)
{
	$('.disablable, #goOn').attr('disabled',  (status == 'on') ? false : true)
			.attr('class', (status == 'on') ? 'disablable' : 'disablable disabled');
	
	
	/*if ($('#goOn').attr('disabled')){
		$('goOn').attr('src', '/medias/images/go_button_off.gif');
	} else {
		$('goOn').attr('src', '/medias/images/go_button.gif');
	}*/
}

