/**
 * 
 */

;define('component/asidebar',function (require, exports, module) {
	var uitip = require("./tip");
	var asidebar = _.Class({
		initial: function (options) {
			var defaultOp = {
				buttons : [{
					
					buttonName : "xcdz",
					href : "#"
					},
					{
						
						buttonName : "glgl",
						href : "#"
						
					},
					{
						
						buttonName :"dtdl",
						href : "#"
						
					
					},
					{
						href : "#",
						buttonName :"lyzs"
							
					},
					
					{
						buttonName :"lyfw",
						href : "#"
					},
					{
						
						href : "#",
						
						buttonName :"top"
						
					}]	
				
					
			};
            this.options = $.extend(true,{},defaultOp,options);
            this.$root = $('<ul class="eui_aside_float_bar"></ul>');
            for (var i = 0, l = this.options.buttons.length; i < l; i++) {
                var button = this.options.buttons[i];
                this.addButton(button);
            }
            this.$root.appendTo("body");
            var self = this;
            self.relocate();
            $(window).resize(function () {
                self.relocate();
            });
            this.bindButtonEvent();
        },

        addButton: function (button) {
            var $dom = $('<li class="eui_aside_fbar_button eui_fbar_'+button.buttonName+'"><a href="'+button.href+'" _target="blank"></a></li>');
            var initFunction = this["_" + button.buttonName];
            if (initFunction) {
                if (initFunction.call(this, $dom)) {
                    this.$root.append($dom);
                }
            }
        },
        bindButtonEvent : function(){
        	this.$root.delegate('[data-rolehover]',"mouseleave",function(){
        		var that = $(this),
			    	tip = that.data("tip");
        		tip&&tip.hide();
        		
        	});
        	this.$root.delegate('[data-rolehover]',"mouseenter",function(){
        			var that = $(this),
        			    tip = that.data("tip");
        			if(!tip){
        				 tip = new uitip({
 							content: that.data("rolename"),
 							arrowOption: {
 								dir: "right",
 								position: {
 								   top:8
 								}
 							},
 	                    
 							con: that,
 							hasCloseBtn: false,
 							position: {
 								my: "right center",
 								at: "left top+4",
 								of: ".eui_fbar_"+that.data("rolehover")
 							},
 							css: {
 								width: "60"
 							}
         			    });
        				 that.data("tip",tip);
        			}
        			tip.show();
        		
        	});
        	
        },
        relocate: function () {
            if ($(window).width() < 1105) {
                this.$root.addClass("eui_afb_compact");
            } else {
                this.$root.removeClass("eui_afb_compact");
            }
        },
        _xcdz : function($dom){
        	$dom.attr("data-rolehover","xcdz");
        	$dom.data("rolename","行程定制");
        	return true;
        },
        _lyfw : function($dom){
        	$dom.attr("data-rolehover","lyfw");
        	$dom.data("rolename","旅游服务");
        	return true;
        },
        _lyzs : function($dom){
        	$dom.attr("data-rolehover","lyzs");
        	$dom.data("rolename","旅游助手");
        	return true;
        },
        _glgl : function($dom){
        	$dom.attr("data-rolehover","glgl");
        	$dom.data("rolename","攻略桂林");
        	return true;
        },
        _next : function($dom){
        	
        	return true;
        },
        _dtdl : function($dom){
        	$dom.attr("data-rolehover","dtdl");
        	$dom.data("rolename","地图导览");
        	return true;
        },
        _top: function ($dom) {
            var $window = $(window);
            if ($window.scrollTop() == 0) {
                $dom.css("visibility", "hidden");
            }
            $dom.click(function () {
                var scrollTimer = null;
                var d_st = $window.scrollTop();
                if($.browser.msie){
                    window.scrollTo(0,0);
                }else{
                    function setScroll(){
                        d_st = Math.floor(d_st / 2);
                        window.scrollTo(0,d_st);
                        if(d_st>2){
                            scrollTimer = setTimeout(setScroll,40);
                        }else{
                            window.scrollTo(0,0);
                            clearTimeout(scrollTimer);
                        }
                    }
                    setScroll();
                }
            });
            var scrollTimer, isShown = false;
            $window.bind('scroll',function(){
                if(scrollTimer) {
                    clearTimeout(scrollTimer);
                }

                scrollTimer = setTimeout(function(){
                    if($window.scrollTop() > 0){
                        if(isShown == false){
                            $dom.css("visibility", "visible");
                            isShown = true;
                        }
                    }else{
                        if(isShown){
                            $dom.css("visibility", "hidden");
                            isShown = false;
                        }
                    }
                },500);
            });
            return true;
        }
	});
	 module.exports = asidebar;
});