var ticker = 0;
var run_cycle = 'true';

$(document).ready(function() {

	$('#refine-form dl.panel').each(function(i){
		$('dt', this).click(function(){
			$(this).next('dd').slideToggle('slow',function(){
				$(this).parent('dl').toggleClass('open');
			});
		});
	});

	$("div.item").wrap('<div class="dialog">'+'<div class="bd">'+'<div class="c">'+'<div class="s">'+'</div>'+'</div>'+'</div>'+'</div>');

	$('div.dialog').prepend('<div class="hd">'+'<div class="c"></div>'+'</div>').append('<div class="ft">'+'<div class="c"></div>'+'</div>');

	$('#nav-primary li').hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});

	$('hr').wrap('<div class="hr"></div>');

	$('legend').each( function() {
		val = $(this).html();
		$(this).after('<h5 class="legend"><span>' + val + '</span></h5>');
		$(this).html('');
	});

	$('div.cta').each(function() {
		$cta = $(this);
		$href = $('a:first', $cta).attr('href');
		$cta.hover(
			function(){ $(this).addClass('hover'); },
			function(){ $(this).removeClass('hover');}
		).click(function(){
			document.location = $href;
		});
	});

	$('#idx-detail .description p').truncate({'max_length': 525});

	/* IDX Details Images */
	$('#idx-detail .photos').each(function() {
        $(this).wrap('<div class="gallery"></div>');
        $(this).parent('.gallery').prepend('<div id="image_wrap"></div>');
        $(this).wrapInner('<div class="slides"></div>');
        $('img', this).wrap('<div class="slide"></div>');
        var $num = $('.slide', this).length;
        var photosurl = window.location + 'photos.html';
        $(this).after('<div class="controls"><a href="javascript:void(0);" class="prev">&laquo; Previous</a><a href="javascript:void(0);" class="next">Next &raquo;</a></div>');
    });

    $('#idx-detail .gallery img').rewImgSizer({method:'scale', align: 'center', scale: 1.6});
     
	$('.cat-idx .set.listings.grid .listing .photos img.photo').rewImgSizer({method:'scale', align: 'center', scale: 1.6});
	$('.cat-cms .set.listings.grid .listing .photos img.photo').rewImgSizer({method:'scale', align: 'center', scale: 1.6});

	$('#content-primary .set.listings .listing .photos img.photo').rewImgSizer({method:'scale', align: 'center', scale: 1.0});
	
	$('.pg-homepage .set.listings .listing.featured .photos img.photo').rewImgSizer({method:'crop', align: 'center', scale: 1});





    var hoverConfig = {
        'over' : function() {
            // mouseover
            $(this).trigger('hoverIntent');
        },
        'out' : function () {
            // mouseout
        }
    };

    /* Ok, let's prep colorbox stuff */
    $('#idx-detail .gallery').each(function(){
        $gallery = $(this);
        $gallery.prepend('<div id="colorbox_gallery"></div>');
    });

    var $wrap = $("#image_wrap");


    $("#content .slides .slide").each(function(i){

        $slide = $(this);
        var $slideImg = $('img', $slide);
        var $si_src = $slideImg.attr('src');
        var $si_rel = 'imgnum_' + i;
        $slideImg.attr('rel', $si_rel);

        $nImg = $('<img src="' + $si_src  + '" id="'+ $si_rel +'" />').appendTo($wrap).hide();

        /* colorbox */
        $imgLink = $('<a href="' + $si_src + '" rel="colorbox" class="' + $slideImg.attr('rel') + '">Click Image to Enlarge</a>');
        $imgLink.appendTo('#colorbox_gallery');

        $slide.bind('mouseover',function(){
            if ($(this).is(":not(.active)")) {
                $rel = $('img',this).attr('rel');
                $showImg = $('img#'+ $rel);
                $showImg.siblings('img').fadeOut();
                $showImg.rewImgSizer({method:'scale', align: 'center', scale: 1.25});
                $(this).addClass('active').siblings().removeClass('active');
                $("a[rel='colorbox']").hide().filter('.' + $rel).show();
            }
        });

        $("#content .slides .slide").filter(":first").trigger('hover').addClass('active');

        // Make Large Image Clickable
        $nImg.bind('click', function () {
            $('a.' + $slideImg.attr('rel')).trigger('click');
        });

    });


    $('#imgnum_0').rewImgSizer({method:'scale', align: 'center', scale: 1.25});

    $("a[rel='colorbox']").each(function(i) {
        $(this).colorbox({
            photo: true,
            slideshow: false,
            opacity: 0.8,
            initialWidth: 60,
            initialHeight: 60
        });
        if (i > 0) {
            $(this).hide();
        }
    });

    $('#idx-detail .photos.disabled').hide();

    $('#idx-detail .photos').scrollable({
        size: 5,
        next: '',
        prev: '',
        nextPage: '.next',
        prevPage: '.prev'
    });

});

