﻿(function($){
	$.fn.extend({
		edgeButton: function(custom_settings) {
            // default settings
            var defaults = {
                buttonPrefix: "edgeButton_",
                buttonText: "I am a button",
                buttonTop: 100,
                buttonDirection: "left",
                buttonPosition: "fixed",
                buttonFunction: "scrollTop",
                colorText: "#ffffff",
                colorBackground: "#222222",
                rotateButton: false,
                debugInfo: false
            };
            
            var settings = $.extend(defaults, custom_settings);
            
            return this.each(function() {
            
                var obj                  = $(this),
                    theButton            = "<div class=\""+ settings.buttonPrefix +"edgeButton\">"+ settings.buttonText +"</div>",
                    scrollTopDistance    = "",
                    scrollBottomDistance = "",
                    scrollDistance       = "",
                    durationTop          = "",
                    durationBottom       = "",
                    durationScroll       = "",
                    buttonLink           = "",
                    buttonImage          = "",
                    buttonWidth          = "",
                    buttonHeight         = "",
                    scrollTimer          = 0;
                        
                // Append theButton in the obj
                obj.append(theButton);
                
                // Add some styling to the obj
                obj.css("margin", 0)
                   .css("top", settings.buttonTop +"px")
                   
                   .css("cursor", "hand")
                   .css("cursor", "pointer")

                   .css("color", settings.colorText)
                   .css("font-weight", "bold")
                   .css("background", settings.colorBackground)
                   .css("position", "absolute");
                
                if(settings.buttonDirection == "left"){
                    obj.css("left", 0);
                }else if(settings.buttonDirection == "right"){
                    obj.css("right", 0);
                }else{
                    if (settings.debugInfo) {
                        alert("The \"buttonDirection\" is not set correctly!");
                    }
                }
                
                if(settings.buttonPosition == "fixed"){
                    $(window).scroll(function () {
                        obj.css("top",settings.buttonTop + $(document).scrollTop());
                    });
                }else if(settings.buttonPosition == "absolute"){
                    obj.css("position", "absolute");
                }else{
                    if (settings.debugInfo) {
                        alert("The \"buttonPosition\" is not set correctly!");
                    }
                }
                
                buttonWidth  = obj.width();
                buttonHeight = obj.height();
                
                //Add some styling to theButton
                $("."+ settings.buttonPrefix +"edgeButton").css("padding", "5px");
                
                $(document).ready(function(){
                    $("."+ settings.buttonPrefix +"edgeButton").click(handleClick);
                    
                    buttonImage = obj.find(".buttonImage").attr("src");
                    
                    if(buttonImage){
                        setTimeout(setBackgroundImage, 10);
                    }
                    
                    if(settings.rotateButton){
                        rotateButton();
                    }
                });

                
                function handleClick()
                {
                    durationTop    = $(window).scrollTop();
                    durationBottom = $(document).height() - $(window).scrollTop();
                    durationScroll = $(window).height();
                    
                    if(settings.buttonFunction == "scrollTop"){
                        scrollTopDistance = 0;
                        
                        $("html, body").animate({scrollTop: scrollTopDistance}, durationTop);
                    }else if(settings.buttonFunction == "scrollBottom"){
                        scrollBottomDistance = $(document).height() - $(window).height();
                        
                        $("html, body").animate({scrollTop: scrollBottomDistance}, durationBottom);
                    }else if(settings.buttonFunction == "scrollDown"){
                        scrollDistance = $(window).scrollTop() + $(window).height() - 50;
                        
                        $("html, body").animate({scrollTop: scrollDistance}, durationScroll);
                    }else if(settings.buttonFunction == "scrollUp"){
                        scrollDistance = $(window).scrollTop() - $(window).height() + 50;
                        
                        $("html, body").animate({scrollTop: scrollDistance}, durationScroll);
                    }else if(settings.buttonFunction == "link"){
                        buttonLink = obj.find(".buttonLink").attr("href");
                        
                        if(buttonLink){
                            obj.click(function(){
                                window.open(buttonLink);
                            });
                        }else{
                            if (settings.debugInfo) {
                                alert("There is no link present!");
                            }
                        }
                    }
                }
                
                function setBackgroundImage()
                {
                    var imageHeight = obj.find(".buttonImage").height(),
                        imageWidth = obj.find(".buttonImage").width();
                    
                    $("."+ settings.buttonPrefix +"edgeButton").css("padding", 0)
                                                               .css("background", "url("+ buttonImage +")")
                                                               .css("height", imageHeight +"px")
                                                               .css("width", imageWidth +"px");
                }
                
                function rotateButton()
                {
                    if (!$.browser.msie && !$.browser.opera) {
                        var rotationOffset = "",
                            rotateDeg      = "";
                        
                        if(settings.rotateButton == "rotateLeft"){
                            rotateDeg      = 90;
                            rotationOffset = (buttonWidth - buttonHeight) / 2;
                        }else if(settings.rotateButton == "rotateRight"){
                            rotateDeg      = 270;
                            rotationOffset = (buttonWidth - buttonHeight) / 2;
                        }else if(settings.rotateButton == "rotateUpsideDown"){
                            rotateDeg = 180;
                        }else{
                            if (settings.debugInfo) {
                                alert("The \"rotateButton\" is not set correctly!");
                            }
                        }
                        
                        if(settings.buttonDirection == "left"){
                            obj.css("margin-left", "-"+ rotationOffset +"px");
                        }else if(settings.buttonDirection == "right"){
                            obj.css("margin-right", "-"+ rotationOffset +"px");
                        }
                        
                        obj.css("-moz-transform", "rotate("+ rotateDeg +"deg)")
                           .css("-webkit-transform", "rotate(-"+ rotateDeg +"deg)")
                           .css("-o-transform", "rotate(-"+ rotateDeg +"deg)")
                           .css("-transform", "rotate(-"+ rotateDeg +"deg)");
                           
                            
                    }else{
                        if(settings.rotateButton == "rotateLeft"){
                            rotateDeg      = 90;
                            rotationOffset = (buttonWidth - buttonHeight) / 2;
                        }else if(settings.rotateButton == "rotateRight"){
                            rotateDeg      = 270;
                            rotationOffset = (buttonWidth - buttonHeight) / 2;
                        }else if(settings.rotateButton == "rotateUpsideDown"){
                            rotateDeg = 180;
                        }else{
                            if (settings.debugInfo) {
                                alert("The \"rotateButton\" is not set correctly!");
                            }
                        }
                        if(rotateDeg = 0)
                           {
                           }
                           else if (rotateDeg = 90)
                           {
                            obj.css("filter", "progid:DXImageTransform.Microsoft.BasicImage(rotation=3)");
                           }
                           else if (rotateDeg = 180)
                           {
                            obj.css("filter", "progid:DXImageTransform.Microsoft.BasicImage(rotation=2)");
                           }
                           else if (rotateDeg = 270)
                           {
                            obj.css("filter", "progid:DXImageTransform.Microsoft.BasicImage(rotation=1)");
                           }
//                        if (settings.debugInfo){
//                            alert("The \"rotateButton\" doesn't work in IE and Opera!");
//                        }
                    }
                }
            });
        }
	});
})(jQuery);