$(document).ready(function(){
									
	// Removes SEO text in navItems
	$(".navItem").html("");
	$(".navItem").removeClass("navItem");
	
	// Clears input fields on Focus
	$(".clearField").one("focus", function() {
		$(this).val("");
	});
	
	
/* CONTACT FORM *******************/
	$('.show_propertyinvest, .show_valuations, .show_seminars, .contactstate_notice')
		.hide();
	
	$('#contact_service').change(function (ev) {
		var value = $(this).val();
		
		switch (value) {
			
			case 'Residential property advice':
			
				$('.show_propertyinvest').fadeIn('fast')
					.find('input, select').addClass('required');
				$('.show_valuations, .show_seminars').hide()
					.find('input, select').removeClass('required');
				$('#contact_state').change();
				break;
			
			case 'Valuations':
			
				$('.show_valuations').fadeIn('fast')
					.find('input, select').addClass('required');
				$('.show_propertyinvest, .show_seminars, .contactstate_notice').hide()
					.find('input, select').removeClass('required');
				break;
			
			case 'Seminars and Events':
			
				$('.show_seminars').fadeIn('fast')
					.find('input, select').addClass('required');
				$('.show_propertyinvest, .show_valuations, .contactstate_notice').hide()
					.find('input, select').removeClass('required');
				break;
			
			default:
				$('.show_propertyinvest, .show_valuations, .show_seminars, .contactstate_notice')
					.hide()
					.find('input, select').removeClass('required');
		}
	});
	
	$('#contact_state').change(function (ev) {
		var value = $(this).val();
		
		if ($('#contact_service').val() != 'Property investment advice') {
			$('.contactstate_notice').hide();
			return;
		}
		
		switch (value) {
			case '':
			case 'VIC':
			case 'NSW':
				$('.contactstate_notice').hide();
				break;
			
			default:
				$('.contactstate_notice').fadeIn('fast');
		}
	});

/* FAQ close button *******************/
	$('.faqClose a').click(function (e) {
		e.preventDefault();
		var answer = $(this).closest('dd');
		answer.slideUp(function(){
			// on close, go to top of page.
			$(window).scrollTop(0);
			//window.scroll(0,0);
			//$('#main').animate({scrollTop:0},200,function(){}); // Can't get animation to work
		});
		var question = $('dt.active');
		question.removeClass('active').find('a').click(function (ev) {
			answer.slideDown();
			question.addClass('active');
			
			ev.preventDefault();
			$(this).unbind(ev);
		})
	})

/* HOME PAGE FEATURED *******************/
 var tabContainers = $('.contentTab');
 tabContainers.hide().filter(':first').show();

 $('#tab ul a').click(function () {
 tabContainers.hide();
 tabContainers.filter(this.hash).fadeIn();
 $('#tab ul a').removeClass('selective');
 $(this).addClass('selective');
 return false;
 }).filter(':first').addClass('selective');
 // subscribe popup
 $('.home_subscribe').click( function (ev) {
  ev.preventDefault();
  GB_showCenter('', '../../popup_content.asp?id=26');
 });

/* SLIDES *********************************/
 var currentPosition = 0;
 var slideWidth = 222;
 var slides = $('.slide');
 var numberOfSlides = slides.length;
 var visibleSlides = 3;

 // Remove scrollbar in JS
 $('#slidesContainer').css('overflow', 'hidden');

 // Wrap all .slides with #slideInner div
 slides
 .wrapAll('<div id="slideInner"></div>')
 // Float left to display horizontally, readjust .slides width
 .css({
 'float' : 'left',
 'width' : slideWidth
 });

 // Set #slideInner width equal to total width of all slides
 $('#slideInner').css('width', slideWidth * numberOfSlides);

 // Insert controls in the DOM
 $('#slideshow')
 .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
 .append('<span class="control" id="rightControl">Clicking moves right</span>');

 // Hide left arrow control on first load
 manageControls(currentPosition);

 // Create event listeners for .controls clicks
 $('.control:not(.activeRight):not(.activeLeft)')
 .live('click', function(){
 // Determine new position
 currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

 // Hide / show controls
 manageControls(currentPosition);
 // Move slideInner using margin-left
 $('#slideInner').animate({
 'marginLeft' : slideWidth*(-currentPosition)
 });
 });

 // manageControls: Hides and Shows controls depending on currentPosition
 function manageControls(position){

 $('#leftControl').toggleClass("activeLeft", position == 0);
 $('#rightControl').toggleClass("activeRight", position == numberOfSlides - visibleSlides);

 }

/* HOME PAGE NEWS & EVENTS *****************/
	// initialise
	homeTabSideInit();
});

function homeTabSideInit() {
	var containHeight = 0;
	// set up divs
	$('#tabSide .tabContent div').css({
		'position':'absolute',
		'z-index':'1',
		'opacity':'0'
		})
		.each(function(index){
			$(this).addClass('h'+$(this).height());
			if ($(this).outerHeight() > containHeight) {
				containHeight = $(this).outerHeight();
			}
	});
	// set up container
	$('#tabSide .tabContent').css({'height':containHeight});
	
	// set up btns
	$('#tabSide .tabMenu a')
		// remove href
		.removeAttr('href')
		.removeAttr('class')
		// remove text
		.html('')
		// bind clicks
		.click(function(){
			homeTabSideSwitch($(this));
		});
		
	// set initial tab
	homeTabSideSwitch($('#tabSide .tabMenu a:first'));
}

function homeTabSideSwitch(nextObjBtn) {
	var nextObj, currObj;
	var nextObjID, currObjID;
	var fadeSpeed = 200;
	
	// get new rel
	nextObjID 	= nextObjBtn.attr('rel');
	
	// get curr rel
	var currObjBtn = $('.tabMenu a.active');
	if (currObjBtn.length > 0) {
		currObjID = currObjBtn.attr('rel');
	} else {
		currObjID = '';
	}
	
	// if new rel not active and not animating
	if ( !nextObjBtn.hasClass('active') && $('.tabContent div:animated').length == 0) {
		// find active obj
		if (currObjID != '') {
			currObj 	= $(currObjID);
			// fade active out
			currObj.animate({
				opacity: 0
			},fadeSpeed,function(){
				// set active to not
				currObj.css({'z-index':1}).removeClass('active');
				currObjBtn.removeClass('active');
			});
		}
		
		// find new obj
		nextObj 	= $(nextObjID);
		// fade new in
		nextObj.animate({
			opacity: 1
		},fadeSpeed,function(){
			// set new to active
			nextObj.css({'z-index':2}).addClass('active');
			nextObjBtn.addClass('active')
		});
		
		
	} // end if
}