/* REWFW: FORMS */

$(document).ready(function(){

	$('#content form.rewfw').each(function(){
		 var dform = $(this);
		 $(dform).wrapInner('<div class="form-wrap"></div>');
		 $('.field input:text,.field input:password', dform).wrap('<span class="input-wrap"></span>');
		 $('.field select', dform).wrap('<span class="select-wrap"></span>');
		 $('.field textarea', dform).not('.richtext').wrap('<span class="textarea-wrap"></span>');
		 $('.field', dform).wrapInner('<span class="field-iwrap"></span>');
		 $('.token', dform).wrapInner('<span class="token-iwrap"></span>');
		 $('legend', dform).each(function() {
			var content = $(this).html();
			$(this).after('<h4 class="legend">'+ content +'</h4>').hide();
			$(this).remove();
		 });

		$('button[type=submit]', dform).each(function(){
			$button = $(this);
			$label = $(this).html();
			$(this).wrap('<div class="button submit"></div>').after('<span class="label">'+$label+'</span>');
		});

		$('.button.submit,button[type=submit]', this).live('click', function() {
			$(this).parents('form').trigger('submit');
		});

	});


	$('.actions a.button').each(function(){
		$button = $(this);
		$label = $(this).html();
		$classes = $(this).attr('class').replace('button','');

		$(this).wrap('<div class="button link ' + $classes + '"></div>').after('<span class="label">' + $label + '</span>');

		$('.button', this).live('click', function() {
			document.location = $('a', this).attr('href');
		});

	});

});

/* IDX STUFF */

