var countries;
var countryregions;
var cities;
var functionsStores = {

    /*searchfield: function(){
        var searchText = $("#searchfield").attr("value");
        $("#searchfield").bind('focus', function() {
			$(this).val("");
		});
		$("#searchfield").bind('blur', function() {
			$(this).val(searchText);
		});
    },*/

    selectCountries: function(region, country){
        if(!country) var country= '';
        $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=getcountries&continentid=' + region +'&countryid='+country,
            beforeSend: function() {
                if(countries) {
                    countries = countries.remove();
                }
                if(cities) {
                    cities = cities.remove();
                }
                if(countryregions) {
                    countryregions = countryregions.remove();
                }
                $('#countriesSelector').empty();
                $('#citiesSelector').empty();
                functionsStores.displayCountryRegions(false);
			},
			success: function(msg) {
				countries = jQuery(msg).appendTo('#countriesSelector');
			}
		});
	},
	
	selectCities: function(country, countryregion, city){ //countryregion et city, paramètres non obligatoires
	   if(!city) var city = '';
	   if(!countryregion) var countryregion = '';
	   
	   if(country!=195) {
	       functionsStores.displayCountryRegions(false);    
       }
	   
	   if(country==195 && countryregion=='') {
	       functionsStores.submitForm('search');
           //functionsStores.selectCountryRegions(country);    
       } else {
            $.ajax({
    			type: 'POST',
    			url: '?ajax_stores',
    			data: 'action=getcities&countryid=' + country + '&countryregionid='+ countryregion +'&city='+city,
                beforeSend: function() {
                    if(cities) {
                        cities = cities.remove();
                    }
                    $('#citiesSelector').empty();
    			},
    			success: function(msg) {
    				cities = jQuery(msg).appendTo('#citiesSelector');
    			}
    		});
    	}
    	if(country!=195) {
    	    functionsStores.submitForm('search');
    	}
	},
	
	
	selectCountryRegions: function(country){
	   
       $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=getcountryregion&countryid=' + country,
            beforeSend: function() {
                if(countryregions) {
                    countryregions = countryregions.remove();
                }
                $('#countryRegionsSelector').empty();
			},
			success: function(msg) {
				countryregions = jQuery(msg).appendTo('#countryRegionsSelector');
				$('#countryRegionsSelector').css("display","inline");
			}
		});
		
    },
	
	displayCountryRegions: function(display) {
	    if(display==true) {
	        $('#countryRegionsSelector').css("display","inline");
        } else {
            $('#countryRegionsSelector').css("display","none");
        }  
    },
	
	/*selectStores: function(city, country, region) {
	   $.ajax({
	       type: 'POST',
	       url: '?ajax_stores',
	       data: 'action=search&regions='+region+'&countries='+country+'&cities='+city,
	       success: function (msg) {
	           stores = jQuery(msg).appendTo('#results');
	       }
	    });
    },*/
	
	showStores: function() {
	   $("div#representatives").hide(); 
	   $("div#customerservice").hide();
	   $("div#stores").show();
	   $("#btn_stores").css({background: "#666", color: "#fff"});
	   $("#btn_repres").css({background: "#000", color: "#c6c7c8"});
	   $("#btn_customer").css({background: "#000", color: "#c6c7c8"});
	   $("#option").attr("value", "stores");
	},
	
	showRepresentatives: function() {
	    $("div#stores").hide();
        $("div#customerservice").hide(); 
		$("div#representatives").show();
        $("#btn_repres").css({background: "#666", color: "#fff"});
	    $("#btn_stores").css({background: "#000", color: "#c6c7c8"});
        $("#btn_customer").css({background: "#000", color: "#c6c7c8"});
        $("#option").attr("value", "repres");     
    },
    
    showCustomerService: function() {
	    $("div#stores").hide(); 
		$("div#representatives").hide();
		$("div#customerservice").show();
		$("#btn_customer").css({background: "#666", color: "#fff"});
        $("#btn_repres").css({background: "#000", color: "#c6c7c8"});
	    $("#btn_stores").css({background: "#000", color: "#c6c7c8"});
        $("#option").attr("value", "cs");     
    },
    
    submitForm: function(action) {
        if(action=="search") {
            document.form_stores.submit() ;
        }
        /*$.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=getcities&countryid=' + country + '&countryregionid='+ countryregion +'&city='+city,
            beforeSend: function() {
                if(cities) {
                    cities = cities.remove();
                }
                $('#citiesSelector').empty();
			},
			success: function(msg) {
				cities = jQuery(msg).appendTo('#citiesSelector');
			}
		});*/
    },
    
    
    clickToGeo: function() {
        $(".btnGeo").bind('click', function() {
            $(this).attr("alt","clicked");
            functionsStores.showGeo($(this)); 
       });
    },
    
    //Affichage de la montre en ZOOM
    showGeo: function(data) {
        var id = data.attr("stid");
        //alert(pos);
        $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=geolocalize&id='+id ,
			success: function(msg) {
                //alert(msg);
                $("#map").html(msg);
                $("#map").animate({opacity: 'toggle'}, function() {
                    
                });
                $("#map").css({'display':'block', 'position':'absolute', 'top': 100});
            }
        });
    },
    
    clickToClose: function() {
        $("#btnCloseMap").bind('click', function() {
            $(this).attr("alt","clicked");
            functionsStores.closeMap($(this)); 
       });
    },
    
    closeMap: function(item) {
        //alert('test');
        $("#map").animate({opacity: 'toggle'}, function() {
            $("#map").css({'display':'none'});            
        });
            
    },
    
    clickToCheckWebsite: function() {
        $("#btnCheck").bind('click', function() {
            $(this).attr("alt","clicked");
            functionsStores.showFormWebsite($(this)); 
       });
    },
    
    showFormWebsite: function(){
        $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=drawvalidity' ,
			success: function(msg) {
                //alert(msg);
                $("#validitybox").html(msg);
                $("#validitybox").animate({opacity: 'toggle'}, function() {
                    
                });
                $("#validitybox").css({'display':'block', 'position':'absolute'});
            }
        });     
    },
    
    clickToCheckValidity: function() {
        $("#btnCheckValidity").bind('click', function() {
            $(this).attr("alt","clicked");
            functionsStores.checkValidity($(this)); 
       });
    },
    
    /**
     * Appel ajax demandant si l'adresse web est dans la base
     */         
    checkValidity: function(item){
        www = $("#address").val();
        $.ajax({
            type: 'POST',
            url: '?ajax_stores',
            data: 'action=checkvalidity&www='+www,
            success: function(msg) {
                $("#result").html(msg);
            }
        });
    },
    
    /**
     * Ferme la box "Validity"
     */         
    closeValidity: function(item) {
        $("#validitybox").animate({opacity: 'toggle'}, function() {
            $("#validitybox").css({'display':'none'});            
        });
            
    }
}; 
