var map;
var currAddress;
var doAnimateChandelier = true;

function initContact() {
    currAddress = $("#contact .location:first .address").html().replace(/<\/?[^>]+>/gi, '');
    initGoogleMap(currAddress);
    
    $("#directions form .submit").click(function() {
        $("#directions input:hidden").val(currAddress);
        $("#directions form")[0].submit();
    });
    
    $("#contact_jobs").accordion({
	    header: '.contact_job',
	    autoHeight: false
	});
	
	$("#contact_jobs .cv_upload input").change(function() {
	    var jobSelector = "[data-job-id='" + $(this).parent().attr("data-job-id") + "']";
	    var jobHeader = $("#contact_jobs .contact_job" + jobSelector);
	    var jobDesc = $("#contact_jobs .description" + jobSelector);
	    if($(this).val() === "") {
	        $(this).parent().find(".upload_text").show();
	        $(this).parent().find("img").attr("src", "/static/images/icon_upload_resume.png");
	        $(this).parent().find("input").width(115);
	        jobDesc.find(".uploaded_actions").hide();
	    } else {
	        $(this).parent().find(".upload_text").hide();
	        $(this).parent().find("img").attr("src", "/static/images/icon_upload_resume_done.png");
	        $(this).parent().find("input").width(25);
	        jobDesc.find(".uploaded_actions").show();
	        $("#contact_jobs").accordion("activate", "div" + jobSelector);
	    }
	});
	
	$("#contact_jobs form").ajaxForm({
	    beforeSubmit: function(arr, form, options) {
	        form.parent().find(".description .notifier img").show();
	    },
	    success: function(responseText, statusText, xhr, form) {
	        form.parent().find(".description .notifier img").hide();
	        form.parent().find(".description .notifier span").show();
	    }
	});
}

function changeUpload(id) {
    $("#contact_jobs .cv_upload[data-job-id='" + id + "'] input").click();
}

function submitUpload(id) {
    $("#contact_jobs .cv_upload[data-job-id='" + id + "']").parent().submit();
}

function initGoogleMap(address) {
    var mapOptions = {
        zoom: 16,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( {'address': address}, function(results, status) {
       if(status == google.maps.GeocoderStatus.OK) {
           mapOptions['center'] = results[0].geometry.location;
           map = new google.maps.Map(document.getElementById("google_map"), mapOptions);
           var marker = new google.maps.Marker({
               position: results[0].geometry.location, 
               map: map, 
               title: "Pereira & O'Dell",
               icon: "/static/images/pod_logo_circle.png"
           });
       } 
    });
    return false;
}

function toggleMap(selected, locationNum) {
    currAddress = $("#contact .location[data-loc='" + locationNum + "'] .address").html().replace(/<\/?[^>]+>/gi, '');
    $("#directions a").removeClass('on');
    $(selected).addClass('on');
    return initGoogleMap(currAddress);
}

function animateChandelier() {
    if(doAnimateChandelier && $(window).scrollTop() > $("#contact").offset().top - 80) {
        doAnimateChandelier = false;
        $("#chandelier img").slice(1,2).attr("src", "/static/images/chandelier.gif");
    } else if($(window).scrollTop() <= $("#contact").offset().top - 80) {
        doAnimateChandelier = true;
        $("#chandelier img").slice(1,2).attr("src", "/static/images/chandelier.jpg");
    }
}

