function testEmail(value)
{
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

function testURL(value)
{
	return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}

function testDigits(value)
{
	return /^\d+$/.test(value);
}

function testSecurity(value)
{
	return ((value==3)||(value=='three')||(value=='THREE'));
}

function AwesomeSlider(selector,xml)
{

	function Slide2Next(selector,count,content,items,slideID)
	{	
		if(slideID>0) count = slideID;
		else if(count<items) count++;
		else count = 1; // reached the last slide, get back to first

		$(selector+' .splash-content').animate({opacity:0},1000, function(){
			// faded out, changing content
			$(selector+ ' .splash-content').html(content[count]);
			// fancy box 
			//$("a.fancy").fancybox({
			//	'overlayOpacity' : 0.8,
			//	'overlayColor' : '#333333'
			//});

			// animating
			$(selector+' .splash-content').animate({height:'toggle'},1000, function(){
				// change style of controls
				$(selector+' .splash-controls a.selected').removeAttr("class");
				$(selector+' .splash-controls a[rel='+count+']').attr("class","selected");
				// fading back in
				$(selector+' .splash-content').animate({height: 'toggle'}, 1000, function(){
					$(selector+' .splash-content').animate({opacity:1},1000);
				});
			});
		});
	}
	
	// slider
	var items = 0;
	var content = new Array(10);
	var control = '';
	var count = 1;
	
	$.get(xml, function(data)
	{ // get contents from xml

		$(data).find('slide').each(function()
		{
			// populate array
			items++;
			var $slide = $(this); 
			content[items] = $slide.find('content').text();
			// create controls
			if(items==1) control = control + '<a href="#" class="selected" rel="'+items+'">'+items+'</a>';
			else control = control + '<a href="#" rel="'+items+'">'+items+'</a>';
		});
		
		$(selector+' .splash-content').html(content[count]);
		
		// fancy box 
		//$("a.fancy").fancybox({
		//	'overlayOpacity' : 0.8,
		//	'overlayColor' : '#333333'
		//});
		
		// add controls
		
		$(selector+' .splash-controls').html(control);
	});
	
	// control is clicked
	$(selector+' .splash-controls a').live('click',function (){
		var slideID = $(this).attr('rel');
		Slide2Next(selector,count,content,items,slideID);
		//clearInterval(slideInterval);
		//slideInterval = setInterval("Slide2Next(selector,count,content,items)", 10000 );
		return false;
	});
	
 	//var slideInterval = setInterval("Slide2Next(selector,count,content,items)", 10000 );
}

jQuery(document).ready(function($)
{
	// clear the clear
	$('.clear').height(0);
		
	//cufon things - font replacement on inline pages
	Cufon.replace('#header > ul > li a.replace', { fontFamily: 'Museo 500' });
	Cufon.replace('#header > ul > li a.replace > span', { fontFamily: 'Museo 300' });
	Cufon.replace('h2, h3', { fontFamily: 'Sansation' });
	
	//cufon things - font replacement on homepage
	Cufon.replace('#header-content p.blockquote, #menu > li a.replace > span, .contact-hp a', { fontFamily: 'Museo 300' });
	Cufon.replace('#header-content p.blockquote strong, #menu > li a.replace, .contact-hp .tel', { fontFamily: 'Museo 500' });
	
	// homepage behaviours
	$('#header-content p a, #contactform input.button').hover(
		function(){$('#header-content p span, #contactform span.arrow').animate({left: '+=5'});},
		function(){$('#header-content p span, #contactform span.arrow').animate({left: '-=5'});}
		);
	
	Cufon.now();
	
	// contact page bahaviours
	$('textarea').elastic();
	$('textarea').height(50);
	$('<small>This field will expand as you type</small>').insertAfter('textarea');
	
	// check for errors on submit
	$('#contactform').live('submit',function(){
		$('#contactform input.button').attr('disabled','disabled');
		$('#contactform input.button').val('Processing request');
		errors = 0;
		$('#contactform span.error').remove();
		$.each($('input[rel=error],textarea[rel=error],select[rel=error]'), function(){
			errors++; // count if there are errors
				error = 0; 
				inputText = '';
				inputText = $(this).val(); // this works for inputs
				if(inputText.length==0) inputText = $(this).text(); // this works for textareas

				classes = $(this).attr('class');
				classesArray = classes.split(' ');
				$.each(classesArray, function(index, value){
					if((value=='v-required')&&(inputText.length==0)&&(error==0))
					{// required field empty
						error = 1;
					}
					if((value=='v-digits')&&(testDigits(inputText)==false)&&(error==0))
					{ // field needs to be digits only
						error = 2;
					}
					if((value=='v-email')&&(testEmail(inputText)==false)&&(error==0))
					{ // field needs to be email format
						error = 3;
					}
					if((value=='v-url')&&(testURL(inputText)==false)&&(error==0))
					{ // field needs to be URL format
						error = 4;
					}
					if((value=='v-security')&&(testSecurity(inputText)==false)&&(error==0))
					{ // field needs to be URL format
						error = 5;
					}
				});

				if(error>0)
				{// there's some errors
					if(error==1) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">This is a required field</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==2) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Digits only please</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==3) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Invalid email</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==4) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Invalid URL</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==5) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Incorrect answer</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Unknown error</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
				}
				else
				{
					$(this).next('span.error').remove();
					$(this).next('span.ok').remove();
					$('<span class="ok">Ok</span>').insertAfter($(this));
					$(this).removeAttr('rel');
				}
		});
		
		if(errors==0) return true;
		else 
		{
			$('#contactform input.button').removeAttr('disabled');
			$('#contactform input.button').val('Correct errors and resend');
			return false; 
		}
	});
	
	// add error to required fields
	$('#contactform input.v-required,#contactform textarea.v-required').attr('rel','error');
	
	$('#contactform input:not(.button), #contactform textarea, #contactform select').focusout(function(){
		error = 0; // 1 for required,
		inputText = '';
		inputText = $(this).val(); // this works for inputs
		if(inputText.length==0) inputText = $(this).text(); // this works for textareas

		classes = $(this).attr('class');
		classesArray = classes.split(' ');
		$.each(classesArray, function(index, value){
			if((value=='v-required')&&(inputText.length==0)&&(error==0))
			{// required field empty
				error = 1;
			}
			if((value=='v-digits')&&(testDigits(inputText)==false)&&(error==0))
			{ // field needs to be digits only
				error = 2;
			}
			if((value=='v-email')&&(testEmail(inputText)==false)&&(error==0))
			{ // field needs to be email format
				error = 3;
			}
			if((value=='v-url')&&(testURL(inputText)==false)&&(error==0))
			{ // field needs to be URL format
				error = 4;
			}
			if((value=='v-security')&&(testSecurity(inputText)==false)&&(error==0))
			{ // field needs to be URL format
				error = 5;
			}
		});

		if(error>0)
		{// there's some errors
			if(error==1) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">This is a required field</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==2) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Digits only please</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==3) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Invalid email</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==4) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Invalid URL</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==5) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Incorrect answer</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Unknown error</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
		}
		else
		{
			$(this).next('span.error').remove();
			$(this).next('span.ok').remove();
			$('<span class="ok">Ok</span>').insertAfter($(this));
			$(this).removeAttr('rel');
		}
	});

	/*$('#contactform textarea').focusin(function(){
		$(this).next().animate({opacity:0},function(){$(this).animate({opacity:1},function(){$(this).animate({opacity:0},function(){$(this).animate({opacity:1},function(){$(this).delay(2500).animate({opacity:0,height:0})})})})});
	});*/
	
	// clients page functionality
	$('.description').text('Click services on the left to see clients that bought that service. Click a client on the right to see their acquired services.');
	
	$('#services a').click(function(){
		
		$('.rmbidc').remove();
		$('<a href="request-quote.php" class="button all-rounded right rmbidc">Let\'s talk about you.</a><span class="clear rmbidc">&nbsp;</span>').insertBefore('.s580by12 h2');
		
		$('.selected').attr('class','');
		// when we click one service we need to emphasize that it is selected
		$(this).attr('class','selected'); // set current link as selected	
		
		// get the services classes
		var classesString  = $(this).parent().attr('class');
		var classes = classesString.split(' ');
		// loop through all classes
		for(var i=0;i<classes.length;i++)
		{
			// set selected to all clients that have such a service
			$('#clients li.'+classes[i]+' a').attr('class','selected');
		}
	});
		
	// if clients on the right are clicked
	$('#clients li a').click(function(){
		$('.selected').attr('class','');
		// when we click one service we need to emphasize that it is selected
		$(this).attr('class','selected'); // set current link as selected
		
		// get the services classes
		var classesString  = $(this).parent().attr('class');
		var classes = classesString.split(' ');
		// loop through all classes
		for(var i=0;i<classes.length;i++)
		{
			// set selected to all clients that have such a service
			$('#services li.'+classes[i]+' a').attr('class','selected');
		}
	});
	
	// animate clients div
	var $scrollingDiv = $(".cli");	
	maxH = $('#serv').height();
	$(window).scroll(function() {
		
		var windowHeight = $(window).height();  
		var contentHeight = $('.cli').height(); // and height of content area
		//Set height and width to mask to fill up the whole screen  
		top = (windowHeight-contentHeight)/2;
		if(($(window).scrollTop()-top>0)&&($(window).scrollTop()+contentHeight<maxH))
		{
			$scrollingDiv
				.stop()
				.animate({"marginTop": ($(window).scrollTop())-top + "px"}, "fast" );	
		}
		else if($(window).scrollTop()+contentHeight>maxH)
		{
			$scrollingDiv
				.stop()
				.animate({"marginTop": maxH-contentHeight + "px"}, "fast" );
		}
		else
		{
			$scrollingDiv
				.stop()
				.animate({"marginTop": 0 + "px"}, "fast" );
		}
	});

	AwesomeSlider('#prefcon_slide','xml/prefcon.xml');
	AwesomeSlider('#weedbot_slide','xml/weedbot.xml');
	AwesomeSlider('#rebrisoreana_slide','xml/rebrisoreana.xml');
	AwesomeSlider('#niki_slide','xml/evenimente_by_niki.xml');
	AwesomeSlider('#almet_slide','xml/almet.xml');
	AwesomeSlider('#promotie_slide','xml/promotie.xml');
	
});
