$(function() {

$('.trackpixel').click(function() {
	var url = 'http://atani101.com/update/lead_pixel.php?'+$(this).attr('data');
	$.ajax({
		url: url,
	    async: false
    });
});

    $('input.ui-state-default').hover(function(){

        $(this).addClass('ui-state-hover').css('cursor','pointer');

    }, function() {

        $(this).removeClass('ui-state-hover');

    });


       var arr = '{';

        $('p.pdata').each(function(){
            var data = $.metadata.get(this);
            arr += '"' + data.pid + '":' + data.procid + ',';
        });

        arr = arr.substr(0, arr.length - 1);

        arr += '}';




    $("div#resultsWrapper").quickPager();

    $('a.make_provider_offer').click(function() {

        jQuery.ready();

        var data = $(this).metadata();

        $('input#pid').val(data.pid);

        $('input#procid').val(data.procid);

        if(data.oc != '') {

            $('p#conditions').append('<span><b>Conditions: </b></span>' + data.oc);

        }

        $("div#makeOffer").dialog({
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
            bgiframe: true,
            modal: true,
            buttons: {
                Close: function() {

                    $(this).dialog('close').dialog('destroy');
                            
                    $('input#pid').val('');

                    $('input#procid').val('');

                    $('input#localOffer').val('');

                    $('p#conditions').empty();

                    $('div#response').empty();


                }
            }

        });

        if($('form#success').not(':visible')) {

                        $('form#makeOffer1').show();

                    }
        

        return false;
    });
    
    $('a.raform').click(function() {

       jQuery.ready();

       var pid= $(this).attr('data');

       $('input#rqpid').val(pid);
        
       $('p.procs').load('moreinfo.php?pid='+pid+ ' select#proceduresOffered');

       $("div#requestappnt").dialog({
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
            bgiframe: true,
            modal: true,
            width:450,
            buttons: {
                Close: function() {
                    $(this).dialog('close').dialog('destroy');
                            
                    $('input#rqpid').val('');
                    
                    $('p.procs').empty();
                }
            }
        });
	return false;
    });

		//Here begins Request Appointment Submit w/Validation
    	var raform = $('form#requestappointment');
    	var validate_rqa = {
	        errorClass: 'invalid',
	        submitHandler: function(raform) {
		       // do other stuff for a valid form
		       var vars = $('form#requestappointment').serialize();	       
		       $.ajax({
	            url: 'searchLeads.ajax.php',
	            async: false,
	            beforeSend: function() {
	            	$('#rqsubmit').parent().append(' <img class="ldr" src="../images/loading.gif" alt="" />');
	            },
	            data: vars,	            
	            success: function(html){					
					$('form#requestappointment').hide();
					$('#requestappnt').append('<p class="uinotice">Thank you!<br/>Your information has been received and you will hear from the provider soon.</p>');					
					window.setTimeout(function(){
						$('p.uinotice').remove();
						$('img.ldr').remove();
						$('form#requestappointment').show();
						$('#requestappnt').dialog('close').dialog('destroy');
					}, 5000);					
	                return true;
	            }
        });
		      	   		
			},
	        rules: {
	          rqfname: {
	            required: true
	          },
	          rqlname: {
	            required: true
	          },
	          rqemail: {
	            required: true,
	            email: true
	          },
	          rqphone: {
	            required: true,
	            phoneUS: true
	          },
	          proceduresOffered: {
	            required:true
	          }
	        },
	        messages: {
	          rqfname: {
	            required: "Please provide your First Name<br/>"
	          },
	          rqlname: {
	            required: "Please provide your Last Name<br/>"
	          },
	          rqemail: {
	            required: "Please provide your Email Address<br/>"
	          },
	          rqphone: {
	            required: "Please provide your Phone Number<br/>"
	          },
	          proceduresOffered: {
	            required: "Please select a procedure<br/>"
	          }
	        }
	      }
    	raform.validate(validate_rqa);
//Here ends Request Appointment Submit w/Validation
    	
    		


    //Submit Offer to Provider
    $('input#submitLocal').click(function() {
        
        jQuery.ready();

        var pid = $('input#pid').val();
        
        var procid = $('input#procid').val();

        var localOffer = $('input#localOffer').val();

		if(localOffer > 0 ) {

        $.ajax({
            
            type: "POST",

            url: 'makeoffer.ajax.php',

            beforeSend: function() {

                $('div#response').empty();

                $("div#makeOffer").block({
                    message: '<img src="../images/busy.gif"/> Submitting Offer...'
                });

            },
            
            data: 'pid=' + pid + '&procid=' + procid + '&localOffer=' + localOffer,

            success: function(msg){

                $("div#makeOffer").unblock();

                $(msg).appendTo('div#response');

                if($('form#success').is(':visible')) {

                        $('form#makeOffer1').hide();

                    }


                return true;
            }
        });

		}

        return false;

    });




    $('.GlobalNameYourPriceSubmit').live('click',function() {

        var globalOffer = $(this).prev('input.submittedPrice').val();

		if(globalOffer > 0){

        $.ajax({

            type: "POST",

            url: 'makeoffer.ajax.php',

            //dataType: "txt",
            beforeSend: function() {

                $.blockUI({ message: '<h1><img src="../images/busy.gif"/> Submitting Offer...</h1>'
                });

            },

            data: 'globalOffer=' + globalOffer + '&arr=' + arr,

            success: function(data){

                $.unblockUI();

                $('input.submittedPrice').val('');
                    
                $('div#globalResponse').append(data);

                $("div#makeGlobalOffer").dialog({
                    open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                    bgiframe: true,
                    modal: true,
                    buttons: {
                        Close: function() {

                            $(this).dialog('close').dialog('destroy');

                            $('div#globalResponse').empty();


                        }
                    }
                });

                return true;
            }
        });

		}

        return false;
        
    });

   
           $('a.giveProviderReferral').click(function() {

            var data = $(this).metadata();

            var pid = data.pid;

            var thisSpan = $(this).parent().find('span.referralCount');

            $.ajax({

                type: "POST",

                url: 'giveReferral.ajax.php',

                dataType: "json",

                beforeSend: function() {

                },

                data: 'pid=' + pid,

                success: function(data){

                    thisSpan.fadeOut();

                    thisSpan.html(data.refCount);

                    return true;
                }
            });

            return false;

        });

    

    $('#map').jmap('init', {
        'mapType':'map',
        'mapCenter':[$centerLat ,$centerLong]
    });

    $('a.call_now').click(function() {

        jQuery.ready();
        
        $('span#credential').html($(this).attr('data'));
        
   		$.post('../lib/logcallnow.ajax.php?c='+$(this).attr('data'),'',function(data) {});

        $("div#callNow").dialog({
            open: function(event, ui) { 
            	$(".ui-dialog-titlebar").hide(); 
//  				$(".ui-dialog").css("background-color","#7d922b");
            	},
            bgiframe: true,
            modal: true,
            width:600,
            height:380
/*
            ,
            buttons: {
                Close: function() {
                    $(this).dialog('close').dialog('destroy');
                }
            }
*/

        });

        return false;
    });
    
    $('.callNowClose').click(function() {
    	$("div#callNow").dialog('close').dialog('destroy');
    });
       

});

