//-------------------------------
function init_mobile_menu() {
	/*
	$(".sidebar-icon").click(function(e) {
		e.preventDefault();
		$(".wrapper").toggleClass("toggled");
	});
	*/
	$('.sidebar-icon').click(function(e){
		e.preventDefault();
		openNav();
	});
	
	$('.sidebar-close').click(function(e){
		e.preventDefault();
		closeNav();
	});
	
	$("#overlay").click(function(e){
		e.preventDefault();
		closeNav();
	});
	
	/*
	//  Swipe Left
	var mc = new Hammer($("[off-canvas]")[0]);
	mc.on("swipeleft", function (event) {
		controller.close('menu');
		$("#overlay").removeClass("overlay");
		$("#overlay").unbind('touchmove');
	});
	*/
}


function drop_down()
{
	//-- init --
	var get_option = $("#s_type").val();
	if(get_option != "") {
		var text = $("#"+get_option).text();
		$("#option-text").text(text);
	}
	
	//-- hover --
	$("#ts-type").toggle(function() {
		$("#drop-down-list").show();
	}, function() {
		$("#drop-down-list").hide();
	});
	
	//-- click --
	$("#drop-down-list li").click(function(e) {
		var v    = $(this).data('option');
		var text = $(this).text();
		//--- text && input's val ---
		$("#option-text").text(text);
		$("#s_type").val(v);
		$("#drop-down-list").hide();
	});
}

function fixed_header()
{
	if(isMobileView())
	{
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
		if (scrollTop <= 200 ) {
			$('.logoWrap').removeClass('fixed');
			$('#filter-bar').removeClass('fixed');
			$('.box-store .tbox2-head').removeClass('fixed');
		} else {
			$('.logoWrap').addClass('fixed');
			$('#filter-bar').addClass('fixed');
			$('.box-store .tbox2-head').addClass('fixed');
		}
	}else{
		$('.logoWrap').removeClass('fixed');
		$('#filter-bar').removeClass('fixed');
		$('.box-store .tbox2-head').removeClass('fixed');
	}
}

function send_search() {
	re = /bbprice\.tw/;
	if (re.test(location.host)){
		var path = "/s/";
	}else{
		var path = "/search/";
	}
	//console.log(location.host);
	
	//------ param 1
	var q = $("#q").val();
	if(q == "php") q = "PHP";
	
	//------ param 2
	var ct_id = $('#search_ct_id').val();
	
	
	if(typeof(ct_id) != 'undefined') {
		var send_url = (ct_id == "s") ? "" : "?ct_id="+ct_id;
	}else{
		var send_url = "";
	}
	var url = path + rawurlencode(q) + send_url;
	setTimeout(function(){
		location.assign(url);
		//window.location = url;
		//alert(url);
	}, 300);
	return false;
}

function openNav() {
	//$("#sidebar-wrapper").show();
	$("body").addClass("overflow-hidden"); 
	$("#wrapper").addClass('toggled');
	$("#overlay").addClass("overlay");
}

function closeNav() {
	//$("#sidebar-wrapper").hide();
	$("body").removeClass("overflow-hidden");
	$("#wrapper").removeClass('toggled');
	$("#overlay").removeClass("overlay"); 
	//$("#overlay").unbind('touchmove');
}

function change_placeholder(desktop_title, mobile_title){
	if (window.matchMedia('(max-width: 768px)').matches) {
		$("#q").attr('placeholder',mobile_title);
	}else{
		$("#q").attr('placeholder',desktop_title);
	}
}

/***********************
  init_accordion
/***********************/
function init_accordion()
{
	$('.accordion-toggle').click(function(){
		if($(this).hasClass('collapsed')){
			$(this).find('i').removeClass('fa-caret-right');
			$(this).find('i').addClass('fa-caret-down');
		}else{
			$(this).find('i').removeClass('fa-caret-down');
			$(this).find('i').addClass('fa-caret-right');
		}
	});
}

/*********************
/* init sort
/*********************/
function init_sort()
{
	// 切換 sort
	$('.sort-list > a').on('click', function(e) {
		e.preventDefault();
		$('.sort-list > a').removeClass('active');
		$(this).addClass('active');
		var sort_id = $(this).attr("id");
		$.cookie("sort", sort_id, { expires: 18000});
		$("#s_sort").val(sort_id);
		send_data();
	});
}

