$(document).ready(function () {
	
    ///////////////// Superfish Menu
    $("ul.sf-menu").superfish({  pathClass: 'current' });
	$("#topNav").fadeIn(1600);
    
	
    ///////////////// Toggle InfoWindow
    $('#info_toggle, #pageTitle').click(function () {
        if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        } else {
            $('#infoWindow').fadeIn();
            $('#pageInfo #realestate_nav').fadeIn();
        }
        if ($('body#home').length) {
            $('body#home #pageInfo').toggleClass('bttn_about');
        }
        if ($('#member_login_form').is(':visible')) {
            $('#member_login_form').fadeOut();
        }
		if ($('#popup').is(':visible')) {
            $('#popup').fadeOut();
        }
        return false;
    });
    $('#info_close').click(function () {
        $('#infoWindow').fadeOut();
        if ($('body#home').length) {
            $('body#home #pageInfo').toggleClass('bttn_about');
        }
        $('#pageInfo #realestate_nav').fadeOut();
        return false;
    });
	
    ///////////////// Member Login Form
    $('#login').click(function () {
        $.fancybox.showActivity();
		
		if ($('#popup').is(':visible')) {
            $('#popup').fadeOut();
        }
		// new
		$("#popup").fadeOut("fast", function () {
            $("#popupContent").load('/members/login.php #form', function (response, status, xhr) {
				inputClass();
                
            if (status != 'success') {
				 
                $.fancybox({
						'content': "<p>There was an Error with this request.</p>",
						'showPrintButton' : false,
						'titleShow': false,
						'overlayColor': '#29231b'
					});
            } else { 
                
				
				if ($('#infoWindow').is(':visible')) {
                    $('#infoWindow').fadeOut();
                }
                if ($('body#home #pageInfo').is(':hidden')) {
                    $('body#home #pageInfo').toggleClass('bttn_about');
                }
				$("#popup").fadeIn("fast");
				$('#username').focus();
				$("#memberlogin-form").bind("submit", function(event) {
						var options = { 
							//target:        '#popup',   // using member form div to load messages that will be displayed with fancybox. target element(s) to be updated with server response
							beforeSubmit:  showRequest,  // pre-submit callback will add additinal validation at a later date
							success:       processJson,  // post-submit callback 
							dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
						}; 
					 
						// bind to the form's submit event 
						
						$(this).ajaxSubmit(options); 
						return false; 
	
					});
					$.fancybox.hideActivity();	
			 }
            });
        });
       
        return false;
    });
	
///// Forgot Password Form 
	$('a.forgotpass').live('click', function(e) {
    //$('.requestinfo').click(function (e) {
		e.preventDefault();
		$.fancybox.showActivity();
		if ($('#member_login_form').is(':visible')) {
            $('#member_login_form').fadeOut();
        }
		if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        }
        $("#popup").fadeOut("fast", function () {
            $("#popupContent").load('/members/forgot_password.php #form', function (response, status, xhr) {
                inputClass();
                $('#username').focus();
                $("#popup").fadeIn("fast");
				$("#forgotpassword-form").bind("submit", function(event) {
					var options = { 
						//target:        '#member_login_form',   // target element(s) to be updated with server response 
						beforeSubmit:  showRequest,  // pre-submit callback 
						success:       processJson,  // post-submit callback 
						dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
					}; 
				 
					// bind to the form's submit event 
					
					$(this).ajaxSubmit(options); 
					return false; 
					 
					
				});
				$.fancybox.hideActivity();
            });
        });
		
        if ($('#popup').is(':hidden')) {
            $('#popup').fadeIn();
        } else {
            return false;
        };
		
		
    });	
	
    ///////////////// Send to a Friend Form
    $('#sendtoafriend').click(function (e) {
        e.preventDefault();
		$.fancybox.showActivity();
        var pagetosend = $("#sendtoafriend").attr('rel');
		if ($('#member_login_form').is(':visible')) {
            $('#member_login_form').fadeOut();
        }
		if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        }
        $("#popup").fadeOut("fast", function () {
            $("#popupContent").load('/sendtoafriend/ #form', { 'page': pagetosend}, function (response, status, xhr) {
                inputClass();
                $('#first_name').focus();
                $("#popup").fadeIn("fast");
				$("#sendtofriend-form").bind("submit", function(event) {
					var options = { 
						//target:        '#member_login_form',   // using member form div to load messages that will be displayed with fancybox. target element(s) to be updated with server response
						beforeSubmit:  showRequest,  // pre-submit callback will add additinal validation at a later date
						success:       processJson,  // post-submit callback 
						dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
					}; 
				 
					// bind to the form's submit event 
					
					$(this).ajaxSubmit(options); 
					return false; 
					 
					
				});
				$.fancybox.hideActivity(); 
            }); 
        });
        if ($('#popup').is(':hidden')) {
            $('#popup').fadeIn();
        } else {
            return false;
        };
       
    });
	
	//close popup 
	$(".popupClose").click(function () {
			$('#popup').fadeOut();
			
	});
	
    ///////////////// Request More Info Form
	$('.requestinfo').live('click', function(e) {
    //$('.requestinfo').click(function (e) {
		e.preventDefault();
		$.fancybox.showActivity();
		if ($('#member_login_form').is(':visible')) {
            $('#member_login_form').fadeOut();
        }
		if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        }
        $("#popup").fadeOut("fast", function () {
            $("#popupContent").load('/request-info/ #form', function (response, status, xhr) {
                inputClass();
                $('#first_name').focus();
                $("#popup").fadeIn("fast");
				$("#request-info-form").bind("submit", function(event) {
					var options = { 
						//target:        '#member_login_form',   // target element(s) to be updated with server response 
						beforeSubmit:  showRequest,  // pre-submit callback 
						success:       processJson,  // post-submit callback 
						dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
					}; 
				 
					// bind to the form's submit event 
					
					$(this).ajaxSubmit(options); 
					return false; 
					 
					
				});
			    $.fancybox.hideActivity();
            });
			
        });
        if ($('#popup').is(':hidden')) {
            $('#popup').fadeIn();
        } else {
            return false;
        };
    });
	
	
    ///////////////// Stop slideshow and Show master or floor plan windows
    $(".floorplan").click(function () {
        $.fancybox.showActivity();
		$('#popup').fadeOut();
        var gallery = Galleria.get(0);
        var themap = $(this).attr('rel') == '' ? 'siteplan' : $(this).attr('rel');
        gallery.pause();
        var plan = "<img src='" + $(this).attr('href') + "'/>";
        $('#floorplans').fadeIn(200);
        $('#slideshow').fadeTo('slow', 0);
        $('#loadoverslide').html(plan).fadeIn();
        $('.masterPlan').fadeOut(100);
        $('.downloadPDF').fadeIn(100);
        $('#close_over').fadeIn();
		$.fancybox.hideActivity();
        return false;
    });
    $(".siteplan").click(function () {
        $.fancybox.showActivity();
		$('#popup').fadeOut();
        $('#floorplans').fadeOut(200);
        var gallery = Galleria.get(0);
        var themap = $(this).attr('rel') == '' ? 'siteplan' : $(this).attr('rel');
        gallery.pause();
        var plan = "<img src='" + $(this).attr('href') + "' usemap='#" + themap + "' class='maphi'/>";
        $('.masterPlan').fadeIn(100);
        $('.downloadPDF').fadeOut(100);
        $('#slideshow').fadeTo('slow', 0);
        $('#loadoverslide').html(plan).fadeIn();
        $('.maphi').maphilight();
        $('#close_over').fadeIn();
		$.fancybox.hideActivity();
        return false;
    });
    $('#close_over').click(function () {
        var gallery = Galleria.get(0);
        $('#floorplans').fadeOut();
        $('#popup').fadeOut();
        $('#loadoverslide').fadeOut();
        $('#close_over').fadeOut();
        $('#slideshow').fadeTo('slow', 1);
        $('.btnSpecial').fadeOut();
        gallery.play();
        return false;
    });
    $('.view_photos').click(function () {
        var gallery = Galleria.get(0);
        $('#floorplans').fadeOut();
        $('#popup').fadeOut();
        $('#loadoverslide').fadeOut();
        $('.btnSpecial').fadeOut();
        $('#close_over').fadeOut();
        $('#slideshow').fadeTo('slow', 1);
        gallery.play();
        return false;
    });
    $(".propertyinfo").click(function (e) {
        e.preventDefault();
		$.fancybox.showActivity();
        var lotInfo = $(this).attr('href') + ' ' + $(this).attr('rel');
        if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        }
	    $("#popup").fadeOut("fast", function () {
            $("#popupContent").load(lotInfo, false, function () {
                $("#popupContent a.fancybox").fancybox({
                    'showPrintButton' : true,
					'titleShow': false,
                    'overlayColor': '#29231b'
                });
				//$("#popupContent a.requestinfo").click();
                $("#popup").fadeIn("fast");
            });
     		$.fancybox.hideActivity();
        })
        if ($('#popup').is(':hidden')) {
            $('#popup').fadeIn();
        } else {
            return false;
        }

    });
	
	$(".moreinfo").click(function (e) {
        e.preventDefault();
		$.fancybox.showActivity();
        var info = $(this).attr('href') + ' ' + $(this).attr('rel');
        if ($('#infoWindow').is(':visible')) {
            $('#infoWindow').fadeOut();
        }
	    $("#popup").fadeOut("fast", function () {
            $("#popupContent").load(info, false, function () {
                $("#popupContent a.fancybox").fancybox({
                    'showPrintButton' : false,
					'titleShow': false,
                    'overlayColor': '#29231b'
                });
				//$("#popupContent a.requestinfo").click();
                $("#popup").fadeIn("fast");
            });
     		$.fancybox.hideActivity();
        })
        if ($('#popup').is(':hidden')) {
            $('#popup').fadeIn();
        } else {
            return false;
        }

    });
    ///////////////// Load Specific Floorplan's PDF Download Button
	// TODO: is this a dup from line 271
    $('.floorplan').click(function () {
        if ($(this).attr('rel') == "floorplan_lascanadas_a") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/lascanadas/Canada_A_4bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_lascanadas_b") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/lascanadas/Canada_B_3bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_lascanadas_c") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/lascanadas/Canada_C_3bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_lascanadas_d") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/lascanadas/Canada_D_2bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_clubvillas_a") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/clubvillas/ClubVilla_A_4bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_clubvillas_b") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/clubvillas/ClubVilla_B_3bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_clubvillas_c") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/clubvillas/ClubVilla_C_3bedrooms.pdf");
        } else if ($(this).attr('rel') == "floorplan_clubvillas_d") {
            $(".btnSpecial.downloadPDF a").attr("href", "/assets/pdf/floorplans/clubvillas/ClubVilla_D_2bedrooms.pdf");
        }
    });
