///////////////////////
/////// GENERAL ///////
///////////////////////


// for testing & debugging
function trace(content, add) {
	if (add) {
		$('#tdp')[0].innerHTML += content;
	} else {
		$('#tdp').html(content);
	}
}


// kick start
$(document).ready(function(){

	// grid overlay
	$('#tdp').click(function(){
		$("#grid").toggle();
	});
	
	// slider arrows
	slider = new sliderObj('slider');
	$('#arrowLeft').click(function(){
		slider.left(this, slider);
	});
	$('#arrowRight').click(function(){
		slider.right(this, slider);
	});
	$("#sliderWrapper1").hover(
		function(){
			clearInterval(slider.iv);
			$("#arrowLeft, #arrowRight").animate({
				opacity: 1
			}, {
				duration:200,
				queue: false
			});
		},
		function(){
			slider.iv = setInterval(slider.right, 8000);
			$("#arrowLeft, #arrowRight").animate({
				opacity: 0
			}, {
				duration:200,
				queue: false
			});
		}
	);
	slider.iv = setInterval(slider.right, 8000);
	
	// cufon
	Cufon.replace('h1');
	$(function() {
		$("#date").datepicker();
	});
	
});


//////////////////////
/////// SLIDER ///////
//////////////////////

function sliderObj(div) {
	this.left = function() {
		alert('left');
	}
	this.right = function(btnId, obj) {
		$(btnId).unbind('click');
		$('#'+div).animate({
		  marginLeft: '-=952'
		}, {
			duration: 700,
			easing: "easeInOutCubic",
			queue: false,
			complete: function() {
				// alert(parseFloat($(this).css('marginLeft')));
				if (parseFloat($(this).css('marginLeft')) <= -4760) {
					$(this).css('marginLeft',-952);
				}
				$(btnId).click(function(){
					obj.right(this, obj);
				})
			}
		});
	}
	this.left = function(btnId, obj) {
		$(btnId).unbind('click');
		$('#'+div).animate({
		  marginLeft: '+=952'
		}, {
			duration: 700,
			easing: "easeInOutCubic",
			queue: false,
			complete: function() {
				if (parseFloat($(this).css('marginLeft')) >= 0) {
					$(this).css('marginLeft',-3808)
				}
				$(btnId).click(function(){
					obj.left(this, obj);
				})
			}
		});
	}
}



//////////////////////////
/////// EMAIL FORM ///////
//////////////////////////

function validateEmail(field) {
	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos < 1 || dotpos - apos < 2) {
			return false;
		} else {
			return true;
		}
	}
}

function validateForm(thisForm, inputArray, defaultsArray) {
	// defaults array containd pre-filled values to check with, but those are left blank in this form
	var status = true;
	with (thisForm) {
		for (var i in inputArray) {
			var node = inputArray[i];
			// is not empty / default value / unvalid email?
			var error = node.value == '' || (defaultsArray && node.value == defaultsArray[i]) || (inputArray[i].name =='email' && !validateEmail(node));
			// mark if error
			if (error) {
				node.className = 'ipText mandatory';
				if (node.name == 'email') {
					$('#formError').html('Oops. Seems like you provided an invalid email address.').css('display','block');
				} else {
					$('#formError').html('Oops. Seems like you forgot some information.').css('display','block');
				}
			} else {
				node.className = 'ipText';
			}
			// focus on first node
			if (error && status) {
				status = false;
				node.focus();
			}
		}
		//if (status) { swapForm(); }
		// this is done from iFrame after captcha confirm
		return status;
	}
}

function swapForm() {
  // gets called from iframe processing form
	$('#form', window.parent.document).toggle();
	$('#thankYouForm', window.parent.document).toggle();
}

function markField(field) {
	$(field).addClass('fieldHighlight');
}
function unmarkField(field) {
	$(field).removeClass('fieldHighlight');
}
