$(document).ready( function(){        
	/*when click on a state, browse cities page with the cities for the specified State and Country*/
	$(".browseCityState").click(function(ev){
                             ev.preventDefault();
		var CountryID = $("#SelCountry").val();
		var StateID = $(this).attr("id");
		// window.location = 'content=browse_cities&country='+CountryID+'&state='+StateID;
                             window.location = baseUrl + $(this).attr('href');   
	});
	if($("#countrySel").val() && $("#countrySel").val() >0){
		$("#SelCountry").val($("#countrySel").val());
	}
	
	$("#SelCountry").change( function(){
			getCitiesByCountry(); 
			getStatesByCountry();
	});
	
	getCitiesByCountry();
	getCitiesCountByCountry();
	getStatesByCountry();
});


function getCitiesByCountry(){
	var CountryID = $("#SelCountry").val();        
	if(CountryID > 0){
                
		var param = {className : 'City', func: 'getFeaturedCityByCountry','CountryID':CountryID};
		$.post(baseUrl+"ajax_manageadminrss.php", param,
				function(data, textStatus){                                       
					if(textStatus != 'success' || data == '-1'){
						alert("Sorry, there's been an error in processing the request. Please try again!");
						return;
					}							
					//fill in the lists #listCities1, #listCities2
					$("#listCities1").html('');
					$("#listCities2").html('');
					if(data.length >0 ){
						var nList1 = parseInt(data.length/2 + data.length%2);
						var nList2 = data.length;
						for(var i=0; i< nList1; i++){
                                                       
							if($("#selCity").val() && $("#selCity").val() == data[i].CityID){
								cls = ' active';
							}else{
								cls = '';
							}
							var el = document.createElement("LI");
							el.innerHTML = '<a href="'+baseUrl+data[i].PathUrl+'" title="'+data[i].City+'" class='+cls+'>'+data[i].City+'</a>';
							$("#listCities1").append(el);
							
						}
						for(i= nList1; i< nList2; i++){
							if($("#selCity").val() && $("#selCity").val() == data[i].CityID){
								cls = ' active';
							}else{
								cls = '';
							}
							el = document.createElement("LI");
							el.innerHTML = '<a href="'+baseUrl+data[i].PathUrl+'" title="'+data[i].City+'" class='+cls+'>'+data[i].City+'</a>';
							$("#listCities2").append(el);							
						}
                                                $("#moreCities").attr('href','/'+data[0].PathUrl.substr(0, data[0].PathUrl.indexOf("/")))
						getCitiesCountByCountry();
					}
				}
		,"json");
	}
}

function getCitiesCountByCountry(){
	var CountryID = $("#SelCountry").val();
	if(CountryID > 0){
		var param = {className : 'City', func: 'getFeaturedCityCountryCount','CountryID':CountryID};
		$.post(baseUrl+"ajax_manageadminrss.php", param,
				function(data, textStatus){
					if(textStatus != 'success' || data == '-1'){
						alert("Sorry, there's been an error in processing the request. Please try again!");
						return;
					}	
					if(data > 2){
						$("#moreCities").css({"display":"block"});
					}
		},"text");
		
	}
}

function getStatesByCountry(){
	var CountryID = $("#SelCountry").val();
	if(CountryID > 0){
		var param = {className : 'State', func: 'getStateByCountry','CountryID':CountryID};
		$.post(baseUrl+"ajax_manageadminrss.php", param,
				function(data, textStatus){
					if(textStatus != 'success' || data == '-1'){
						alert("Sorry, there's been an error in processing the request. Please try again!");
						return;
					}
					//fill in the lists #listStates1, #listStates2
					$("#listStates1").html('');
					$("#listStates2").html('');
					if(data.length >0 ){
						var nList1 = parseInt(data.length/2 + data.length%2);
						var nList2 = data.length;
						for(var i=0; i< nList1; i++){
							if($("#stateSel").val() && $("#stateSel").val() == data[i].StateID){
								cls = ' active';
							}else{
								cls = '';
							}
							var el = document.createElement("LI");
							el.innerHTML = '<a href="'+baseUrl+data[i].PathUrl+'" class="browseCityState '+cls+'" id="'+data[i].StateID+'" title="'+data[i].State+'">'+data[i].State+'</a>';
							$("#listStates1").append(el);
						}
						for(i= nList1; i< nList2; i++){
							if($("#stateSel").val() && $("#stateSel").val() == data[i].StateID){
								cls = ' active';
							}else{
								cls = '';
							}
							var el = document.createElement("LI");
							el.innerHTML = '<a href="'+baseUrl+data[i].PathUrl+'" class="browseCityState ' +cls+ '" id="'+data[i].StateID+'" title="'+data[i].State+'">'+data[i].State+'</a>';
							$("#listStates2").append(el);
						}

					}
				}
		,"json");
	}
}

