// cufon font conversions
Cufon.replace('#nav-bar a', { fontFamily: 'helvetica-neue-53', hover: true });
Cufon.replace('#sidebar-bar a', { fontFamily: 'helvetica-neue-53', hover: true });
Cufon.replace('#sidebar-contact-details', { fontFamily: 'helvetica-neue-53', hover: true });
Cufon.replace('#sidebar-subnav', { fontFamily: 'helvetica-neue-53' });
Cufon.replace('.post h3.page-title', { fontFamily: 'helvetica-neue-53' });
Cufon.replace('.post h2.post-title', { fontFamily: 'helvetica-neue-23' });
Cufon.replace('.post h1.post-title', { fontFamily: 'helvetica-neue-23' });
Cufon.replace('.news-right h2', { fontFamily: 'helvetica-neue-23' });
Cufon.replace('.right-column h3', { fontFamily: 'helvetica-neue-63' });

$(document).ready(function() {
	$('#client-login-btn').mouseover(function(){
		$(this).attr('src', 'http://ckvo.production.tectonic.net.au/assets/images/media-client-login-over.gif');
		return false;
	});
	$('#client-login-btn').mouseout(function(){
		$(this).attr('src', 'http://ckvo.production.tectonic.net.au/assets/images/media-client-login.gif');
		return false;
	});
	
	// keep updating shadows for 2 seconds, 10 times per second
	var interval = setInterval(updateShadows, 100);
	setTimeout(function(){ clearInterval(interval); }, 2000);
	
	// if we're on the contact page, initialise the form
	if ( $('#contact-form').length ) {
		$('#contact-form').click(function(){
			open_form();
			$('#contact-form').unbind('click');
		});
		
		contactForm();
	}
	
	// embed flash video player
	var flashvars = {};
	var params = { wmode:'transparent', allowscriptaccess:'always', scale:'noscale' };
	var attributes = { id:'small_vid', name:'small_vid' };
	swfobject.embedSWF("http://ckvo.production.tectonic.net.au/assets/swf/ckuk-player.swf?xml_file=http://ckvo.production.tectonic.net.au/assets/xml/ckuk_smallvid.xml", "small_vid", "284", "161", "9.0.0", false, flashvars, params, attributes, swfcallback);
	
	function swfcallback(e) {
		/*if (e.success == false) {
			$('#vid object').attr('data', 'http://ckvo.production.tectonic.net.au/assets/videos/screenshot.jpg');
			$('#vid object param[name="src"]').attr('value', 'http://ckvo.production.tectonic.net.au/assets/videos/screenshot.jpg');
			$('#vid object param[name="href"]').attr('value', 'http://ckvo.production.tectonic.net.au/assets/videos/video.mp4');
		}*/
	}
});

function updateShadows() {
	var h = 1;
	//if 		($.browser.webkit)	{ h = 1; }
	//else if ($.browser.msie)	{ h = 1; }
	
	$('#shadow-left, #shadow-right').css({ 'height' : $('#container2').height() + h });
}



