// customized slideshow
jQuery.fn.slideshow = function(options) {
	var settings = {
		timeout: '3000',
		type: 'sequence'
	}
	if(options)
		jQuery.extend(settings, options);
	
	var pauseState = 0;
	var current = 1;
	var last = 0;
	var timer = '';
	
	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0');
			jQuery(slides[current]).css('zIndex', '1').fadeIn(1500);
			
			if ( ( current + 1 ) < slides.length ) {
				current = current + 1;
				last = current - 1;
			}
			else {
				current = 0;
				last = slides.length - 1;
			}
			timer = setTimeout(change, settings.timeout);
		}
	}
	
	this.css('position', 'relative').css('height','600px');
	var slides = this.find('.photowrap').get();
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
	timer = setTimeout(change, settings.timeout);

	return this;
};

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


// mailform submittal
function sendtofriend(form) {
	if(form.yourname.value.length == 0) 
	{
		alert('You need to enter your name');
		return false;
	}
	
	if(!form.youremail.value.match(/^[A-Za-z0-9._%\+-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,8}$/)) 
	{
		alert('You need to enter your valid email address');
		return false;
	}
	
	if(form.friendname.value.length == 0) 
	{
		alert('You need to enter your friend\'s name');
		return false;
	}
	
	if(!form.youremail.value.match(/^[A-Za-z0-9._%\+-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,8}$/)) 
	{
		alert('You need to enter your friend\'s valid email address');
		return false;
	}
	
	tb_remove();	
	return true;
}

$(function(){
	
	// Nav
	if($('#nav').length>0) {
		jQuery('#nav').Accordion({
			active: 'a.selected',
			alwaysOpen: false,
			header: 'a.head',
			hideSpeed: 'slow'
		});
		jQuery('#nav').Accordion({
			active: 'a.propon',
			alwaysOpen: false,
			header: 'a.prop',
			hideSpeed: 'slow'
		});
	
		// Hide subnavs in Safari
		$('#nav ul ul ul:not(.current)').css('display','none');

		// Close onclick
		$('#nav a').click(function(){
			if(this.parentNode.parentNode.id == "menu" && this.id != "aboutnav" && this.id != "properties") {
				if(document.getElementById("aboutnav").className.indexOf("selected") != -1 ) {
					jQuery('#nav').activate(0);
					gotohref = $(this).attr('href');
					var t = setTimeout("window.location = gotohref;",900);
					return false;
				}
				if(document.getElementById("properties").className.indexOf("selected") != -1 ) {
					jQuery('#nav').activate(1);
					gotohref = $(this).attr('href');
					var t = setTimeout("window.location = gotohref;",900);
					return false;					
				}
			}
		});		
	}	

	// Flash replacement
	$('a.flash').flash({
		version:	5,	 
		caption:	false
	});
	
	// Fading photos	
	jQuery(".photowrap").css('zIndex', '1').fadeIn('slow');
	if ($('.photowrap').length>1) {
		$('#secondaryContent').slideshow({ });
	}
	
	// Custom selects - bring back if form changes. currently replaced with check boxes
	// if($('form').length>0 && $('.property').length==0) { formInit(); }
	
	// hide mailto for thickbox
	$('#formwrap').hide();
	
	// Google Map
	if(document.getElementById("mapwrap")) {
		if (GBrowserIsCompatible()) {
			var mapwrap = document.getElementById("mapwrap");
			mapwrap.className = "mapped";			
			var property = document.getElementsByTagName("h2")[0].innerHTML;

			var address = $('#mapwrap .address').html();
			
			var map = new GMap2(mapwrap);
			map.addControl(new GSmallMapControl());
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(address.replace(/<br>/i," "), function(point) {
				if (!point) { return } else {
					map.setCenter(point, 13);
					
					var marker = new GMarker(point);
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml("<p><strong>"+property+"</strong><br />"+address+"</p>");
					});	
					map.addOverlay(marker);
				}
				});		
		}
		window.onunload=GUnload;
	}
	
	if(document.getElementById("searchtips")) {
		$('#searchtips').hide();
		$('#searchshow').click(function(){ $('#searchtips').slideToggle("slow"); return false; })
	}
	
	// add spans in print template li's
	//$("table#property td.advantages li *").wrap("<span></span>");
	$("table#property td.advantages li").each(function(){ var str = $(this).html(); $(this).html("<span>"+str+"</span>"); });
	/*if(document.all){
		if($("#availability").length>0 && $("#availability").height()>130) {
			$("#availability").css({ height:"1.35in" });
		}
	}*/
	
	//PDF launcher
	$(".property a.pdf").click(function(e){
		
		if($(this).attr("href").indexOf("/print/")==-1 || $(this).attr("href").indexOf("PDF.cfm?id")==-1) { return true; }
		
		var winW = screen.availWidth - 40;
		var winH = screen.availHeight - 60;
		var winX = 20;
		var winY = 20;
		var features = 'left='+winX+',top='+winY+',width='+winW+',height='+winH+',toolbar=0,location=0,status=0,scrollbars= 1,resizable=0';
		
		var pdf = escape($(this)[0].href);
		
		window.open("/print/pdfviewer.cfm#"+pdf,"pdfviewer",features);
	
		e.preventDefault();
	
	});	
	
});