var hx={};
/*------------------------------------Base-------------------------------------------*/
/*window addMask*/
function addMask(){
    var m_h=$(document).height();
    $('body').append('<div class="win_mask"></div>');
    $('.win_mask').css('height',m_h).fadeIn();
    $('select').hide();
    }
function addMask2(){
    var m_h=$(document).height();
    $('body').append('<div class="win_mask alert"></div>');
    $('.win_mask.alert').css('height',m_h).fadeIn();
    $('select').hide();
    }
/*window delMask*/
function delMask(){
    $('.ui-dialog,.win_mask').remove();
    $('select').show();
};
function delMask2(){
    $('.ui-dialog.alert,.win_mask.alert').remove();
    $('select').show();
};
/*dealing add_loading*/
function add_loading(msg){
    var m_h=$(document).height(),tip='数据正在处理中...请稍等';
    if(msg){tip=msg};
    var html='<div class="win_mask_loading"></div><div class="hx_load"><img src="../images/platform/frame/public/hx_loading.gif"> '+tip+'</div>';
    $('body').append(html);
    var win=$(window),
        load=$('.hx_load'),
        it_w=load.width(),
        it_h=load.height(),
        pop={'left':(win.width()-it_w)/2,'top':(win.height()-it_h)/2+win.scrollTop()};
    load.css(pop).show();
    $('.win_mask_loading').css('height',m_h).fadeIn();
    $('select').hide();
    };
/*dealing end_loading*/
function end_loading(){
    $('.hx_load,.win_mask_loading').remove();
    $('select').show();
    };
/*window notice*/
function notice(msg,fun,type){
    addMask();
    var title='温馨提示';
    var html='<div class="ui-dialog"><div><h2><span class="cancel">×</span>'+title+'</h2><p class="cont">'+msg+'</p><p class="button sp"><span><a href="#" class="buttonGreen ok">确定</a> <a href="#" class="buttonGreen cancel">取消</a></span></p></div></div>';
    $('body').append(html);
    var win=$(window),
        dialog=$('.ui-dialog'),
        it_w=dialog.width(),
        it_h=dialog.height(),
        pop={'left':(win.width()-it_w)/2,'top':(win.height()-it_h)/2+win.scrollTop()};
    dialog.css(pop).show().find('.cancel').bind('click',function(){delMask();return false;});
    if(fun){
        dialog.find('a.ok').bind('click',fun);
        if(type){dialog.find('p.button').removeClass('sp');}
    }
    dialog.find('a.ok').bind('click',function(){delMask();return false;});
}

