
$(document).ready(function(){

    $('a[rel*=external],#content a[href^=http://]:not([href*=' + location.hostname + '])"').newWindow();
	
});




$(window).load(function(){  

	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-693909-5']);
	  _gaq.push(['_trackPageview']);


});

var articles = (function() {
	var $content;
	
    var init = function() {
    
        if ( ! $('body').hasClass('article')) return false;
		
		$content = $('#content');
		
        // Setup images for rounded corners
        roundedCorners();        
        
        // Setup Flash videos
        // (magically converts the Flash into JavaScript/HTML/CSS for Apple portable devices... just kidding ʘ‿ʘ)
        appendFlash();
        
        // Setup a class on PDF links to attach a little PDF icon with CSS
        parseLinks();
           
    };
    
    var parseLinks = function() {
        
        var extensions = [
            'pdf',
            'doc',
            'xls',
            'jpg',
            'png',
            'gif'
        ];
        
        $.each(extensions, function(i, extension) {
            $content.find('a[href$=' + extension + ']').addClass('file ' + extension).newWindow();
        });
        
    
    
    };
    
    var appendFlash = function() {
    
    	var isAreaWeCover = !! $('body.about-utopia.area-we-cover').length;
    	
    	if ( ! isAreaWeCover) return false;
    	
    	$content.append('<div id="area-we-cover" />');
    	
    	$areaWeCover = $('#area-we-cover');
    	
    	$areaWeCover.flash({
            src: config.basePath + 'assets/swf/maps.swf',
            width: $areaWeCover.width(),
            height: $areaWeCover.height(),
            wmode: 'transparent'
        });
    	
    };
    
    var roundedCorners = function() {
    
    	$content.find('.article-image').each(function() {
            
           
            
             $(this).load(function() {
                var imgSrc = $(this).attr('src')
                    imgHeight = $(this).height(),
                    imgWidth = $(this).width();
                
                $(this).wrap('<div class="article-image-container" style="width: ' + imgWidth + 'px; height: ' + imgHeight + 'px; background-image: url('  + imgSrc + ')" />');
                
                $(this).remove();
            
            });    
                
                
                
            
           
            
        });

    
    };

    $(init);


})();

var contact = (function() {
    
    var init = function() {
    
        if ( ! $('body').hasClass('contact')) return false;
        

        
        $googleMap =  $('#google-map a');
        
        var href = $googleMap.attr('href') + '&output=embed';
        
        $googleMap.unbind('click'); // remove open new window event binded
        
        $googleMap.fancybox({
            	scrolling		: 'no',
            	titleShow		: false,
            	type            : 'iframe',
            	href            : href   
        });
        
           
    };

    $(init);

})();

var searchPropertiesModal = (function() {
	var $modal,
	    $overlay, 
	    $loading,
	    href;

	var init =  function() {
    	// Style the page one too
    	if ($('body').hasClass('search')){
    		styleForm($('#content #search-form'));
    	};
    	
    	
    	$('body').append('<div id="modal" style="display: none;" /><div id="loading" style="display: none;" /><div id="overlay" style="display: none;" />');
        $modal = $('#modal');
        $overlay = $('#overlay');
		$loading = $('#loading');
    	
    	var $searchLink = $('#search a');
    	
    	href = $searchLink.attr('href');
    	
    	$(document).keyup(function(e) {
		  if (e.keyCode == 27) {  hide(); }
		});
    	
    	$(document).click(function() {
			hide();
		});
		
		$modal.click(function(event) {
			event.stopPropagation();
		});
    	
    	$searchLink.click(function(event) {
    		event.preventDefault();
     		event.stopPropagation();
     		show();
    	});

	
	};
	
	var styleForm = function($form) {
		
		$form.find('.input-row').css({ overflow: 'visible' }).append('<br style="clear: both;" />');
		
		$form.jqTransform({imgPath: config.basePath + 'assets/packages/jqtransform/img/'});
	
	};
	
	var loadForm = function(callback) {
	   
	   if (typeof callback !== 'function') {
	       throw 'not a callback';
	   };
	   
	   if ($modal.find('*').length === 0) {
			
			// load the jqtransform plugin
			
			$.getScript(config.basePath + 'assets/packages/jqtransform/jquery.jqtransform.js', function() {
			
    			$('head link[rel=stylesheet]').after('<link rel="stylesheet" href="' + config.basePath + 'assets/packages/jqtransform/jqtransform.css" type="text/css" />');
    			
    			$modal.load(href, function(response, status, xhr) {
    			  if (status === 'error') {
    			    $modal.html('Error: ' + xhr.status + ' ' + xhr.statusText);
    			  } else {
    
    				 callback();  
     
    			  };
			});
			
			});
			
			
        
	   } else { 
	       callback();
	   };
	};
	
	var show = function() {
		$loading.show();
		loadForm(function() {
		  
		  var opacity = $overlay.css('opacity');
		        
		  
		  $overlay.fadeTo(500, opacity, function() { 
				$modal.fadeIn(500);
				
				if ($modal.find('div.jqTransformSelectWrapper').length === 0) {
              	    styleForm($modal.find('form'));
				};
				
				$loading.hide();
				$(this).show();
			});
		
		});
		
		

			
	};
	

	
	var hide = function() {
		$modal.hide();
		$overlay.fadeOut(500);
	
	};
	
	$(init);

})();

