/* 
* author: "Muslum TORUN (Interactive Solutions Art Director)",
* company: "Mtdesign Interactive Solutions",
* web: "http://www.muslumtorun.com",
* mail: "info@muslumtorun.com or by_muslum@hotmail.com",
* product: "jQuery Page UP",
* version: "1.0",
* created: "17.09.2011 | 17 Sept 2009",
* desicription: "Licensed under GPL (GPL-LICENSE.txt) license."

// defaults
		isText: true, //if you will use a text this value should be true.
		text: "PAGE UP", //display text
		max: 300, //max height of the scrollbar
		image: "", //image path. If you want to use an image button that 'isText' should be false.
		direction: "rb", //lt: Left-Top, rt: Right-Top, lb: Left-Bottom, rb: Right-Bottom
		autoAnimate: true, //the animate function of jQuery library. If this value is false then you should write your animate function under the 'onClick' function.
		onClick: function() {} // After clicking on the button the callback method will run.
		
** For Example:
		1) $.pageup(); //default settings; format text
		2) $.pageup({text: 'up', max: 100}); //format text
		2) $.pageup({isText: false, image: 'your_path'}); //format image
*/

;(function($){
$.pageup = function (args) {
    args = $.extend($.pageup.defaults, args);
    var btnHTML;
    if (args.isText)
        btnHTML = $.pageup.texthtml(args.text, $.pageup.css(args.direction));
    else
        btnHTML = $.pageup.imagehtml(args.image, $.pageup.css(args.direction));

        $("body").append(btnHTML);

    $(window).scroll(function () {
        if ($(document).scrollTop() > args.max)
            $.pageup.object().fadeIn();
        else
            $.pageup.object().fadeOut();
     });
     $.pageup.refresh();
     $.pageup.click($.pageup.object(), args);
     return $.pageup.object();
};
    
	$.pageup.object = function() {
		return $("#pagebtn-v1");
	};

    $.pageup.ID = function() {
		return "pagebtn-v1";
	};
	
	$.pageup.defaults = {
		isText: true,
		text: "PAGE UP",
		max: 300,
		image: "",
		direction: "rb",
		autoAnimate: true,
		onClick: function() {}
	};
	
    $.pageup.refresh = function() {
        if ($.client.browser.isIphone() || $.client.browser.isIpad()) {
            $.pageup.object().remove();
        }
    };

	$.pageup.css = function(dir) {
		var _css = "position:fixed;z-index:10000;display:none;cursor:pointer;";
		switch (dir) {
			case "lt" :
				_css += "left:10px;top:10px;";
				break;
			case "rt" :
				_css += "right:10px;top:10px;";
				break;
			case "lb" :
				_css += "left:10px;bottom:10px;";
				break;
			case "rb" :
				_css += "right:10px;bottom:10px;";
				break;
		};
        return _css;
	};
	
	$.pageup.imagehtml = function(path, css) {
			return "<img src=" + path + " id=" + $.pageup.ID() +" style=" + css + " />";
	};
	
	$.pageup.texthtml = function(txt, css) {
			return "<a href=javascript:; id=" + $.pageup.ID() + " style=" + css + ">" + txt +"</a>";
	};
	
	$.pageup.animate = function() {
		$("body, html").animate({
			scrollTop: 0
		}, {duration: 1000});
	};
	
	$.pageup.isButton = function() {
		return $("body").has($.pageup.object());
	};
	
	$.pageup.click = function(btn, opt) {
		btn.live("click", function(){
            if (opt.autoAnimate)
                $.pageup.animate();
            opt.onClick();
        });
	};
})(jQuery);