/*
		function toggleInfoWindow() {
		  //determine top position. 728 open or 918 closed
		  var position = $('#infoWindow').position();
		  //alert(position.top);
		  if(position.top == 356){
			  $('#infoWindow').animate( {top : 530});
			  //$('#info_close').toggleClass('hide');
		  }
		  if(position.top == 530){
			  $('#infoWindow').animate( {top : 356});
			  //$('#info_close').toggleClass('hide');
		  }
		  return false;
		}
		//info_toggle	
*/

 
    // Mexico Flag Alert
    $('.flag_mexico').click(function () {
        //alert("Spanish Version Coming Soon");
		$.fancybox({
						'showPrintButton' : false,
						'content': "<p>Próximamente Versión en Español</p>",
						'titleShow': false,
						'overlayColor': '#29231b'
					});
    });

    function inputClass() {
        $(':input').addClass("idleField");
        $('input[type="text"]').focus(function () {
            $(this).removeClass("idleField").addClass("focusField");
        });
        $(':input').blur(function () {
            $(this).removeClass("focusField").addClass("idleField");
        });
    }
	
	
//ajax form plugin	
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
 $.fancybox.showActivity();
   //alert('About to submit: \n\n' + queryString); 
 
    
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.'); 
} 
// json callback
function processJson(data) { 
    // 'data' is the json object returned from the server 
    //alert(data.message); 
	//alert(data.processed);
	
	var processed = data.processed;
	var message = data.message;
	
	if( $("ul#messages").length > 0 ){
	  // remove existing messages
	  $("ul#messages").remove();	
	}
	
	$( "#loadoverslide" ).append ( message );
	if ($('a#show_messages').length > 0) {
		
	} else {
		$("<a href='#messages' class='errors' id='show_messages'>View Errors</a>").insertBefore('#form_messages');
	}
	$( "a#show_messages" ).fancybox({
						'titleShow': false,
						'showPrintButton' : false,
						'overlayColor': '#29231b',
						'type':'inline'
					});
	
	/*$.fancybox({
                'titleShow': false,
				'overlayColor': '#29231b',
				'content' : message
            });*/
	$.fancybox.hideActivity();
	if(processed == 1){
		//success. hide form div and load fancybox success notice
		$("#popup").fadeOut();
		$( "a#show_messages").trigger('click');
		if(data.page_url){
		 window.location.replace(data.page_url);
		}
		
	} else {
		//fail.
		// default fail is 0 
		// user login fail is 2 for bad password or 3 for bad username
		$( "a#show_messages").trigger('click');
		if(processed == 2){
			//console.log(data.bad_password);
			//console.log(data.email_address);
			//valid user bad pass append email to forgot password link
			var forgotpassurl = $('a.forgotpass').attr('href');
			forgotpassurl = forgotpassurl + '?email=' + data.email_address;
			$('a.forgotpass').attr('href', forgotpassurl);
		}
		if(processed == 4){
			// forgot password email success
		}
	}
	
	
}

$("#icons img").fadeTo(0, 0.6);

$("#icons img").stop().hover(function(){  
	$(this).stop().fadeTo("fast", 1.0)},function(){  
	$(this).stop().fadeTo("fast", 0.6); 
	}); 
	
	
}); /* document(ready) */


///////// ThickBox Print Button Function
	
   var win = null;

    function printIt(printThis) {
        win = window.open();
        self.focus();
        win.document.open();
        win.document.write('<' + 'html' + '><' + 'head' + '><' + 'style' + '>');
        win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
        win.document.write('<' + '/' + 'style' + '><' + '/' + 'head' + '><' + 'body' + '>');
        win.document.write(printThis);
        win.document.write('<' + '/' + 'body' + '><' + '/' + 'html' + '>');
        win.document.close();
        win.print();
        win.close();
    }