/*select default*/
function select_default(obj){
    var _obj=$(obj).find('input'),value=_obj.val(),_value=$(obj).find('ul li a[rel="'+value+'"]').text();
    _obj.next().text(_value);
}
/*window alert*/
(function($){
    $.fn.mask=function(options){
        var obj=$(this);
        var op={
            comfirm:true,
            title:'信息标题',
            message:'内容填写区',
            event:function(){alert('ok')},
            close:function(){},
            type:1
        };
        var options=$.extend(op,options);
        this.each(function(){
            obj.unbind('click').bind('click',function(){
                var html='';
                if(op.comfirm){
                    if(op.type==1){
                        html='<div class="ui-dialog"><div><h2><span class="cancel">×</span>'+op.title+'</h2><p class="cont">'+op.message+'</p><p class="button" style="width:60px;"><span><a href="#" class="buttonGreen ok">确定</a></span></p></div></div>'
                    }else{
                        html='<div class="ui-dialog"><div><h2><span class="cancel">×</span>'+op.title+'</h2><p class="cont">'+op.message+'</p><p class="button"><span><a href="#" class="buttonGreen ok">确定</a> <a href="#" class="buttonGreen cancel">取消</a></span></p></div></div>'
                    }
                }
                else{html='<div class="ui-dialog"><div><h2><span class="cancel">×</span>'+op.title+'</h2><p class="cont">'+op.message+'</p><p class="button"></p></div></div>'}
                addMask();
                $('body').append(html);
                var it_w=$('.ui-dialog').width(),it_h=$('.ui-dialog').height(),pop={'left':($(window).width()-it_w)/2,'top':($(window).height()-it_h)/2+$(window).scrollTop()};
                $('.ui-dialog').css(pop).show();
                $('.ok').bind('click',function(){op.event();delMask(); return false;});
                $('.cancel').bind('click',function(){op.close();delMask(); return false;});
            })
        });
    }
})(jQuery);
/*------------------------------------Function-------------------------------------------*/
/*str cut*/
function cutstr(obj,num){
	obj.each(function(index,ele){
		var _obj=$(ele).text(),value=_obj.substr(0,num-1),len=_obj.length;
		if(len>num){$(ele).attr('title',_obj).text(value+'...');}
	})
};
/*page paging*/
(function($){
    $.fn.page=function(options){
        var _obj=$(this);
        var op={
            url:'',
            info:{},
            deal:'',
            fun:function(data){}
        };
        var options=$.extend(op,options);
        function paging(obj,cur,count,perpage,records){
            var pagePrev='<a href="#" class="ui-paging-prev"><i class="new">&#xf104;</i> 上一页</a>',
                pageNext='<a href="#" class="ui-paging-next">下一页 <i class="new">&#xf105;</i></a><span class="ui-paging-info"><span class="ui-paging-bold">'+cur+'/'+count+'</span>页，共<span class="ui-paging-bold">'+records+'</span>条，第 </span><span class="ui-paging-which"><input name="some_name" type="text"></span><span class="ui-paging-bold">页</span><a class="ui-paging-info ui-paging-goto" href="#">跳转</a>',
                temp=[],
                pageHtml='';
            function pageMid(start,end){
                for(var i=start;i<end;i++){
                    var curpage=parseInt(i+1);
                    temp[i]='<a href="#" class="ui-paging-item" rel="'+curpage+'">'+curpage+'</a>';
                }
            };
            if(count<=perpage){
                pageMid(0,count);
                pageHtml=pagePrev+temp.join('')+pageNext;
            }else{
                if(cur<perpage){
                    pageMid(0,perpage);
                    pageHtml=pagePrev+temp.join('')+'<span class="ui-paging-ellipsis">...</span>'+'<a href="#" class="ui-paging-item" rel="'+count+'">'+count+'</a>'+pageNext;
                }else{
                    if(cur>=count-perpage){
                        pageMid(count-perpage-2,count);
                        pageHtml=pagePrev+temp.join('')+pageNext;
                    }else{
                        pageMid(cur-2,parseInt(cur)+perpage);
                        pageHtml=pagePrev+temp.join('')+'<span class="ui-paging-ellipsis">...</span>'+'<a href="#" class="ui-paging-item" rel="'+count+'">'+count+'</a>'+pageNext;
                    }
                }
            }       
            obj.html(pageHtml);
            /*------2015-02-03-start----*/
            var keep={
                    normal:function(){
                    	$('a.ui-paging-item').bind('click',function(){
                            var page=parseInt($(this).attr('rel'));
                            get_data(page);
                            return false;
                        });
                    },
                    prev:function(){
                    	$('a.ui-paging-prev').bind('click',function(){
                            var page=parseInt($(this).siblings('a.ui-paging-current').attr('rel'));
                            if(page>1){page--;}else{return false;}
                            get_data(page)
                            return false;
                        });
                    },
                    next:function(){
                    	$('a.ui-paging-next').bind('click',function(){
                            var page=parseInt($(this).siblings('a.ui-paging-current').attr('rel')),
                                _page=parseInt($(this).prev().attr('rel'));
                            if(page!=_page){page++;}else{return false;}
                            get_data(page)
                            return false;
                        });
                    },
                    topage:function(){
                    	$('a.ui-paging-goto').bind('click',function(){
                            var page=$(this).parent().find('input').val(),max_page=parseInt($('.ui-paging-next').prev().attr('rel'));
                            if(page==''||page>max_page){return false;}
                            get_data(page)
                            return false;
                        });
                    }
                };
            keep.normal();
            keep.prev();
            keep.next();
            keep.topage();
            /*------2015-02-03-end----*/
            var prevVlaue = obj.find(".ui-paging-ellipsis").prev(".ui-paging-item").attr("rel");
            var nextValue = obj.find(".ui-paging-ellipsis").next(".ui-paging-item").attr("rel");
            if(parseInt(nextValue) - parseInt(prevVlaue) == 1){
                obj.find(".ui-paging-ellipsis").remove();
            }
            obj.find('a[rel="'+cur+'"]').addClass('ui-paging-current');
            if($('.ui-paging-current').attr('rel') == 1){
                $('.ui-paging-prev').css('cursor','text');
            }
            if($('.ui-paging-current').attr('rel') == count){
                $('.ui-paging-next').css('cursor','text');
            }
            else{
                $('.ui-paging-prev').css('cursor','hand');
                $('.ui-paging-next').css('cursor','hand');
            }
        };      
        function get_data(cur){
            op.info.page=cur;
            op.info.pages=5;
                $.ajax({
                    url:op.url,
                    dataType:'json',
                    type:'post',
                    data:op.info,
                    error:function(){return false;},
                    success:function(_data){
                        op.fun(_data);
                        if(_data.records>op.info.pageSize){paging(_obj,cur,_data.total,op.info.pages,_data.records);_obj.show();}else{_obj.hide().html('');}
                    }
                    
                });
            };
        this.each(function(){
            op.info=op.send();
            if(op.deal){
                $(op.deal).bind('click',function(){
                    op.info=op.send();
                    get_data(1);
                });
            }
            get_data(1);
        });
    }
})(jQuery);
/*------------------------------------Function-------------------------------------------*/
/*pop windowdrop*/
(function($){
	$.fn.windowdrop=function(){
		$(this).bind('mousedown',function(v){
			var $this=$(this),
				pos={x:v.clientX,y:v.clientY},
				_pos=$this.offset(),
				win_pos={x:_pos.left,y:_pos.top},
			 	move={x:pos.x-win_pos.x,y:pos.y-win_pos.y};
			win_pos.w=$(document).width();win_pos.itw=$this.width();win_pos.ith=$this.height();win_pos.h=$(document).height();
			$this.bind('mousemove',function(e){			
				win_pos.x=e.clientX-move.x;
				win_pos.y=e.clientY-move.y;
				win_pos.maxW=win_pos.w-win_pos.itw;
				win_pos.maxH=win_pos.h-win_pos.ith;
				if(win_pos.y<0||win_pos.x<0||win_pos.x>=win_pos.maxW-10||win_pos.y>=win_pos.maxH-1){return false;}
				move.to={"left":win_pos.x,"top":win_pos.y};
				$this.css(move.to).css('cursor','move');
			})
		}).bind('mouseup',function(){
			$(this).unbind('mousemove').css('cursor','');
		});
	}
})(jQuery);
/*window justTip*/
function justTip(msg){
    $('body').append('<div class="justTip">'+msg+'</div>');
    center($('.justTip'));
    setTimeout(function(){$('.justTip').fadeOut(1000,function(){$('.justTip').remove();});},1000);
}
/*window pop center*/
function center(obj){
	var $window=$(window),
		win_h=$window.height(),
		win_w=$window.width(),
		obj_h=obj.innerHeight(),
		obj_w=obj.innerWidth(),
		scroll=$window.scrollTop(),
		pos={'left':(win_w-obj_w)/2,'top':(win_h-obj_h)/2+scroll,'position':'absolute'};
		obj.css(pos).show();
        obj.find('.close').bind('click',function(){obj.remove();delMask();});		
};
/*imgShow*/
(function($){
	$.fn.imgShow=function(){
		$(this).bind('mouseenter',function(){$(this).css('cursor','pointer');}).bind('mouseleave',function(){$(this).css('cursor','default');});
		$(this).bind('click',function(){
			addMask();
			var box='<style type="text/css">.proimg{ width: 70%; display: none; border:10px solid #999; border-radius:5px; background: #fff; z-index:1000; }.proimg img{ display: block; margin: 0 auto; cursor: pointer; }.proimg span{ width: 50px; height: 50px; text-align: center; line-height: 50px; background: #000; color:#fff; display: block; font-size: 30px; position: absolute; right: 10px; top: 10px; opacity:0.6; cursor: pointer; }</style><div class="proimg"></div>';
			if($('.proimg').length<1){$('body').append(box);}
			var imgbox={"height":Math.floor($('.proimg').width()*0.6),"width":Math.floor($('.proimg').width())},
				img={"height":0,"width":0},
				obj=$('.proimg'),
				$this=$(this),
				src=$this.attr('trueSrc');
			obj.html('<img src="'+src+'"><span>X</span>');
			obj.find('img').load(function(){
				var $this=$(this);
				img.width=$this.width();
				img.height=$this.height();
				if(img.width>=imgbox.width){img.height=imgbox.width;$this.css({"width":img.width});};
				if(img.height>=imgbox.height){img.height=imgbox.height;$this.css({"height":img.height});};
				$this.css({"margin-top":(imgbox.height-img.height)/2});
			});
			obj.css(imgbox);
			obj.find('span,img').bind('click',function(){
				$(this).parent().hide();
				delMask();
			});
			center(obj);
		})
	}
})(jQuery);

