function init()
{
    $("#city").autocomplete('/buFront/ajax/list-city/format/json/',
        {
            extraParams: {
                county: function() { return $("#county").val(); }
            },
            dataType: "json",
            parse: function(data) {
                return $.map(data.cityList, function(row) {
                    return {
                        data: row,
                        value: row.buCityName,
                        result: row.buCityName
                    } ;
                });
            },
            formatItem: function(item) {
                return (item.buCityZip+' '+item.buCityName);
            }
        }
    );
    $('#city').result(function(event, data, formatted) {
        if (data) {
        	getCount($('#jobType').val(), $('#county').val(), $('#city').val());
        }
    });
    $('#county').change(
        function (e) {
            if ($(this).val() != '')
            {
                $('#city').val('');
            }
        }
    );
    //$('#jobType').select_autocomplete();        
}
function getCount(jobTypeVar, countyVar, cityVar)
{
	/*
    $.getJSON(
       "/buFront/ajax/count-builder/format/json/",
       { "jobType[]": jobTypeVar, "county": countyVar, "city": cityVar },
       function(data){
            $('.professionCounter .number').html(data.count);
       });
    */
    jQuery.ajax({
		type: "POST",
		url: "/buFront/ajax/count-builder/format/json/",
		data: { "jobType[]": jobTypeVar, "county": countyVar, "city": cityVar },
		success: function(data){
            $('.professionCounter .number').html(data.count);
	       },
		dataType: 'json'
	});
}

function validateSearch()
{
    var ret = false;
    var jobTypeArray=$('#jobType').val();
    var check = $('.professionCounter .number').html();
    if (jobTypeArray!= null && jobTypeArray.length>0 && check>0 && ($('#county').val() > 0 || jQuery.trim($('#city').val()) != '' ) ) {
        ret = true;
    }
    if ( ! ret && $('#searchForm').attr('submitPressed')== 'true') {
        $('#searchInfo').show();
        $('.searchInfoIndexBubi').show();
        $('.searchInfoIndexX').show();
    } else {
        $('#searchInfo').hide();
        $('.searchInfoIndexBubi').hide();
        $('.searchInfoIndexX').hide();
    }
    return ret;
}
$().ready(
    function() {
        init();
        $("#searchForm select").change(function () {
            getCount($('#jobType').val(), $('#county').val(), $('#city').val());
        });
        $('#searchForm').submit(function (f) {
            $('#searchForm').attr('submitPressed', 'true');
            return validateSearch();
        }).attr('submitPressed', 'false');
        //$('#searchForm').change(validateSearch);
        if(typeof secondaryReady == 'function') {
            secondaryReady();
        }
        if(typeof modifyJobReady == 'function') {
        	modifyJobReady();
        }
        
        if ($('#googleShopMap') != null) {        	
        	//initShopMap();
        }
    }        
);


$('.searchInfoIndexX').click(
    function() {
        $('#searchInfo').hide();
        $('.searchInfoIndexBubi').hide();
        $('.searchInfoIndexX').hide();
    }
);

function initShopMap()
{	
	initShopCityAutofill();
	initMarkerStyle();
	
	var latlngShop = new google.maps.LatLng(config.lat,config.lng);	
	var myOptionsShop = {
			zoom: 6,
			center: latlngShop,
            mapTypeControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
	};
    config.map = new google.maps.Map(document.getElementById("googleShopMap"), myOptionsShop);
    setMarkers(config.map, config.builders);
    
    markerCluster = new MarkerClusterer(config.map, config.markers, {
    	maxZoom: null,
    	gridSize: 24,
    	styles: markerStyles[0]
    });
                       
}

function initMarkerStyle()
{
	markerStyles = [[{
         url: config.markerImg,
         height: 29,
         width: 30,
         opt_anchor: [2, 0],
         opt_textColor: '#000000',
         opt_textSize: 11
       }, {
         url: config.markerImg,
         height: 29,
         width: 30,
         opt_anchor: [2, 0],
         opt_textColor: '#000000',
         opt_textSize: 11
       }, {
         url: config.markerImg,
         height: 29,
         width: 30,
         opt_anchor: [2, 0],
         opt_textColor: '#000000',
         opt_textSize: 11
       }, {
         url: config.markerImg,
         height: 29,
         width: 30,
         opt_anchor: [2, 0],
         opt_textColor: '#000000',
         opt_textSize: 11
       }, {
         url: config.markerImg,
         height: 29,
         width: 30,
         opt_anchor: [2, 0],
         opt_textColor: '#000000',
         opt_textSize: 11
         }
       ]];
}

function setMarkers(map, locations)
{
    var image = new google.maps.MarkerImage(config.imageDir+'/image/tool.png',
        new google.maps.Size(14, 34),
        new google.maps.Point(0,0),
        new google.maps.Point(14, 34));    
    for (var i = 0; i < locations.length; i++) {
        var hq = locations[i];        
        var myLatLng = new google.maps.LatLng(hq.lat, hq.lng);        
        if (i == 0)
        {
            config.bounds=new google.maps.LatLngBounds(myLatLng, myLatLng);
        }
        else
        {
            config.bounds.extend(myLatLng);
        }
        //config.bounds=new google.maps.LatLngBounds(myLatLng, myLatLng);
        hq.i=i;
        config.markers[i] = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: image,            
            title: hq.name,
            zIndex : hq.zIndex,
            specData: hq
        });        
    }
   // config.map.fitBounds(config.bounds);
}

function initShopCityAutofill()
{
	 $("#shopBuCityName").autocomplete('/buFront/ajax/list-city/format/json/', {		            
		 dataType: "json",
		 parse: function(data) {
		 	return $.map(data.cityList, function(row) {
		 		return {
		 			data: row,
		            value: row.buCityName,
		            result: row.buCityName
		        } ;
		    });
		 },
		 formatItem: function(item) {
			 return (item.buCityZip+' '+item.buCityName);
		 }
	});
}



//if ( window.addEventListener ) {
//        var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
//        window.addEventListener("keydown", function(e){
//                kkeys.push( e.keyCode );
//                if ( kkeys.toString().indexOf( konami ) >= 0 ) {
//                    kkeys=[];
//                    if(typeof KONAMI == 'function') {
//                        KONAMI();
//                    }
//                }
//        }, true);
//}
