﻿
$(document).ready(function () {
    var app = angular.module("StateApp", ['ngRoute']).directive('emitNgRenderCountry', function () {
        return function (scope) {
            if (scope.$last) {
                scope.$emit('NgRenderCountry');
            }
        };
    }).directive('emitNgRenderState', function () {
        return function (scope) {
            if (scope.$last) {
                scope.$emit('NgRenderState');
            }
        };
    }).directive('emitNgRenderCounty', function () {
        return function (scope) {
            if (scope.$last) {
                scope.$emit('NgRenderCounty');
            }
        };
    }).directive('emitNgRenderCity', function () {
        return function (scope) {
            if (scope.$last) {
                scope.$emit('NgRenderCity');
            }
        };
    }).directive('emitNgRenderNeighborhood', function () {
        return function (scope) {
            if (scope.$last) {
                scope.$emit('NgRenderNeighborhood');
            }
        };
    });

    app.controller("CountryController", ["$scope", "$window", "$http", function ($scope, $window, $http) {
        /*objects for indicate loading process*/
        var loadCountryObj = [{
            CountryID: 0,
            Country1: "Loading...",
            CountryURL: ""
        }];
        var loadStateObj = [{
            StateID: 0,
            State1: "Loading...",
            StateURL: "",
			Abbr:""
        }];
        var emptyStateObj = [{
            StateID: 0,
            State1: "No data",
            StateURL: "",
			Abbr:""
        }];
        var loadCountyObj = [{
            CountyID: 0,
            CountyName: "Loading...",
            CountyURL: ''
        }];
        /*Empty objects for location without data*/
        var emptyCountyObj = [{
            CountyID: 0,
            CountyName: "No data",
            CountyURL: ''
        }];
        var loadCityObj = [{
            RegionID: 0,
            City: "Loading...",
            RegionURL: ''
        }];
        var emptyCityObj = [{
            RegionID: 0,
            City: "No data",
            RegionURL: ''
        }];
		 var loadNeighborhoodObj = [{
            NeighborhoodID: 0,
            Neighborhood: "Loading...",
            NeighborhoodURL: ''
        }];
        var emptyNeighborhoodObj = [{
			NeighborhoodID: 0,
            Neighborhood: "No data",
            NeighborhoodURL: ''
        }];
        $scope.HidelocationList = false;//variable for hidding  list after submit event
        /*load countries*/
        $scope.countries = loadCountryObj;
        $scope.allCountries = loadCountryObj

        $scope.SelectedCountryID = 0;
        $scope.SelectedStateID = 0;
        $scope.SelectedCountyID = 0;
        $scope.SelectedCityID = 0;
		$scope.SelectedNeighborhoodID = 0;
		$scope.SelectedNYKACatID=0;
				
				
        $scope.DefSelectedCountryName = "Country";
        $scope.DefSelectedStateName = "State";
		$scope.DefSelectedStateAbbr = "State";
        $scope.DefSelectedCountyName = "County";
        $scope.DefSelectedCityName = "City";
		$scope.DefSelectedNeighborhoodName = "Neighborhood";
				
        $scope.SelectedCountryName = "Country";
        $scope.SelectedStateName = "State";
        $scope.SelectedCountyName = "County";
        $scope.SelectedCityName = "City";
		$scope.SelectedNeighborhoodName = "Neighborhood";
		$scope.SelectedNYKACatName="";
				
        $scope.SelectedCountryURL = "Country";
        $scope.SelectedStateURL = "State";
        $scope.SelectedCountyURL = "County";
        $scope.SelectedCityURL = "City";
		$scope.DefSelectedNeighborhoodURL = "Neighborhood";
		
		$scope.SelectedNYKACatURL=""
		$scope.SelectedMediaType="board";
		$scope.SelectedStateAbbr = "State";
				
		$scope.RedirectURL = "";
		$scope.SearchWhere = "";		
        var hideCssClass = "hide-location";
        var nonActiveCssClass = "non-active-location";
		$scope.CountryDdlClass = nonActiveCssClass;
        $scope.StateDdlClass = nonActiveCssClass;
        $scope.CountyDdlClass = nonActiveCssClass;
        $scope.CityDdlClass = nonActiveCssClass;
		$scope.NeighborhoodDdlClass = nonActiveCssClass;
        $scope.init = function (countryId, stateId, countyId, cityId, neighborhoodId, currMode, NYKACatID, NYKACatName,NYKACatURL, isCallFromDdl) { 

			$scope.SelectedNYKACatID=NYKACatID;
			$scope.SelectedNYKACatURL=NYKACatURL;
			$scope.SelectedNYKACatName=NYKACatName;
			$scope.SelectedCountyID =countyId
			$scope.neighborhoodList = loadNeighborhoodObj;
			$scope.allneighborhoodList = loadNeighborhoodObj;
			$.ajax({
				url: '/api/location/GetAllCountries',
				type: 'GET',
				async:false,
				dataType: 'json', // added data type
				success: function(data) {
					
					//save selected
					var CountryAllItemObj = [{
						CountryID: -1,
						Country1: "World",
						CountryURL: "world"
					}];
					$scope.allCountries = CountryAllItemObj.concat(data)
				
					for (var i = 0; i < $scope.allCountries.length; i++) {
						if ($scope.allCountries[i].CountryID == countryId) {
							$scope.SelectedCountryID = countryId;
							$scope.SelectedCountryName = $scope.allCountries[i].Country1;
							$scope.SelectedCountryURL = $scope.allCountries[i].CountryURL;
							
							break;
						}
						
					}
				//	if (countryId != '' && stateId == '' && countyId == '' && cityId == '')
					//	$scope.SetBoardURL();
					
				}
			});
								
            if (countryId == 1) {
                if (currMode == 'S' || currMode == 'C' || currMode == 'L'|| currMode == 'N') {

                    $scope.stateList = loadStateObj;
					$scope.allstateList = loadStateObj;
					$.ajax({
						url: '/api/location/GetAllStates',
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							if (data.length == 0) {
								$scope.stateList = emptyStateObj;
								$scope.allstateList = emptyStateObj;
							}
							else {
								var stateAllItemObj = [{
									StateID: -1,
									State1: "All States",
									StateURL: "",
									Abbr:"State"
								}];

								$scope.stateList = stateAllItemObj.concat(data)
								for (var i = 0; i < $scope.stateList.length; i++) {
									if ($scope.stateList[i].StateID == stateId) {
										$scope.SelectedStateID = stateId;
										$scope.SelectedStateName = $scope.stateList[i].State1;
										$scope.SelectedStateURL = $scope.stateList[i].StateURL;
										$scope.SelectedStateAbbr = $scope.stateList[i].Abbr;
										break;
									}
								}	
								if(!$scope.$$phase) {
								  $scope.$digest();
								}					
								//if (countryId != '' && stateId != '' && countyId == '' && cityId == '')
							//		$scope.SetBoardURL();							
								$scope.allstateList =$scope.stateList 
							}
						}	
					});
					  $scope.MarkDdl('', '', '',hideCssClass);
                  
                    if (currMode == 'S') {
                        $scope.countyList = loadCountyObj;
						$scope.allcountyList = loadCountyObj;
						$.ajax({
							url: '/api/location/GetAllCountiesForStates?stateId=' + stateId,
							type: 'GET',
							async:false,
							dataType: 'json', // added data type
							success: function(data) {
								 if (data.length == 0) {
									$scope.countyList = emptyCountyObj;
								}
								else {
									var CountyAllItemObj = [{
										CountyID: -1,
										CountyName: "All Counties",
										CountyURL: ""
									}];
												
									$scope.countyList = CountyAllItemObj.concat(data) 
										for (var i = 0; i < $scope.countyList.length; i++) {
											if ($scope.countyList[i].CountyID == countyId) {
												$scope.SelectedCountyID = countyId;
												$scope.SelectedCountyName = $scope.countyList[i].CountyName;
												$scope.SelectedCountyURL = $scope.countyList[i].CountyURL;
												break;
											}
										}
										
										$scope.allcountyList=$scope.countyList
										if(!$scope.$$phase) {
										  $scope.$digest();
										}
									//	if (countryId != '' && stateId != '' && countyId != '' && cityId == '')
									//			$scope.SetBoardURL();					
									}
							}	
						});
						
						
                       
                        //load cities
                        $scope.cityList = loadCityObj;
						$scope.allcityList = loadCityObj;
						$.ajax({
							url: '/api/location/GetAllStateCities?stateID=' + stateId,
							type: 'GET',
							async:false,
							dataType: 'json', // added data type
							success: function(data) {
								 if (data.length == 0) {
									$scope.cityList = emptyCityObj;
								}
								else {
									var CityAllItemObj = [{
										RegionID: -1,
										City: "All Cities",
										RegionURL: ""
									}];

									$scope.cityList = CityAllItemObj.concat(data)
									$scope.allcityList=$scope.cityList
									if(!$scope.$$phase) {
									  $scope.$digest();
									}
								//	if (countryId != '' && stateId != '' && countyId != '' && cityId != '')
									//	$scope.SetBoardURL();
								}
							}	
						});
						
                       $scope.MarkDdl('', '', '',hideCssClass);
                    }
                }
                if (currMode == 'C' || currMode == 'L'|| currMode == 'N') {
                    $scope.countyList = loadCountyObj;
					$scope.allcountyList = loadCountyObj;
					$.ajax({
						url: '/api/location/GetAllCountiesForStates?stateId=' + stateId,
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							 if (data.length == 0) {
								$scope.countyList = emptyCountyObj;
								$scope.allcountyList = emptyCountyObj;
							}
							else {
								var CountyAllItemObj = [{
									CountyID: -1,
									CountyName: "All Counties",
									CountyURL: ""
								}];

								$scope.countyList = CountyAllItemObj.concat(data) 
								$scope.allcountyList=$scope.countyList
								for (var i = 0; i < $scope.countyList.length; i++) {
									if ($scope.countyList[i].CountyID == countyId) {
										$scope.SelectedCountyID = countyId;
										$scope.SelectedCountyName = $scope.countyList[i].CountyName;
										$scope.SelectedCountyURL = $scope.countyList[i].CountyURL;
										break;
									}
								}
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
								
								//if (countryId != '' && stateId != '' && countyId != '' && cityId == '')
								//				$scope.SetBoardURL();						 
							}
						}		
					});
					 $scope.MarkDdl('', '', '',hideCssClass);
                   
                    if (currMode == 'C') {
                        $scope.cityList = loadCityObj;
						$scope.allcityList = loadCityObj;
						$.ajax({
							url: '/api/location/GetAllCitiesForCounty?countyId=' + countyId,
							type: 'GET',
							async:false,
							dataType: 'json', // added data type
							success: function(data) {
								 if (data.length == 0) {
									$scope.cityList = emptyCityObj;
									$scope.allcityList = emptyCityObj;
								}
								else {
									var CityAllItemObj = [{
										RegionID: -1,
										City: "All Cities",
										RegionURL: ""
									}];

									$scope.cityList = CityAllItemObj.concat(data)
									$scope.allcityList =$scope.cityList 
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
								//if (countryId != '' && stateId != '' && countyId != '' && cityId != '')
								//				$scope.SetBoardURL();	
								}
							}		
						});
						
                       $scope.MarkDdl('', '', '',hideCssClass);
                    }
                }
                if (currMode == 'L'|| currMode == 'N') {
                    $scope.cityList = loadCityObj;
					$scope.allcityList = loadCityObj;
					$.ajax({
						url: '/api/location/GetAllCitiesForCounty?countyId=' + countyId,
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							 if (data.length == 0) {
								$scope.cityList = emptyCityObj;
								$scope.allcityList = emptyCityObj;
							}
							else {
								var CityAllItemObj = [{
									RegionID: -1,
									City: "All Cities",
									RegionURL: ""
								}];

								$scope.cityList = CityAllItemObj.concat(data)
								$scope.allcityList=$scope.cityList
								for (var i = 0; i < $scope.cityList.length; i++) {
									if ($scope.cityList[i].RegionID == cityId) {
										$scope.SelectedCityID = cityId;
										$scope.SelectedCityName = $scope.cityList[i].City;
										$scope.SelectedCityURL = $scope.cityList[i].RegionURL;
										break;
									}
								}
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
								//if (countryId != '' && stateId != '' && countyId != '' && cityId != '')
								//				$scope.SetBoardURL();	
							
							
							 $scope.MarkDdl('', '', '',hideCssClass);
								$scope.neighborhoodList = loadNeighborhoodObj;
								$scope.allneighborhoodList = loadNeighborhoodObj;
								$.ajax({
									url: '/api/location/GetAllneighbourhoodsForCity?CityID=' + cityId,
									type: 'GET',
									async:false,
									dataType: 'json', // added data type
									success: function(data) {
										 if (data.length == 0) {
											$scope.neighborhoodList = emptyNeighborhoodObj;
											$scope.allneighborhoodList = emptyNeighborhoodObj;
										}
										else {
											
											var NeighborhoodAllItemObj = [{
												NeighborhoodID: -1,
												NeighborhoodName: "All Neighborhoods",
												NeighborhoodURL: ""
											}];
										
											$scope.neighborhoodList = NeighborhoodAllItemObj.concat(data)
											$scope.allneighborhoodList=$scope.neighborhoodList
											
											if(!$scope.$$phase) {
											  $scope.$digest();
											}
											//if (countryId != '' && stateId != '' && countyId != '' && cityId != '')
											//				$scope.SetBoardURL();	
										// $scope.MarkDdl('', '', '','');
											$scope.MarkDdl('', '', '','');
											//$scope.CountryDdlClass = hideCssClass;
											//document.getElementById("showFullBar").style.display="inline-block";
										}
									}		
								});
							
							}
						}		
					});
						
					if (currMode == 'N') {
						$scope.neighborhoodList = loadNeighborhoodObj;
						$scope.allneighborhoodList = loadNeighborhoodObj;
						$.ajax({
							url: '/api/location/GetAllneighbourhoodsForCity?CityID=' + cityId,
							type: 'GET',
							async:false,
							dataType: 'json', // added data type
							success: function(data) {
								 if (data.length == 0) {
									$scope.neighborhoodList = emptyNeighborhoodObj;
									$scope.allneighborhoodList = emptyNeighborhoodObj;
								}
								else {
									var NeighborhoodAllItemObj = [{
										NeighborhoodID: -1,
										NeighborhoodName: "All Neighborhoods",
										NeighborhoodURL: ""
									}];
								
									$scope.neighborhoodList = NeighborhoodAllItemObj.concat(data)
									$scope.allneighborhoodList=$scope.neighborhoodList
									for (var i = 0; i < $scope.neighborhoodList.length; i++) {
										if ($scope.neighborhoodList[i].NeighborhoodID == neighborhoodId) {
											$scope.SelectedNeighborhoodID = neighborhoodId;
											$scope.SelectedNeighborhoodName = $scope.neighborhoodList[i].NeighborhoodName;
											$scope.SelectedNeighborhoodURL = $scope.neighborhoodList[i].NeighborhoodUrl;
											break;
										}
									}
									if(!$scope.$$phase) {
									  $scope.$digest();
									}
									//if (countryId != '' && stateId != '' && countyId != '' && cityId != '')
									//				$scope.SetBoardURL();	
								}
							}		
						});
						
						
						if (neighborhoodId>0){
							$scope.MarkDdl('', '', '','');
							//$scope.CountryDdlClass = hideCssClass;
							//document.getElementById("showFullBar").style.display="inline-block";
						}
					   // $scope.MarkDdl(hideCssClass, hideCssClass, '','');
					}
                }//load states for USA
                else {// (currMode == '' || currMode == 'Y')for united-states url//NEED check why MODE = ''
                    $scope.stateList = loadStateObj;
					$scope.allstateList = loadStateObj;
					$.ajax({
						url: '/api/location/GetAllStates',
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							 if (data.length == 0) {
								$scope.stateList = emptyStateObj;
								 $scope.allstateList = emptyStateObj;
							}
							else {
								var stateAllItemObj = [{
									StateID: -1,
									State1: "All States",
									StateURL: "",
									Abbr:"State"
								}];

								$scope.stateList = stateAllItemObj.concat(data)
								$scope.allstateList=$scope.stateList
								for (var i = 0; i < $scope.stateList.length; i++) {
									if ($scope.stateList[i].StateID == stateId) {
										$scope.SelectedStateID = stateId;
										$scope.SelectedStateName = $scope.stateList[i].State1;
										$scope.SelectedStateURL = $scope.stateList[i].StateURL;
										$scope.SelectedStateAbbr = $scope.stateList[i].Abbr;
										break;
									}
								}
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
							//	if (countryId != '' && stateId != '' && countyId == '' && cityId == '')
								//				$scope.SetBoardURL();	
							}
						}	
					});
					
                  $scope.MarkDdl('', '', '',hideCssClass);
                }
                //mark ddl
                if (currMode == 'S' || currMode == 'C' || currMode == 'L'|| currMode == 'N') {
				//	$scope.MarkDdl('', '', '','');
                   
					
                    if (!isCallFromDdl) {
                      //  $scope.MarkDdl('', '', '','');
                    }
                    else {
                        if (currMode == 'S') {
                            $scope.SelectedCountyName = $scope.DefSelectedCountyName;
                            $scope.SelectedCityName = $scope.DefSelectedCityName;

                            $scope.SelectedCountyURL = "";
                            $scope.SelectedCityURL = "";

                            $scope.SelectedCountyID = 0;
                            $scope.SelectedCityID = 0;
							$scope.SelectedNeighborhoodName=$scope.DefSelectedNeighborhoodName;
							$scope.SelectedNeighborhoodURL=""
							$scope.SelectedNeighborhoodID=0;
                        }
                        else if (currMode == 'C') {
                            $scope.SelectedCityName = $scope.DefSelectedCityName;
                            $scope.SelectedCityURL = "";

                            $scope.SelectedCityID = 0;
							$scope.SelectedNeighborhoodName=$scope.DefSelectedNeighborhoodName;
							$scope.SelectedNeighborhoodURL=""
							$scope.SelectedNeighborhoodID=0;
                        }
                    }
                }
                else {
                   // $scope.MarkDdl('', nonActiveCssClass, nonActiveCssClass,hideCssClass);
                    $scope.countyList = emptyCountyObj;
                    $scope.cityList = emptyCityObj;
					$scope.neighborhoodList = emptyNeighborhoodObj;

                    
                    $scope.SelectedStateName = $scope.DefSelectedStateName;
                    $scope.SelectedCountyName = $scope.DefSelectedCountyName;
                    $scope.SelectedCityName = $scope.DefSelectedCityName;
					$scope.SelectedNeighborhoodName=$scope.DefSelectedNeighborhoodName;
									
                    $scope.SelectedStateURL = "";
                    $scope.SelectedCountyURL = "";
                    $scope.SelectedCityURL = "";
					$scope.SelectedNeighborhoodURL=""
					
                    $scope.SelectedStateID = 0;
                    $scope.SelectedCountyID = 0;
                    $scope.SelectedCityID = 0;
					$scope.SelectedNeighborhoodID=0;
                }
				
				
			//	if (cityId>0){
				//	$scope.MarkDdl('', '', '','');
				//}else
			//	{
				//	$scope.MarkDdl('', '', '',hideCssClass);
				//}
				
            }
            else {
                $scope.MarkDdl(hideCssClass, hideCssClass, '',hideCssClass);
                if (currMode == 'L') {
                    $scope.cityList = loadCityObj;
					$scope.allcityList = loadCityObj;
					$.ajax({
						url: '/api/location/GetAllCountryCities?countryId=' + countryId,
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							 if (data.length == 0) {
								$scope.cityList = emptyCityObj;
								$scope.allcityList = emptyCityObj;
							}
							else {
								var CityAllItemObj = [{
									RegionID: -1,
									City: "All Cities",
									RegionURL: ""
								}];

								$scope.cityList = CityAllItemObj.concat(data)
								$scope.allcityList =$scope.cityList 
								//save selected
								for (var i = 0; i < $scope.cityList.length; i++) {
									if ($scope.cityList[i].RegionID == cityId) {
										$scope.SelectedCityID = cityId;
										$scope.SelectedCityName = $scope.cityList[i].City;
										$scope.SelectedCityURL = $scope.cityList[i].RegionURL;
										break;
									}
								}
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
								//if (countryId != ''  && cityId != '')
							//		$scope.SetBoardURL();
							}	
						}	
					});
					
                  
                }else if (currMode == 'W') {
                     $scope.MarkDdl(hideCssClass, hideCssClass, hideCssClass,hideCssClass);
									
                }
                else {
                    $scope.cityList = loadCityObj;
					$scope.allcityList = loadCityObj;
					$.ajax({
						url: '/api/location/GetAllCountryCities?countryId=' + countryId,
						type: 'GET',
						async:false,
						dataType: 'json', // added data type
						success: function(data) {
							 if (data.length == 0) {
								$scope.cityList = emptyCityObj;
								$scope.allcityList = emptyCityObj;
							}
							else {
								var CityAllItemObj = [{
									RegionID: -1,
									City: "All Cities",
									RegionURL: ""
								}];

								$scope.cityList = CityAllItemObj.concat(data)
								$scope.allcityList = $scope.cityList 
								if(!$scope.$$phase) {
								  $scope.$digest();
								}
								
								//if (countryId != ''  && cityId == '')
								//	$scope.SetBoardURL();

								}
						}	
					});
					
                }
				
				
            }
			if (countryId != '' || stateId != '' || countyId != '' || cityId != ''|| neighborhoodId != ''){
					$scope.SetBoardURL();
				}
			
        }
        $scope.rebindAllCountries = function () {
            $scope.countries = $scope.allCountries;
            $scope.$apply();
        }
			$scope.rebindAllStates = function () {
            $scope.stateList = $scope.allstateList;
            $scope.$apply();
        }
			$scope.rebindAllCounties = function () {
            $scope.countyList = $scope.allcountyList;
            $scope.$apply();
        }
			$scope.rebindAllCities = function () {
            $scope.cityList = $scope.allcityList;
            $scope.$apply();
        }
			$scope.rebindAllNeighborhoods = function () {
            $scope.neighborhoodList = $scope.allneighborhoodList;
            $scope.$apply();
        }
        /*functions for loading data:begin*/
        $scope.loadStateOrCities = function (countryId, countryName, countryURL,isSubmitform) {
            $scope.saveSelectedLocation(countryName, countryId, countryURL,
                $scope.DefSelectedStateName, 0, "",
                $scope.DefSelectedCountyName, 0, "",
                $scope.DefSelectedCityName, 0, "",$scope.DefSelectedStateAbbr,
				$scope.DefSelectedNeighborhoodName, 0, "");

            if (countryId == 1) {
                $scope.MarkDdl('', nonActiveCssClass, nonActiveCssClass,hideCssClass);
                $scope.stateList = loadStateObj;
				$scope.allstateList = loadStateObj;
                $http.get('/api/location/GetAllStates').success(function (data, status, headers, config) {
                    if (data.length == 0) {
                        $scope.stateList = emptyStateObj;
						$scope.allstateList = emptyStateObj;
                    }
                    else {
                       var stateAllItemObj = [{
							StateID: -1,
							State1: "All States",
							StateURL: "",
							Abbr:"State"
						}];

						$scope.stateList = stateAllItemObj.concat(data)
						$scope.allstateList=	$scope.stateList
                    }
                }).error(function (data, status, headers, config) {
                    console.log("Error wit loading data: /api/location/GetAllStates")
                });
            }
            else {
				 if (countryId != -1) {
					$scope.MarkDdl(hideCssClass, hideCssClass, '',hideCssClass);
					$scope.cityList = loadCityObj
					$scope.allcityList = loadCityObj
					$http.get('/api/location/GetAllCountryCities?countryId=' + countryId).success(function (data, status, headers, config) {
						if (data.length == 0) {
							$scope.cityList = emptyCityObj;
							$scope.allcityList = emptyCityObj;
						}
						else {
							var CityAllItemObj = [{
								RegionID: -1,
								City: "All Cities",
								RegionURL: ""
							}];

							$scope.cityList = CityAllItemObj.concat(data)
							$scope.allcityList=$scope.cityList
						}
					}).error(function (data, status, headers, config) {
						console.log("Error with loading data: /api/location/GetAllCountryCities")
					});
				 }else{
					$scope.MarkDdl(hideCssClass, hideCssClass, hideCssClass,hideCssClass);
				}	
            }
							//if (isSubmitform)
							//	$scope.RedirectToLocation();
        }
        $scope.loadCountyForState = function (stateId, stateName, stateURL,stateAbbr,isSubmitform) {
            if (stateId != 0) {
				if (stateId == -1 ) {
						stateName=stateName.replace("All ","")
				}
                $scope.saveSelectedLocation($scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedCountryURL,
                    stateName, stateId, stateURL,
                    $scope.DefSelectedCountyName, 0, "",
                    $scope.DefSelectedCityName, 0, "",stateAbbr,
					$scope.DefSelectedNeighborhoodName, 0, "");
            }
            $scope.MarkDdl('', '', '',hideCssClass);
            $scope.countyList = loadCountyObj;
			 $scope.allcountyList = loadCountyObj;
            $http.get('/api/location/GetAllCountiesForStates?stateId=' + stateId).success(function (data, status, headers, config) {
                if (data.length == 0) {
                    $scope.countyList = emptyCountyObj;
					$scope.allcountyList = emptyCountyObj;
                }
                else {
                    var CountyAllItemObj = [{
						CountyID: -1,
						CountyName: "All Counties",
						CountyURL: ""
					}];

					$scope.countyList = CountyAllItemObj.concat(data)
					$scope.allcountyList=$scope.countyList
                }
            }).error(function (data, status, headers, config) {
                console.log("Error with loading data: /api/location/GetAllCountries")
            });
            //load city for state
            $http.get('/api/location/GetAllStateCities?stateID=' + stateId).success(function (data, status, headers, config) {
                if (data.length == 0) {
                    $scope.cityList = emptyCityObj;
					$scope.allcityList = emptyCityObj;
                }
                else {
                    var CityAllItemObj = [{
						RegionID: -1,
						City: "All Cities",
						RegionURL: ""
					}];

					$scope.cityList = CityAllItemObj.concat(data)
					$scope.allcityList=$scope.cityList
                }
            }).error(function (data, status, headers, config) {
                console.log("Error with loading data: /api/location/GetAllStateCities")
            });
							//if (isSubmitform)
							//	$scope.RedirectToLocation();
        }
        $scope.loadCitiesForCounty = function (countyId, countyName, countyURL,isSubmitform) {
            if (countyId != 0) {
				if (countyId == -1 ) {
					countyName=countyName.replace("All ","")
				}
                $scope.saveSelectedLocation($scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedCountryURL,
                    $scope.SelectedStateName, $scope.SelectedStateID, $scope.SelectedStateURL,
                    countyName, countyId, countyURL,
                    $scope.DefSelectedCityName, 0, "",$scope.SelectedStateAbbr,
					$scope.DefSelectedNeighborhoodName, 0, "");
            }
            $scope.MarkDdl('', '', '',hideCssClass);
            $scope.cityList = loadCityObj;
			$scope.allcityList = loadCityObj;
			if (countyId == -1 ) {
				//load city for state
				$http.get('/api/location/GetAllStateCities?stateID=' + $scope.SelectedStateID).success(function (data, status, headers, config) {
					if (data.length == 0) {
						$scope.cityList = emptyCityObj;
						$scope.allcityList = emptyCityObj;
					}
					else {
						var CityAllItemObj = [{
							RegionID: -1,
							City: "All Cities",
							RegionURL: ""
						}];

						$scope.cityList = CityAllItemObj.concat(data)
						$scope.allcityList=$scope.cityList
					}
				}).error(function (data, status, headers, config) {
					console.log("Error with loading data: /api/location/GetAllStateCities")
				});
			}else{
				$http.get('/api/location/GetAllCitiesForCounty?countyId=' + countyId).success(function (data, status, headers, config) {
					if (data.length == 0) {
						$scope.cityList = emptyCityObj;
						$scope.allcityList = emptyCityObj;
					}
					else {
						var CityAllItemObj = [{
							RegionID: -1,
							City: "All Cities",
							RegionURL: ""
						}];

						$scope.cityList = CityAllItemObj.concat(data)
						$scope.allcityList=$scope.cityList
					}
									
				}).error(function (data, status, headers, config) {
					console.log("Error with loading data: /api/location/GetAllCitiesForCounty")
				});
			}
							//if (isSubmitform)
							//	$scope.RedirectToLocation();
        }
        /*functions for loading data:end*/
        $scope.selectNYKACatID = function (NYKACatID,NYKACatName, NYKACatURL) {
           // if (NYKACatID != 0) {
			  
            $scope.saveSelectedNetworkCategory(NYKACatID,NYKACatName,NYKACatURL);
			ChangeBoardViewByCategory($scope.SelectedCountryID,$scope.SelectedStateID,$scope.SelectedCountyID,$scope.SelectedCityID ,$scope.SelectedNeighborhoodID,NYKACatID,NYKACatName,'','0')				
           // }
			//$scope.RedirectToLocation();
        }
		
		
		 $scope.selectMediaTopic = function (caturl) {
			redirectUrl="/"+caturl+"/topic"
			redirectUrl+=$scope.generateRedirectURL();
			
			$window.open(redirectUrl, "_self");
			
        }
		$scope.selectMediaType = function (MediaType,ev) {
			$scope.saveSelectedMediaType(MediaType);
			
		/*	$('#topMediaType').find('li> span').each(function () {
			   if ($(this).attr('data-MediaType')==$(ev.target).attr("data-MediaType")){
				   $(this).click();
			   }
			});
			*/
			$scope.SelectedNYKACatID = 0;
			$scope.SelectedNYKACatName="";
			$scope.SelectedNYKACatURL = "";
          	$scope.RedirectToLocation(0);
        }
		
		
		  /*functions for loading data:begin*/
        $scope.selectCity = function (cityId, cityName, cityURL,isSubmitform) {
			
			if (cityId != 0) {
				if (cityId == -1 ) {
					cityName=cityName.replace("All ","")
					if ($scope.SelectedCountryID==1)
						$scope.MarkDdl('', '', '',hideCssClass);
					//document.getElementById("showFullBar").style.display="none";
				}
                $scope.saveSelectedLocation($scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedCountryURL,
                    $scope.SelectedStateName, $scope.SelectedStateID, $scope.SelectedStateURL,
                    $scope.SelectedCountyName, $scope.SelectedCountyID, $scope.SelectedCountyURL,
                    cityName, cityId, cityURL,$scope.SelectedStateAbbr,
					$scope.DefSelectedNeighborhoodName, 0, "");
					
			}	
			
            if (cityId > 0 && $scope.SelectedCountryID==1) {
				$scope.neighborhoodList = loadNeighborhoodObj;
				$scope.allneighborhoodList = loadNeighborhoodObj;
				$http.get('/api/location/GetAllneighbourhoodsForCity?CityID=' + cityId).success(function (data, status, headers, config) {
					if (data.length == 0) {
						$scope.neighborhoodList = emptyNeighborhoodObj;
						$scope.allneighborhoodList = emptyNeighborhoodObj;
						$scope.MarkDdl('', '', '',hideCssClass);
						//document.getElementById("showFullBar").style.display="none";
						if (isSubmitform)
							$scope.RedirectToLocation();
					}
					else {
						var NeighborhoodAllItemObj = [{
							NeighborhoodID: -1,
							NeighborhoodName: "All Neighborhoods",
							NeighborhoodURL: ""
						}];
						
						$scope.neighborhoodList = NeighborhoodAllItemObj.concat(data)
						$scope.allneighborhoodList=$scope.neighborhoodList
						$scope.MarkDdl('', '', '','');
						//$scope.CountryDdlClass = hideCssClass;
						//document.getElementById("showFullBar").style.display="inline-block";
						SetLocationBreadcrum('')
					}
				}).error(function (data, status, headers, config) {
					console.log("Error wit loading data: /api/location/GetAllneighbourhoodsForCity?CityID=" + cityId)
				});
			}
			
        }
				 /*functions for loading data:end*/
        $scope.selectNeighborhood = function (neighborhoodId, neighborhoodName, neighborhoodURL,isSubmitform) {
            if (neighborhoodId != 0) {
					if (neighborhoodId == -1 ) {
							neighborhoodName=neighborhoodName.replace("All ","")
					}
					
                $scope.saveSelectedLocation($scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedCountryURL,
                    $scope.SelectedStateName, $scope.SelectedStateID, $scope.SelectedStateURL,
                    $scope.SelectedCountyName, $scope.SelectedCountyID, $scope.SelectedCountyURL,
                    $scope.SelectedCityName, $scope.SelectedCityID, $scope.SelectedCityURL,$scope.SelectedStateAbbr,
					neighborhoodName,neighborhoodId,  neighborhoodURL);
				if (neighborhoodId>0){
					$scope.MarkDdl('', '', '','');
					//$scope.CountryDdlClass = hideCssClass;
					//document.getElementById("showFullBar").style.display="inline-block";
					if (isSubmitform)
							$scope.RedirectToLocation();
					
				}
			}
					//	if (isSubmitform)
						//	$scope.RedirectToLocation();
        }
		
        //store selected by user data
        $scope.saveSelectedLocation = function (country, countryID, countryURL, state, stateID, stateURL, county, countyID, countyURL, city, cityID, cityURL,stateAbbr,neighborhood,neighborhoodID,neighborhoodURL) {
            $scope.SelectedCountryName = country;
            $scope.SelectedStateName = state;
            $scope.SelectedCountyName = county;
            $scope.SelectedCityName = city;
			 $scope.SelectedNeighborhoodName = neighborhood;
           

            $scope.SelectedCountryID = countryID;
            $scope.SelectedStateID = stateID;
            $scope.SelectedCountyID = countyID;
            $scope.SelectedCityID = cityID;
			$scope.SelectedNeighborhoodID=neighborhoodID
		

            $scope.SelectedCountryURL = countryURL;
            $scope.SelectedStateURL = stateURL;
            $scope.SelectedCountyURL = countyURL;
            $scope.SelectedCityURL = cityURL;
			$scope.SelectedNeighborhoodURL=neighborhoodURL
			
			$scope.SelectedStateAbbr = stateAbbr;
										
        }
				  //store selected by user data
        $scope.saveSelectedNetworkCategory = function (NYKACatID,NYKACatName,NYKACatURL) {
					
          	$scope.SelectedNYKACatID = NYKACatID;
						$scope.SelectedNYKACatName=NYKACatName;
						$scope.SelectedNYKACatURL = NYKACatURL;
				}
				  //store selected by user data
        $scope.saveSelectedMediaType = function (MediaType) {
				$scope.SelectedMediaType = MediaType;
				curMediaPage=MediaType
						 
		}
	//mark ddl active or non active
        $scope.MarkDdl = function (stateDdlCss, countyDdlCss, cityDdlCss, neighborhoodDdlCss) {
            $scope.CountryDdlClass =''
			$scope.StateDdlClass = stateDdlCss;
            $scope.CountyDdlClass = countyDdlCss;
            $scope.CityDdlClass = cityDdlCss;
			
			$scope.NeighborhoodDdlClass = neighborhoodDdlCss
			
        }			
		$scope.ShowFullBar = function () {
			$scope.MarkDdl('', '', '','');
			$scope.CountryDdlClass =''
			document.getElementById("showFullBar").style.display="none";
			//document.getElementById("hidLocationBreadcrum").style.display="inline-block";
			
        }		
