$(document).ready(function() {
	if(location.hash!='') {
		hash = location.hash.slice(1);
		if(hash=='top') {
			$('div.provider:first').addClass('focus');
		}
		else {
			$('a#'+hash).parents('div.provider, div.product').addClass('focus');
		}
	}
	
	if($('div#basket_summary').length) {
		$('div#basket_summary').remove().appendTo('body');
		bskSummary = new BasketSummary(
			$('div#basket_summary'),
			$('div#basket_summary_content'),
			$('div#basket_summary_open_tab'),
			$('div#basket_summary_drag_tab')
		);
	}
	
	//back to top scroll function. Any link with a hash (#) will scroll to that id on the page
	$('a[href*=#]').click(function() {
		if(
			location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&&
			location.hostname == this.hostname
		) {
			var $target = $(this.hash);
			var hash = this.hash.slice(1);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if($target.length) {
				targetOffset = $target.offset().top;
				$('div.provider,div.product').removeClass('focus');
				if(hash=='top') {
					$('div.provider:first').addClass('focus');
				}
				else {
					$target.parents('div.provider,div.product').addClass('focus');
				}
				$('html,body').animate(
					{scrollTop: targetOffset},
					500,
					'linear',
					function() {
						location.hash = hash;
					}
				);
				return false;
			}
		}
		return true;
	});
});

function BasketSummary(mainContainer,container,openTab,dragTab) {
	this.mainContainer = mainContainer;
	$(this.mainContainer).removeClass('without-js');
	$(this.mainContainer).addClass('with-js');
	$(this.mainContainer).addClass('rounded-border');
	this.container = container;
	this.openTab = openTab;
	this.dragTab = dragTab;
	
	this.construct();
	this.setHandler();
	
	this.close();
}

BasketSummary.prototype.constructor = BasketSummary;

BasketSummary.prototype = {
	construct: function() {
		this.msie = ($.browser.msie) ? true : false;
		
		this.x = this.currentLeft();
		this.y = this.currentTop();
		this.width = this.mainContainer.width();
		
		this.container.attr('open','true');
		
		defaultPosition = this.getNewPosition();
		this.mainContainer.css({
			position:'absolute',
			top:defaultPosition.top,
			left:defaultPosition.left,
			width:this.width
		});
	},
	
	currentLeft: function() {
		return parseInt(
			this.mainContainer.css('left')
				.substring(0,
					this.mainContainer.css('left').indexOf('px')
				)
			);
	},
	
	currentTop: function() {
		return parseInt(
			this.mainContainer.css('top')
				.substring(0,
					this.mainContainer.css('top').indexOf('px')
				)
			);
	},
	
	getNewPosition: function() {
		var BskSummary = this;
		
		if(document.documentElement) {
			windowWidth = document.documentElement.offsetWidth;
		}
		else if(window.innerWidth&&window.innerHeight) {
			windowWidth = window.innerWidth;
		}
		
		x = $(document).scrollLeft()+windowWidth-this.mainContainer.width();
		y = this.y+$(document).scrollTop();
		
		if(this.msie) {
			//window.alert('MSIE');
			x -= (x>15) ? 21 : 0;
		}
		
		return {left:x,top:y};
	},
	
	setHandler: function() {
		var BskSummary = this;
		
		$(window).bind('resize',function() {
			position = BskSummary.getNewPosition();
			BskSummary.mainContainer.css({
				left:position.left
			});
			currentDuration = (position.left>BskSummary.currentLeft()) ? 500 : 0;
			BskSummary.mainContainer.animate({
				top:position.top/*,
				left:position.left*/
			},{duration:currentDuration,queue:false});
		});
		
		$(window).scroll(function () { 
			if(BskSummary.container.attr('open')=='true') {
				BskSummary.close();
			}
			
			position = BskSummary.getNewPosition();
			
			BskSummary.mainContainer.css({
				left:position.left
			});
			/*
			newLeft = position.left;
			currentLeft = BskSummary.currentLeft();
			if(newLeft!=currentLeft) {
				if(newLeft<currentLeft) {
					BskSummary.mainContainer.css({
						left:newLeft
					});
				}
				else {
					BskSummary.mainContainer.animate({
						left:newLeft
					},{duration:250,queue:false});
				}
			}
			*/
			BskSummary.mainContainer.animate({
				top:position.top
			},{duration:500,queue:false});
		});
		
		this.openTab.click(function() {
			if(BskSummary.container.attr('open')=='false') {
				BskSummary.open();
			}
			else {
				BskSummary.close();
			}
		});
		
		this.mainContainer.draggable({
			appendTo:'body',
			axis:'y',
			handle:this.dragTab,
			stop: function() {
				BskSummary.y = BskSummary.currentTop()-$(document).scrollTop();
			}
		});
		
	},
	
	close: function() {
		var BskSummary = this;
		
		this.container.attr('open','false');
		this.mainContainer.addClass('closed');
		
		position = this.getNewPosition();
		var newLeft = position.left+this.mainContainer.width()-this.openTab.width();
		
		this.container.hide(500,function() {
			BskSummary.mainContainer.css({
				left:newLeft,
				width:BskSummary.openTab.width()
			});
		});
		
		
	},
	
	open: function() {
		var BskSummary = this;
		
		this.container.attr('open','true');
		this.mainContainer.removeClass('closed');
		
		position = this.getNewPosition();
		var newLeft = position.left-this.width+this.mainContainer.width();
		
		this.mainContainer.css({
			left:newLeft,
			width:this.width
		});
		
		this.container.show(500);
		
	}
}
