/***** Code for the slideshow ******/
function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

$(document).ready(function() {
	
	/***** This code applies effects to the sidebar navigation links and loads data using AJAX ******/
	$('.side-nav li').find('a').click(function(event) {
		
		if(!$(this).closest('li').is('.noajax')) {
		
			if($(this).next().is('ul')) {
				
				$(this).next('ul').toggle('slow');
				$url = $(this).attr('href')+' .main div';
				$('.main').load($url).hide().fadeIn('slow');
				$(this).closest('.side-nav').find('a').removeClass('active');
				$(this).toggleClass('active');
				
			} else {

				$url = $(this).attr('href')+' .main div';
				$('.main').load($url).hide().fadeIn('slow');
				$(this).closest('.side-nav').find('a').removeClass('active');
				$(this).toggleClass('active');
			
			}
			
			event.preventDefault();
		
		}
	
	});
	
	/***** Hover effect of top navigation links ******/
	$('#menu ul.nav li').not('.active').hover(function() {
		
		$(this).find('span').fadeIn(300).css('cursor', 'pointer') },
		
	function() {
	
		$(this).find('span').fadeOut(300);
		
	});
	
	/***** Dorpdown menu for the catalog top navigation link ******/
	$('#menu ul.nav li:eq(2)').mouseenter(function() {
	
		$(this).find('ul').slideDown(150);
	
	}).mouseleave(function() {
	
		$(this).find('ul').slideUp(300);
	
	});
	
	$('div.new-products').fadeIn('slow');
	$('span.close').css('cursor', 'pointer').click(function() {
	
		$('div.new-products').fadeOut('fast');
	
	});
	
	/***** Link lighbox to links inside the gallery list ******/
	$('.gallery a, .stone-gallery a, .new-products a, .products a, a.lightbox').lightBox();
	
	/***** Feedback box ******/
	// Store the html for the form in a variable:
	var html = $('div#feedback').html();
	
	// Toggle the box open or close:
	$('div#feedback img').toggle(function() {
		$('div#feedback').animate({right: '0'}, 300);
	}, function () {
		$('div#feedback').animate({right: '-363px'}, 300);
	});
	
	// Validate the form:
	var values = {
		success: 'Your feedback has been sent. Thank you for your input! This window will close in 5 seconds.',
		error: 'Your message could not be sent, please try again.'
	};
	
	$('div#feedback form').validate({
		rules: {
			feed_name: { required: true, minlength: 2 },
			feed_email: { required: true, email: true },
			feed_msg: "required"
		},
		
		messages: {
			feed_name: "Invalid",
			feed_email: "Invalid",
			feed_msg: "Invalid"
		},
		
		submitHandler: function() {
			$('span.loading').show();
			$.post('feedback.php', { feedSubject:'Feedback Form Entry', feedName:$("input[name='feed_name']").val(), feedEmail:$("input[name='feed_email']").val(), feedMessage:$("textarea[name='feed_msg']").val() }, function(data) {
				if (data == 'success') {
					$('div#feedback div').html('<h5>'+values.success+'</h5>').fadeIn('slow');
					$('div#feedback').delay(6000).fadeOut(300);
					$('span.loading').hide();
				} else {
					$('div#feedback div').html('<h5>'+values.error+'</h5><br/><a href="index.php">Reset</a>').fadeIn('slow');
					$('span.loading').hide();
				}
			});
		}
	});
	
	/***** Newsletter Subscription ******/
	$('form#newsletter').validate({
		rules: {
			newsletter_name: { required: true, minlength: 2 },
			newsletter_email: { required: true, email: true }
		},
		
		messages: {
			newsletter_name: "Invalid",
			newsletter_email: "Invalid"
		},
		
		submitHandler: function() {
			$('p.news_error, p.news_success').hide();
			$.post('newsletter.php', { newsletterName:$("input[name='newsletter_name']").val(), newsletterEmail:$("input[name='newsletter_email']").val() }, function(data) {
				if (data == 'success') {
					$('<p class="news_error" style="border: 1px solid green; background-color: #154400; color: #fff; padding: 5px;">You are now subscribed to our newsletter and will receive notification about new arrivals and promtional deals.</p>').insertAfter('form#newsletter').hide().fadeIn('slow');
				} else {
					$('<p class="news_success" style="border: 1px solid red; background-color: #800000; color: #fff; padding: 5px;">'+data+'</p>').insertAfter('form#newsletter').hide().fadeIn('slow');
				}
			});
		}
	});
	
});

