$(document).ready(function(){

	$('#contact_wrapper').hide();
	$('#frmContact').validate();
	$('#query-info').validate();
	$('#contact_wrapper').hide();
	$('#fader').hide();
	$('.modal').hide();
	
	setInterval ("rotateImages()", 8000);

	$('#contact_left select').linkselect();
	
	$('.menu-show').click(function(){
		if(!$.browser.msie) {
			$('#contact_wrapper').fadeIn('slow');
		} else {
			$('#contact_wrapper').show();
		}
		$('#content_wrapper').animate({'margin-top':'400px'},500);
		$(this).addClass('open');
	});
	
	$('.menu-services').hover(
		function(){
			/*if ($.browser.msie && $.browser.version < 8){
				$('#servicesSub').show();
			} else {
				$('#servicesSub').slideDown('fast')
			}*/
			$('#servicesSub').css('visibility','visible');
		},
		function(){
			/*if ($.browser.msie && $.browser.version < 8){
				$('#servicesSub').hide()
			} else {
				$('#servicesSub').slideUp('fast')
			}*/	
			$('#servicesSub').css('visibility','hidden');
		}
	);
	
	$('#hide').click(function(){
		if(!$.browser.msie) {
			$('#contact_wrapper').fadeOut('slow');
		} else {
			window.setTimeout(hideContactForm, 500);
		}
		$('#content_wrapper').animate({'margin-top':'0px'},500);
		$('.menu-show').removeClass('open');
		$('input.error').removeClass('error');
		$('textarea.error').removeClass('error');
	});
	
	// Form validation & send
	$('#frmContact').submit(function(){
												
		if($('#frmContact').valid()){
			
/*			var cName		= $('#txtName').val();
			var cNumber		= $('#txtNumber').val();
			var cEmail		= $('#txtEmail').val();
			var cMessage	= $('#txtMessage').val();
			
			var cData		= "i=contact&name=" + cName +
							  "&number=" + cNumber +
							  "&email=" + cEmail +
							  "&message=" + cMessage;*/
			
			var cData		= "i=contact&" + $('#frmContact').serialize();
			
			//alert (cData);return false;
							  
			$.ajax({
				type: 'POST',
				url: 'sendForm.php',
				data: cData,
				success: function(){
					// If data has been processed correctly
					$('#form').html('<div class="message">The form has been submitted!</div>')
					.hide()
					.fadeIn(2000);
				}
			});
			
			return false; // required to stop page reload

		} // End if
		
	});
	
	$('#query-info').submit(function(){

		if($('#query-info').valid()){
			
			var cName		= $('#query-name').val();
			var cEmail		= $('#query-email').val();
			
			var cData		= "i=query&name=" + cName +
							  "&email=" + cEmail;
							  
			//var cData		= "i=query&" + $('#query-info').serialize();
			
			//alert (cData);return false;
							  
			$.ajax({
				type: 'POST',
				url: 'sendForm.php',
				data: cData,
				success: function(){
					// If data has been processed correctly
					$('#query-box').html('<div class="message">The form has been submitted!</div>')
					.hide()
					.fadeIn(2000);
				}
			});
			
			return false; // required to stop page reload

		}// end if

	});
	
	$('blockquote').each(function(){
		$(this).wrap('<div class="bq"></div>');
		$(this).before('<span class="bq before"></span>');
		$(this).after('<span class="bq after"></span>');
	});


/* DEPRECATED MAP FUNCTIONALITY */
/*******************************************************************
	$('.dc').click(function(){
	
		if ($(this).hasClass('ga')){
			
			$('#mapSA').hide();
			$('#mapGauteng').show();
			
		} else if ($(this).hasClass('back')) {
			
			$('#mapGauteng').hide();
			$('#mapSA').show();
			
		} else {
			if($(this).hasClass('gautengDC')){
				
				city = $(this).html();
				sendThis = 'access=asdh89hsoj32&findSuburbs='+city;
				
				$.ajax({
					type: 'post',
					url: 'getSuburb.php',
      				async:false,
					data: sendThis,
					success: function(data){
								
								//alert(data);
								$('#suburbModal h2 span').html(city);
								$('#suburbModal h2 span').addClass('gauteng');
								$('#suburbModal h2 span').removeClass('country');
								$('#suburbList').html(data);
								showList();
					}
				});
				
			} else {
				
				province = $(this).html();
				sendThis = 'access=9k23dkasm30&findSuburbs='+province;
				
				$.ajax({
					type: 'post',
					url: 'getSuburb.php',
      				async:false,
					data: sendThis,
					success: function(data){
						
								//alert(data);
								$('#suburbModal h2 span').html(province);
								$('#suburbModal h2 span').addClass('country');
								$('#suburbModal h2 span').removeClass('gauteng');
								$('#suburbList').html(data);
								showList();
					}
				});
				
			}
		}
		return false;
	
	});
*******************************************************************/
/* end: DEPRECATED MAP FUNCTIONALITY */

	$('.dc').click(function(){
	
		province = $(this).html();
		sendThis = 'access=9k23dkasm30&findCity='+province;
		
		$.ajax({
			type: 'post',
			url: 'getCity.php',
			async:false,
			data: sendThis,
			success: function(data){
			
				//alert(data);
				$('#suburbModal h2 span').html(province);
				$('#suburbModal h2 span').addClass('country');
				$('#suburbModal h2 span').removeClass('gauteng');
				$('#cityDrop').html(data);
				showList();
				
			}
		});
		
		return false;
	
	});
	
	// City dropdown hack
	$('#cityDrop').change(function() {
		
		suburb = $('#cityDrop').val();
		sendThis = 'access=asdh89hsoj32&findSuburb='+suburb;
		
		$.ajax({
			type: 'post',
			url: 'getSuburb.php',
			async:false,
			data: sendThis,
			success: function(data){
			
				//alert(data);
				$('#suburbDrop').html(data);
				$('#suburbDropWrap').fadeIn(500);
				
			}
		});
		
		return false;
		
	});
	
	$('#btnFindDC').click(function(){
		$('#citySA').val($('#cityDrop').val());
		$('#suburbSA').val($('#suburbDrop').val());
		$('#frm_Location_Search').submit();		
	});

	$('#hide').hover(
		function(){$(this).addClass('over')},
		function(){$(this).removeClass('over')}
	);
	
	$('#anton .blueButton').click(	function(){showModal(1)});
	$('#eben .blueButton').click(	function(){showModal(2)});
	$('#abrie .blueButton').click(	function(){showModal(3)});
	
	$('.modal .close').click(function(){fadeModal();});
	$('#suburbModal .close').click(function(){fadeModal();$('#suburbDropWrap').hide()});
		  
}); // END (document).ready


