$(document).ready(function(){
    $("#city").autocomplete("/cgi-bin/autoCities.cgi");
    $("#agentname").autocomplete("/cgi-bin/autoAgents.cgi",
				 {
				     delay: 250,
				     cacheLength: 0,
				     max: 20
				 });
    $("#agentname2").autocomplete("/cgi-bin/autoAgents.cgi",
				 {
				     delay: 250,
				     cacheLength: 0,
				     max: 20
				 });
    
    $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav

    $("#services").hover(function() { //When trigger is clicked...
	$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 	$(this).parent().hover(function() {
	}, function(){	
	    $(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
	});
    }).hover(function() { 
	$(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });


	$("#buying").hover(function() { //When trigger is clicked...
		$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});
 		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	$("#selling").hover(function() { //When trigger is clicked...
		$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});
 		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	$("#renting").hover(function() { //When trigger is clicked...
		$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});
 		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	$("#aboutus").hover(function() { //When trigger is clicked...
		$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});
 		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	$("ul.topnav li span").hover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});
 
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

    // Set thickbox loading image
    //tb_pathToImage = "/library/jquery/plugins/jcarousel/images/images/loading-thickbox.gif";

    $(".youtubeit").colorbox({iframe:true, innerWidth:425, innerHeight:344});



    url = "/library/cgi-bin/showluxurylist.cgi";
    
    $.ajax({
	url: url,
	dataType: 'xml',
	method: "get",
	success: function(xml) { 
           
	    
	    var photosArray = xml.documentElement.getElementsByTagName("GalleryList");
	  
	    itemList = [];
	    for (var i=0; i < photosArray.length; i++)
        		{
			    var photos2 = xml.documentElement.getElementsByTagName("GalleryList")[i].attributes;
			    $imagesrc = photos2.getNamedItem("src").nodeValue;
			    $imagethumb = photos2.getNamedItem("thumbnail").nodeValue;
			    $price = photos2.getNamedItem("price").nodeValue;
			    $mlsnumber = photos2.getNamedItem("mls").nodeValue;
	                    $baths = photos2.getNamedItem("baths").nodeValue;
			    $beds = photos2.getNamedItem("beds").nodeValue;
			    $city = photos2.getNamedItem("city").nodeValue;
			    $mls = photos2.getNamedItem("mls").nodeValue;
			    $ls = photos2.getNamedItem("ls").nodeValue;
			    //$titledisplay = $price + " | " + $city + " | " + $beds + " Beds | " + $baths + " Baths"+ "<br /> <a href=/" + $mlsnumber + " target=_blank>Learn More...</a>";
			    $titledisplay = "<a href=/unified/standalone/index.php?mls=" + $mlsnumber + "&ls=" + $ls +" target=_blank>Learn More...</a>";
		            $titleplain = $city + " : " + $price + " \n(" + $beds + " Beds : " + $baths + " Baths)";

			    itemList.push({url: $imagethumb, title: $titleplain, display: $titledisplay, full: $imagesrc, mls: $mls, ls: $ls});
				
     			}
		
		itemList;
   	



function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
      
        // Create an object from HTML
        var item = jQuery(mycarousel_getItemHTML(itemList[i-1])).get(0);

        // Apply colorbox

	$(item).colorbox({href: itemList[i-1].full, title:  itemList[i-1].display, height: "345px", width: "720px"  });
	
	        carousel.add(i, item);

    }
};


/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    var url_m = item.url.replace(/_s.jpg/g, '_m.jpg');
    return '<a href="' + url_m + '" title="' + item.title + '"><img src="' + item.url + '" width="100" height="75" border="0"  /></a>';
};

jQuery('#mycarousel').jcarousel({
        size: itemList.length,
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });	
            
        }
	});



	
 
});




