$(document).ready(function() {
    
    // Main Navigation Drop-down Menu
    $('#nav ul > li').hoverIntent(
        function() {
            $(this).stop().addClass('active').children('ul').slideDown('fast','swing');
        },
        function() {
            $(this).stop().removeClass('active').children('ul').slideUp('fast','swing');
        }
    );

    // Activate Tabs
    var $tabs = $('#tabbedpanels').tabs({ 
		fx: { opacity: 'toggle', duration: 800 },
		select: function(event, ui){ window.location = ui.tab.href; } // Show fragment id in url for CrazyEgg
	});
	
	// Trigger Tabs via Anchors
	$('.about-link').click(function() {
	    $tabs.tabs('select', '#about');
	    return true;                   
	});                                 
	                                    
	$('.forsale-link').click(function() {
	    $tabs.tabs('select', '#forsale');
	    return true;                   
	});                                 
	                                    
	$('.salesoffice-link').click(function() {
	    $tabs.tabs('select', '#salesoffice');
	    return true;                  
	});                                 
	                                    
	$('.neighbourhood-link').click(function() {
	    $tabs.tabs('select', '#neighbourhood');
	    return true;                   
	});                                 
	                                    
	$('.projectplans-link').click(function() {
	    $tabs.tabs('select', '#projectplans');
	    return true;                  
	});                                 
	                                    
	$('.contactus-link').click(function() {
	    $tabs.tabs('select', '#contactus');
	    return true; 
	});
    
    // Add first/last classes to Tabs
    $('.tabmenu > li:first-child').addClass('first');
    $('.tabmenu > li:last-child').addClass('last');
	
	// Open inks in new window on click
    $('.external').attr('target', '_blank');
    
    // Hero Slide Gallery
    $('#heroslides').innerfade({
		speed: 'slow',
		timeout: 5000,
		type: 'sequence',
		containerheight: '250px'
	});
	
	// Add Hero Gallery Round Corner
	$('#herogallery').prepend('<img src="http://residential.australand.com.au/media/1/cms/images/hero-top-left-curve.png" alt="" id="topcurve" />');
	
	// Activate Lightbox Galleries
	$('a.lightbox').fancybox({
		'overlayOpacity'	: 0.5    
	});
	
	$('a[rel*=main-gallery]').fancybox({
		'overlayOpacity'	: 0.5    
	});
	
	$('a[rel*=map-gallery]').fancybox({
		'overlayOpacity'	: 0.5
	});
	
	$('a[rel*=plan-gallery]').fancybox({
		'overlayOpacity'	: 0.5
	});
	
	$('a.showform').fancybox({
    	'hideOnContentClick': false,
    	'overlayOpacity'	: 0.5,
    	'frameHeight'       : 451
    });
	
    // Add Enlarge Image icon to images
    $('a.item-image, .item-slideshow li a, a.iframe, .display-list-image').prepend('<img src="http://residential.australand.com.au/media/1/cms/images/enlarge.png" class="enlarge-icon" alt="Enlarge image" />');
    
	// More Info Pop-ups - Add Top and Bottom Edges
	$('div.more-info-pop').prepend('<div class="more-info-top"></div>');
	$('div.more-info-pop').append('<div class="more-info-bottom"></div>');

	// More Info Pop-ups - Display Pop-ups
	$('li.more-info').hoverIntent(
		function () {
			$(this).children('div').fadeIn();
		}, 
		function () {
			$(this).children('div').fadeOut();
		}
	);
	
	$('.more-info-content p:last').css({margin: 0});
	
	
	if ($('ul#heroslides li:first-child a').length) {
		var slideurl = $('ul#heroslides li:first-child a').attr("href");
		$('#viewslideshow').attr("href",slideurl);
		$('#viewslideshow').css({"display":"block"});
		
	} else {
		$('#viewslideshow').css({"display":"none"});
	}
	
    // Pop-up Overlay
    $('.pop-overlay')
        .css({opacity: 0, display: 'block', right: -200 })
        .animate({opacity: 0}, 10000)
        .animate({opacity: 1.0, right: 0}, 1800, 'swing');
        
    $('a.closepop')
        .css({opacity: 0})
        .animate({opacity: 0}, 4000)
        .animate({opacity: 0.2}, 1800);
        
    $("a.closepop").hover(
      function () {
        $(this).animate({opacity: 1.0}, 300);
      }, 
      function () {
        $(this).animate({opacity: 0.2}, 300);
      }
    );
        
    $('a.closepop').click(function(){
        $('.pop-overlay').fadeOut();
    });
    
    // Add Related Dev image frame 
    $('.related-image').prepend('<img src="http://residential.australand.com.au/media/1/cms/images/related-image-overlay.png" alt="" class="related-image-overlay" />');
    
	// Contact Us Form Validation
    $('#subscriber_firstname, #subscriber_lastname, #subscriber_phone, #email').attr('accept', 'true'); // mark all of those fields as required
    $('#email').attr('mask', 'email'); // mark as requiring valid email addresses
    $('#subscriber_phone').attr('mask', 'phone'); // mark as requiring phone data
		
	$("#annotation_brochurerequest").change(function(){
		if ($('#annotation_brochurerequest').is(':checked')) {
			// check if Brochure Request checkbox is checked
			var emailid = $('input[name=src]').attr('title');
			$('input[name=src]').val(emailid);
		} else {
			// Sets Activity ID to Null
			$('input[name=src]').val('');
		}
	});
	
	$("#contactform #lists").change(function(){
		if ($('#contactform #lists').is(':checked')) {
			$('#annotation_subscribe').val('Yes');
		} else {
			// Sets Activity ID to Null
			$('#annotation_subscribe').val('No');
		}
	});
		

	$('.display-list > li').hover(
      function () {
        $(this).addClass("active");
      }, 
      function () {
        $(this).removeClass("active");
      }
    );

	$(".display-list > li:nth-child(3n+1)").addClass("first");
	$(".item-slideshow > li:nth-child(5n+1)").addClass("first");

});

$(window).bind('load', function() {
	$('#heroslides').fadeIn(800);
	$('#herogallery').css({backgroundImage: 'none'});
});


// Cufon Font Replacement
Cufon.replace([ '.section h1','.section h2','#related-projects h2','.pop-overlay h2', '#contactpanel h3']);
Cufon.replace('#projecttypes li a', {
	hover: true
});
Cufon.replace('#projecttypes li strong', {
	hover: true
});