/*handlebars的jquery插件     js模板公用     调用方$('#content').handlebars($('#template'), { name: "Alan" });*/
(function($) {
    var compiled = {};
    $.fn.handlebars = function(template, data) {
        if (template instanceof jQuery) {
            template = $(template).html();
        }
    compiled[template] = Handlebars.compile(template);
    this.html(compiled[template](data));
    };
})(jQuery);

/*在线客服*/
function kefu(url){
	$('body').append('<div class="ui-dialog" style="width:697px;"><h2><span class="cancel">×</span>咨询客服</h2><iframe src="'+url+'" style="width:697px; height:465px;" scrolling="no" frameborder="0"></iframe></div>');
    var win=$(window),
        dialog=$('.ui-dialog'),
        it_w=dialog.width(),
        it_h=dialog.height(),
        pop={'left':(win.width()-it_w)/2,'top':(win.height()-it_h)/2+win.scrollTop()-12};
    dialog.css(pop).show().find('.cancel').bind('click',function(){delMask();return false;});
    dialog.windowdrop();
}
$(window).load(function(){
    /*--------------------------------头部效果------------------------------------*/
    $('.navInfo').bind('click',function(){
        $(this).addClass('open');
    }).bind('mouseleave',function(){
        $(this).removeClass('open');
    });
    $('.tip').bind('mouseenter',function(){
        $(this).addClass('open');
    }).bind('mouseleave',function(){
        $(this).removeClass('open');
    });
    $('.phone').bind('click',function(){
        addMask();
        $('body').append($('#appDownload').html());
        center($('.appDownload'));
        $('.appDownload').find('span.close').bind('click',function(){
        	$('.appDownload').remove();
        	delMask();
        });
    });
    $('table tbody').delegate('td.dot span','mouseenter',function(){
      var $this=$(this),value=$this.attr('rel'),pos={"left":$this.offset().left+20,"top":$this.offset().top};
      if(!value){return false;}
      $('.serverTip').html('<i class="new">&#xf06a;</i> '+value);
      $('.serverTip').css(pos).show();
      }).delegate('td.dot span','mouseleave',function(){$('.serverTip').hide();});
    /*在线客服*/
    $('#kefu').bind('click',function(){
		var link=$(this).attr('rel');
		kefu(link);
	});
    /*菜单选择*/
    $('div.menu a[data-role="'+$('#menuselected').val()+'"]').addClass('cur');

});