//default to 'large' for Ie8. Ie8 = ie h8s media queries :(
var media = 'large';

//start tablet view/end desktop view:
var mqmedium = 1200;
//start smaller tablet view:
var mqsmall = 780;
//start largest mobile view:
var mqxsmall = 600;
//medium mobile view:
var mqxxsmall = 420;
//smallest mobile view:
var mqxxxsmall = 240;

function mediaCheck(){
	if (Modernizr.mq('(min-width: '+(mqmedium+1)+'px)')) {
		//large/desktop
		media = 'large';
	}else{
		if (Modernizr.mq('(min-width: '+(mqsmall+1)+'px) and (max-width: '+mqmedium+'px)')) {
			//medium/tablet
			media = 'medium';
		}else{
			if (Modernizr.mq('(min-width: '+(mqxsmall+1)+'px) and (max-width: '+(mqsmall)+'px)')) {
				//small/tablet
				media = 'small';
			}else{
				if (Modernizr.mq('(min-width: '+(mqxxsmall+1)+'px) and (max-width: '+mqxsmall+'px)')) {
					//xsmall/mobile
					media = 'xsmall';
				}else{
					if (Modernizr.mq('(min-width: '+(mqxxxsmall+1)+'px) and (max-width: '+mqxxsmall+'px)')) {
						//xxsmall/mobile
						media = 'xxsmall';
					}
					else{
						if (Modernizr.mq('(min-width: 0px) and (max-width: '+mqxxxsmall+'px)')) {
							//xxxsmall/mobile
							media = 'xxxsmall';
						}
					}
				}
			}
		}

	}
}

//responsive background images
function resppic(){
	$('.resppic').each(function(){
		var newpic = '';
		if(media == 'large' || media == 'medium'){
			newpic = $(this).attr('data-dtpic');
		}else{
			if(media == 'small'||media == 'xsmall'){
				newpic = $(this).attr('data-tabpic');
			}else{
				if(media == 'xxsmall'||media == 'xxxsmall'){
					newpic = $(this).attr('data-mobpic');
				}
			}
		}
		if(newpic!==""){
			if($(this).hasClass('respbg')){
				$(this).css("background-image","url("+newpic+")");
			}else{
				$(this).attr("src",newpic);
			}

		}
	});
}

function toggleActv(selector, target){
	$(''+selector+'').on('click',function(){
		$(''+target+'').toggleClass('active');
	});
}


//checkout delivery options toggle
$('.opt').on('click',function(){
	$('.opt').removeClass('active');
	$(this).addClass('active');
	var dtype = $(this).attr('data-type');
	$('.togopt').removeClass('active');
	$('.togopt[data-type="'+dtype+'"]').addClass('active');
	$("#delmethod").val(dtype);
});



$( window ).resize( function(e){
	mediaCheck();
	resppic();
});

mediaCheck();
resppic();

//main menu subnav
$('.mainnav li a').mouseenter(function() {

	i = $(this).data('parent');

	$('.subnav-wrap').addClass('hidden');

	if ($('.subnav-wrap[data-parent=' + i + ']').length == 1) {

		$('.subnav-wrap[data-parent=' + i + ']').removeClass('hidden');

	} else {
		$('.subnav-wrap').addClass('hidden');
	}

});

$('.subnav-wrap').mouseleave(function() {
	$('.subnav-wrap').addClass('hidden');
});

//mobile menu toggle open
$('.togglenav').on('click',function(){
	if(!$('#wrapper, html').hasClass('active')){
		$('#wrapper, html').addClass('active');
		var scrollby = $('.mobmenu').outerWidth();
		$('html, body').animate({scrollLeft: scrollby}, 400);
	}else{
		var scrollby = $('.mobmenu').outerWidth();
		$('html, body').animate({scrollLeft: -+scrollby}, 400,function(){
			$('#wrapper, html').removeClass('active');
		});
	}
});

$(".lihead").on('click', function(){
	$(this).next().toggleClass("hidden");
});

//toggleActv('.carttoggle', '.carttoggle');

$('.carttoggle').mouseenter(function(){
	$(this).addClass('active');
});

$('.carttoggle').mouseleave(function(){
	$(this).removeClass('active');
});



//mobile footer toggle
$('.footsect h3').on('click',function(){
	$(this).parent('.footsect').toggleClass('active');
});

$('.filteritem .itemname').on('click',function(){
	$(this).toggleClass("orange");
	$(this).next().toggleClass("active");
	$(this).parent('.itemname').toggleClass('orange');
	$('.filteritem .itemname').not(this).next().removeClass("active");
	$('.filteritem .itemname').not(this).removeClass("orange");

});

if ($(".filterlist>li").hasClass("active")) {
  $(this).hasClass("orange");
}

// $(this).parent('.filteritem').toggleClass('active');


	// var thisid = $(this).parent('.filteritem').attr('data-id');
	// $('.itemcontent[data-id="'+thisid+'"]').toggleClass('active');
	// if('.itemcontent[data-id]' != thisid){
	// 	$(".itemcontent").removeClass('active');
	// }

//$('.itemcontent li').on('click',function(){
//	$(this).toggleClass('active');
//});

//add to cart popup
$(document).on( "click", ".minictrl", function() {
	if($(this).hasClass('mightlike-right')){
		var nextitem = $('ul.mightlike-list li.listitem.active').next('.listitem');
	}else{
		var nextitem = $('ul.mightlike-list li.listitem.active').prev('.listitem');
	}
	if(!nextitem.length){
		if($(this).hasClass('mightlike-right')){
			var nextitem = $('ul.mightlike-list li.listitem').first();
		}else{
			var nextitem = $('ul.mightlike-list li.listitem').last();
		}
	}
	nextitem.addClass('next');
	$('ul.mightlike-list li.listitem').removeClass('active');
	nextitem.addClass('active');
	nextitem.removeClass('next');
});



