var infowindow;
$(document).ready(function() {
	
	if($('#relaxBtn').length > 0) {
		$('#relaxBtn').mouseenter(function() {
			$('#relaxHover').fadeIn();
		}).mouseout(function() {
			$('#relaxHover').fadeOut();
		});
	}
	
	// Prefill contact form if requesting a showing.
	if(GET('mlsid') != null) {
		var t = GET('mlsid');
		$('#wpcf7-f1-p25-o1 textarea').text("I would like to request a showing of the property with MLSID: "+t);
	}
	
	if($('#map_of_region').length > 0) {
		map_of_region();
	}
	
	var transition = (window.location.hash) ? window.location.hash.substr(1) : "fade";
	
	if($('#heroShots').length > 0) {
		$('#heroShots').cycle({
			fx: transition // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
	}
	
	
	if($('#featuredProperties').length > 0) {
		
		jQuery(document).ready(function() {
			jQuery('#featuredProperties').carousel({
			    loop: true,
			    autoSlide: true,
			    autoSlideInterval: 8000
			});
			
			$('.carousel-wrap').css('height', 800);
		});
		
		/*
$('#featuredProperties > ul').each(function(i, val) {
			$(this).hover(
				function() {
					$('#featuredProperties > ul').children().effect("drop");
					$('#featuredProperties > ul').tabs("rotate", 0);
				},
				function() {
					$('#featuredProperties > ul').children().effect("slide");
					$('#featuredProperties > ul').tabs("rotate", 3000);
				}
			);
		});
	
	    $('#featuredProperties').tabs({ 
	    	event: 'mouseover',
	    	fx: { 
	    		opacity: 'toggle', duration:'fast'
	    	 }
	    }).tabs('rotate', 3500);
	
		$('.ui-tabs-nav li').mouseout(function() {
			$(this).children().effect("slide");
			//$('.ui-tabs-hide').css('opacity','1');
		});
*/
	
	}
	
	
	initMenu();
	
	$(document).pngFix();
	
	/*
	$('div.page img').lazyload({
		placeholder: "/wp-content/themes/sullivan/library/images/grey.gif",
		effect: "fadeIn"
	});
	
	
	$('#pagecontent div.center img').lazyload({
		placeholder: "/wp-content/themes/sullivan/library/images/grey.gif",
		effect: "fadeIn"
	});
	*/
	
	// Init photo slideshows
	if($('div.listingPhotos').length > 0) {
		$('div.listingPhotos a.prev').each(function() {
			$(this).click(function(event) {
				event.preventDefault();
				var i = parseInt($(this).siblings('.current').text());
				var max = parseInt($(this).siblings('.max').text());
				i = (i > 1) ? i-1 : max;
				var image = $(this).parent().siblings('img');
				var img = ($(this).parent().siblings('img').attr('src')).split('/');
				var imgSrc = img[2].split('-');
				imgSrc = imgSrc[0];
				var src = imgSrc + "-" + i + ".jpg";
				$(this).parent().siblings('img').attr('src', '/ctmls_photos/'+src);
				$(this).siblings('.current').text(i);
			});
		});
		
		$('div.listingPhotos a.next').each(function() {
			$(this).click(function(event) {
				event.preventDefault();
				var i = parseInt($(this).siblings('.current').text());
				var max = parseInt($(this).siblings('.max').text());
				i = (i < max) ? i+1 : 1;
				var image = $(this).parent().siblings('img');
				var img = ($(this).parent().siblings('img').attr('src')).split('/');
				var imgSrc = img[2].split('-');
				imgSrc = imgSrc[0];
				var src = imgSrc + "-" + i + ".jpg";
				$(this).parent().siblings('img').attr('src', '/ctmls_photos/'+src);
				$(this).siblings('.current').text(i);
			});
		});
	}
});

function initMenu() {
	//Only shows drop down trigger when js is enabled â€“ Adds empty span tag after ul.subnav
	$('ul.sub-menu').parent().append('');

	$('ul.topnav li a').hover(function() {
	
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find('ul.sub-menu').slideDown(300).show(); //Drop down the subnav on hover
		
		$(this).parent().hover(function() {
			// do nothing
		}, function(){
			$(this).parent().find('ul.sub-menu').slideUp(200); //When the mouse hovers out of the subnav, move it back up
		});
	}, function () {
		// do nothing
	});
}

function map_of_region() {
	var latlng = new google.maps.LatLng(41.624682,-72.128677);
    var myOptions = {
      zoom: 9,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID,
      panControl: true,
      zoomControl: true,
      scaleControl: false,
      streetViewControl: false
	};
	
	var locations = [
		['Old Saybrook', 41.301024,-72.378273],
		['Black Point', 41.298186,-72.208157],
		['Giant’s Neck', 41.300475,-72.230151],
		['Essex', 41.358644,-72.389431],
		['Centerbrook', 41.348206,-72.417154],
		['Ivoryton', 41.348206,-72.443333],
		['Lyme', 41.400634,-72.346716],
		['Old Lyme', 41.362895,-72.305145],
		['East Lyme', 41.3969,-72.245407],
		['South Lyme', 41.306182,-72.25914],
		['Niantic', 41.333514,-72.218971],
		['New London', 41.356711,-72.095718],
		['Waterford', 41.352588,-72.130737],
		['Quaker Hill', 41.404046,-72.105811],
		['Gales Ferry', 41.433718,-72.080269],
		['Groton', 41.358257,-72.033577],
		['Mystic', 41.354649,-71.966629],
		['West Mystic', 41.344276,-71.976328],
		['Ledyard Center', 41.325135,-71.98843],
		['Noank', 41.325135,-71.98843],
		['Groton Long Point', 41.315917,-72.0086],
		["Mumford's Cove", 41.323975,-72.012978],
		['Stonington', 41.36573,-71.917191],
		['Stonington Borough', 41.340216,-71.903458],
		['Mystic', 41.355165,-71.949463],
		['North Stonington', 41.446587,-71.881142],
		["Clark's Falls", 41.477204,-71.880455],
		["Mason's Island", 41.333578,-71.967916],
		["Lord's Point", 41.338669,-71.930544],
		['Pawcatuck', 41.378097,-71.84063],
		['Deep River', 41.371656,-72.48436],
		['East Hampton', 41.58412,-72.493286],
		['Canterbury', 41.694962,-72.023277],
		['Haddam', 41.451476,-72.508736],
		['Amston', 41.623142,-72.365913],
		['Lebanon', 41.652906,-72.210388],
		['Hadlyme', 41.413027,-72.440157],
		['Montville', 41.463505,-72.157431],
		['Uncasville', 41.432431,-72.110138],
		['Oakdale', 41.465885,-72.156487],
		['Norwich', 41.525287,-72.076149],
		['Norwichtown', 41.553875,-72.105381],
		['Taftville', 41.570444,-72.048004],
		['Pomfret Center', 41.890522,-71.954269],
		['Pomfret', 41.898763,-71.962337],
		['Pomfret', 41.885921,-71.964058],
		['Preston', 41.530427,-71.974182],
		['Putnam', 41.91633,-71.900024],
		['Salem', 41.490321,-72.276993],
		['Colchester', 41.580011,-72.312698],
		['Scotland', 41.698936,-72.083359],
		['Woodstock', 41.958469,-71.985855],
		['Woodstock Valley', 41.944426,-72.074089],
		['South Woodstock', 41.94417,-71.959076],
		['East Woodstock', 41.984026,-71.978559],
		['Yantic', 41.560491,-72.123663],
		['Thamesville', 41.510859,-72.088172],
		['Occum', 41.595707,-72.05075],
		['Greenville', 41.534411,-72.057316],
		['East Great Plains', 41.536338,-72.100754],
		['Chesterfield', 41.428795,-72.217433],
		['Lisbon', 41.60633,-72.011461],
		['Gilead', 41.677784,-72.409716],
		['Amston', 41.626382,-72.341287],
		['Hebron', 41.690348,-72.413006],
		['Groton City', 41.342794,-72.067595],
		['Center Groton', 41.360834,-72.051802],
		['Glastonbury', 41.70701,-72.533512],
		['North Franklin', 41.634145,-72.163403],
		['Centerbrook', 41.352298,-72.416003],
		['East Haddam', 41.472573,-72.391377],
		['Cresent Beach', 41.305666,-72.232032],
		['North Westchester', 41.580349,-72.400056]
	];
	
    var map = new google.maps.Map(document.getElementById("map_of_region"), myOptions);
	
	for (var i = 0; i < locations.length; i++) {
	    var location = locations[i];
	    var myLatLng = new google.maps.LatLng(location[1], location[2]);
	    var marker = createMarker(map,myLatLng,location[0], i);
	}
}

function createMarker(map,latlng,msg,i) {
	var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        zIndex: i,
        html: msg
    });  
    infowindow = new google.maps.InfoWindow();
    google.maps.event.addListener(marker, 'click', function() {
    	if(infowindow) infowindow.close();
    	infowindow.setContent(this.html);
    	infowindow.open(map,this);
    });
    return marker;
}

function GET(v) {
	var $_GET = {};

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
	    function decode(s) {
	        return decodeURIComponent(s.split("+").join(" "));
	    }
	
	    $_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	return ($_GET[v] != undefined) ? $_GET[v] : null;
}