$(document).ready(function(){

	$('.options .view a').click(function() {

		var viewclass = $(this).attr('class').replace('current','');
		var viewclass = viewclass.replace('viewas-','');
		var viewclass = jQuery.trim(viewclass);

		if (viewclass == 'map') {

		    if($('#office-menu').length > 0) {
                $('#content .set.listings').attr('class', 'set listings basic');
		    } else {
                $('#content .set.listings').attr('class', 'set listings grid');
		    }

            if ($('#listings-map').length <= 0) {
                $('#content').prepend('<div id="listings-map" style="display: none;"></div>');
            }

			$('#listings-map').slideDown('slow', function() {
				// load map
				initialize('listings-map', { 'tooltip' : true, 'infowin' : false });
			});

		} else {

			$('#listings-map').slideUp('slow', function() {
			    //$(this).remove();
			});
			$('#content .set.listings').attr('class','set listings');
			$('#content .set.listings').addClass(viewclass);

		}

		$('.options .view a.viewas-' + viewclass).addClass('current').siblings('a').removeClass('current');
//		selectorImagesLoaded('.set.listings .listing .photos');

		$.cookie('listingsview', viewclass);

		return false;

	});

	/* Select Listing Result View */
	if ($('.options a[class*=viewas].current:first').length == 0) {
    	if (listingsview = $.cookie('listingsview')) {
    	    if ($('.options a.viewas-' + listingsview)) {
                $('.options a.viewas-' + listingsview).trigger('click');
    	    }
    	} else {
    		$('.options a:first').addClass('current').siblings('a').removeClass('current');
    		$('.set.listings').addClass('viewas-grid');
    	}
	} else {
	    $('.options a[class*=viewas].current:first').trigger('click');
	}

	/* IDX Saved Favorites */
	$favsmenuoffset     = $('#navi-favorites').offset();
	$favsmenuoffset.top = $favsmenuoffset.top + $('#navi-favorites').outerHeight();
	$('body').append('<div id="favs-menu"></div>');
	$('#navi-favorites').bind('mouseover', function() {
	    $('#favs-menu').show();
	});
	$('#favs-menu').hide().addClass('menu').css({
	    left: $favsmenuoffset.left + 'px',
	    top: $favsmenuoffset.top + 'px'
	}).load('/idx/inc/php/ajax/favourites.php');

	/* IDX Save this Search */
	$('body').append('<div id="form-save"></div>');
	$('#navi-save').bind('mouseover', function() {
    	$savemenuoffset      = $('#navi-save').offset();
    	$savemenuoffset.top  = $savemenuoffset.top  + $('#navi-save').outerHeight();
	    $('#form-save').show().css({
    	    left: $savemenuoffset.left + 'px',
    	    top: $savemenuoffset.top + 'px'
    	});
	});
	$('#form-save').hide().addClass('menu');

	/* Save this Search Form */
	$('#form-save').html('<form action="' + window.location.href + '" onsubmit="this.action += ((this.action.indexOf(\'?\') != -1) ? \'&\' : \'?\') + \'search_title=\' + this.search_title.value;" method="post" class="rewfw">'
                           + '<input id="search_title" name="search_title" type="text" value="Search Title"'
	                       + ' onfocus="this.value = this.value == \'Search Title\' ? \'\' : this.value;"'
                           + ' onblur="this.value = this.value == \'\' ? \'Search Title\' : this.value;"'
                           + ' />'
                           + '<div class="buttonset"><button type="submit" class="positive" value="Save">Save</button>'
                       + '</form>').css('width', '140px');

    /* IDX Nav Toolbar */
	$('#nav-supplementary li a').mouseover(function(){closeAllMenus();})
	$('.menu').hover(function(){},function(){closeAllMenus();})

	function closeAllMenus() {
		$('.menu').hide();
	}

	$('.compact .listing').live('mouseover',function(){$(this).addClass('hover');});
	$('.compact .listing').live('mouseout',function(){$(this).removeClass('hover');});
	$('.compact .listing').live('click',function(){document.location = $('a:first',this).attr('href');});

	/* Social Networking Links */
	$('#action-socialnetworking').removeClass('open').click(function(){
		$('#social-network-panel').slideToggle('slow',function(){
			$('#action-socialnetworking').toggleClass('open');
		});
	});
	$('#social-network-panel').hide();

});

/* IDX Auto Complete */
$(document).ready(function () {
    $('form.rewfw input.autocomplete').each(function () {
        $(this).autocomplete('/idx/inc/php/ajax/json.php?search=' + $(this).attr('name'), {
		    cacheLength: 1,
			multiple: true,
			dataType: 'json',
			parse: function (data) {
        		var parsed = [];
        		var rows   = data.options;
        		for (var i=0; i < rows.length; i++) {
        			var row = $.trim(rows[i].title);
        			if (row) {
        				row = row.split("|");
        				parsed[parsed.length] = {
        					data: row,
        					value: row[0],
        					result: row
        				};
        			}
        		}
        		return parsed;
			}
		});
	});
});


$(document).ready(function () {

				$("div#content-primary ul.rssfeed-item").splitList({numColumns : 2});
                $("div#content-primary ul.specializing-item").splitList({numColumns : 3});

				$(".client-directory").splitList({numColumns : 3});
				$('.listing-box ul:first').addClass('first');
				$('.listing-box ul:last-child').addClass('last');

				$(".basics ul").splitList({numColumns : 2});
				$('.basics :last-child').addClass('last');

				$('#property-features ul :last-child').addClass('last');

				$('#content-secondary dl.nav dt').corner("10px top");
				$('#idx-featured-listings h2').corner("10px top");



				$('.tabset ul.clearfix li a').corner("5px top");
				$('.tabset ul.clearfix li ul a').uncorner();
				$('#rss-reader').corner("10px bottom");
// REWMOD
//				$('#content-feature').text('').flash({
//			   		 src: '/inc/flash/r3SurrterreHomeFlash.swf',
//			   		 width: '100%',
//			   		 height: '100%',
//			   		 wmode: 'transparent'
//					});

				$('#idx-detail .tabset li ').hover(
					function() {$(this).addClass('hover');},
					function() {$(this).removeClass('hover');}
					);


});



/* Open Facebox Iframe */
var openPage = function openPage (href) {
    $.facebox({'iframe' : href});
};

/* Global Facebox Close Function */
var closeFacebox = function () {
    $(document).trigger('close.facebox');
};