$scope.SetBoardURL = function () {
		var redirectUrl;//Creeate redirect url:begin
		
		redirectUrl=$scope.generateRedirectURL();
		var LocationTitleNew
		
		setLocationCookies(redirectUrl, $scope.SelectedCountryID,$scope.SelectedStateID,$scope.SelectedCountyID,$scope.SelectedCityID,$scope.SelectedNeighborhoodID)
		

		if ($scope.SelectedCountryName!="Country" &&  $scope.SelectedCountryName!="All")
			LocationTitleNew=$scope.SelectedCountryName
		if ($scope.SelectedStateName!="State" && $scope.SelectedStateName!="States"){
			LocationTitleNew=$scope.SelectedStateName
		}
		if ($scope.SelectedCountyName!="County" &&  $scope.SelectedCountyName!="Counties"){
			LocationTitleNew=$scope.SelectedCountyName 
			LocationTitleNew +=" County, "+$scope.SelectedStateAbbr
		}
		if ($scope.SelectedCityName!="City" && $scope.SelectedCityName!="Cities"){
			LocationTitleNew=$scope.SelectedCityName
			if ($scope.SelectedCountryID=="1") 	
				LocationTitleNew +=", "+$scope.SelectedStateAbbr
		}
		if ($scope.SelectedCityName!="Neighborhood" && $scope.SelectedCityName!="Neighborhoods"){
			LocationTitleNew=$scope.SelectedNeighborhoodName
			LocationTitleNew +=", "+$scope.SelectedCityName
		}
		if (document.getElementById("LocationTitle"))
			document.getElementById("LocationTitle").innerHTML=$scope.SearchWhere
		if (document.getElementById("BoardURL"))
			document.getElementById("BoardURL").href=redirectUrl;
		if (document.getElementById("BoardURL1"))
			document.getElementById("BoardURL1").href=redirectUrl;

	if (document.getElementById("iconBoardURL"))
		document.getElementById("iconBoardURL").href=redirectUrl.replace("/people/board","");
		$scope.SetPageURL();
	}
	
	$scope.SetPageURL = function () {
	
		if (URLRewriteOnThisPage=="" || URLRewriteOnThisPage=="false"){
			return false;
		}
		var MediafixedTag="media"
		if (MediaDisplay=="scroll")
			var MediafixedTag="board"
			
		var MediaURLTag=""
		
		if (curMediaPage=="profile"){
			MediaURLTag="members"
			 MediafixedTag="network"
		}else if (curMediaPage=="image"){
			MediaURLTag="images"
			
		}else if (curMediaPage=="video"){
			MediaURLTag="videos"
		}else if (curMediaPage=="blog"){
			MediaURLTag="blogs"
			
		}else if (curMediaPage=="audio"){
			MediaURLTag="audios"
		}else if (curMediaPage=="product"){
			MediaURLTag="products"
		}else if (curMediaPage=="deal"){
			MediaURLTag="deals"
		}else if (curMediaPage=="coupon"){
			MediaURLTag="coupons"
		}else if (curMediaPage=="store"){
			MediaURLTag="stores"
		}else if (curMediaPage=="peoples"){
			MediaURLTag="people"
		}else if (curMediaPage=="channels"){
			MediafixedTag="channels"
			MediaURLTag=""
		}


			
			var redirectUrl;//Creeate redirect url:begin
			
			if (curMediaPage=="peoples"){
				if (MediaDisplay=="scroll")
					redirectUrl = '/board/'+MediaURLTag;	
				else
					redirectUrl = '/'+MediaURLTag;	
			}else{
				if (document.getElementById("category")){
					if (document.frmMedia.category.value != "" ){
						if (MediaURLTag!='')
							redirectUrl = '/' +document.frmMedia.category.value+'/'+MediafixedTag+'/'+MediaURLTag;
						else
							redirectUrl = '/' +document.frmMedia.category.value+'/'+MediafixedTag;
						
					}else{
						if (MediaURLTag!='')
							redirectUrl = '/all/'+MediafixedTag+'/'+MediaURLTag;	
						else
							redirectUrl = '/all/'+MediafixedTag;	
					}
				}
			}
			mediacatvalue=""
			if (document.frmMedia.category){
				mediacatvalue=document.frmMedia.category.value
			}
			
			if ( mediacatvalue=="featured" ||   mediacatvalue=="power" ||   mediacatvalue=="premium" ||   mediacatvalue=="certified" )
				{
				redirectUrl += '/world';
			}else{
				
			
				if ($scope.SelectedCountryID ==-1) {
					redirectUrl += '/world';
				}	else if ($scope.SelectedCountryID == 1) {
					if ($scope.SelectedStateID != 0 && $scope.SelectedStateID != -1) {
						if ($scope.SelectedCountyID != 0 && $scope.SelectedCountyID != -1) {
							if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1)//city
							{
								redirectUrl += '/us/' + $scope.SelectedCityURL;
							}
							else {//redirect ot county
								if ($scope.SelectedCountyID == 23) {
										redirectUrl += '/parish/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
								}
								else {
										redirectUrl += '/county/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
								}
							}
						}
						else if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1)//city
						{
								redirectUrl += '/us/' + $scope.SelectedCityURL;
						}
						else {//stale url
								redirectUrl +=  "/"+$scope.SelectedStateURL;
						}
					}
					else {
							redirectUrl += '/united-states';
					}
				}
			
				else if ($scope.SelectedCountryID > 1) {
					if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1) {
					redirectUrl += "/int/" +  $scope.SelectedCityURL;
					}
					else {
						redirectUrl += "/int/" + $scope.SelectedCountryURL;
					}
				}
			}
			redirectUrl += '/';//Creeate redirect url:end
			
			var sq = getUrlVars()["sq"];
			
			if (sq!="" &&  typeof sq != "undefined"){
				redirectUrl += "?sq="+sq
			}
		
			if (curMediaPage=="profile" || curMediaPage=="image" || curMediaPage=="video" || curMediaPage=="blog" || curMediaPage=="audio" || curMediaPage=="product" || curMediaPage=="coupon" || curMediaPage=="store" || curMediaPage=="deal"|| curMediaPage=="peoples" || curMediaPage=="channels"){  
				//if (document.getElementById("worldlocal")){
				//	if (document.getElementById("LocationTitle").innerHTML!="World"){
					
				//		document.getElementById("localhref").innerHTML=document.getElementById("LocationTitle").innerHTML;
				//		document.getElementById("worldlocal").style.display="block";
				///		ChangeWorldLocalSelection(curMediaPage,"L")
						
				//	}else{
						
				
					//	document.getElementById("worldlocal").style.display="none";
				//	}
				//}
				if (MediaDisplay=="scroll")
				{
					if (curMediaPage=="profile")
						redirectReplaceUrl=redirectUrl.replace("/board/","/network/")
					else if (curMediaPage=="peoples")
						redirectReplaceUrl=redirectUrl.replace("/board","")
					else
						redirectReplaceUrl=redirectUrl.replace("/board/","/media/")
					
					if (document.getElementById("ImageBoardURL"))
						$('#ImageBoardURL').attr("href", redirectReplaceUrl);
				}else{
					if (curMediaPage=="profile")
						redirectReplaceUrl=redirectUrl.replace("/network/","/board/")
					else if (curMediaPage=="peoples")
						redirectReplaceUrl='/board'+redirectUrl
					else
						redirectReplaceUrl=redirectUrl.replace("/media/","/board/")
					if (document.getElementById("ImageBoardURL"))
						$('#ImageBoardURL').hide();
				}
				
				
			//	window.history.pushState(curMediaPage,curMediaPage, redirectUrl);
			}
			$('#TOPPAGGING').html($('#BOTTOMPAGING').html())
	}	
        //submit event
        $scope.RedirectToLocation = function (blnsubmit) {
            var redirectUrl;//Creeate redirect url:begin
				var whathappeningtitleAbbr=""
			if ($scope.SelectedNYKACatID==0 || $scope.SelectedNYKACatID=="")
			{
				redirectUrl=$scope.generateRedirectURL();
				boardurl=redirectUrl
			}else{
		
				/*redirectUrl="/"+$scope.SelectedNYKACatURL
				if ($scope.SelectedCountryID == 1) {
					if ($scope.SelectedStateID != 0 && $scope.SelectedStateID != -1) {
						if ($scope.SelectedCountyID != 0 && $scope.SelectedCountyID != -1) {
							if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1)//city
							{
								redirectUrl += '/network/us/' + $scope.SelectedCityURL;
								whathappeningtitleAbbr=$scope.SelectedCityName+", "+$scope.SelectedStateName
							}
							else {//redirect ot county
								if ($scope.SelectedCountyID == 23) {
									redirectUrl += "/network/" + 'parish/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
								}
								else {
									redirectUrl += "/network/" +'county/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
								}
								whathappeningtitleAbbr=$scope.SelectedCountyName+" County, "+$scope.SelectedStateName							
								//redirectUrl += "/network/" + $scope.SelectedStateURL;
							}
						}
						else if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1)//city
						{
							redirectUrl += '/network/us/' + $scope.SelectedCityURL;
							whathappeningtitleAbbr=$scope.SelectedCityName+", "+$scope.SelectedStateName
						}
						else {//stale url
							redirectUrl += "/network/" + $scope.SelectedStateURL;
							whathappeningtitleAbbr= $scope.SelectedStateName
						}
					}
					else {
						redirectUrl += '/network/united-states';
						whathappeningtitleAbbr= "United States"
					}
				}
				else if ($scope.SelectedCountryID != 0) {
					if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1) {
							redirectUrl += "/network/" + $scope.SelectedCityURL;
							whathappeningtitleAbbr=$scope.SelectedCityName+", "+$scope.SelectedCountryName
					}
					else {
							redirectUrl += "/network/" + $scope.SelectedCountryURL;
							whathappeningtitleAbbr=$scope.SelectedCountryName
					}
				}*/
				
				/*redirectUrl += '/';//Creeate redirect url:end
				boardurl=$scope.generateRedirectURL();*/
				redirectUrl=$scope.generateRedirectURL();
				boardurl=redirectUrl
				
			}
		//	console.log(redirectUrl)
			
			//if (getCookie("LockBoardURL")!=""){
			//	redirectUrl=getCookie("LockBoardURL");
			//}
		  ////hide opened location list
            $scope.HidelocationList = true;
            //do redirect to selected page
            //set the sesssion variable so that VBS can access show the appropriate page

				if ($scope.SelectedCountryID != 0) {
					//$http.get('/ajax/ajax_set_dashboard_pref_session.asp?pgPref=selectedlocation').success(function (data, status, headers, config) {
					//}).error(function (data, status, headers, config) {
					//console.log("Error Unable to set Session : /ajax/ajax_set_dashboard_pref_session.asp?")
					//});
			
			if (curMediaPage!="board" && curMediaPage!="peopleboard" && curMediaPage!="locations"  && curMediaPage!="directory"   && curMediaPage!="map" && curMediaPage!="home" && curMediaPage!="network" && curMediaPage!="group"&& curMediaPage!="premium" && curMediaPage!="forums" ){
				
				$scope.SetBoardURL();
				SetlocationDash(boardurl,$scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedStateName,
				$scope.SelectedStateID,$scope.SelectedCountyName, $scope.SelectedCountyID ,$scope.SelectedCityName, 
				$scope.SelectedCityID,$scope.SelectedNeighborhoodName, 
				$scope.SelectedNeighborhoodID,$scope.SelectedNYKACatID,$scope.SelectedNYKACatName,$scope.SelectedNYKACatURL,$scope.SelectedMediaType,$scope.SelectedStateAbbr)
				
			}else if (curMediaPage=="board" && blnsubmit==0){
				
					GetLeftMenuVj(curMediaPage);
					PrevMediaPage=curMediaPage
					ChangeBoardViewByLocation()
				
				/*SetlocationDash(boardurl,$scope.SelectedCountryName, $scope.SelectedCountryID, $scope.SelectedStateName,
				$scope.SelectedStateID,$scope.SelectedCountyName, $scope.SelectedCountyID ,$scope.SelectedCityName, 
				$scope.SelectedCityID,$scope.SelectedNeighborhoodName, 
				$scope.SelectedNeighborhoodID,$scope.SelectedNYKACatID,$scope.SelectedNYKACatName,$scope.SelectedNYKACatURL,$scope.SelectedMediaType,$scope.SelectedStateAbbr)
			*/
			}else{
				setLocationCookies(boardurl, $scope.SelectedCountryID,$scope.SelectedStateID,$scope.SelectedCountyID,$scope.SelectedCityID,$scope.SelectedNeighborhoodID)
				
				if(curMediaPage == "map")
				{
					var CountryId = $scope.SelectedCountryID;
					if(CountryId == "0")
					{
						CountryId="";
					}
					var StateId = $scope.SelectedStateID;
					if(StateId == "0")
					{
						StateId="";
					}
					var CountyId = $scope.SelectedCountyID ;
					if(CountyId == "0")
					{
						CountyId="";
					}
					var CityId = $scope.SelectedCityID;
					if(CityId == "0")
					{
						CityId="";
					}
									
					var data = {
						Token:'W3aIQ1qJkEqN35t9RnY-ow',
						CountryId: CountryId,
						StateId:StateId,
						CountyId:CountyId,
						CityId:CityId

					};
												
					$http.post('/API1/LocalPageInfo/LocalPageInfo',data).success(function (data) 
					{
						if(data.SearchList[0] != null)
						{
							if(data.SearchList[0]["SITSMapURL"] != null)
							{
								
								var MapredirectUrl = data.SearchList[0]["SITSMapURL"];
								setCookie("BoardsitsURL",MapredirectUrl,30);
								$window.open(MapredirectUrl, "_self");
								
							}

							else
							{
									var MapredirectUrl = "/streetmap/counties/"
									setCookie("BoardsitsURL",MapredirectUrl,30);
									$window.open(MapredirectUrl, "_self");
							}
						}else
							{
								var MapredirectUrl = "/streetmap/counties/"
								setCookie("BoardsitsURL",MapredirectUrl,30);
								$window.open(MapredirectUrl, "_self");
						}
					});
									
				}
				else if(curMediaPage == "network" && $scope.SelectedNYKACatURL=="")
				{
					if (getCookie("LockBoardURL")!=""){
						redirectUrl=getCookie("LockBoardURL");
					}else{
						redirectUrl="/world/"
					}
					$window.open("/business-networking/network"+redirectUrl, "_self");
					
				}
				else if(curMediaPage == "group")
				{
					$window.open("/groups/all/", "_self");
				}
				else if(curMediaPage == "premium")
				{
					$window.open("/premiumpackages/", "_self");
				}
				else if(curMediaPage == "channels")
				{
					$window.open("/channels/", "_self");
				}
				else if(curMediaPage == "forums")
				{
					$window.open("/forums/", "_self");
				}
				else if(curMediaPage == "locations")
				{
					$window.open("/locations/", "_self");
				}
				
				else if(curMediaPage == "directory")
				{
					$window.open("/directory/locations/", "_self");
				}
				else if(curMediaPage == "home")
				{
					$window.open(USERHOMEPAGEURL, "_self");
				}
				/*else if(curMediaPage == "classifieds")
				{
					$window.open("/ads/all/", "_self");
				}*/
				
				else
				{
				$window.open(redirectUrl, "_self");
				}
			}
		}
		else {
			$window.alert('Please, select location')
		}
		
    }
      $scope.setLocation = function (countryId, stateId, countyId, cityId, neighborhoodId,  NYKACatID, NYKACatName,NYKACatURL) { 
		//debugger
		$scope.saveSelectedLocation($scope.DefSelectedCountryName, 0, "",
		$scope.DefSelectedStateName, 0, "",
		$scope.DefSelectedCountyName, 0, "",
		$scope.DefSelectedCityName, 0, "",$scope.DefSelectedStateAbbr,
		$scope.DefSelectedNeighborhoodName, 0, "");
			
			
			//debugger
			$scope.SelectedNYKACatID=NYKACatID;
			$scope.SelectedNYKACatURL=NYKACatURL;
			$scope.SelectedNYKACatName=NYKACatName;
			$scope.SelectedCountyID =countyId
			$scope.SelectedCityID = cityId;
			$scope.neighborhoodList = loadNeighborhoodObj;
			$scope.allneighborhoodList = loadNeighborhoodObj;
				
			$.ajax({
				url: '/api/location/GetAllCountries',
				type: 'GET',
				async:false,
				dataType: 'json', // added data type
				success: function(data) {
					
					//save selected
					var CountryAllItemObj = [{
						CountryID: -1,
						Country1: "World",
						CountryURL: "world"
					}];
					$scope.allCountries = CountryAllItemObj.concat(data)
				
					for (var i = 0; i < $scope.allCountries.length; i++) {
						if ($scope.allCountries[i].CountryID == countryId) {
							$scope.SelectedCountryID = countryId;
							$scope.SelectedCountryName = $scope.allCountries[i].Country1;
							$scope.SelectedCountryURL = $scope.allCountries[i].CountryURL;
							
							break;
						}
						
					}
				}
			});
			
            if (countryId == 1) {
				$scope.stateList = loadStateObj;
				$scope.allstateList = loadStateObj;
				$.ajax({
					url: '/api/location/GetAllStates',
					type: 'GET',
					async:false,
					dataType: 'json', // added data type
					success: function(data) {
						if (data.length == 0) {
							$scope.stateList = emptyStateObj;
							$scope.allstateList = emptyStateObj;
						}
						else {
							var stateAllItemObj = [{
								StateID: -1,
								State1: "All States",
								StateURL: "",
								Abbr:""
							}];

							$scope.stateList = stateAllItemObj.concat(data)
							for (var i = 0; i < $scope.stateList.length; i++) {
								if ($scope.stateList[i].StateID == stateId) {
									$scope.SelectedStateID = stateId;
									$scope.SelectedStateName = $scope.stateList[i].State1;
									$scope.SelectedStateURL = $scope.stateList[i].StateURL;
									$scope.SelectedStateAbbr = $scope.stateList[i].Abbr;
									break;
								}
							}	
							if(!$scope.$$phase) {
							  $scope.$digest();
							}					
							$scope.allstateList =$scope.stateList 
							
							$scope.MarkDdl('', hideCssClass, hideCssClass,hideCssClass);
               
							if (stateId>0) {
								$scope.countyList = loadCountyObj;
								$scope.allcountyList = loadCountyObj;
								$.ajax({
									url: '/api/location/GetAllCountiesForStates?stateId=' + stateId,
									type: 'GET',
									async:false,
									dataType: 'json', // added data type
									success: function(data) {
										 if (data.length == 0) {
											$scope.countyList = emptyCountyObj;
										}
										else {
											var CountyAllItemObj = [{
												CountyID: -1,
												CountyName: "All Counties",
												CountyURL: ""
											}];
														
											$scope.countyList = CountyAllItemObj.concat(data) 
											for (var i = 0; i < $scope.countyList.length; i++) {
												if ($scope.countyList[i].CountyID == countyId) {
													$scope.SelectedCountyID = countyId;
													$scope.SelectedCountyName = $scope.countyList[i].CountyName;
													$scope.SelectedCountyURL = $scope.countyList[i].CountyURL;
													break;
												}
											}
											
											$scope.allcountyList=$scope.countyList
											if(!$scope.$$phase) {
											  $scope.$digest();
											}
											
											$scope.MarkDdl('', '', hideCssClass,hideCssClass);
											if (countyId>0) {
                   
												$scope.cityList = loadCityObj;
												$scope.allcityList = loadCityObj;
												$.ajax({
													url: '/api/location/GetAllCitiesForCounty?countyId=' + countyId,
													type: 'GET',
													async:false,
													dataType: 'json', // added data type
													success: function(data) {
														 if (data.length == 0) {
															$scope.cityList = emptyCityObj;
															$scope.allcityList = emptyCityObj;
														}
														else {
															var CityAllItemObj = [{
																RegionID: -1,
																City: "All Cities",
																RegionURL: ""
															}];

															$scope.cityList = CityAllItemObj.concat(data)
															$scope.allcityList=$scope.cityList
															for (var i = 0; i < $scope.cityList.length; i++) {
																if ($scope.cityList[i].RegionID == cityId) {
																	
																	$scope.SelectedCityID = cityId;
																	$scope.SelectedCityName = $scope.cityList[i].City;
																	$scope.SelectedCityURL = $scope.cityList[i].RegionURL;
																	break;
																}
															}
															if(!$scope.$$phase) {
															  $scope.$digest();
															}
															
															$scope.MarkDdl('', '', '',hideCssClass);
				
															if (cityId>0) {
																$scope.neighborhoodList = loadNeighborhoodObj;
																$scope.allneighborhoodList = loadNeighborhoodObj;
																$.ajax({
																	url: '/api/location/GetAllneighbourhoodsForCity?CityID=' + cityId,
																	type: 'GET',
																	async:false,
																	dataType: 'json', // added data type
																	success: function(data) {
																		 if (data.length == 0) {
																			$scope.neighborhoodList = emptyNeighborhoodObj;
																			$scope.allneighborhoodList = emptyNeighborhoodObj;
																		}
																		else {
																			var NeighborhoodAllItemObj = [{
																				NeighborhoodID: -1,
																				NeighborhoodName: "All Neighborhoods",
																				NeighborhoodURL: ""
																			}];
																		
																			$scope.neighborhoodList = NeighborhoodAllItemObj.concat(data)
																			$scope.allneighborhoodList=$scope.neighborhoodList
																			for (var i = 0; i < $scope.neighborhoodList.length; i++) {
																				if ($scope.neighborhoodList[i].NeighborhoodID == neighborhoodId) {
																					$scope.SelectedNeighborhoodID = neighborhoodId;
																					$scope.SelectedNeighborhoodName = $scope.neighborhoodList[i].NeighborhoodName;
																					$scope.SelectedNeighborhoodURL = $scope.neighborhoodList[i].NeighborhoodUrl;
																					break;
																				}
																			}
																			if(!$scope.$$phase) {
																			  $scope.$digest();
																			}
																			$scope.MarkDdl('', '', '','');
																		}
																	}		
																});
																
															}
														}
													}		
												});
											}
										}
									}	
								});
								
							}
							
							
						}
					}	
				});
				   
				
                
	
			}
            else if (countryId >1)  {
                $scope.MarkDdl(hideCssClass, hideCssClass, '',hideCssClass);
				$scope.cityList = loadCityObj;
				$scope.allcityList = loadCityObj;
				$.ajax({
					url: '/api/location/GetAllCountryCities?countryId=' + countryId,
					type: 'GET',
					async:false,
					dataType: 'json', // added data type
					success: function(data) {
						 if (data.length == 0) {
							$scope.cityList = emptyCityObj;
							$scope.allcityList = emptyCityObj;
						}
						else {
							var CityAllItemObj = [{
								RegionID: -1,
								City: "All Cities",
								RegionURL: ""
							}];

							$scope.cityList = CityAllItemObj.concat(data)
							$scope.allcityList =$scope.cityList 
							//save selected
							
							for (var i = 0; i < $scope.cityList.length; i++) {
								if ($scope.cityList[i].RegionID == cityId) {
									$scope.SelectedCityID = cityId;
									$scope.SelectedCityName = $scope.cityList[i].City;
									$scope.SelectedCityURL = $scope.cityList[i].RegionURL;
									break;
								}
							}
							if(!$scope.$$phase) {
							  $scope.$digest();
							}
							
						}	
					}	
				});
					
              
				
            } else  {
				  $scope.MarkDdl(hideCssClass, hideCssClass, hideCssClass,hideCssClass);
			}
			
			if (countryId != '' || stateId != '' || countyId != '' || cityId != ''|| neighborhoodId != ''){
					$scope.SetBoardURL();
					callMedia()
			}
        }    
        //bind events when render has finished:begin
        $scope.$on('NgRenderCountry', function () {
            $('#LocationCountryDDL').find('li span').each(function () { //fot ddl with search input
                $(this).on('click', function () {
				
                    if ($(this).parents('ul').parent().hasClass('js-ul-ddl'))
                    {
                        $(this).parents('ul').parent().removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').parent().slideUp(0);
                        $('#SerachCountryTb').val('');
							
                    }
                    else {                    
                        $(this).parents('ul').removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').slideUp(0);
						
                    }
                })
            });
        });
        $scope.$on('NgRenderState', function () {
            $('#LocationStateDDL').find('li span').each(function () {
                $(this).on('click', function () {
                    if ($(this).parents('ul').parent().hasClass('js-ul-ddl'))
                    {
                        $(this).parents('ul').parent().removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').parent().slideUp(0);
                        $('#SerachStateTb').val('');
                    }
                    else {                    
                        $(this).parents('ul').removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').slideUp(0);
                    }
                  
                })
            });
        });
        $scope.$on('NgRenderCounty', function () {
            $('#LocationCountyDDL').find('li span').each(function () {
                $(this).on('click', function () {
                     if ($(this).parents('ul').parent().hasClass('js-ul-ddl'))
                    {
                        $(this).parents('ul').parent().removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').parent().slideUp(0);
                        $('#SerachCountyTb').val('');
                    }
                    else {                    
                        $(this).parents('ul').removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').slideUp(0);
                    }
                })
            });
        });
        $scope.$on('NgRenderCity', function () {
            $('#LocationCityDDL').find('li span').each(function () {
                $(this).on('click', function () {
                     if ($(this).parents('ul').parent().hasClass('js-ul-ddl'))
                    {
                        $(this).parents('ul').parent().removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').parent().slideUp(0);
                        $('#SerachCityTb').val('');
                    }
                    else {                    
                        $(this).parents('ul').removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').slideUp(0);
                    }
                })
            });
        });
		 $scope.$on('NgRenderNeighborhood', function () {
            $('#LocationNeighborhoodDDL').find('li span').each(function () {
                $(this).on('click', function () {
                     if ($(this).parents('ul').parent().hasClass('js-ul-ddl'))
                    {
                        $(this).parents('ul').parent().removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').parent().slideUp(0);
                        $('#SerachNeighborhoodTb').val('');
                    }
                    else {                    
                        $(this).parents('ul').removeClass(selectLocationPanel.openItemCss);
                        $(this).parents('ul').slideUp(0);
                    }
                })
            });
        });
        //bind events when render has finished:end
        //search country by letters
        $('#SerachCountryTb').on('keyup', function (e) {
            var countriesArr = [];
            var searchString = $(this).val().toLowerCase().trim();
            for (var i = 0; i < $scope.allCountries.length; i++) {
                if ($scope.allCountries[i].Country1.toLowerCase().indexOf(searchString) > -1) {
                    countriesArr.push($scope.allCountries[i]);                   
                }
            }
            $scope.countries = countriesArr;
            $scope.$apply();
        });
        	//search state by letters
        $('#SerachStateTb').on('keyup', function (e) {
            var statesArr = [];
            var searchString = $(this).val().toLowerCase().trim();
            for (var i = 0; i < $scope.allstateList.length; i++) {
                if ($scope.allstateList[i].State1.toLowerCase().indexOf(searchString) > -1) {
                    statesArr.push($scope.allstateList[i]);
                }
            }
            $scope.stateList = statesArr;
            $scope.$apply();
        });
				//search County by letters
        $('#SerachCountyTb').on('keyup', function (e) {
            var countiesArr = [];
            var searchString = $(this).val().toLowerCase().trim();
            for (var i = 0; i < $scope.allcountyList.length; i++) {
                if ($scope.allcountyList[i].CountyName.toLowerCase().indexOf(searchString) > -1) {
                    countiesArr.push($scope.allcountyList[i]);
                }
            }
            $scope.countyList = countiesArr;
            $scope.$apply();
        });
				//search City by letters
        $('#SerachCityTb').on('keyup', function (e) {
            var citiesArr = [];
            var searchString = $(this).val().toLowerCase().trim();
            for (var i = 0; i < $scope.allcityList.length; i++) {
                if ($scope.allcityList[i].City.toLowerCase().indexOf(searchString) > -1) {
                    citiesArr.push($scope.allcityList[i]);
                }
            }
            $scope.cityList = citiesArr;
            $scope.$apply();
        });
					//search City by letters
        $('#SerachNeighborhoodTb').on('keyup', function (e) {
            var neighborhoodsArr = [];
            var searchString = $(this).val().toLowerCase().trim();
            for (var i = 0; i < $scope.allneighborhoodList.length; i++) {
                if ($scope.allneighborhoodList[i].NeighborhoodName.toLowerCase().indexOf(searchString) > -1) {
                    neighborhoodsArr.push($scope.allneighborhoodList[i]);
                }
            }
            $scope.neighborhoodList = neighborhoodsArr;
            $scope.$apply();
        });
		
		$scope.generateRedirectURL = function () {
			var redirectUrl;//Creeate redirect url:begin
			
			var searchWhere="";
			document.getElementById("TScountryid").value=""
			document.getElementById("TSstateid").value=""
			document.getElementById("TScountyid").value=""
			document.getElementById("TScityid").value=""
			
			if ($scope.SelectedCountryID == 1) {
				redirectUrl = '/united-states';
				searchWhere= "United States"
				document.getElementById("TScountryid").value=$scope.SelectedCountryID
			
				if ($scope.SelectedStateID != 0 && $scope.SelectedStateID != -1) {
					redirectUrl ="/" + $scope.SelectedStateURL;
					searchWhere= $scope.SelectedStateAbbr + ", United States"
					document.getElementById("TSstateid").value=$scope.SelectedStateID 
				}
				
				if ($scope.SelectedCountyID != 0 && $scope.SelectedCountyID != -1) {
					if ($scope.SelectedCountyID == 23) {
						redirectUrl = '/parish/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
					}
					else {
						redirectUrl = '/county/' + $scope.SelectedStateURL + '/' + $scope.SelectedCountyURL;
					}
					searchWhere= $scope.SelectedCountyName + " County, "+ $scope.SelectedStateAbbr
					document.getElementById("TScountyid").value=$scope.SelectedCountyID
					document.getElementById("TSstateid").value=$scope.SelectedStateID 
				}
				if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1)//city
				{
					redirectUrl ='/us/' + $scope.SelectedCityURL;
					searchWhere= $scope.SelectedCityName + ", "+ $scope.SelectedStateAbbr
					document.getElementById("TScityid").value=$scope.SelectedCityID
					document.getElementById("TSstateid").value=$scope.SelectedStateID
				}
				if ($scope.SelectedNeighborhoodID != 0 && $scope.SelectedNeighborhoodID != -1)//city
				{
					redirectUrl = '/neighborhood/' + $scope.SelectedCityURL +'/'+$scope.SelectedNeighborhoodURL;
					searchWhere= $scope.SelectedCityName + ", "+ $scope.SelectedStateAbbr
					document.getElementById("TScityid").value=$scope.SelectedCityID
					document.getElementById("TSstateid").value=$scope.SelectedStateID
				}
			}else if ($scope.SelectedCountryID >1) {
				
				redirectUrl = "/" + $scope.SelectedCountryURL;
				searchWhere=  $scope.SelectedCountryName
				if ($scope.SelectedCityID != 0 && $scope.SelectedCityID != -1) {
						redirectUrl = "/" + $scope.SelectedCityURL;
						searchWhere= $scope.SelectedCityName + ", "+ $scope.SelectedCountryName
						document.getElementById("TScityid").value=$scope.SelectedCityID
				}
				document.getElementById("TScountryid").value=$scope.SelectedCountryID
			}else{
					document.getElementById("TScountryid").value="-1"
				redirectUrl = "/world"
			}
			
			if (View4box=="1" )
				redirectUrl="/board"+redirectUrl
			
			if (curMediaPage == "peopleboard"){
				redirectUrl="/people/board"+redirectUrl
			}
			
			redirectUrl += '/';//Creeate redirect url:end
			$scope.RedirectURL = redirectUrl;
			$scope.SearchWhere = searchWhere;
				
			if (document.getElementById("LocationTitle"))
				document.getElementById("LocationTitle").innerHTML=$scope.SearchWhere 
			if (document.getElementById("tbSearchWhere"))
				document.getElementById("tbSearchWhere").value=$scope.SearchWhere 
		
		
			SetLocationBreadcrum('')
			return redirectUrl;
		}

    }]);

    angular.bootstrap(document.getElementById("NgStateApp"), ['StateApp']);

});