/**
 * @author mcDev3
 */
var r = 3963.0;
var multiplier = 1;
var geocoder = null;
var location1 = {};

function initialize(){
    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
    }
}

function findAddress(address){
    if (geocoder) {
        geocoder.getLatLng(address, function(point){
            if (!point) {
                alert(address + " not found");
            }
            else {
                alert("Latitude: " + point.lat());
                alert("Longitude: " + point.lng());
            }
        });
    }
}

function distanceSort(){


}

function getParameterByName(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) 
        return "";
    else 
        return results[1];
    
}


jQuery(document).ready(function(){


    jQuery(".featuredAgentTable tbody tr").each( //HoverOn
function(){
        //select each cell in the current row
        jQuery(this).children("td").addClass("RowHover");
        var premium = jQuery(this).children("td:nth-child(11)").text();
        var results = "<span class='Header'>State Farm Agent</span>";
        var image = jQuery(this).children("td:nth-child(7)").html();
        var name = jQuery(this).children("td:nth-child(1)").text();
        var namehtml = jQuery(this).children("td:nth-child(1)").html();
        var phone = jQuery(this).children("td:nth-child(6)").html();
        var city = jQuery(this).children("td:nth-child(3)").text();
        var state = jQuery(this).children("td:nth-child(4)").text();
        //var link = jQuery(this).children("td:nth-child(7)").html();
        var id = jQuery(this).children("td:nth-child(10)").text();
        
        //Build results
        results = results + "<span class='image'><img src=" + image + "></img></span>";
        if (premium == "True") {
            results = results + "<a href='GetAQuote.aspx?" + id + "&agentname=" + name + "&KeepThis=true&TB_iframe=true&height=325&width=360' class='GetQuote thickbox'>Get a Quote</a>";
        }
        results = results + "<span class='name'>" + name + "</span>";
        results = results + "<span class='phone'>" + phone + "</span>";
        results = results + "<span class='city'>" + city + ", " + state + "</span>";
        
        
        //inject results into results container
        jQuery("#Results").html(results);
        
        
        jQuery("#Results").css({
            //display results
            'display': 'block'
        });
        jQuery(this).children("td:nth-child(1)").html(namehtml);
        tb_init('a.thickbox, area.thickbox, input.thickbox');
    });
    
    jQuery(".tablesorter thead tr").children("th:nth-child(7)").css({
        'display': 'none'
    });
    jQuery(".tablesorter thead tr").children("th:nth-child(8)").css({
        'display': 'none'
    });
    jQuery(".tablesorter thead tr").children("th:nth-child(9)").css({
        'display': 'none'
    });
    jQuery(".tablesorter thead tr").children("th:nth-child(10)").css({
        'display': 'none'
    });
    jQuery(".tablesorter thead tr").children("th:nth-child(11)").css({
        'display': 'none'
    });
    
    jQuery(".featuredAgentTable thead tr").children("th:nth-child(7)").css({
        'display': 'none'
    });
    jQuery(".featuredAgentTable thead tr").children("th:nth-child(8)").css({
        'display': 'none'
    });
    jQuery(".featuredAgentTable thead tr").children("th:nth-child(9)").css({
        'display': 'none'
    });
    jQuery(".featuredAgentTable thead tr").children("th:nth-child(10)").css({
        'display': 'none'
    });
    jQuery(".featuredAgentTable thead tr").children("th:nth-child(11)").css({
        'display': 'none'
    });
    jQuery(".featuredAgentTable tbody tr").each(function(){
    
        // Use This code to hide specific columns
        //hide image column
        jQuery(this).children("td:nth-child(7)").css({
            'display': 'none'
        });
        //hide lat
        jQuery(this).children("td:nth-child(8)").css({
            'display': 'none'
        });
        //hid long
        jQuery(this).children("td:nth-child(9)").css({
            'display': 'none'
        });
        //hide agent id
        jQuery(this).children("td:nth-child(10)").css({
            'display': 'none'
        });
        //hide premium
        jQuery(this).children("td:nth-child(11)").css({
            'display': 'none'
        });
    });
    jQuery(".tablesorter tbody tr").each(function(){
    
        // Use This code to hide specific columns
        //hide image column
        jQuery(this).children("td:nth-child(7)").css({
            'display': 'none'
        });
        //hide lat
        jQuery(this).children("td:nth-child(8)").css({
            'display': 'none'
        });
        //hid long
        jQuery(this).children("td:nth-child(9)").css({
            'display': 'none'
        });
        //hide agent id
        jQuery(this).children("td:nth-child(10)").css({
            'display': 'none'
        });
        //hide premium
        jQuery(this).children("td:nth-child(11)").css({
            'display': 'none'
        });
    });
    jQuery(this).children("td:nth-child(9)").css({
        'display': 'none'
    });
    jQuery(".tablesorter thead tr th").wrapInner("<span class='outer' style=''><span class='inner' style=''></span></span>");
    
    
    
    jQuery("#Results").css({
        //display results
        'display': 'none'
    });
    
    //tablesorter onhover
    jQuery(".tablesorter tbody tr").hover( //HoverOn
function(){
        //select each cell in the current row
        jQuery(this).children("td").addClass("RowHover");
        var premium = jQuery(this).children("td:nth-child(11)").text();
        var results = "<span class='Header'>State Farm Agent</span>";
        var image = jQuery(this).children("td:nth-child(7)").html();
        var name = jQuery(this).children("td:nth-child(1)").text();
        var namehtml = jQuery(this).children("td:nth-child(1)").html();
        var phone = jQuery(this).children("td:nth-child(6)").html();
        var city = jQuery(this).children("td:nth-child(3)").text();
        var state = jQuery(this).children("td:nth-child(4)").text();
        //var link = jQuery(this).children("td:nth-child(7)").html();
        var id = jQuery(this).children("td:nth-child(10)").text();
        
        //Build results
        results = results + "<span class='image'><img src=" + image + "></img></span>";
        if (premium == "True") {
            results = results + "<a href='GetAQuote.aspx?" + id + "&agentname=" + name + "&KeepThis=true&TB_iframe=true&height=325&width=360' class='GetQuote thickbox'>Get a Quote</a>";
        }
        results = results + "<span class='name'>" + name + "</span>";
        results = results + "<span class='phone'>" + phone + "</span>";
        results = results + "<span class='city'>" + city + ", " + state + "</span>";
        
		  var display = jQuery(this).hasClass("noitems");
		if (display){
			
		}
        else{        //inject results into results container
        jQuery("#Results").html(results);
        jQuery("#Results").css({
            //display results
            'display': 'block'
        });
        jQuery(this).children("td:nth-child(1)").html(namehtml);
        tb_init('a.thickbox, area.thickbox, input.thickbox');
        }
		       
        //inject results into results container
        jQuery("#Results").html(results);
        jQuery("#Results").css({
            //display results
            'display': 'block'
        });
        jQuery(this).children("td:nth-child(1)").html(namehtml);
        tb_init('a.thickbox, area.thickbox, input.thickbox');
        
    }, //HoverOff
 function(){
        //select each cell in the current row
        jQuery("td").removeClass("RowHover")
        
        //tb_init('a.thickbox, area.thickbox, input.thickbox');
    
    
    });
    
    jQuery(".featuredAgentTable tbody tr").hover( //HoverOn
function(){
        //select each cell in the current row
        jQuery(this).children("td").addClass("RowHover");
        var premium = jQuery(this).children("td:nth-child(11)").text();
        var results = "<span class='Header'>State Farm Agent</span>";
        var image = jQuery(this).children("td:nth-child(7)").html();
        var name = jQuery(this).children("td:nth-child(1)").text();
        var namehtml = jQuery(this).children("td:nth-child(1)").html();
        var phone = jQuery(this).children("td:nth-child(6)").html();
        var city = jQuery(this).children("td:nth-child(3)").text();
        var state = jQuery(this).children("td:nth-child(4)").text();
        //var link = jQuery(this).children("td:nth-child(7)").html();
        var id = jQuery(this).children("td:nth-child(10)").text();
        
        //Build results
        results = results + "<span class='image'><img src=" + image + "></img></span>";
        if (premium == "True") {
            results = results + "<a href='GetAQuote.aspx?" + id + "&agentname=" + name + "&KeepThis=true&TB_iframe=true&height=325&width=360' class='GetQuote thickbox'>Get a Quote</a>";
        }
        results = results + "<span class='name'>" + name + "</span>";
        results = results + "<span class='phone'>" + phone + "</span>";
        results = results + "<span class='city'>" + city + ", " + state + "</span>";
        
        
        //inject results into results container
        jQuery("#Results").html(results);
        jQuery("#Results").css({
            //display results
            'display': 'block'
        });
        jQuery(this).children("td:nth-child(1)").html(namehtml);
        tb_init('a.thickbox, area.thickbox, input.thickbox');
        
    }, //HoverOff
 function(){
        //select each cell in the current row
        jQuery("td").removeClass("RowHover")
        
        //tb_init('a.thickbox, area.thickbox, input.thickbox');
    
    
    });
    
    var lat = jQuery("span.lat").text();
    var lon = jQuery("span.long").text();
    var address = jQuery("div.AgentDetailView div.Address").html();
    var agentname = jQuery("div.AgentDetailView .name").html();
    var agentphone = jQuery("div.AgentDetailView .phone").html();
    
    
    var map = jQuery('div.jmap').jmap('init', {
        'mapType': G_NORMAL_MAP,
        'mapCenter': [lat, lon]
    });
    
    jQuery('div.jmap').jmap('AddMarker', {
        'pointLatLng': [lat, lon],
        'pointHTML': address
    });
    
    //zip search input behavior
    jQuery('.ZipSearchTextBox').attr("value", "Enter Zip Code");
    /*
     dropValue = jQuery('.CityDropDown option:first').attr("value");
     
     if(dropValue=="URL Parameter: pRID is Missing"){
     
     jQuery('.CityDropDown option:first').attr("value", "Select a City");
     jQuery('.CityDropDown option:first').html("Select a City");
     }
     */
    dropValue = jQuery('.CityDropDown option:eq(1)').attr("value");
    
      if (dropValue == "URL Parameter: pRID is Missing" || dropValue == "Incorrect syntax near ')'.") {
		var CityOptionString = '<option value="Select a City">Select a City</option><option value="Chapel Hill">Chapel Hill</option><option value="Charlotte">Charlotte</option><option value="Durham">Durham</option><option value="Elizabeth City">Elizabeth City</option><option value="Goldsboro">Goldsboro</option><option value="Greensboro">Greensboro</option><option value="Greenville">Greenville</option><option value="Henderson">Henderson</option><option value="Raleigh">Raleigh</option><option value="Winston Salem">Winston Salem</option>';
    	jQuery('.CityDropDown option:eq(1)').remove();
		//jQuery('.CityDropDown').html(CityOptionString);
        //jQuery('.CityDropDown option:eq(1)').attr("value", "");
        //jQuery('.CityDropDown option:eq(1)').html("");
		
    }
    
    jQuery('.ZipSearchTextBox').focus(function(){
        if (jQuery('.ZipSearchTextBox').val() == "Enter Zip Code") 
            jQuery('.ZipSearchTextBox').attr("value", "");
        
    });
    jQuery('.ZipSearchTextBox').blur(function(){
        var text = jQuery('.ZipSearchTextBox').val();
        if (text == "") 
            jQuery('.ZipSearchTextBox').attr("value", "Enter Zip Code");
    });
    
    var pzip = getParameterByName("pzip");
    var pcity = getParameterByName("pcity");
    
    if (pzip != "") {
        jQuery("#Results").hide();
        initialize();
        geocoder.getLatLng(pzip, function(point){
            if (!point) {
                alert("Zip code provided is not valid");
            }
            else {
                var location1 = {
                
                    latitude: point.lat(),
                    longitude: point.lng()
                };
                var output = "<p class='lg-font-red'>Search results for " + pzip + "</p>"
                jQuery("#dnn_ctr416_ModuleContent").prepend(output);
                jQuery(".tablesorter tbody tr").each(function(){
                    //Second location
                    var location2 = {
                        //set latitude equal to contents of column 2
                        latitude: jQuery(this).children("td:nth-child(8)").text(),
                        //set longitude equal to contents of column 3
                        longitude: jQuery(this).children("td:nth-child(9)").text()
                    };
                    
                    var distance = 0;
                    //Calculate distance in miles
                    //alert(distance);
                    
                    distance = multiplier * r *
                    Math.acos(Math.sin(location1.latitude / 57.2958) *
                    Math.sin(location2.latitude / 57.2958) +
                    Math.cos(location1.latitude / 57.2958) *
                    Math.cos(location2.latitude / 57.2958) *
                    Math.cos(location2.longitude / 57.2958 -
                    location1.longitude / 57.2958));
                    
                    //alert ("Latitude1: " + location1.latitude + " Longitude1: " + location1.longitude);
                    //alert ("Latitude2: " + location2.latitude + " Longitude2: " + location2.longitude);
                    //alert ("Distance: " + distance);
                    
                    //insert calculated distance into column 4
                    //alert (distance);
                    jQuery(this).children("td:nth-child(9)").text(Math.round(distance) / 100);
                    
                    
                    
                    
                });
                //sort by new column
                jQuery(".tablesorter").tablesorter({
                    //sort column 4 in ascending order
                    headers: {
                        1: {
                            sorter: false
                        },
                        5: {
                            sorter: false
                        }
                    },
                    sortList: [[8, 0]]
                }).tablesorterPager({
                    container: jQuery("#pager"),
                    positionFixed: false
                });
                
            }
        });
    }
    else {
    
    
    
        if (pcity != "") {
            jQuery("#Results").hide();
            initialize();
            pcity = pcity + ", " + jQuery(".tablesorter tbody tr:first").children("td:nth-child(4)").text();
            
            geocoder.getLatLng(pcity, function(point){
                if (!point) {
                    alert(address + " not found");
                }
                else {
                    var location1 = {
                        //default location Raleigh, NC
                        latitude: point.lat(),
                        longitude: point.lng()
                    };
                    var output = "<p class='lg-font-red'>Search results for " + pcity + "</p>"
                    jQuery("#dnn_ctr425_ModuleContent").prepend(output);
                    jQuery(".tablesorter tbody tr").each(function(){
                        //Second location
                        var location2 = {
                            //set latitude equal to contents of column 2
                            latitude: jQuery(this).children("td:nth-child(8)").text(),
                            //set longitude equal to contents of column 3
                            longitude: jQuery(this).children("td:nth-child(9)").text()
                        };
                        var distance = 0;
                        //Calculate distance in miles
                        distance = multiplier * r *
                        Math.acos(Math.sin(location1.latitude / 57.2958) *
                        Math.sin(location2.latitude / 57.2958) +
                        Math.cos(location1.latitude / 57.2958) *
                        Math.cos(location2.latitude / 57.2958) *
                        Math.cos(location2.longitude / 57.2958 -
                        location1.longitude / 57.2958));
                        
                        //alert ("Latitude1: " + location1.latitude + " Longitude1: " + location1.longitude);
                        //alert ("Latitude2: " + location2.latitude + " Longitude2: " + location2.longitude);
                        //alert("Distance: " + distance);
                        
                        //insert calculated distance into column 4
                        jQuery(this).children("td:nth-child(9)").text(Math.round(distance) / 100);
                        
                        
                        
                        
                    });
                    //sort by new column
                    jQuery(".tablesorter").tablesorter({
                        //sort column 4 in ascending order
                        headers: {
                            1: {
                                sorter: false
                            },
                            5: {
                                sorter: false
                            }
                        },
                        sortList: [[8, 0]]
                    }).tablesorterPager({
                        container: jQuery("#pager"),
                        positionFixed: false
                    });
                    
                }
            });
        }
        else {
            jQuery(".tablesorter").tablesorter({
                //sort column 4 in ascending order
                headers: {
                    1: {
                        sorter: false
                    },
                    5: {
                        sorter: false
                    }
                }
            }).tablesorterPager({
                container: jQuery("#pager"),
                positionFixed: false
            });
        }
    }
    
    
    
    jQuery(".tablesorter tbody tr").click(function(){
        var premium = "";
        var id = jQuery(this).children("td:nth-child(10)").text();
        var name = jQuery(this).children("td:nth-child(1)").text();
        premium = jQuery(this).children("td:nth-child(11)").text();
        var t = "";
        var a = "GetAQuote.aspx?" + id + "&agentname=" + name + "&KeepThis=true&TB_iframe=true&height=325&width=360";
        //alert(a);
        var g = false;
        if (premium == "True") {
            tb_show(t, a, g);
            this.blur();
            return false;
        }
        
    });
    
    jQuery(".featuredAgentTable tbody tr").click(function(){
    
        var id = jQuery(this).children("td:nth-child(10)").text();
        var name = jQuery(this).children("td:nth-child(1)").text();
        var t = "";
        var a = "GetAQuote.aspx?" + id + "&agentname=" + name + "&KeepThis=true&TB_iframe=true&height=325&width=360";
        //alert(a);
        var g = false;
        
        tb_show(t, a, g);
        this.blur();
        return false;
        
    });
});



