jQuery(function($){
	//mega menu



	$('li.mega-menu').mouseenter(function() {
		$('.mega-menu-contents').hide();
		$(this).children('.mega-menu-contents').show();								  
	});	
	//popup shopping bag
	$('#btn_shopping_bag').mouseenter(function() {
		$	('#shopping-bag-popup').show();								  
	});
	$('#btn_shopping_bag').mouseleave(function() {
		$	('#shopping-bag-popup').hide();								  
	});

	$('.main-menu').mouseleave(function(event) {
		if ($(event.target).closest('.wrapper').length ) {
				$('.mega-menu-contents').hide();
		};

	});

	
	//$('.search-results #adj-nav-container .listing-item:nth-child(3n+1)').after('<div class="cf"></div>');
	
	
	// DROP DOWN STYLING

	$('select.select').each(function(){
		var title = $(this).attr('title');
		if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
		$(this)
			.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
			.after('<span class="select">' + title + '</span>')
			.change(function(){
			val = $('option:selected',this).text();
				$(this).siblings('span.select').text(val);
				})
	});


	$('.product-price-header h3, .bag-name-col, .basket-item h5, td.attributes-col h4').each(function()
	{

		
		
		var text = $(this).html();
		
		var hasDash = text.indexOf('-');

		if(hasDash < 0)
		{
			
		}

		else
		{
		
			var textParts = text.split('-');
			$(this).html(textParts[0] + "<br>" + "<span class='product-title-under'>" + textParts[1] + "</span>");	
		}
	
	});
	
	

	$('select').each(function () {
		if($(this).hasClass('error')) {
			$(this).siblings('span.select').addClass('error-select');
		}
	});
	
//Blog toggle
	//Hide (Collapse) the toggle containers on load
	$(".toggle").hide();

	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$(".trigger").click(function(){
		$(this).toggleClass("active-trigger").next().slideToggle("300")
		.siblings(".toggle:visible").slideUp("300");
		$(this).siblings(".trigger").removeClass("active-trigger");

	});

//image fader
	$('#image-rotation img:not(":first")').hide();
    var count=0;
    var countImages = $('#image-rotation img').size();

    if(countImages>1){
        setInterval(nextImage,4000); //The number here dictates the length of time between the animation
    }

    function nextImage() {
        $('#image-rotation img:eq('+count+')').fadeOut(1500); //Length of FadeOut transition
        if(count<countImages-1){
            count++;
        } else {
           	count=0;
        }
       $('#image-rotation img:eq('+count+')').fadeIn(1500); //Length of FadeIn transition
    }	


if ($(".cms-daisyblog").size() > 0) {

	var postImage = $('.post-box img.size-full, .post-box img.size-medium');
		$(postImage).each(function() {

			if ($(this).width() > 200 && $(this).height() > 150) {
					//console.log('dan is a bellend');
		    	$(this).addClass('post-image');

			}
		});

}

//Shop Categories Dropdown

var li = $('.left-navigation ul.level0 li');

$(li).each(function() {
	if ($(this).hasClass('active')) {
		$(this).children('ul').show();
	} else {
		$(this).children('ul').hide();
	}
});
$(li).mouseenter(function(){
	if (!$(this).hasClass('active')) {
		$(this).children('ul').stop(true, true).slideToggle("300");
	}
});
$(li).mouseleave(function(){
	if (!$(this).hasClass('active')) {
		$(this).children('ul').stop(true, true).slideToggle("300");
	}
});




//Lookbook
var sliderWidth = 0;
var articleWidth = $('#lookbook-inner .article:first').width();
var articleLeft = 0;



$('#lookbook-inner .article').each(function () {
    sliderWidth = sliderWidth + $(this).width();
});



$('#lookbook-inner').width(sliderWidth);

$('.lb-next').click(function (e) {
	e.preventDefault();

    if ($('#lookbook-inner .article:not(":animated")').length != 0) {

        if ($('#lookbook-inner .article').css('left').replace('px', '') > -(articleWidth * ($('#lookbook-inner .article').size() - 1))) {
            $('#lookbook-inner .article').animate({
                'left': '-=' + articleWidth
            })
        }
    }



});



$('.lb-prev').click(function (e) {

	e.preventDefault();

    if ($('#lookbook-inner .article:not(":animated")').length != 0) {

        if ($('#lookbook-inner .article').css('left').replace('px', '') != 0) {

            $('#lookbook-inner .article').animate({

                'left': '+=' + articleWidth

            })

        };

    }


});


var articleNavCounter = 1;
var articleCounter = 1;


$('#lookbook-inner .article').each(function () {

    $(this).attr('id', 'article-' + articleCounter);

    articleCounter++;

});

//fancybox - lookbook
$(".video").click(function() {
	$.fancybox({
		'padding'		: 0,
		'autoScale'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'title'			: this.title,
		'width'			: 640,
		'height'		: 385,
		'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
		'type'			: 'swf',
		'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			}
	});
	return false;
});

$('.close-delivery').click(function() {
	$('.delivery-block').hide();
})

//Add to bag Validation Message

var qtySelect = $('.add-to-cart-box .qty-box select');
var sizeSelect = $('.add-to-cart-box .product-options select');


$('.product-add-to-cart .form-button').click(function(e)
{
	if ($(qtySelect).val() == '' && $(sizeSelect).val() == '') {
		$('.validation-warning').text('Please select a Quantity and a Size');
		$('.validation-warning').fadeIn();
	}
	else if($(qtySelect).val() == '' && $(sizeSelect).val() != '')
	{
		$('.validation-warning').text('Please select a Quantity');
		$('.validation-warning').fadeIn();
	}
	else if($(qtySelect).val() != '' && $(sizeSelect).val() == '')
	{
		$('.validation-warning').text('Please select a Size');
		$('.validation-warning').fadeIn();
	}
	else
	{
		$('.validation-warning').hide();
	}
		
	e.preventDefault();
});
	
})
		
		
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
} 


