// JavaScript Document

//手机字体适配
window.onload = function(){
	
	var pageWidth = document.body.offsetWidth;
    var px = 20/375;
	
	if(pageWidth>375){
		var newPx = px*pageWidth;
		document.getElementsByTagName('html')[0].style.fontSize = newPx+'px'
		
	}else{
		document.getElementsByTagName('html')[0].style.fontSize = '20px'
	};
};

//如果购课车有课程，出现红点提示
$(function(){
	var cartnum = $(".asidemenu .gk-cart .numbg").text()
	if(cartnum==0){
		$("#asideclick .ybg").hide();
	}
});

//标题配置
function setPageTitle(tit){
	document.getElementById('header-title').innerHTML = tit;
};

//实现滚动条无法滚动
var scrollMove = function(e){e.preventDefault();};

/***禁止滑动***/
function scrollStop(){
	document.addEventListener("touchmove",scrollMove,false);//禁止页面滑动
}
/***取消滑动限制***/
function scrollCancel(){
	document.removeEventListener("touchmove",scrollMove,false);        
}
function asideJs(){
	
	$('#asideclick').click(function(){
		$('.asidemenu').css({right:0});
		$('#asidebg').fadeIn(300);
		$('.asidemenu ul').height($(window).height()-$('#PopLoginTips').height()-$('.gk-cart').height());
		scrollStop();	
		$('.asidemenu ul').bind('touchmove',function(){
			event.stopPropagation();
		});
	});
	
	$('#asidebg').click(function(){
		$('.asidemenu').css({right:'-100%'});
		$('#asidebg').fadeOut(300);
		setTimeout(function(){
			scrollCancel();
		
		},300);
	});
	
	
	 //获取登录信息
	 $.ajax({
		  type: "POST",
		  datatype: "xml",
		  url: "/plus/ajaxs.ashx",
		  data: "a=checkislogin",
		  success: function (xml) {
			  var islogin = jQuery(xml).find("login islogin").text();
			  if (islogin == 'false') return;
			  var str = " <div class='asideuser' onclick=\"window.location='/?c=user'\"><div class='face'><img src='/UploadFiles/avatar/" + jQuery(xml).find("login userid").text() + ".jpg'  onerror=\"this.src='/sysimg/avatar.jpg'\"/></div><div class='name'><div class='rbg iconfont'></div><span>" + jQuery(xml).find("login username").text() + "</span></div><div class='message' id='NewMessage'>0</div></div>";
			  $("#PopLoginTips").html(str);
			  
			 getNewMessage(); //短消息
			  
		  }
	  });
	 
	 function getNewMessage() {
		jQuery.ajax({ url: "/plus/ajaxs.ashx", cache: false, data: "type=1&a=GetNewMessage&now=" + new Date().getTime(), success: function (num) {
			$("NewMessage").html(num);
		} 
		});
		
	};
	setInterval(getNewMessage,1000);

};

function filertJs(){
	
	//分类下拉
	var oldIndex = -1;
	$(".filertbox").on('click','dt',function(){
		var i = $(this).index();
		if(i!=oldIndex){
			oldIndex = i;
			$(this).addClass('curr').siblings().removeClass('curr')
			$(this).find('.ksicon').addClass("iconrotate");
			$(this).siblings().find('.ksicon').removeClass("iconrotate");
			$("#myfilertbg").fadeIn(300)
			$("#myfilert").find(".box:eq("+i+")").fadeIn(300).siblings().hide();
			
		}else{
			closeFilert();
			oldIndex = -1;
		};
	});
	$('#myfilertbg').click(function(){
	   closeFilert();
	});
	
	function closeFilert(){
		$(".filertbox").find('dt').removeClass('curr');
	    $(".filertbox").find('.ksicon').removeClass("iconrotate");
		$("#myfilertbg").fadeOut(300)
		$("#myfilert").find(".box").fadeOut(300);
		
	};
	
	
	//获取选中信息
	$('#myfilert').find('.box').each(function(index) {
        var currText = $(this).find(".curr").text();
		var type = $(this).find('li').attr('field');
		if(type=='price_member'&&currText=='不限'||currText==''){
			currText = $(".filertbox").find('dt').eq(index).find('span').text();
		};
		$(".filertbox").find('dt').eq(index).find('span').html(currText);
		
    });
	
		  
};

