var imageLinkClicked = false; //user must upload image

$(document).ready(function(){
    // prepare the form when the DOM is ready 
    $(document).ready(function(){
	
	//Connect this INN/Pub with temp image id for this Inn/Pub
	$("#tempInnId").val($("#tempImageID").html());
		
    var options = {
        	beforeSubmit: validateAll, // pre-submit callback 
            success: showResponse, // post-submit callback 
            clearForm: true ,       // clear all form fields after successful submit 
            resetForm: true        // reset the form after successful submit 
        };
        // bind form using 'ajaxForm' 
        $('#AddInnForm').ajaxForm(options);
    });
    
	//make blink effect on tip text
	function blink(selector){
		$(selector).fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast");
	}
	
	//add listener to image up load link, //user must upload image
	$("#uploadImageLink").click(function(){
		imageLinkClicked = true;
	});
    
    // post-submit callback 
    function showResponse(responseText, statusText){
		$("#AddInnForm .submit").fadeIn();
		$("#Step" + currentStep).hide();
		$("#FormResult").show();
    }
    
    //Add opacity hover
    $('.add-hover').hover(function(){
        $(this).addClass('pretty-hover');
    }, function(){
        $(this).removeClass('pretty-hover');
    });
    
    
	function resetForm(){
		$("#Step" + currentStep).hide();
		$("#Step1").show();
		currentStep = 1;
	}

    //setup Previous and Next buttons
    var currentStep = "1";
    $('.hidden').hide();
    $('.navigation').click(function(){
		if(validateStep(currentStep)){
	        var gotoStep = $(this).attr('href');
	        
	        $("#Step" + currentStep).hide()
	        $("#Step" + gotoStep).show();
			
	        currentStep = gotoStep;
			
			if (currentStep == "5") {
				blink("#map-tip");
			}
		}
        return false;
    });
	
	/*********************************************************
	 * Validation
	 *********************************************************/
	$("#addedBy").blur(function(){testRequired(this.id);});
	$("#name").blur(function(){testRequired(this.id);});
	$("#country").change(function(){testRequired(this.id);});
	$("#region").change(function(){testRequired(this.id);});
	
	$("#address").blur(function(){testRequired(this.id);});
	//$("#postcode").blur(function(){testRequired(this.id);});
	$("#city").change(function(){testRequired(this.id);});
	$("#email").change(function(){validateEmail(this.id);});
	
	$("#description").change(function(){testRequired(this.id);});
	$("#toSeeNearby").change(function(){testRequired(this.id);});
	//$("#userComment").change(function(){testRequired(this.id);});
	
	// pre-submit callback 
    function validateAll(formData, jqForm, options){
		if(!validateStep("all")){
			blink(".error");
			return false;
		}
		else if($("input:radio[@name=rating]:checked").size() == 0){
			blink("#RatingTip");
			//blink("#map-tip");
			return false;
		}
		else if($("input:checkbox[@name=amenitiy]:checked").size() == 0){
			blink("#Amenities-label");
			return false;
		}
		else if($("#googleLat").val()==""){
			blink("#map-tip");
			return false;
		}
		/*
		else if(!imageLinkClicked){
			blink($("#uploadImageLink"));
			return false;
		}*/
		$("#AddInnForm .submit").fadeOut("fast");
		return true;
    }
	
	function validateStep(step){
		var errors = "";
		if (step == "1") {
			errors = errors + testRequired("addedBy");
			errors = errors + testRequired("name");
			errors = errors + testRequired("city");
			errors = errors + testRequired("region");
		}
		else if (step == "2") {
			errors = errors + testRequired("address");
		}
		else if (step == "3") {
			errors = errors + testRequired("description");
			errors = errors + testRequired("toSeeNearby");
			//errors = errors + testRequired("userComment");
		}
		else if (step == "4") {
			errors = errors + validateEmail("email");
		}
		else if (step == "all") {
			errors = errors + testRequired("addedBy");
			errors = errors + testRequired("name");
			errors = errors + testRequired("region");	
			errors = errors + testRequired("address");
			errors = errors + testRequired("city");
			errors = errors + validateEmail("email");
			errors = errors + testRequired("description");
			errors = errors + testRequired("toSeeNearby");
			//errors = errors + testRequired("userComment");
		}	
		
		if(errors == ""){
			return true;
		}else{
			blink(".error");
			return false;
		}
	}
	
	function validateEmail(id){
		var valid = false;
		var email = $("#" + id).attr('value');

		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){ 
			valid = true;
		}
		if(!valid){
			$("#valid" + id).remove();
			$("#error" + id).remove();
			$("#" + id).after('<div class="error" id="error' + id + '"></div>');
			return "-";
		}else{
			$("#valid" + id).remove();
			$("#error" + id).remove();
			$("#" + id).after('<div class="valid" id="valid' + id + '"></div>');
			return "";
		}
	}
	
	function testRequired(id){
		if($("#" + id).attr('value')==""){
			$("#valid" + id).remove();
			$("#error" + id).remove();
			$("#" + id).after('<div class="error" id="error' + id + '"></div>');
			return "-";
		}else{
			$("#valid" + id).remove();
			$("#error" + id).remove();
			$("#" + id).after('<div class="valid" id="valid' + id + '"></div>');
			return "";
		}
	}
});

var marker = null;
var map;

function showPostCode(postcode){
	postcode = postcode.replace(/\s/g, "");
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(postcode + " ,UK", function(point){
        if (point) {
            map.setCenter(point, 14);
        }
    });
}

function initialize(){
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("add-inn-map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
        map.setCenter(new GLatLng(55.5964, -7.7065), 5);
        GEvent.addListener(map, "click", function(overlay, point){
            document.getElementById("googleLat").value = point.y;
            document.getElementById("googleLng").value = point.x;
            if (marker != null) {
                map.removeOverlay(marker);
            }
            marker = new GMarker(point, {
                draggable: true
            });
            map.addOverlay(marker);
            marker.enableDragging();
            GEvent.addListener(marker, "drag", function(){
                document.getElementById("googleLat").value = marker.getPoint().y;
                document.getElementById("googleLng").value = marker.getPoint().x;
            });
        });
    }
}


