//Do stuff when the DOM is dressed and ready!
$(function(){
	$('#banner').rotatingSlides({
		targetElms: 'li',
		navigation: true,
		Height: '250px',
		fadeToSlide: true
	});
	
	$('#quote-box-text').rotatingSlides({
		targetElms: 'div',
		fadeToSlide: false
	});
	
	headingContent = $('#showQuoteForm').html();
	$('#showQuoteForm').toggle(
		function(){
			$('#quote-form').slideDown(500, function(){
				$('html,body').animate({scrollTop: $(".feature-area:first").offset().top},'slow', function(){
					$('#quoteBtn').fadeIn(500, function(){
						$('#showQuoteForm').html("Enter your details below then click 'submit your request'...");
					});
				});
			});
		}, function(){
			$('#quoteBtn').fadeOut();
			$('#quote-form').slideUp();
			$(this).html(headingContent);
		});
	
		/*var count = $('.copy').size(),
			randomNum = Math.floor(Math.random()*count);
		$('div#quote-box-text div.copy:eq(' + randomNum + ')').slideDown(500, function(){
			$('.quote').fadeIn();
		});*/
	
});



//SIMPLE LIST ROTATOR
$.fn.rotatingSlides = function(options){
	
	var opts = {
		targetElms: 'li', //The HTML elements we will animate
		Height: 'auto', //The height to set the container of the target elements to avoid overflow
		fadeToSlide: true, //true = fade visible slide to next slide, false = fade visible slide into nothing
		duration: 5000, //How long each slide should be display for before fading
		speed: 1000, //How long each slide should be display for before fading
		navigation: false //How long each slide should be display for before fading
	}
	
	//call in the default options/vars
	var options = $.extend(opts, options),
	me = $(this),
	parent = me.find('.parent').parent(),
	elems = me.find(opts.targetElms+'.parent'), //Only target the '.parent' elements incase we decide to add any conflicting HTML inside them
	navLink,
	index = 0,
	interval,
        
        interval_function = function() {
			if(opts.fadeToSlide == false){
				elems.eq(index).removeClass('active').fadeOut(opts.speed, function(){
				index = (index + 1) % elems.length;
				elems.eq(index).fadeIn(opts.speed).addClass('active');
				});
			}else{
				elems.eq(index).removeClass('active').fadeOut(opts.speed);
				index = (index + 1) % elems.length;
				elems.eq(index).fadeIn(opts.speed).addClass('active');
			}
			if(opts.navigation){
				setActiveLink();
			}
        };
		
		function setActiveLink(){
			var activeSlide = me.find('.parent.active').index();
			navLink.removeClass('active');
			me.find('.slide-link:eq('+activeSlide+')').addClass('active');
		}
		
	if(opts.navigation == true){
		me.prepend('<ul class="slide-nav">');
		for(var i=1; elems.length >i-1; i++){
			me.find('.slide-nav').append('<li><a class="slide-link" href="#" rel="'+i+'">'+i+'</a></li>');
		}
	} navLink = me.find('.slide-link');
	
	me.css({'height':opts.Height,'overflow':'hidden', 'position': 'relative'});
	elems.css({'height':opts.Height, 'position': 'absolute'});

	navLink.click(function(e){
		e.preventDefault();
		navLink.removeClass('active');
		$(this).addClass('active');
		
		var activeLink = $(this).attr('rel') -1;
		if(opts.fadeToSlide == false){
			me.find(opts.targetElms+'.parent.active').removeClass('active').stop(false, true).fadeOut(opts.speed, function(){
			me.find(opts.targetElms+'.parent:eq('+activeLink+')').fadeIn(opts.speed).addClass('active');
			setActiveLink();
			});
		}else{
			me.find(opts.targetElms+'.parent.active').stop(false, true).fadeOut(opts.speed).removeClass('active');
			me.find(opts.targetElms+'.parent:eq('+activeLink+')').fadeIn(opts.speed).addClass('active');
			setActiveLink();
		}
	});
	
	elems.hide();
    elems.eq(0).show().addClass('active');
    navLink.eq(0).addClass('active');
    interval = setInterval(interval_function, opts.duration);

    me.hover(function () {
        clearInterval(interval);
    }, function () {
        interval = setInterval(interval_function, opts.duration);
    });
}
//END SIMPLE LIST ROTATOR


function quoteFormSubmit() {

	var name = $("#name").val(),
		email = $("#email").val(),
		telephone = $("#telephone").val(),
		message = $("#message").val(),
		str = $('#quote-form').serialize();
	
	function checkMail(email){
		var x = email;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (filter.test(x)){
			return true;
		}else{
			return false;
		}
	}
	
	if (name=="Name"){
		$('#name').addClass("req");
		$(this).val('Please enter your name')
	}else if (telephone=="telephone" || isNaN(telephone)){
		$('#telephone').addClass("req");
		$(this).val('Please enter a phone number')
	}else if (checkMail(email)==false || email=="Email" ){
		$('#email').addClass("req");
		$(this).val('Please enter a valid email')
	}else if (message=="Your message"){
		$('#message').addClass("req");
		$(this).val('Please enter a message')
	}else{
		$('#quote-form .req').removeClass('req');

			$.ajax({
				type: "POST",
				url: '_includes/form-action.php?action=quote',
				data: str,
				success: function(){
					$("#quote-form").ajaxComplete(function(event, request, settings){
						$(this).slideUp().fadeOut(500, function(){
						result = '<h3>Thank you '+name+'. Your request has been received.</h3><p>We will contact you accordingly.<br/>In the meantime, why not have a look at our <a href="portfolio.php">portfolio</a>';
						$(this).html(result);
						});
						$(this).fadeIn().slideDown();
						$('html,body').animate({scrollTop: $(".feature-area:first").offset().top},'slow');
					});
					
				}
			});
	}
}

function wishlistAdd(rowID, e){
	e.preventDefault;
	$.ajax({
		type: "POST",
		url: 'wishlist.php?p1=add&p='+rowID,
		success: function(){
			$("#wishlistConf").ajaxComplete(function(event, request, settings){
			});
		}
	});
}

