/* 
 * 
 * Last Modified : 20 September 2010 15:45:32 GMT+1
 * 
 */

$(function() {
	
	/* 
	 * External Links
	 */
	
	$('a[rel=external]').click(function(e) {
		open(this.href);
		e.preventDefault();
	}).attr('title', 'Opens in a new window');
	
	/* 
	 * Random Little Niceties
	 */
	
	$('li:last-child').addClass('last-item');
	
	$('li:nth-child(odd)').addClass('alternate');
	
	$('#sidebar').append('<div class="end"></div>');
	
	/* 
	 * Manipulate Umbraco's image alignment
	 */
	
	$('#content img[style="float: left;"]').addClass('left').removeAttr('style');
	$('#content img[style="float: right;"]').addClass('right').removeAttr('style');
	
	/* 
	 * Fancybox
	 */
	
	$('.fancybox-image').fancybox({
		titlePosition : 'over',
		transitionIn  : 'elastic',
		transitionOut : 'elastic',
		type          : 'image'
	});
	
	/*
	 * Simple Gallery
	 */
	
	$('.images').prepend('<img class="full" height="240" src="" width="360" />');
	
	$('.images .extras ul li a').each(function() {
		
		$(this).click(function () {
			
			var thumbAlt = $(this).find("img").attr("alt");
			
			var thumbSrc = $(this).find('img').attr('src');
				thumbSrc = thumbSrc.replace('width=102', 'width=360');
			
			$('.images img.full').attr({
				alt : thumbAlt,
				src : thumbSrc
			});  
			
			return false;                
		});
	});
	
	$('.images .extras ul li a:first').trigger('click');
	
	/* 
	 * Simple Tabs
	 */
	
	$('.tab_content').hide();
	
	if (Functions.GetQueryString('isBuying') == 'false') {
		$('ul.tabs li:last').addClass('active').show();
		$('.tab_content:last').show();
	}
	else {
		$('ul.tabs li:first').addClass('active').show();
		$('.tab_content:first').show();
	}
	
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeClass('active');
		$(this).addClass('active');
		$('.tab_content').hide();
		var activeTab = $(this).find('a').attr('href');
		$(activeTab).fadeIn();
		return false;
	});
	
	/*
	 * Header Animation
	 */
	
	$('#header').prepend('<div class="car" />');
	
	var cookie = 'HomesHub';
	
	var date = new Date();
		date.setTime(date.getTime() + (60 * 1000));
	
	if ($.cookie(cookie) != 'Animation') {
		
		$('#header .car').animate({
			left : (($(window).width() / 2) + 260) + 'px'
		}, 8000);
		
		$.cookie(cookie, 'Animation', { path: '/', expires: date });
		
	}
	
	else {
		
		$('#header .car').css('left', (($(window).width() / 2) + 260) + 'px')
		
	}
	
	/*
	 * Tweet
	 */
	$('.tweet').each(function(i) {
		$(this).tweet({
			auto_join_text_default : '',
			auto_join_text_ed      : '',
			auto_join_text_ing     : '',
			auto_join_text_reply   : 'replied to',
			auto_join_text_url     : 'checking out',
			avatar_size            : null,
			count                  : 1,
			join_text              : 'auto',
			loading_text           : 'loading tweets...',
			username               : $(this).attr('id')
		});
	});
	
	/*
	 * VTip
	 */
	vtip();
	
});

/*
 * Useful Functions
 */
Functions = {
	GetQueryString : function(item) {
		var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
		return svalue ? svalue[1] : svalue;
	}
}