function rotateImages() {
	var oCurPhoto = $("#photoShow div.current");
	var oNxtPhoto = oCurPhoto.next();
	if (oNxtPhoto.length == 0) oNxtPhoto = $("#photoShow div:first");
	
	oCurPhoto.removeClass('current').addClass('previous');
	oNxtPhoto.css({'opacity':0}).addClass('current')
		.animate(
			{'opacity':1},
			1000,
			function(){ oCurPhoto.removeClass('previous') }
		);
}

function hideContactForm(){
	$('#contact_wrapper').hide(0);
}

function fadeModal(){
	$('#fader').fadeOut(500);
	$('.modal').fadeOut(500);
}

function showModal(id){
	
	$('.modal').css({
		'left'	:	($(document).width() * 0.5) - ($('#mg'+id).width() * 0.5),
		'top'	:	200
	});
	
	$('#fader').css({
		'width'	:	$(document).width(),
		'height':	$(document).height()
	});
	
	if ( $.browser.msie ) {
		$('#mg'+id).css('position','absolute');
		$(document).scrollTo(0);
		$('#mg'+id).show(0);
		$('#fader').show(0);
	} else {
		$('#fader').fadeIn(500);
		$('#mg'+id).fadeIn(500);
	}
}

function showList(){
	
	$('#suburbModal').css({
		'left'	:	($(document).width() * 0.5) - ($('#suburbModal').width() * 0.5),
		'top'	:	200
	});
	
	$('#fader').css({
		'width'	:	$(document).width(),
		'height':	$(document).height()
	});
	
	if ( $.browser.msie ) {
		$('#suburbModal').css('position','absolute');
		$(document).scrollTo(0);
		$('#suburbModal').show(0);
		$('#fader').show(0);
	} else {
		$('#fader').fadeIn(500);
		$('#suburbModal').fadeIn(500);
	}
}