﻿$(document).ready(function() {
    
    $('#search-box').focus(function() {

      //$(this).val('');

    });
    
    $('.directionbox').focus(function() {

      $(this).val('');

    });
    
    $('.textbox').focus(function() {

      $(this).val('');

    });
    

    
    //nav functions
    $(".right-nav").hover(function () {
        $(this).children("a").addClass("selected");
        $(this).children("ul").show();
      }, 
      function () {
        $(this).children("a").removeClass("selected");
        $(this).children("ul").hide();
      }
    );
     
     if($("#location-list").length > 0)
     {
         //scroll bar styles
         $("#location-list").jScrollPane({
            showArrows:true,
            scrollbarWidth:18, 
            scrollbarMargin:10
         
         });
     }
   
//     if($("#location-list-details").length > 0)
//     {
//         $("#location-list-details").jScrollPane({
//            showArrows:true,
//            scrollbarWidth:18, 
//            scrollbarMargin:10
//         
//         });
//     }

     $(".gallery-thumbs").jScrollPane({
        showArrows:true,
        scrollbarWidth:18, 
        scrollbarMargin:10
        
     });
          
     //carousel functions
     $('.carousel-image-container').each(function() {
        var self = this;
        $(this).find('.left').click(function(e) {
            e.preventDefault();
            var img = $(self).find('img:visible');
            if (img.prev('img').length > 0) 
            { 
                img.hide(); 
                img.prev('img').show(); 
                if (!img.hasClass('prop'))
                {
                    var caption = img.prev('img').attr('alt');
                    $(self).parent().find(".carousel-legend .small").first().html(caption);
                }
            } 
            else 
            {
                img.hide(); 
                $(self).find('img:last').show();
                if (!img.hasClass('prop'))
                {
                    var caption = $(self).find('img:last').attr('alt');
                    $(self).parent().find(".carousel-legend .small").first().html(caption);
                }
            }
        });
        $(this).find('.right').click(function(e) {
            e.preventDefault();
            var img = $(self).find('img:visible');
            if (img.next('img').length > 0) 
            { 
                img.hide(); 
                img.next('img').show(); 
                if (!img.hasClass('prop'))
                {
                    var caption = img.next('img').attr('alt');
                    $(self).parent().find(".carousel-legend .small").first().html(caption);
                }
            } 
            else 
            {
                img.hide(); 
                $(self).find('img:first').show();
                if (!img.hasClass('prop'))
                {
                    var caption = $(self).find('img:first').attr('alt');
                    $(self).parent().find(".carousel-legend .small").first().html(caption);
                }
            }
        });
    });
    
    //floorplan modal
    $(".modal").click(function (e) {
		e.preventDefault();
		$('#floorplan-modal-container').modal();
		    $("#simplemodal-container").css("margin-top", "-100px");
		    var beds = $(this).parent().siblings(".name").children("p").children("strong").html();
		    var baths = $(this).parent().siblings(".bathroom").html();
		    var size = $(this).parent().siblings(".size").html();
		    var price = $(this).parent().siblings(".price").html();
		    var src = $(this).parent().siblings(".plan").find("img").attr("longdesc");
		    
		    $("#modal-bedroom").html(beds);
		    $("#modal-bathroom").html(baths);
		    $("#modal-size").html(size);
		    $("#modal-price").html(price);
		    $("#modal-image").attr("src", src);
		    
		    //need to pas these values to the modal
		    
		  return false;
	});
	
	//Send to a friend
	$(".send-to-a-friend").click(function (e) {
		e.preventDefault();
		$('.send-to-a-friend-modal-container').modal();
		$("#simplemodal-container").css("margin-top", "0px");
		return false;
    });
	
	
	//gallery
	var galleryIndex = 0;
	function getImage()
	{
	    var counter = 0;
	    $(".gallery-thumbs div").each(function() {
	        if(counter == galleryIndex)
	        {
	            galleryImageClick($(this).children("a"));
	        }
	        counter++;
	    });
	}
	
	function setGalleryIndex()
	{
	    var counter = 0;
	    $(".gallery-thumbs div").each(function() {
	        if($(this).attr("class") == 'close-by-item-container-selected')
	        {
	            galleryIndex = counter;
	        }
	        counter++;
	    });
	}
	
	function galleryImageClick(img)
	{
	    $(".close-by-item-container-selected").attr("class", "close-by-item-container");
	    var heroImg = img.children($("img")).attr("longdesc");
	    $("#gallery-hero-container").css("background","url(" + heroImg + ") no-repeat 0 0");
	    $(".hero-caption span").html(img.children($("img")).attr("alt"));
	    img.parent().attr("class", "close-by-item-container-selected");
	    setGalleryIndex();
	}
	
	$(".gallery-thumbs .close-by-item-container a").click(function (e) {
	    e.preventDefault();
	    galleryImageClick($(this))
	});

    $("#gallery-hero-container a.scroll-left").click(function (e) {
        e.preventDefault();
        galleryIndex--;
        if(galleryIndex < 0)
            galleryIndex = 7;
        getImage();
    });
    
    $("#gallery-hero-container a.scroll-right").click(function (e) {
        e.preventDefault();
        galleryIndex++;
        if(galleryIndex > 7)
            galleryIndex = 0;
        getImage();
    });
    
    //video
    $(".video-item-container a").click(function (e) {
        e.preventDefault();
        var embedCode = $(this).parent().find("div").html();
        $("#video-hero-container").html(embedCode);
    });

});


