$('html').addClass('js');

$(document).ready(function(){

	$('html').removeClass('js');	
	
	//Overlay for YouTube vids
	$("a.overlay-youtube").click(function() {
		$.fancybox({
				'overlayOpacity': 0.75,
				'overlayColor'	: '#000',
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'speedIn'		: 800,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'				: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});


	// Header Tools widgets
	$('#contact-widget').hide(0);
	$('#buttons-widget').show(0);
	$('#contact-widget div.right').css("display", "block");
	$('#contact-widget div.left').css("width", "160px");
		
	$('a.contact-info').click(function(){
		$('#buttons-widget').hide(0);
		$('#contact-widget').show(0);
	});
	
	$('a.get-email-updates').click(function(){
		$('#buttons-widget').hide(0);
		$('#subscribe-widget').show(0);
	});
	
	$('a.close').click(function(){
		$(this).parent().parent().hide(0);
		$('#buttons-widget').show(0);
	});
	
	$('a.email').click(function(){
		$(this).parent().parent().hide(0);
		$('#subscribe-widget').show(0);
	});
	
	$('a.contact').click(function(){
		$(this).parent().parent().hide(0);
		$('#contact-widget').show(0);
	});	
	
	// Header Tools Form
    $('#subscribe-widget form').ajaxForm({
    	target:			'#subscribe-widget div.left',
    	beforeSubmit:	validate,
    	resetForm:		true	
    });
    
    function validate(formData, jqForm, options) { 
	    // jqForm is a jQuery object which wraps the form DOM element 
	    // 
	    // To validate, we can access the DOM elements directly and return true 
	    // only if the values of both the username and password fields evaluate 
	    // to true 
	 
	    var form = jqForm[0];
	    
		var mailData = form.email.value.toLowerCase();
		var mailTester = (mailData.search(/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/));
	     
	    if (!form.first_name.value) { 
	        $('#label_first_name').css({'color' : '#902703', 'font-weight' : 'bold'});
	    	$('#first_name').css({'background-color' : '#902703', 'color' : '#FFF'}); 
	        return false;
	    } else if (!form.last_name.value) {
	    	$('#label_last_name').css({'color' : '#902703', 'font-weight' : 'bold'});
	    	$('#last_name').css({'background-color' : '#902703', 'color' : '#FFF'});
	    	return false; 
	    } else if (mailTester == -1) {
	    	$('#label_email').css({'color' : '#902703', 'font-weight' : 'bold'});
	    	$('#email').css({'background-color' : '#902703', 'color' : '#FFF'});
	    	return false;
	    }
	    alert('All fields contain values.');
	}	
	
	// Listings Browsers	
	$('#active-listings-browser div, .listings-nav a#sold-listings, .listings-prev, .listings-next').css("display", "block");
	$('.listings-browser div.first').css("margin-left", "0");
		
	$('#active-listings-browser').cycle({ 
	    fx:     'scrollHorz', 
	    speed:   500, 
	    timeout: 0, 
	    next:   '#active-listings-next', 
	    prev:   '#active-listings-prev' 
	});
	
	$('#sold-listings').click(function(){
		$('#active-listings-module').hide(0);
		$('#sold-listings-module').show(0);
		$('#sold-listings-browser').cycle({ 
		    fx:     'scrollHorz', 
		    speed:   500, 
		    timeout: 0, 
		    next:   '#sold-listings-next', 
		    prev:   '#sold-listings-prev' 
		});
	});
	
	$('#active-listings').click(function(){
		$('#sold-listings-module').hide(0);
		$('#active-listings-module').show(0);
	});	
	
	// Testimonial Browser	
	$('#sub-content #testimonial-browser div, #sub-content .left .options').css("display", "block");
	
	$('#testimonial-browser').cycle({ 
	    fx:     'fade', 
	    speed:   500, 
	    timeout: 0, 
	    next:   '#testimonial-next', 
	    prev:   '#testimonial-prev' 
	});	
	
	// MLS Search Result Mods
	$('li.dsidx-listing').each(function(){
		var theLink = $(this).find('div.dsidx-address a').attr('href');
		$(this).find('div.dsidx-secondary-data').after('<a class="button" href="'+theLink+'">VIEW DETAILS</a>');
	});
	
	$('#dsidx-tiny-photos a').addClass('overlay').removeAttr('target');
	$('#dsidx-primary-photo').removeAttr('href');
	
	$('#refine-search-container').hide(0);
	
	$('#btn-expand').toggle(function() {
		$('#refine-search-container').slideDown(500);
		$(this).css('backgroundPosition', 'top right').text('Collapse Search');
	}, function() {
		$('#refine-search-container').slideUp(500);
		$(this).css('backgroundPosition', 'top left').text('Expand Search');
	});
	
	// Overlays
	$('a.overlay').fancybox({
		'overlayOpacity'		: 0.75,
		'overlayColor'			: '#000',
		'titleShow'				: false,
		'speedIn'				: 800
	});		
	
	$('a.overlay-iframe').fancybox({
		'width' : '75%',
	    'height' : '75%',
	    'autoScale' : false,
	    'transitionIn' : 'none',
	    'transitionOut' : 'none',
	    'type' : 'iframe'
	});
		
	jQuery('a.overlay-prop-detail').fancybox({
		'width' : 662,
		'height' : 496,
		'autoScale' : false,
	    'transitionIn' : 'none',
	    'transitionOut' : 'none',
	    'type' : 'iframe'
	});	
	
	$('a.help-overlay').fancybox({
	    'autoScale' : false,
	    'transitionIn' : 'none',
	    'transitionOut' : 'none',
	    'type' : 'iframe'
	});
	
	$('a.quick_search_overlay').fancybox({
		'width' : '75%',
	    'height' : '75%',
	    'autoScale' : false,
	    'transitionIn' : 'none',
	    'transitionOut' : 'none'
	});
	
	$('a.schedule_showing_overlay').fancybox({
		'width' : '75%',
	    'height' : '75%',
	    'autoScale' : false,
	    'transitionIn' : 'none',
	    'transitionOut' : 'none'
	});
		
});
