//Location aware script
//This method is always called from all three pages onload
function localAwareInit(page){
    var city = jaaulde.utils.cookies.get('city');
    var zip = jaaulde.utils.cookies.get('zip');
    this.page = page;
    if(city){
        setUI(city+":"+zip);
    }
    else{
        lookUpIp();
    }
}

//This methods looks up the ip if the page does not find a zip or city cookie
function lookUpIp(){
    $.post('/locationaware/ipLookUp.html', {}, setUI);
}

//These methods allows users to changes their location by their zip code or city
function changeZip(){
    $(".modal").hide(300);
    var zip = $("#visitorLocale").val();
    $.post('/locationaware/getZip.html', { zipCode: zip }, setUI);

}

function setUI(cityZip){
    var city = cityZip.split(':')[0];
    var zip = cityZip.split(':')[1];
    $("#localButtonSpan").text("Not in " + city + "?");
    $("#localTabs .coloredTitle2").text(city);
    jaaulde.utils.cookies.set('city', city, {hoursToLive: 1440});
    jaaulde.utils.cookies.set('zip', zip, {hoursToLive: 1440});
    if(page == 'PMN'){
        getPmn(city);
    }
    else if (page == 'SERVICES'){
        getServiceCity();
        $("h4").removeClass("hover");
//	    $("#localServicesBlock .coloredTitle2").text(city);
        $("#servicesCity").addClass("hover");
        $(".services").hide(500);
        $("#services-City").slideToggle("slow");
        $("#services-City").fadeIn("show");
    }
    else if (page == 'LOCATION'){
        getCity();
        $("h4").removeClass("hover");
//	    $("#governmentOfficesBlock .coloredTitle2").text(city);
        $("#govEntityCity").addClass("hover");
        $(".mappings").hide(500);
        $("#mappings-City").slideToggle("slow");
        $("#mappings-City").fadeIn("show");
    }
}

//PMN script. These methods only gets called from the index page to get a list of meetings in the area
function getPmn(cityName){
    $.post('/locationaware/getMeetings.html', { cityName: cityName, listSize:5 },
        function(data){
            $("#meetingOupt").html(data);
            scrollCalendar();
        });
}

function scrollCalendar(){
    if($('.calendarOuput').size() > 1){
        $("#calendar").scrollable({
            items:'.calendarOuput',
            horizontal:false,
            speed:300,
            navi:'p.navi',
            size: 1
        });
    }
}


//Services script
function getServiceCity(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getOnlineServices.html', {zipCode:zip,locationType:'CITY',type:'html',listSize:5}, renderServiceCityList);
}

function renderServiceCityList(html){
    $("#services-County").html('');
    $("#services-City").html(html);
    $(".serviceName:first").trigger('click');
    $(".serviceName:first").attr('class','highlight');// add background

}

function getServiceCounty(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getOnlineServices.html', {zipCode:zip,locationType:'COUNTY',type:'html',listSize:5}, renderServiceCountyList);
}

function renderServiceCountyList(html){
    $("#services-City").html('');
    $("#services-County").html(html);
    $(".serviceName:first").trigger('click');
    $(".serviceName:first").attr('class','highlight');// add background
}

function getServiceSnapShot(url){
    $("#screenshots").css({'background': 'url(/locationaware/getSnapShot.html?url=' + url + ') no-repeat 50%', cursor:'pointer'});
	// Then Remove the paragraph from display...
    $("#screenshots p").css({'display': 'none'});
}

function getServiceSnapShotByName(image){
	$("#screenshots").html("");
//	$("#screenshots").css({'background': 'url(/locationaware/images/ajax-loader.gif) no-repeat 50% 50%'});
	$("#screenshots").css({'background': 'url(/locationaware/images/' + image + ') no-repeat 50%', cursor:'pointer'});
}

function gotToService(url){
    $("#screenshots").unbind("click").click(function(){
        window.open(url);
    });
}



//Locations script. These methods are used on the location page
//City = 1;
//School = 2;
//Library = 3
//Park = 4
function getCity(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:1,type:'html',listSize:5}, renderCityList);
    $.getJSON('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:1,type:'json',listSize:5}, renderGoogleMap);
}

function renderCityList(html){
    $("#mappings-Parks").html('');
    $("#mappings-Library").html('');
    $("#mappings-School").html('');
    $("#mappings-City").html(html);
}

function getParks(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:4,type:'html',listSize:5}, renderParksList);
    $.getJSON('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:4,type:'json',listSize:5}, renderGoogleMap);
}

function renderParksList(html){
    $("#mappings-Parks").html(html);
}

function getLibrary(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:3,type:'html',listSize:5}, renderLibraryList);
    $.getJSON('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:3,type:'json',listSize:5}, renderGoogleMap);
}

function renderLibraryList(html){
    $("#mappings-Library").html(html);
}

function getSchools(){
    var zip = jaaulde.utils.cookies.get('zip');
    $.post('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:2,type:'html',listSize:5}, renderSchoolList);
    $.getJSON('/locationaware/getNearByLocations.html', {zipCode:zip,locationType:2,type:'json',listSize:5}, renderGoogleMap);
}

function renderSchoolList(html){
    $("#mappings-School").html(html);
}

function renderGoogleMap(locations){
    $("#googleMap").googlemap({addresses: locations});
}

function renderSearchGoogleMap(astreet, acity, aname, aurl, aphone, alat, alon){
	$("#stateAgencyMap").css({display:''});
	$("#agencyResults .hint").css({display:''});
	$("#stateAgencyMap").googlemap({controls: true, zoom: 12, addresses: [{street:astreet,city:acity,name:aname,url:aurl,phone:aphone,lat:alat,lon:alon}]});
}