// This code generates a "Raw Searcher" to handle search queries. The Raw Searcher requires
// you to handle and draw the search results manually.
google.load('search', '1');

var localSearch;
function searchComplete() {

  // Check that we got results
  document.getElementById('content').innerHTML = '';
  if (localSearch.results && localSearch.results.length > 0) {
    for (var i = 0; i < localSearch.results.length; i++) {

      // Create HTML elements for search results
      var p = document.createElement('p');
      var a = document.createElement('a');
      var b = document.createElement('b');
      var c = document.createElement('c');
      a.href = localSearch.results[i].url;
      a.innerHTML = localSearch.results[i].title;
      b.innerHTML = "<br>" +
        localSearch.results[i].streetAddress;
      c.innerHTML = "<br>" +
        localSearch.results[i].city + "," +
        localSearch.results[i].region;

      // Append search results to the HTML nodes
      p.appendChild(a);
      p.appendChild(b);
      p.appendChild(c);
      document.getElementById('content').appendChild(p);
    }
  }
}

function onLoad() {

  var sr = document.getElementById('branding');
  if (sr != null) {
	  // Create a LocalSearch instance.
	  localSearch = new google.search.LocalSearch();

	  // Set the Local Search center point
	  localSearch.setCenterPoint(googleloc);

	  // Set searchComplete as the callback function when a search is complete. The
	  // localSearch object will have results in it.
	  localSearch.setSearchCompleteCallback(this, searchComplete, null);

	  // Specify search quer(ies)
	  localSearch.execute(googlespec+" "+googleloc);
  
	  // Include the required Google branding.
	  // Note that getBranding is called on google.search.Search
	  google.search.Search.getBranding('branding');
	}
}

// Set a callback to call your code when the page loads
google.setOnLoadCallback(onLoad);