//打开页面
var pageOfTop = 0;
function openPage(pageid,type){
	pageOfTop = $(window).scrollTop();
	var $pageId = $('#'+pageid);
	if(type=='right'){
		$pageId.attr('pageType','right');
		$pageId.addClass('page-animate-right');
	};
	setTimeout(function(){
		$('#defaultPage').hide();
		$(window).scrollTop(0);
	},300);
	setTimeout(function(){
		$pageId.addClass('page-clear-fixed');
	},400);
	
	
};
//关闭页面
function closePage(pageid){
	var type = $('#'+pageid).attr('pageType');
	$('#defaultPage').show();
	if(type=='right'){
		$('#'+pageid).removeClass('page-clear-fixed').removeClass('page-animate-right');
	};
	$(window).scrollTop(pageOfTop);
};

//弹出搜索
function goSearch(){
	//搜索
	$("#sctab dt").click(function(){
	  $(".scmain .sctextbox").find("select").show();
	  $(".scmain .sctextbox").removeClass("sctextbox2");
	  $("#sctab dt").removeClass("curr");
	  $(this).addClass("curr");
	  document.getElementById("clicktype").value='1'; 
	});
	$("#sctab #jgclick").click(function(){
	  $(".scmain .sctextbox").find("select").hide()
	  $(".scmain .sctextbox").addClass("sctextbox2")
				  document.getElementById("clicktype").value='2'; 
	});
	
	$("#keyword").keyup(function(e){
	  if (e.keyCode == 13){
		  $('#scbutton').click();
	   }
	});  
};

function searchByClass(courseUrl, faceUrl, liveUrl, teacherUrl) {
	 // console.log(courseUrl,faceUrl,liveUrl,teacherUrl)
        var typevalue = document.getElementById("clicktype").value;
        var url = '{=GetRewriteUrl(courselist)}';
        var obj = document.getElementById('coursetype');
        var index = obj.selectedIndex; //序号，取当前选中选项的序号
        var course = obj.options[index].value;
        if (typevalue == '1') {
            if (course == '1') {
                url = courseUrl;

            }
            else if (course == '2') {
                url = faceUrl;
            }

            else if (course == '3') {
                url = liveUrl;
            }

        }
        else if (typevalue == '2') {
            var url = teacherUrl;
        }
      //  alert(url);
        var newurl = '';

        //if (url.indexOf('?') != -1) {  //没有启用伪静态
        //    var param = url.split('?')[1];
        //} else {
        //    param = ''
        //}
    //var newparam = getNewSearchParam(param);
        var key = jQuery("#keyword").val();
        if (url.indexOf("?") == -1) {
            url = url + "?key-" + key;
           // if (m < 100) url += ",m-" + m;
        } else {
            url = url + "&key=" + key;
          //  if (m < 100) url += "&m=" + m;
        }


       // newurl = url + '?' + newparam;
       
        location.href = url;

	
}
function getNewSearchParam(param) {

	var key = jQuery("#keyword").val();
	var parr = param.split(',');
	var newparam = '';
	for (var i = 0; i < parr.length; i++) {
		var pparr = parr[i].split('-');
		if (pparr[0] == 'key') {
			if (newparam == '') {
				newparam = 'key-' + (key);
			} else {
				newparam += ',key-' + (key);
			}
		} else {
			if (newparam == '') {
				newparam = parr[i];
			} else {
				newparam += ',' + parr[i];
			}
		}
	}
	if (newparam.indexOf('key-') == -1 && key != '') {
		if (newparam == '') {
			newparam = 'key-' + key;
		} else {

			newparam += ',key-' + (key);
		}
	}

	return newparam;
}


//比例配置
$.fn.aspectRatio = function(parameter){
	var $this = $(this);
	var set = parameter.set;
	$this.each(function(){
		$(this).height($(this).width()*set);
		$(window).resize(function(){
			$(this).height($(this).width()*set);
		});
	});
};