// contact form
function contactForm() {
	$('.wide, textarea').clearField();
	
	$('#scriptfile').uploadify({
		'uploader'		: '/assets/js/uploadify/uploadify.swf',
		'script'		: '/assets/js/uploadify/uploadify.php',
		'cancelImg'		: 'http://ckvo.production.tectonic.net.au/assets/js/uploadify/cancel.gif',
		'buttonImg'		: 'http://ckvo.production.tectonic.net.au/assets/images/browse-file-whitebg.gif',
		'buttonText'	: '',
		'width'			: 131,
		'height'		: 34,
		'auto'			: true,
		'onSelect'		: fileselected,
		'onComplete'	: fileuploaded,
		'onCancel'		: fileCancelled
	});
	
	function fileselected(e,q_id,file_obj) {
		$('#send_btn').attr({ 'disabled':'disabled', 'src':'http://ckvo.production.tectonic.net.au/assets/images/send-btn-whitebg-disabled.jpg' });
		
		// keep updating shadows for 2 seconds, 10 times per second
		var interval = setInterval(updateShadows, 100);
		setTimeout(function(){ clearInterval(interval); }, 2000);
		
		return true;
	}
	
	function fileCancelled(e,q_id,file_obj,data) {
		$('#send_btn').attr({ 'disabled':null, 'src':'http://ckvo.production.tectonic.net.au/assets/images/send-btn-whitebg.jpg' });
		
		// keep updating shadows for 2 seconds, 10 times per second
		var interval = setInterval(updateShadows, 100);
		setTimeout(function(){ clearInterval(interval); }, 2000);
		
		return true;
	}
	
	function fileuploaded(e,q_id,file_obj,response,data) {
		var url = response + ',';
		var orig_value = $('#attachments').val();
		$('#attachments').val( orig_value + url );
		
		$('#send_btn').attr({ 'disabled':null, 'src':'http://ckvo.production.tectonic.net.au/assets/images/send-btn-whitebg.jpg' });
		
		$('#scriptfileQueue .cancel').fadeOut(250);
		
		return false;
	}
	
	var errorCount = 0; // if the error fields is already showing, pulsate it to be more visible
	$('#requestquote_form').submit(function(){
		//console.log('submit');
		var valid = validateFields();
		
		if (valid == false) {
			errorCount++;
			if (errorCount >= 2) {
				$('#form-errors').fadeOut(50);
				$('#form-errors').fadeIn(100);
				$('#form-errors').fadeOut(50);
				$('#form-errors').fadeIn(100);
			} else {
				$('#form-errors').text('Please check the highlighted fields above.').fadeIn();
			}
		} else {
			$('#form-errors').fadeOut();
			sendEmail();
		}

		return false;
	});
}

function open_form() {
	$('#hidden-form').slideDown(500);
	
	// keep updating shadows for 2 seconds, 10 times per second
	var interval = setInterval(updateShadows, 100);
	setTimeout(function(){ clearInterval(interval); }, 2000);
}

function close_form() {
	$('#hidden-form').slideUp(500);
	
	// keep updating shadows for 2 seconds, 10 times per second
	var interval = setInterval(updateShadows, 100);
	setTimeout(function(){ clearInterval(interval); }, 2000);
}

function validateFields() {
	var valid = true;
	
	// trim name field
	$('#name').val( $.trim($('#name').val()) );
	if ( $('#name').val() == 'Name *' || $('#name').val().length < 3 ) {
		highlightField('#name', 'error');
		valid = false;
	} else {
		highlightField('#name');
	}
	
	$('#email').val( $.trim($('#email').val()) );
	if ( validateEmail($('#email').val()) == false ) {
		highlightField('#email', 'error');
		valid = false;
	} else {
		highlightField('#email');
	}
	
	$('#telephone').val( $.trim($('#telephone').val()) );
	if ( $('#telephone').val() == 'Telephone *' || $('#telephone').val().length < 3 ) {
		highlightField('#telephone', 'error');
		valid = false;
	} else {
		highlightField('#telephone');
	}
	
	// trim
	$('#_company').val( $.trim($('#_company').val()) );
	$('#comments').val( $.trim($('#comments').val()) );

	return valid;
}

function validateEmail(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(address) == false) {
		return false;
	}

	return true;
}

function sendEmail() {
	var data = new Object();
	data.send_btn = 'Send';
	data.name = $('#name').val();
	data.company = $('#_company').val();
	data.email = $('#email').val();
	data.telephone = $('#telephone').val();
	data.comments = $('#comments').val();
	data.attachments = $('#attachments').val();
	
	$.post("/requestquote-simple.php", data, function(res){
		if (res == 'success') {
			setTimeout(function(){ close_form(); }, 2000);
			$('.uploadifyQueueItem').hide();
			$('#form-message').show().addClass('success').html('Thank you - your request was sent successfully.');
			$('#send_btn').attr({ 'disabled':'disabled', 'src':'http://ckvo.production.tectonic.net.au/assets/images/send-btn-whitebg-disabled.jpg' });
		} else {
			$('#form-message').show().addClass('error').html('Your request was not sent, please try again.');
		}
	});
}

function highlightField(field_id, error_type) {
	switch (error_type) {
		case 'error':
			$(field_id).css('border-color','#FF9900');
			$(field_id).css('background-color','#FFFFFD');
			break;
		default:
		case null:
			$(field_id).css('border-color','#ABADB3');
			$(field_id).css('background-color','#FFFFFF');
			break;
	}
}