var header = (function() {

    var init = function() {
    	// Flash logo
        $('#header').append('<div id="flash-logo" />');
        
        var $flashLogo = $('#flash-logo');
        
        $flashLogo.flash({
            src: config.basePath + 'assets/swf/logo.swf',
            width: $flashLogo.width(),
            height: $flashLogo.height(),
            wmode: 'transparent'
        });
    
    	
        
     };
    
    $(init);

})();

var propertyInfo = (function() {

	var init = function() {
	
		if ( ! $('body').hasClass('view-property')) {
			return false;
		};

        googleMap();
        
        imageGallery();
        

	
	
	};
	
	var imageGallery = function() {
		
		// add little image to show that these images may be clicked
		
		var $mainImage = $('#main-image');
		
		$mainImage.wrap('<div style="position: relative; width: ' + $mainImage.width()  +'px; float: left; cursor: pointer;" />');
		
		$mainImage.parent('div').prepend('<span id="view-more-photos" style="display: none;">View more photos of this property</span>');
		
		$('#view-more-photos').fadeIn(500).click(function() { $(this).next('img').click(); });
		
		var $fancyBoxPhotos = $('#all-photos ul li a');
		$fancyBoxPhotos.attr({ rel: 'house-gallery' }).fancybox();
		
		$('#property-gallery').find(' > div > img, a').each(function(i) {
		
    		$(this).click( function (event) {
    		  event.preventDefault();
    
    		  $fancyBoxPhotos.eq(i).click();
    		 
    		});
		
		});
		
	
	};
	
	var googleMap = function() {
	
		$('body').append('<a href="#" id="hidden-link" style="display: none;"></a>');
        
        var staticGoogleMap = $('#property-location img').attr('src');
        
        // Parse it and get the address
        
        var staticGoogleMapTokens = staticGoogleMap.split('|');
        
        var address = staticGoogleMapTokens[staticGoogleMapTokens.length - 1];

        $('#hidden-link').fancybox({
        	scrolling		: 'no',
        	titleShow		: false,
        	type            : 'iframe',
        	href            : 'http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=' + address + '&amp;sll=-26.387151,153.088313&amp;sspn=0.007237,0.013937&amp;ie=UTF8&amp;output=embed'    
        });
        
        $('#property-location').unbind('click').click(function(event) {
            event.preventDefault();
            $('#hidden-link').trigger('click');
            
        });
	
	};

	$(init);

})();





var menu = (function() {
    var $menu,
        $menuHeadings;
    
    var init = function() {
    
        $menu = $('#primary-menu');
        $menuHeadings = $menu.find('li strong');
        
        // Add bullet in front of menu items
        $menuHeadings.prepend('<span class="bullet">●</span>');
        
        // Add elements as hooks for borders
        $menu.find('> li:gt(0)').prepend('<span class="division" />');
    
        // Hide all submenus
        $menu.find('> li > ul').hide();
    
        // Now we will do the sliding of the menus
        $menuHeadings.click(function() {
            $(this).next('ul').slideToggle(500);
        });
        
        // Show the correct menu
        
        var url = window.location.pathname;
        
        if (config.basePath !== '/') {
        	url = url.replace(config.basePath, '');
        } else {
        	url = url.substr(1);
        };
        
        var urlTokens = url.split('/');
        
        var parentSlug = urlTokens[0];
        
        if (parentSlug) {
        
        	$menu.find('li[id=menu-' + parentSlug + '] strong').click();
   		} else {
   		
        	$menuHeadings.eq(0).click();
   		};
        
        
    
    };
    
    $(init);


})();


/*
 * newWindow 1.1 - jQuery plugin for adding new window functionality to links.
 * http://www.alexanderdickson.com/projects/jquery-plugins/newwindow/
 *
 * Copyright (c) 2009 Alex Dickson
 * Licensed under the MIT licenses.
 * See website for more info.
 *
 * Date: 2009-08-22 09:03:00 +1000 (Sunday, 23 Aug 2009)
 */

 (function($){  
  $.fn.newWindow = function(options) {   
    var defaults = {
		titleText: 'Link opens in a new window'
	};	
	var options = $.extend(defaults, options);   
     return this.each(function() {  
	   var obj = $(this);
       if ( ! obj.is('a')) {
           return;
       };
	   if (options.titleText != '') {   
		   if (obj.attr('title')) {
				var newTitle = obj.attr('title') + ' (' + options.titleText + ')';
		   } else {
				var newTitle = options.titleText;   
		   };		   
		   obj.attr('title', newTitle);   
	   };
	   obj.bind('click', function(event) {
	   	  event.preventDefault();  
		  var newBlankWindow = window.open(obj.attr('href'), '_blank');
		  newBlankWindow.focus();
		}); 
	   });
  };  
 })(jQuery); 
