//AJAX START

$(function() {

	// CALL ME BACK BUTTONS 	
/*
	$(".call-me-back").fancybox({
		'width'				: '75%',
		'height'			: '75%',
        'autoScale'     	: false,
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe',
		'overlayColor' 		: '#000',
		'overlayOpacity' 	: '.7'
	});

*/

	//SLIDER CONTROL
    $('#slideshow').cycle({
        fx:     'scrollHorz',
        speed:  'fast',
        timeout: 0,
        pager:  '#nav',
        prev:   '#prev', 
	    next:   '#next',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#nav li:eq(' + (idx) + ') a';
        }
    });
    
    $('#int_slideshow').cycle({
        fx:     'scrollHorz',
        speed:  'fast',
        timeout: 0,
        pager:  '#int_nav',
        prev:   '#int_prev', 
	    next:   '#int_next',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#int_nav li:eq(' + (idx) + ') a';
        }
    });
    
     $('#floorplanSlide').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
        pager:  '#floorplanList',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#floorplanList li:eq(' + (idx) + ')';
        }
    });
    
     $('#facadeSlide').cycle({
        fx:     'scrollHorz',
        speed:  'fast',
        timeout: 0,
        pager:  '#facadeList',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#facadeList li:eq(' + (idx) + ')';
        }
    });
    
    $('#floorplanGallery').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
        pager:  '#floorplanList',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#floorplanList li:eq(' + (idx) + ')';
        }
    });
	
	// CUFON replace text
	//Cufon.replace('.quotes', { fontFamily: 'LoosieScript', hover: true }); 
	Cufon.replace('.caption, .quotes', { fontFamily: 'WinterthurCondensedOblique', hover: true }); 
	//Cufon.replace('.winterthurcondensed', { fontFamily: 'WinterthurCondensed', hover: true }); 
	
	//TAB CONTROLS
	$('#tabsContainer a').click(function() {
		// get target+variables
		var target = $(this).attr('name');
		var infoContainer = $('#infoContainer');
		
		// controls the infoContainer tabs
		$('.tabs').addClass('hidden');
		
		if ($('.galleryTabs').hasClass(target)) {
			// alert('has class');
			// controls the galleryTabs
			//$('.galleryTabs').addClass('hidden');
			//$("." + target).removeClass('hidden');
			
			//var galleryHeight = $('#galleryContainer').children("." + target).height();
			//if (galleryHeight<100) { var galleryHeight = 285; }
			
			//$('#galleryContainer').animate({
			//		height: galleryHeight
			//	});
				
		}
		
		$("#" + target).removeClass('hidden');
		
		
		// add selected event on tabs //
		
		$('#tabsContainer a').removeClass('tabSelected');
		$(this).addClass('tabSelected');
		
	});
	
	//PNG FIX
	$('html').pngFix();
	
	// HIGHLIGHT CURRENT PAGE
		var winlocation = window.location.pathname; // empty pathname fix
		if (winlocation=="/" || winlocation=="/newWeb/") { 
			pathname = "index.php";
		} else {
			var pathname = (window.location.pathname.match(/[^\/]+$/)[0]);
		}
		
		if(pathname=="display.php") { var pathname = "displays.php" };
		
		$("#topnav li a").removeClass("selected");
		
		if((pathname=="home.php") || (pathname=="???.php")) {
			var pathname = "our-homes.php";
		}
		
		$("#topnav li").find("a[href='" + pathname  + "']").addClass("selected"); // highlight current page
		$("#topnav li").find("a[href='" + pathname  + "']").parents('.parentNav').children('a').addClass("selected"); // highlight current parent page
	
	
	// NAVBAR HOVER
	//On Hover Over
	function megaHoverOver(){
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	        };
	    })(jQuery); 
	
	    if ( $(this).find(".row").length > 0 ) { //If row exists...
	
	        var biggestRow = 0;	
	
	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });
	
	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	
	    } else { //If row does not exist...
	
	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : rowWidth}); //Set Width
	
	    }
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 0, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 100, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
//	$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
		

	// CLICKABLE COLUMN BUTTONS
	
	$('.columnBtn').click(function() {
	x = $(this).children('a').attr('href');
	target = $(this).children('a').attr('target');
	
	if (target=="_blank") {
		window.open(x);
	} else {
		window.location = x;
	}	
	
	
	});

	
	// ALTERNATING LIST COLOUR
	$('.subSpec li:even').addClass('even');
	$('.subSpec li:odd').addClass('odd');
	
	$('.tableStyle tbody > tr:even').addClass('even');
	$('.tableStyle tbody > tr:odd').addClass('odd');
	
	//BACKGROUND CHANGE
	
	var bgGallery = [
					{ title:'', path:''},
					{ title:'Valencia/Grovedale', path:'01.jpg'},
					{ title:'Meridian/Mernda', path:'02.jpg'},
					{ title:'Sierra/Saltwater Coast', path:'03.jpg'},
					{ title:'Stamford/Wallara Waters', path:'04.jpg'},
					{ title:'Valencia/Bairnsdale', path:'05.jpg'},
					{ title:'Stamford/Cranbourne', path:'06.jpg'}
					];
	
	
	var totalGallery = bgGallery.length;
	
	//alert(bgGallery[0]['title']);
	
	// randomize the background images
	function randomNumber(maxVal) {
		var n = (Math.floor(Math.random()*maxVal));
		if(n==0) {
			n=1;
		}
		return n;
	}
	
	// reset current Image
	$('#bgGallery').text(randomNumber(totalGallery));
	var currentN = $('#bgGallery').text();
	// onload set background image
	$('#bgGalleryPic').css("background-image", "url(images/background/"+bgGallery[currentN]['path']+")");		
	$('#bgTitle').text(bgGallery[currentN]['title']);
	$('#bgGallery').text(currentN);
	
	
	// background controls	
	$('#nextBg').click(function() {
		
		currentN = $('#bgGallery').text();
		
		if (currentN >= totalGallery-1) {
			nextImage = 1;
		} else if (currentN <= totalGallery) {
			nextImage = parseInt(currentN) + 1;
		}
		
		obj = $('body');
		//obj.css("background-image", "url(images/background/"+bgGallery[nextImage]['path']+")");

		$('#bgGalleryPic').fadeOut(0).css("background-image", "url(images/background/"+bgGallery[nextImage]['path']+")").fadeIn('fast');
		
		$('#bgTitle').text(bgGallery[nextImage]['title']);
		$('#bgGallery').text(nextImage);
		
		return false;
	});
	
	$('#prevBg').click(function() {
		currentN = $('#bgGallery').text();
		
		if (currentN <= 1) {
			nextImage = totalGallery-1;
		} else if (currentN >= 1) {
			nextImage = parseInt(currentN) - 1;
		}
		
		obj = $('body');
		//obj.css("background-image", "url(images/background/"+bgGallery[nextImage]['path']+")");
		
		$('#bgGalleryPic').fadeOut(0).css("background-image", "url(images/background/"+bgGallery[nextImage]['path']+")").fadeIn('fast');
		
		$('#bgTitle').text(bgGallery[nextImage]['title']);		
		$('#bgGallery').text(nextImage);
		
		return false;

	});
	
	// promo toggle
	// name the button #tnc-toggle and the copy #tnc-copy
	$('#tnc-toggle').click(function() {
		$('#tnc-copy').slideToggle();
		return false;
	});

});