/***********************************
/* init mode 切換(list/grid)
/***********************************/
function init_mode()
{
	$('#mode-box a').on('click', function (e) {
		e.preventDefault();
		$("#mode-box a").removeClass('active');
		$(this).addClass('active');
		var _id = $(this).attr("id");
		$.cookie("mode", _id, { expires: 18000});
		if( _id == "mode-grid"){
			$('.mode-wrap').addClass('mode-grid');
			$('.mode-wrap').removeClass('mode-list');
			$(function () {
				$('.product-item').matchHeight();
				$.fn.matchHeight._throttle = 80;
				$.fn.matchHeight._update('.product');
			});
		}else{
			$('.mode-wrap').addClass('mode-list');
			$('.mode-wrap').removeClass('mode-grid');
			$(function () {
				$('.product-item').matchHeight('remove');
			});
		}
	});
}

function init_moveTop()
{
	$("#back-top").hide();
	$('#back-top a').click(function(e) {
		e.preventDefault();
		move_to_top();
		return false;
	});
}

function init_search()
{
	var options = {
		url: function(q) {
			return "/suggestion.php?q=" + q ;
		},
		list: {
			maxNumberOfElements: 10,
			match:{
				enabled: true
			},
			onClickEvent: function() {
				send_search();
			}	
		},
		requestDelay: 500
	};
	$("#q").easyAutocomplete(options);
}

function init_swiper(el) {
	var el = (typeof(el) == "undefined" || el == "null") ? ".swiper-container" : el ;
	if($(el).length > 0)
	{
		var swiper = new Swiper(el, {
			pagination: '.swiper-pagination',
			nextButton: '.swiper-button-next',
			prevButton: '.swiper-button-prev',
			paginationClickable: true,
			slidesPerView: 4,
			slidesPerGroup: 4,
			spaceBetween: 20,
			breakpoints: {
				768: {
					slidesPerView: 3,
					slidesPerGroup: 3
				},
				640: {
					slidesPerView: 2,
					slidesPerGroup: 2
				},
				480: {
					slidesPerView: 1,
					slidesPerGroup: 1
				}
			}
		});
	}
}

function init_owl(num, el) {
	var el   = (typeof(el) == "undefined" || el == "null") ? ".owl-carousel" : el ;
	var num = (typeof(num) == "undefined" || num == "null") ? 6 : num ;
	
	switch (num)
	{
		case 2:
			var responsive_list = {
				0:{
					items: 2,
					slideBy: 2
				}
			};
			break;
			
		case 5:
			var responsive_list = {
				0:{
					items: 1
				},
				300:{
					items: 2,
					slideBy: 2
				},
				480:{
					items: 3,
					slideBy: 3
				},
				640:{
					items: 4,
					slideBy: 4
				},
				768:{
					items: 5,
					slideBy: 5
				}
			};
			break;
		
		//-- 滿版 --
		default:
			var responsive_list = {
				0:{
					items: 1
				},
				300:{
					items: 2,
					slideBy: 2
				},
				480:{
					items: 3,
					slideBy: 3
				},
				640:{
					items: 4,
					slideBy: 4
				},
				768:{
					items: 5,
					slideBy: 5
				},
				1000:{
					items: 5,
					slideBy: 5
				},
				1200:{
					items: 6,
					slideBy: 6
				}
			};
			break;
	}
	
	if($(el).length > 0)
	{	
		$(el).imagesLoaded() 
			.always(function( instance ) {
			$(el).owlCarousel({
				loop: false,
				autoplay: false,
				nav: true,
				dots: true,
				lazyLoad: true,
				margin: 20,
				responsiveClass: true,
				navText: [
					'<i class="fa fa-chevron-left">',
					'<i class="fa fa-chevron-right">'
				],
				responsive: responsive_list
			});
		})
		.done( function( instance ) {
			//console.log('all images successfully loaded');
		})
		.fail( function() {
			//console.log('all images loaded, at least one is broken');
		})
		.progress( function( instance, image ) {
			/*
			var result = image.isLoaded ? 'loaded' : 'broken';
			//console.log( '' + result + ' : ' + image.img.src );
			
			if(!image.isLoaded) {
				image.img.src = "/themes/default/images/no_img.jpg";
			}
			*/
		});
	}
}