$(".mailformwrap form").submit(function(e) {

	e.preventDefault();
	var _this = this;
	$.post($(this).attr('action'),$(this).serializeArray(),function(po_response){
		$('input[name=\'ps_email\']',_this).val(po_response.message);
	},'jSON');

});

function safelog(s) {
	if (window.console && console.log) {
		console.log(s);
	}
}

//read more product page stuff
$('.readmorelink').on('click',function(){
	$(this).toggleClass('active');
	$(this).siblings('.readmorelink').toggleClass('active');
	$(this).siblings('.extras').toggleClass('active');
	$(this).parent().siblings('.extras').toggleClass('active');
});

$('.descr a').on('click',function(e){
	e.preventDefault();
	$('.readmore').toggleClass('active');
	$('.descr a').toggleClass('hidden');
});


$('.prodthumbs li').click(function() {

	$('#mainimg').attr('src',$(this).data('bigpic'));
	$('.prodthumbs li').removeClass('active').filter(this).addClass('active');
});


$('.faqquestion').on('click',function(){
	$(this).parent('.faqitem').toggleClass('active');
});

$('.togglefs h2').on('click',function(){
	$(this).parent('.togglefs').toggleClass('active');
});

$("a.magnific").magnificPopup({
	type: 'ajax'
});


$(document).on('click','.add-to-cart',function() {

	q = $(this).parent().find('.quant').val() || 1;

	$.getJSON('/cart',{a:'add','p':$(this).data('productid'),'q':q,'pb_skiptemplate':1}).then(function(lo_data) {

		if (lo_data.error != '') {

			alert('error' + lo_data.error);

		} else {

			//$('.dd.topnav').html(lo_data.content);
			//$('.cartcount').html(lo_data.itemcount);
			//$('.carttotal').html(lo_data.carttotal);
			$("#popupcontent").html(lo_data.cartcontent);
			$(".minicart-content").html(lo_data.cartdropdown);
			$.magnificPopup.open({
				closeBtnInside:true,
				items: {
					src  : '#popupcontent',
					type : 'inline'
				}
			});


		}
	});
});
$(document).on('click','.pop-me',function(e) {
	e.preventDefault();
	$.magnificPopup.open({
		items : {
			src : $(this).attr('href'),
			type: 'ajax'
		}
	});
});

var callbacks = {

	toggleloginlinks : function (po_json) {

		$('.loginpop .ajax-form').slideUp();

		$(".loginlink").hide();
		$("#custname").html(po_json.firstname);
		$(".hidden.memberlink").removeClass('hidden');

		window.setTimeout(closemagnific,1000);
	},

	reviewsaved   : function(po_json) {
		$('.reviewpop .ajax-form').slideUp();
	}
}

function closemagnific()
{
	$.magnificPopup.close();
}

$(document).on('submit','.ajax-form',function(e) {

	e.preventDefault();

	var opts = $(this).data('options');

	$.post($(this).attr('action'),$(this).serializeArray(),function(po_json) {

		if (po_json.response) {
			$("#response").html(po_json.response);
		}

		if (po_json.success == 1 && typeof callbacks[opts.onsuccess] != 'undefined') {

			callbacks[opts.onsuccess](po_json);
		}

	},'jSON');


});
$(document).on('click','.popclosebtn',function(e) {
	e.preventDefault();
	$.magnificPopup.close();
	return false;
});


$('.storeselect').change(function() {

	id = $(this).val();
	$('.storeinfo').hide().filter('[data-storeid=' + id + ']').show();

	$.post('/your-details?a=setstoreid',{storeid:id},function(ps_html) {

		$("#cart-summary-container").html(ps_html);

	});

});


$("#membermobnav").change(function() {
	v = $(this).val();
	if (v) {
		window.location = '/' + v;
	}

});

$(function() {
	var autosearch = (function() {

		var timeout = null;
		var busy    = false;

		return {

			'init' : function() {

				$(".autosearch").keyup(function() {

					if (!busy && $(".autosearch:focus").val().length > 2) {

						window.clearTimeout(timeout);
						timeout = window.setTimeout(autosearch.getresults,300);
					}

					return true;

				});

				//hide when clicked outside of
				$('#wrapper').click(function() {
					$('.search-quickfind').hide('fast');
				});

				$('.search-quickfind').click(function(e) {
					e.stopPropagation();
				});

				$(document).keyup(function(e) {
				  if (e.keyCode == 27) {
					$('.search-quickfind').hide('fast');
					window.clearTimeout(timeout);
				}   // esc

				});
			},

			getresults : function() {

				busy = true;
				v = $(".autosearch:focus").val();

				//make mobile input the same
				$(".autosearch").val(v);

				$.getJSON('/type-ahead-search',{q:v},function(po_response) {
					$('.search-quickfind').html(po_response.html).show('fast');
					busy = false;
				});
			}
		}

		})(); //call autosearch immediately;

		autosearch.init();

});

$("#orderref").blur(function() {

	$.post('/payment?a=addaccountpayment',{'__orderref':$(this).val()},function(po_response) {

		$("#accountamount").html(po_response.accountpayment.toFixed(2));
		$("#creditcardamount").html(po_response.creditcardpayment.toFixed(2));

		if (po_response.creditcardpayment <= 0) {
			$("#ccpayment").slideUp();
		} else {
			if ($('#ccpayment:hidden').length == 1) {
				$("#ccpayment").slideDown();
			}
		}

	},'jSON');

});

$("#deliverypostcode").blur(function() {
	$.post('/your-details?a=determinefreightpolicy',{postcode:$(this).val()},function(ps_html) {

		$("#cart-summary-container").html(ps_html);

	});
});
