$(document).ready(function() {


	/* IE6 PNG SUPPORT
	---------------------------------------------------------- */
	// Fix background image caching problem
	if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch (err) {
			console.log(err.toString());
		}

		DD_belatedPNG.fix('#logo.branding');
		DD_belatedPNG.fix('.action-items.listings');
		DD_belatedPNG.fix('.action-items.obx');
		DD_belatedPNG.fix('.action-items.agents');
		DD_belatedPNG.fix('#main-nav');
		DD_belatedPNG.fix('img.label.new');
		DD_belatedPNG.fix('img.label.sold');
	}


	/* MODAL WINDOW FOR IMAGES
	---------------------------------------------------------- */
	//$("a[rel^='prettyPhoto']").prettyPhoto();
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', 		/* fast/slow/normal */
		padding: 40, 					/* padding for each side of the picture */
		opacity: 0.5, 					/* Value betwee 0 and 1 */
		showTitle: false, 				/* true/false */
		allowresize: true, 				/* true/false */
		counter_separator_label: '/', 	/* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_square', 			/* light_rounded / dark_rounded / light_square / dark_square */
		callback: function() { } 		/* A user defined call back function */
	});

	/* MODAL WINDOW FOR IMAGES
	---------------------------------------------------------- */
	// disable this effect for IE6 (performance gain)
	if ($('a.photo').length > 0) {
		if (!$.browser.msie && !$.browser.version.substr(0, 1) < 7) {
			$('a.photo').imgPreview({ /* Reference 'a.photo' to target ALL photos, not just the small ones */
				containerID: 'thumbnail-preview',
				imgCSS: {
					// Limit preview size:
					height: 150
				},
				// When container is shown:
				onShow: function(link) {
					// Animate link:
					$(link).stop().animate({ opacity: 0.4 });
					// Reset image:
					$('img', this).stop().css({ opacity: 0 });
				},
				// When image has loaded:
				onLoad: function() {
					// Animate image
					$(this).animate({ opacity: 1 }, 250);
				},
				// When container hides: 
				onHide: function(link) {
					// Animate link:
					$(link).stop().animate({ opacity: 1 }, 150);
				}
			});
		}
		else {
			$('a.photo').imgPreview({
				containerID: 'thumbnail-preview',
				imgCSS: {
					// Limit preview size:
					height: 150
				}
			});
		}
	}

	/* SEARCH
	---------------------------------------------------------- */
	var strDefault = 'Enter search terms (MLS code, property name, location, etc.)';

	$("#search #search-terms").blur(function() {
		var val = $(this).attr('value');
		if (val.length == 0 || val == strDefault) {
			$(this).attr('value', strDefault);
			$(this).addClass('blank');
		}
	});

	$("#search #search-terms").focus(function() {

		var val = $(this).attr('value');
		if (val == strDefault) {
			$(this).attr('value', '');
			$(this).removeClass('blank');
		} else if (val.length != 0) {
			$(this).removeClass('blank');
		}
	});

	$("#search #search-terms").attr("value", strDefault);
	$("#search #search-terms").addClass("blank");

	/* SEARCH (CONTAINER ADJUSTMENTS)
	---------------------------------------------------------- */
	$('#advanced-search-fields').hide();
	$('a#search-advanced').click(function() {
		$('#advanced-search-fields').slideToggle('fast', showFields());
	});

	function showFields() {
		// disable this effect for IE6 (performance gain)
		if (!$.browser.msie && !$.browser.version.substr(0, 1) < 7) {

			var objTarget = $('#advanced-search-fields .field-wrapper');
			if ($('#advanced-search-fields:visible').is(':visible')) {
				objTarget.fadeOut('slow');
			}
			else {
				objTarget.fadeIn('slow');
			}
		}
		else {
			return false;
		}
	}


	/* SEARCH (ADVANCED SEARCH FIELDS)
	---------------------------------------------------------- */
	/*#search advanced-search-fields*/
	var objTarget = $('#search #advanced-search-fields');
	//objTarget.addClass( 'hidden' );


	// TODO: Advanced Search functionality

	/* DETAILS: CONTROL BAR
	---------------------------------------------------------- */
	var objCtrlBar = $('#page-content #flash-content .control-bar');
	var objCtrlBarBg = $('#page-content #flash-content .control-bar .transparent-bg');
	objCtrlBarBg.css('opacity', 0.8);
	objCtrlBar.hide();
	objCtrlBar.slideDown('slow');

	/* DETAILS: YES/NO ICONS
	---------------------------------------------------------- */
	// replace list items with class: yes | no
	$("dd.yes").html('<img src="/images/icon_yes.png" width="14" height="14" alt="Yes" />').removeClass('yes');
	$("dd.no").html('<img src="/images/icon_no.png" width="14" height="14" alt="No" />').removeClass('no');

	/* VFX: HOVER STATE FOR ITEMS
	---------------------------------------------------------- */
	/*
	$('.agent-brief').hover( function() {
	$(this).addClass('item-hover');
	} );
	*/

});