var vidcode; var searchActivated = false;

$(document).ready(function(){	


$(".hoverinfo_list li .tt_trigger").tooltip({ position: "bottom right", opacity: 1 }).dynamic({ bottom: { direction: 'up', bounce: true } });




	$(".vidpop").click(function(){ vidcode = $(this).find('span').attr('id'); loadVid(); });
	$(".popvidClose").click(function(){ disableVid(); });

	$("#newspop").click(function(){ loadPopup(); });
	$(".popformClose").click(function(){ disablePopup(); });
	$(".popformCloselink").click(function(){ disablePopup(); });

// VALIDATION CODE ////////////////

	$('.error').hide(); $('.newserror').hide(); //$('.searcherror').hide();
						   
	$('#contact').submit(function() {
		$('.error').hide();  
		var comfields = [ "firstname", "telephone", "email", "message" ];
		var strval; var err = false; var i = 1; 
	
		$.each(
			comfields,
			function( intIndex, objValue ){
				  
				strval = $("#" + objValue).val(); 
				
				if (strval == "") { 
					$('.error').show(); 
					$("input#" + objValue).prev("label").css('color', 'red');
					$("#" + objValue).prev("label").css('color', 'red');
					err = true;		
				} else {
					$("input#" + objValue).prev("label").css('color', 'gray');
					$("#" + objValue).prev("label").css('color', 'gray');
					if(objValue == "email"){ 
						if(!validateEmail(strval)){
							$('.error').show(); 
							$("input#" + objValue).prev("label").css('color', 'red');
							$("#" + objValue).prev("label").css('color', 'red');
							err = true;
						} else { 
							$("input#" + objValue).prev("label").css('color', 'gray');
							$("#" + objValue).prev("label").css('color', 'gray');
						}
					} else if(objValue == "telephone") {
						if(!validatePhone(strval)){
							$('.error').show(); 
							$("input#" + objValue).prev("label").css('color', 'red');
							$("#" + objValue).prev("label").css('color', 'red');
							err = true;
						} else { 
							$("input#" + objValue).prev("label").css('color', 'gray');
							$("#" + objValue).prev("label").css('color', 'gray');
						}
					}
				}
				i++;
		
			} 
		);
		
		if(err == false){
			return true;
		} else {
			return false;	
		}
		
	});
						   
	$('#searchForm').submit(function() {
		//$('.searcherror').hide();  
		var comfields = [ "keywords" ];
		var strval; var err = false; var i = 1; 
	
		$.each(
			comfields,
			function( intIndex, objValue ){
				  
				strval = $("#" + objValue).val(); 
				
				if (strval == "") { 
					//$('.searcherror').show(); 
					$("input#" + objValue).prev("label").css('color', 'red');
					$("input#" + objValue).focus();
					err = true;		
				} i++;
		
			} 
		);
		
		if(err == false){
			return true;
		} else {
			return false;	
		}
		
	});
	
	$('#newsletter').ajaxForm( { 
		target:"#hiddenDIV",
		beforeSubmit:function() {
		   $('.newserror').hide();  
			var comfields = [ "newsname", "newsemail" ];
			var strval; var err = false; var i = 1; 
		
			$.each(
				comfields,
				function( intIndex, objValue ){
					  
					strval = $("#" + objValue).val(); 
					
					if (strval == "") { 
						$('.newserror').show(); 
						$("input#" + objValue).prev("label").css('color', 'red');
						$("#" + objValue).prev("label").css('color', 'red');
						err = true;		
					} else {
						$("input#" + objValue).prev("label").css('color', 'gray');
						$("#" + objValue).prev("label").css('color', 'gray');
						if(objValue == "email"){ 
							if(!validateEmail(strval)){
								$('.newserror').show(); 
								$("input#" + objValue).prev("label").css('color', 'red');
								$("#" + objValue).prev("label").css('color', 'red');
								err = true;
							} else { 
								$("input#" + objValue).prev("label").css('color', 'gray');
								$("#" + objValue).prev("label").css('color', 'gray');
							}
						} else if(objValue == "telephone") {
							if(!validatePhone(strval)){
								$('.newserror').show(); 
								$("input#" + objValue).prev("label").css('color', 'red');
								$("#" + objValue).prev("label").css('color', 'red');
								err = true;
							} else { 
								$("input#" + objValue).prev("label").css('color', 'gray');
								$("#" + objValue).prev("label").css('color', 'gray');
							}
						}
					}
					i++;
			
				} 
			);
			
			if(err == false){
				return true;
			} else {
				return false;	
			}
		},
		success:function()  {
			$("#newsformcontent").html("<p>Thank you for signing up to the newsletter. <a href=\"#\ onclick=\disablePopup(); return false;\>Close window</a></p>");
			popupStatus = 1;
		}
	});
	
	$('.search-text').focus(function(){
		if(!searchActivated){
			//Reset the searchbox value
			$('.search-text').val('');
			searchActivated = true;
			
			//Change thecss
			$('.search-text').css('color','#333');
		}						  
	});
	
});

function validateEmail(theEmail) {
	var str = theEmail;
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	if (!reg1.test(str) && reg2.test(str)) {
		return true;
	}
}

function validatePhone(theNum) {
	var validchars = '0123456789+()- ';
	var charstovalidate = theNum.split('');
	for( var i=0; i < charstovalidate.length; i++){
		if (validchars.indexOf(charstovalidate[i]) == -1){ 
			return false;
		}
	}
	return true;
}

// POP UP CODE ////////////////

var popupStatus = 0;

function loadPopup(){  
	if(popupStatus==0){ 
		$("#popformBg").css('left', 319 + $("#wrapper").offset().left);
		$("#popformBg").css('top', 100 + $("#popformBg").offset().top);
		$("#popformBg").fadeIn("fast");  
		$("#popform").fadeIn("fast");  
		popupStatus = 1;  
	} 
	
} 

function disablePopup(){ 
	//disables popup only if it is enabled  
	if(popupStatus == 1){  
		$("#popformBg").fadeOut("fast");  
		$("#popform").fadeOut("fast");  
		popupStatus = 0;  
	}  
}

var vidStatus = 0;

function loadVid(){  
	if(vidStatus==0){ 
		$("#popvid .vid").html('<object type="application/x-shockwave-flash" style="width:580px; height:350px;" data="http://www.youtube.com/v/' + vidcode + '?rel=0"><param name="movie" value="http://www.youtube.com/v/' + vidcode + '?rel=0" /><param name="wmode" value="transparent" /><embed src="http://www.youtube.com/v/' + vidcode + '?rel=0" width="580" height="350" wmode="transparent"></embed></object>');
		$("#popvidBg").css('left', 160 + $("#wrapper").offset().left);
		$("#popvidBg").css('top', 100 + $("#popvidBg").offset().top);
		$("#popvidBg").fadeIn("fast");  
		$("#popvid").fadeIn("fast");  
		vidStatus = 1;  
	} 
	
} 

function disableVid(){ 
	//disables popup only if it is enabled  
	if(vidStatus == 1){  
		$("#popvidBg").fadeOut("fast");  
		$("#popvid").fadeOut("fast");  
		vidStatus = 0;  
	}  
}
