//** jQuery Scroll to Top Control script- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** Available/ usage terms at http://www.dynamicdrive.com (March 30th, 09')
//** v1.1 (April 7th, 09'):
//** v2.0 (Updated by M.TRY 11/12/2009)
//** v2.1 (M.TRY 04/05/2011 changed $ to jquery so it did not conflict with other jquery applications)
//** 1) Adds ability to scroll to an absolute position (from top of page) or specific element on the page instead.
//** 2) Fixes scroll animation not working in Opera. 
//** a) Now uses external parameters
//** b) fixed scroll to section to work
// NORMAL SETTINGS FOR THE FOLLOWING
// startline_stt:100
// scrollto_stt:0
// scroll_stt_duration:1000
// fade_stt_duration:[500, 100]
// control_stt_HTML: '<img src="./images/stt_up.png" style="width:48px; height:48px">', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
// control_stt_attrs: {offsetx_stt:5, offsety_stt:5}, //offset of control relative to right/ bottom of window corner
// anchor_stt_keyword: '#scrolltotop', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

//$jQuery = jQuery;

var scrolltotop={
	//startline_stt: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
	//scrollto_stt: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top).
	setting_stt:{startline_stt:stt_startline, scrollto_stt:stt_scrollto, scroll_stt_duration:stt_scrollduration, fade_stt_duration:[500, 100]},
	control_stt_HTML:stt_controlimg, //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	control_stt_attrs:{offsetx_stt:stt_offsetx, offsety_stt:stt_offsety}, //offset of control relative to right/ bottom of window corner
	anchor_stt_keyword: stt_anchorkeyword, //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

	state_stt: {is_stt_visible:false, should_stt_visible:false},

	scrollup:function(){
		if (!this.cssfixedsupport){ //if control is positioned using JavaScript
			this.$control.css({opacity:0}); //hide control immediately after clicking it
			}
		var dest=isNaN(this.setting_stt.scrollto_stt)? this.setting_stt.scrollto_stt : parseInt(this.setting_stt.scrollto_stt,10);
		if (typeof dest=="string"){
		 if (jQuery('#'+dest).length==1){ //check element set by string exists
			dest=jQuery('#'+dest).offset().top;
			}else{
			dest=0;
			}
			}
		this.$body.animate({scrollTop: dest}, this.setting_stt.scroll_stt_duration);
	},

	keepfixed:function(){
		var $window_stt=jQuery(window);
		var controlx_stt=$window_stt.scrollLeft() + $window_stt.width() - this.$control.width() - this.control_stt_attrs.offsetx_stt;
		var controly_stt=$window_stt.scrollTop() + $window_stt.height() - this.$control.height() - this.control_stt_attrs.offsety_stt;
		this.$control.css({left:controlx_stt+'px', top:controly_stt+'px'});
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop();
		if (!this.cssfixedsupport){
			this.keepfixed();
			}
		this.state_stt.should_stt_visible=(scrolltop>=this.setting_stt.startline_stt)? true : false;
		if (this.state_stt.should_stt_visible && !this.state_stt.is_stt_visible){
			this.$control.stop().animate({opacity:1}, this.setting_stt.fade_stt_duration[0]);
			this.state_stt.is_stt_visible=true;
		}
		else if (this.state_stt.should_stt_visible===false && this.state_stt.is_stt_visible){
			this.$control.stop().animate({opacity:0}, this.setting_stt.fade_stt_duration[1]);
			this.state_stt.is_stt_visible=false;
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var main_stt_obj=scrolltotop;
			var iebrws=document.all;
			main_stt_obj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest; //not IE or IE7+ browsers in standards mode
			main_stt_obj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body');
			main_stt_obj.$control=$('<div id="topcontrol">'+main_stt_obj.control_stt_HTML+'</div>')
				.css({position:main_stt_obj.cssfixedsupport? 'fixed' : 'absolute', bottom:main_stt_obj.control_stt_attrs.offsety_stt, right:main_stt_obj.control_stt_attrs.offsetx_stt, opacity:0, cursor:'pointer'})
				.attr({title:'Scroll Back to Top'})
				.click(function(){main_stt_obj.scrollup(); return false})
				.appendTo('body');
			if (document.all && !window.XMLHttpRequest && main_stt_obj.$control.text()!=''){ //loose check for IE6 and below, plus whether control contains any text
				main_stt_obj.$control.css({width:main_stt_obj.$control.width()}); //IE6- seems to require an explicit width on a DIV containing text
				}
			main_stt_obj.togglecontrol();
			$('a[href="' + main_stt_obj.anchor_stt_keyword +'"]').click(function(){
				main_stt_obj.scrollup();
				return false;
			});
			$(window).bind('scroll resize', function(e){
				main_stt_obj.togglecontrol();
			});
		});
	}
};

scrolltotop.init();
