
jQuery(document).ready(function(){

function hideBlocks(anim){
		$("#shop_selector").addClass("hideThis");	
		$("#shopCard").addClass("hideThis");
		$("#shop_detail").removeClass("showThis");	
		$("#shop_detail").addClass("hideThis");		
		$(".box_display_shop").removeClass("showThis");
		$(".box_display_shop").addClass("hideThis");
		$(".infoC1").addClass("hideThis");
		//hauteur du box bleu:
		//$("#container").animate({ height: 159 }, "slow");
		//$("#container").css({"height":159});
}
function showBlocks(){
		$("#shops").removeClass("hideThis");	
		$("#shops").addClass("showThis");
		$("#shopCard").removeClass("showThis");	
		$("#shopCard").addClass("hideThis");
		$(".box_display_shop").removeClass("showThis");	
		$(".box_display_shop").addClass("hideThis");
		$(".infoC1").addClass("showThis");
		//hauteur du box bleu:
		//$("#container").animate({ height: 825 }, "slow");
		//$("#container").css({"height":825});
}
function showBlocks12(){
		$("#shops").removeClass("hideThis");		
		$(".infoC1").addClass("showThis");
//		$("#shopCard").removeClass("hideThis");	
		//hauteur du box bleu:
		//$("#container").animate({ height: 490 }, "slow");
		//$("#container").css({"height":490});
}

// Adds by californie-smaulaz

function initHideBlocks(){
        $("#sel1").removeClass("hideThis");
        $("#sel1").addClass("showThis");
        	        
		$("#sel2").removeClass("showThis");
		$("#sel2").addClass("hideThis");	
        
		$("#sel3").addClass("hideThis");
}

function showBlocksUp(){
		$("#shop_selector").removeClass("hideThis");	
		$("#shop_selector").addClass("showThis");
		$("#shopCard").removeClass("hideThis");	
		$("#shopCard").addClass("showThis");		
		$("#shop_detail").removeClass("hideThis");	
		$("#shop_detail").addClass("showThis");
		$(".infoC1").addClass("showThis");
}

function showListe23(){
    $("#sel2").removeClass("hideThis");
    $("#sel2").addClass("showThis");
}

function showIt(idEl){
    $("#"+idEl).removeClass("hideThis");
    $("#"+idEl).addClass("showThis");
}

function showOff(idEl){
    $("#"+idEl).removeClass("showThis");
    $("#"+idEl).addClass("hideThis");
}

function showCOff(idEl){
    $("."+idEl).removeClass("showThis");
    $("."+idEl).addClass("hideThis");
}

function showClassOff(idEl){
    $("."+idEl).removeClass("showThis");
    $("."+idEl).addClass("hideThis");
}

//HIDE ALL AT BEGINNING OF PROCESSUS
//$("#container").css({"height":159});
$("#shops").addClass("hideThis");	
$("#shopCard").addClass("hideThis");
$(".infoC1").addClass("hideThis");
$(".box_display_shop").addClass("hideThis");


// FLikflak

// init state parts
$("#shop_detail").addClass("hideThis");
$("#shop_selector").addClass("hideThis");

		
//Si on clique sur city, afficher les bloques
$("#worldzone").change(function(event, a, b) {    
		
        hideBlocks();
		
		var foo = [];   
        $('#worldzone :selected').each(function(i, selected){   
            foo[i] = $(selected).text();
            if(foo[i] != "---"){                
                showIt("sel2");
            } else {
                initHideBlocks();
            }         
        });

} );

$("#country").change(function(event, a, b) {
		hideBlocks();
		var foo2 = [];   
        $('#country :selected').each(function(i, selected){   
            foo2[i] = $(selected).text();
            if(foo2[i] != "---"){                
		      showIt("sel3"); 
            } else {
              showOff("sel3");
            }      
        });
} );  

// Masquer bloc 2, 3 au chargement 1
initHideBlocks();

$("#country").change(function(event, a, b) {
		hideBlocks();
} );
$("#city").change(function(event, a, b) {
		//hideBlocks();
		var foo3 = []; 
		$('#city :selected').each(function(i, selected){   
            foo3[i] = $(selected).text();
            if(foo3[i] != "---"){
        		showBlocksUp();
            } else {
                showOff("shopCard");
                showOff("shop_selector");
                showOff("shop_detail");
            }
        });
		$("#contentForm").addClass("hideThis");
} );

/* HACK FAILED
$("#state").change( function() {
      setTimeout( function() {
      if( $("#sel4").is(":visible") && $('#state :selected').val() == "---" ){
          // showOff("sel4");
          // showIt("sel4");
      }    
    }, 2000)
});
*/

/*$("#country").change(function(event, a, b) {
} );
$("#city").change(function(event, a, b) {
} );*/


/******************************************************
ADRESS ACCORDION
*******************************************************/
$("#add1 div").hide();
$("#add1").click(function(event, a, b) {
		$("#add1 div").toggle();
		$("#add4 div").hide();
		$("#add2 div").hide();
		$("#add3 div").hide();
		$("#add5 div").hide();
} );
$("#add2 div").hide();
$("#add2").click(function(event, a, b) {
		$("#add2 div").toggle();
		$("#add1 div").hide();
		$("#add4 div").hide();
		$("#add3 div").hide();
		$("#add5 div").hide();
} );
$("#add3 div").hide();
$("#add3").click(function(event, a, b) {
		$("#add3 div").toggle();
		$("#add1 div").hide();
		$("#add2 div").hide();
		$("#add4 div").hide();
		$("#add5 div").hide();
} );
$("#add4 div").hide();
$("#add4").click(function(event, a, b) {
		$("#add4 div").toggle();
		$("#add1 div").hide();
		$("#add2 div").hide();
		$("#add3 div").hide();
		$("#add5 div").hide();
} );
$("#add5 div").hide();
$("#add5").click(function(event, a, b) {
		$("#add5 div").toggle();
		$("#add1 div").hide();
		$("#add2 div").hide();
		$("#add3 div").hide();
		$("#add4 div").hide();
} );



/******************************************************
FILTER flow

Worldzone > country

*******************************************************/
    $.fn.selectChain = function (options) {
    
        var defaults = {
            key: "id",
            value: "label"
            //,country_detect: "country_detect"
        };
             
        var settings = $.extend({}, defaults, options);
        
        if (!(settings.target instanceof $)) settings.target = $(settings.target);
        
        return this.each(function () {
            var $$ = $(this);
            
            $$.change(function () {
			
                var data = null;
                if (typeof settings.data == 'string') {
                    data = settings.data + '&' + this.name + '=' + $$.val();
                } else if (typeof settings.data == 'object') {
                    data = settings.data;
                    data[this.name] = $$.val();
                } 
                
                settings.target.empty();
                
                $.ajax({
                    url: settings.url,
                    data: data,
                    type: (settings.type || 'get'),
                    dataType: 'json',
                    success: function (j) {
                    var options = [], i = 0, o = null;
                      
                    // ------------------------------------ Next part added by californie-smaulaz
        			// ------------------------------------ (generate spec "---" first list value)
        			//alert("2:"+$("#state option[index=1]").text());
                    
                    o = document.createElement("OPTION");
                    o.value = "---";
                    o.text = "---";
                    settings.target.get(0).options[0] = o;
				                        
                    var newNbIt = j.length + 1;
                    
                    var stateCount = 0;
                    var isSelCur   = 0;
                    
                    for (i = 1; i < newNbIt; i++) {
                        // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                        o = document.createElement("OPTION");
                        
                         // TODO first case
                        if (typeof(j[i - 1]) == 'object'){
                            if(j[i - 1][settings.country_detect] == "ok"){                                    
                                o.text = j[i - 1][settings.value];
                                o.setAttribute('selected', 'selected');
                            } else {
                                o.text = j[i - 1][settings.value];   
                            }
                            o.value = j[i - 1][settings.key];                                
                        } else {
                           var reg=new RegExp("[|]+", "g");
                           var tableau = j[i - 1].split(reg);
                           
                           if(tableau[1] != "" && tableau[1] == 1){
                               // This option must be pre-selected
                               o.setAttribute('selected', 'selected');
                           }                               
                           
                           o.value = tableau[2];
                           o.text  = tableau[0];
                        }
                        settings.target.get(0).options[i] = o;
                        stateCount ++;
                    }
                    // ------------------------------------ end adds     
                                            
                       
        		      // hand control back to browser for a moment
        		      setTimeout(function () {
                        
                        /*-------------------------------------------------------------------------------------------------
                         *
                         * !!! 03.11.10 - case managed by californie-smz (if state = 0, then show off this select object)
                         *
                         */ 
                         
                        // tools to get target name : settings.target.get(0).id
                             
                        if( settings.spec == 1 ){                               // Call by country select
                        
                          if( stateCount < 2 ){
                              $("#sel4").hide();
                          } else {
                              // Mask all
                              $("#sel3").hide();
                              $("#sel4").show();
                          }

                        } else if( settings.spec == 2 ){                        // Call by state select 
                            
                            // Master hack - if state current is "---", show off sel3   
                            if( $('#state').val() != "---" ){
                                $("#sel3").show();
                                $("#shop_selector").show(); 
                                $("#shop_detail").show(); 
                            } else if( $('#sel4').is(":visible") ) {
                                $("#sel3").hide();
                                $("#sel4").hide();                              // adds by californie-smz
                                $("#shop_selector").hide(); 
                                $("#shop_detail").hide();                                                               
                            }
                             
                        } else {                                                // Call by country (for loading city) OR worldzone
                            $("#sel3").show();

                        }
                        
                        /*------------------------------------------------------------------------------------------------- end adds */
                        //if(settings.spec != 2){
                            settings.target.trigger('change');
                        //}
                        
			          }, 0);
  						
                    },
                    error: function (msg,xhr, desc, er) {
                        // add whatever debug you want here.
  						//alert("sorry, an error occurred in main script, try later. ");
  						jAlert('sorry, an error occurred in main script, try later.', 'Alert Dialog');
                    }
                  });
                
            });
        });
    };
    


    $.fn.selectChainUp = function (options) {
    
        var defaults = {
            key: "id",
            value: "label"
            //,country_detect: "country_detect"
        };
             
        var settings = $.extend({}, defaults, options);

        if (!(settings.target instanceof $)) settings.target = $(settings.target);
        
        return this.each(function () {
            var $$ = $(this);
            
            $$.change(function () {

if(settings.target.get(0).id == "city"){
    
}
			
if( $("#state option[index=1]").text() && $("#sel4").is(":visible") ){ 
                var data = null;
                if (typeof settings.data == 'string') {
                    data = settings.data + '&' + this.name + '=' + $$.val();
                } else if (typeof settings.data == 'object') {
                    data = settings.data;
                    data[this.name] = $$.val();
                }
                
                settings.target.empty();
}
                 
                
                $.ajax({
                    url: settings.url,
                    data: data,
                    type: (settings.type || 'get'),
                    dataType: 'json',
                    success: function (j) {

if( $("#state option[index=1]").text() && $("#sel4").is(":visible") ){                     // V1

                    var options = [], i = 0, o = null;
                    
                    //alert("1:"+$("#state option[index=1]").text());
                    o = document.createElement("OPTION");
                    o.value = "---";
                    o.text = "---";
                    settings.target.get(0).options[0] = o;
				                        
                    var newNbIt = j.length + 1;
                    var stateCount = 0;
                    var isSelCur   = 0;
                    
                    for (i = 1; i < newNbIt; i++) {
                        o = document.createElement("OPTION");
                        if (typeof(j[i - 1]) == 'object'){
                            if(j[i - 1][settings.country_detect] == "ok"){                                    
                                o.text = j[i - 1][settings.value];
                                o.setAttribute('selected', 'selected');
                            } else {
                                o.text = j[i - 1][settings.value];   
                            }
                            o.value = j[i - 1][settings.key];                                
                        } else {
                           var reg=new RegExp("[|]+", "g");
                           var tableau = j[i - 1].split(reg);
                           
                           if(tableau[1] != "" && tableau[1] == 1){
                               o.setAttribute('selected', 'selected');
                           }                               
                           
                           o.value = tableau[2];
                           o.text  = tableau[0];
                        }
                        settings.target.get(0).options[i] = o;
                        stateCount ++;
                    }
                      // hand control back to browser for a moment
        		      setTimeout(function () {
                        
                        //if( $("#state option[index=1]").text() != "undefined" ){
                         
                          // Master hack - if state current is "---", show off sel3   
                          if( $('#state').val() != "---" ){
                              $("#sel3").show();
                              $("#shop_selector").show(); 
                              $("#shop_detail").show(); 
                          } else if( $('#sel4').is(":visible") ) {
                              $("#sel3").hide();
                              $("#shop_selector").hide(); 
                              $("#shop_detail").hide();                                                               
                          }
                        
                        //}
                             
                        settings.target.trigger('change');
                        
			          }, 0);

} /*else {//if(  $("#city option[index=1]").text() != "" ) {
    alert( "YO"+ $("#city option[index=1]").text() );
    // settings.target.trigger('change');    
    //$("#city").trigger('change');
} */ 
						
                    },
                    error: function (msg,xhr, desc, er) {
                        // add whatever debug you want here.
  						//alert("sorry, an error occurred in main script, try later. ");
  						//jAlert('sorry, an error occurred in main script, try later.', 'Alert Dialog');
                    }
                  });
                
            });
        });

        
    };


    
    
       
   
    
	
/******************************************************
GMAP <> list of shops

*******************************************************/
$.fn.selectShop = function (options) {
    
     var defaults = {
        key: "id",
        value: "label"
     };
        
     var settings = $.extend({}, defaults, options);
  	 var $$ = $(this);

     $$.change(function () {
     
     var data = null;
     var postData = settings.data.serialize()+"&country="+$('#country').val();
     $.ajax({ 	
		type: 'post',
		datatype: 'html',
		url: 'includes/viewArrayGmap.php',
		data: settings.data,
		success: function (data) {
		    //ouvrir
             //$('#content_file_ajax_GMAP').hide();
             $('#content_file_ajax_GMAP').html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax_GMAP').empty().html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax_GMAP').html(data);
             //$('#content_file_ajax_GMAP').show();
	    },
		error: function (msg,xhr, desc, er) {
			// add whatever debug you want here.
			//alert("sorry, an error occurred from google map, try later. ");
			jAlert('sorry, an error occurred in google map script, try later.', 'Alert Dialog');
		}
	 });     
	 $.ajax({ 	
		type: 'post',
		datatype: 'html',
		url: 'includes/viewArrayCard.php',
		data: postData,
		success: function (data) { //ouvrir
			//alert(data);
             $('#content_file_ajax_shop').hide();
             $('#content_file_ajax_shop').html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax_shop').empty().html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax_shop').html(data);
             $('#content_file_ajax_shop').show();            
	    },
		error: function (msg,xhr, desc, er) {
			// add whatever debug you want here.
			//alert("sorry, an error occurred from shop, try later. ");
			jAlert('sorry, an error occurred in shop script, try later.', 'Alert Dialog');
		}
	 	});
	$.ajax({ 	
		type: 'post',
		datatype: 'html',
		url: 'includes/viewArrayRow.php',
		data: postData,
		success: function (data) { //ouvrir
             $('#content_file_ajax').hide();
             $('#content_file_ajax').html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax').empty().html('<img src="templates/prototype/data/loading.gif" />');
             $('#content_file_ajax').html(data);
             $('#content_file_ajax').show();          
	    },
		error: function (msg,xhr, desc, er) {
			// add whatever debug you want here.
			//alert("sorry, an error occurred from list of shops, try later.");
			jAlert('sorry, an error occurred in shops script, try later.', 'Alert Dialog');
		}
	 	});

	 });   
};

});

