$(document).ready(function(){

	// facebook
	
	(function(d, s, id) {
  	var js, fjs = d.getElementsByTagName(s)[0];
  	if (d.getElementById(id)) {return;}
  	js = d.createElement(s); js.id = id;
  	js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  	fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));

	// form placeholders
	
	$('input.text').each(function(){
		$(this).val($(this).attr('title'));
	});
	
	$('input.text').click(function(){
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('input.text').focusout(function(){
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});

	// header and nav
		
	$('a.login').toggle(function(){
		$('#login').slideDown('fast');
		$(this).addClass('active');
	},function(){
		$('#login').slideUp('fast');
		$(this).removeClass('active');
	});
	
	$('a.cart').toggle(function(){
		$('#cart').slideDown('fast');
		$(this).addClass('active');
	},function(){
		$('#cart').slideUp('fast');
		$(this).removeClass('active');
	});

	$('#menu-main-nav>li').mouseenter(function(){
		$(this).addClass('active');
		$(this).children('ul.sub-menu').css('width', $(this).width());
		$(this).children('ul.sub-menu').slideDown('fast');
	});
	
	$('li.active').live('mouseleave', function(){
		$(this).children('ul.sub-menu').slideUp('fast');
		$(this).removeClass('active');
	});
	
	// homepage news widget 
	
	var npos = $('#latestnews').scrollTop();
	var refreshId = setInterval(function() {
		 npos = npos + 100;
		 $('#latestnews').animate({
		 	scrollTop: npos
		 }, 300);
		 if (npos > ($('#latestnews div.post').length*100)) {
		 	npos = 0;
		 } 
	}, 7500);
	
	$('a.nnext').click(function(e){
		e.preventDefault();
		npos = npos + 100;
		$('#latestnews').animate({
		   scrollTop: npos
		}, 300);
		if (npos > ($('#latestnews div.post').length*100)) {
		   npos = 0;
		} 
	});
	
	$('a.nprev').click(function(e){
		e.preventDefault();
		npos = npos - 100;
		$('#latestnews').animate({
		   scrollTop: npos
		}, 300);
		if (npos > ($('#latestnews div.post').length*100)) {
		   npos = 0;
		} 
	});
	
	// image carousels
	
	$('#slides.home').slides({
		preload: true,
		preloadImage: '/wp-content/themes/epionce/images/loading.gif',
		effect: 'fade',
		crossfade: true,
		play: 7000,
		pause: 7000,
		hoverPause: true,
		pagination: true,
		generatePagination: true
	});
	
	// fancybox
	
	$("a.fancyframe").fancybox({
		'width'				: 575,
		'height'			: 795,
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'titlePosition' 	: 'inside',
		'overlayColor'		: '#d7d2cb',
		'overlayOpacity'	: 0.9
	});
	
	$("a.fancyhow").fancybox({
		'width'				: 820,
		'height'			: 620,
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'titlePosition' 	: 'inside',
		'overlayColor'		: '#d7d2cb',
		'overlayOpacity'	: 0.9
	});
	
	$("a.fancybox").fancybox();
	
	// product thumbnail cascade in
	
	boxes = $("div.thumbnail");
	var z = 0;
	var refreshId = setInterval(function() {
		 boxes.eq(z).animate({ opacity:1 },300);
		 z++;
		 if (z > boxes.length) {
		 	clearInterval(refreshId);
		 }
	}, 50);
		
	// form validation
	
	$("#contactform").validate();
	
	// date filtered content 
	
	$('a.changeYear').click(function(){
		var targ = $(this).attr('title');
		$('.date-filtered-result').hide();
		$('.'+targ).show();
		$('a.current').removeClass('current');
		$(this).addClass('current');
	});
	
});
