$(document).ready(function() {
	
	 typeID = $('#typeID').val();
	 limoID = $('#limoID').val();
	 packageID = $('#packageID').val();
	 
	 $('#filesB').val( limoID );
	
	
	
	 brand = $('#filesB').val();
     
     // Send an AJAX request to get all the states.
	    $.get("/php/request.php?type="+ typeID +"&brand=" + brand + "&packageID=" + packageID, function(response)
	    {
	        // Append the response to the div.
	        $("#allModels").html('');
	        $("#allModels").html(response);
	        $("#packageDetails").html('<div id="packageDetails">Choisir votre package pour obtenir le prix / description</div>');
	        
	        $('#filesA').attr('name', "filesA");
	        $('#filesA').attr('id', 'filesA');
	
	        // Enable the state drop-down.
	        $('#filesA').attr('disabled', false);
	        
	        $('#filesA').change(function () {
        
		       updatePackage();   
		  
		    });
	    });    

	
	 $('#filesB').change(function () {
        
        brand = $('#filesB').val();
     
     // Send an AJAX request to get all the states.
	    $.get("/php/request.php?type="+ typeID +"&brand=" + brand + "&packageID=" + packageID, function(response)
	    {
	        // Append the response to the div.
	        $("#allModels").html('');
	        $("#allModels").html(response);
	        $("#packageDetails").html('<div id="packageDetails">Choisir votre package pour obtenir le prix / description</div>');
	        
	        $('#filesA').attr('name', "filesA");
	        $('#filesA').attr('id', 'filesA');
	
	        // Enable the state drop-down.
	        $('#filesA').attr('disabled', false);
	        
	        $('#filesA').change(function () {
        
		       updatePackage(); 
		  
		    });
	    });    
  
    });
    
   	
	$('#filesA').change(function () {
        
       updatePackage(); 
  
    });
    
    setTimeout( "updatePackage()", 700 );
     
});

$(function(){
	
	$('select#filesA').selectmenu({maxHeight: 300,menuWidth: 390});
	
	$('select#filesB').selectmenu({
		maxHeight: 300,
		menuWidth: 390,
		icons: [
			{find: '.xx'},
			{find: '.aa'},
			{find: '.bb'},
			{find: '.cc'},
			{find: '.dd'},
			{find: '.ee'},
			{find: '.ff'},
			{find: '.gg'},
			{find: '.hh'},
			{find: '.ii'},
			{find: '.jj'},
			{find: '.kk'},
			{find: '.ll'},
			{find: '.mm'},
			{find: '.nn'},
			{find: '.oo'},
		]
	});
	
});

function updatePackage() {
	
	 id = $('#filesA').val();
     
     // Send an AJAX request to get all the states.
	    $.get("/php/request.php?id=" + id, function(response)
	    {
	        // Append the response to the div.
	        $("#packageDetails").html('');
	        $("#packageDetails").html(response);
	        
	    });   	
}

function updateTimingMode( timing ) {
	if( timing == "b" ) {
		$('#timing_b').slideDown();
		$('#timing_a').slideUp();
		$('#timing').val( "1" );
		$('#re_to').slideDown();
		$('#re_to_label').slideUp();
		$('.timing_a input, .timing_a select').attr( 'disabled', 'disabled' );
		$('.timing_b input, .timing_b select').removeAttr( 'disabled' );
	} else {
		$('#timing_b').slideUp();
		$('#timing_a').slideDown();
		$('#re_to').slideUp();
		$('#re_to').val(' ');
		$('#re_to_label').slideDown();
		$('#timing').val( "0" );
		$('.timing_b input, .timing_b select').attr( 'disabled', 'disabled' );
		$('.timing_a input, .timing_a select').removeAttr( 'disabled' );
	}
	
}

//a custom format option callback
var addressFormatting = function(text){
	var newText = text;
	//array of find replaces
	var findreps = [
		{find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
		{find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
		{find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>'}
	];
	
	for(var i in findreps){
		newText = newText.replace(findreps[i].find, findreps[i].rep);
	}
	return newText;
}
