$bgpatt_pick = {
	src: 'http://bgpatterns.com/widgets/pick/',
	isOpen: false,
	useCookies: true,
	deployed: false,
	lastPicked: null,
	defaultBg: null,
	
	styles: {
		box: {
			display: 'none',
			margin: 0,
			padding: 0,
			position: 'absolute',
			left: '50%',
			top: '50%',
			width: '670px',
			height: '470px',
			overflow: 'visible',
			background: 'url(http://bgpatterns.com/images/widgets/pick/bg_widget.png) no-repeat'
		},
		iframe: {
			display: 'none',
			margin: '15px',
			width: '640px',
			height: '440px',
			border: 'none'
		},
		closeButton: {
			position: 'absolute',
			right: '5px',
			top: '-28px',
			display: 'block',
			width: '25px',
			height: '25px',
			outline: 'none',
			background: 'url(http://bgpatterns.com/images/widgets/pick/bg_close.png) no-repeat'
		},
		acceptButton: {
			position: 'absolute',
			right: '33px',
			top: '-28px',
			display: 'block',
			width: '25px',
			height: '25px',
			outline: 'none',
			background: 'url(http://bgpatterns.com/images/widgets/pick/bg_accept.png) no-repeat'
		},
		loading: {
			position: 'absolute',
			left: '50%',
			top: '50%',
			width: '260px',
			height: '65px',
			margin: '-40px 0 0 -130px',
			border: 'none',
			textAlign: 'center',
			textDecoration: 'none'
		},
		imgLink: {
			border: 'none'
		}
	},
	
	onPick: function(imgUrl){},
	onAccept: function(imgFile){},
	onClose: function(){},
	
	_onPick: function(imgUrl){
		this.lastPicked = imgUrl;
		this.onPick.call(null, imgUrl);
	},
	
	_onAccept: function(){
		if(this.useCookies){
			try{
				(new Image()).src = 'http://bgpatterns.com/widgets/pick/save.php?f='+this.lastPicked; //i hate IE6;
			}catch(err){
				alert('Sorry your browser doesn\'t let to save a cookie with your settings'+err);
			}
		}
		this.defaultBg = document.getElementsByTagName('body')[0].style.backgroundImage;
		this.onAccept.call(null, this.lastPicked);
		return false;
	},
	
	_onClose: function(){
		document.getElementsByTagName('body')[0].style.backgroundImage = this.defaultBg;
		this.onClose.call(null);
	},
	
	placeLink: function(id){
		if(id){
			this.openButton = document.getElementById(id);
		}else{
			this.openButton = document.getElementsByTagName('body')[0].appendChild(document.createElement('a'));
			this.openButton.href = "#pick-background";
			this.openButton.innerHTML = "Change background";
			this.openButton.id = "bgpatternsOpen";
			this.openButton.onclick = function(){
				$bgpatt_pick.show();
				return false;
			};
		} 
	},
	
	show: function(){
		if(this.isOpen)
			return;
			
		if(!this.deployed){
			this.deploy();
			this.deployed = true;
		}
		
		with(this.box.style){
			display = 'block';
			marginLeft = -1*parseInt(parseInt(this.styles.box.width)/2)+'px';
			marginTop = -1*parseInt(parseInt(this.styles.box.height)/2)+'px';
		}
		this.isOpen = true;
	},
	
	hide: function(){
		this.box.style.display = 'none';
		this.isOpen = false;
	},
	
	deploy: function(){
		var b = document.getElementsByTagName('body')[0];
		this.defaultBg = b.style.backgroundImage;
		this.box = b.appendChild(this.applyStyle(document.createElement('div'), this.styles.box));
		
		//this.iframe = this.box.appendChild(this.applyStyle(document.createElement('iframe'), this.styles.iframe));
		//this.iframe.setAttribute('frameBorder','0');
		
		this.box.innerHTML = "<iframe src=\""+this.src+"\" frameBorder=\"0\" scrolling=\"no\"></iframe>";
		this.iframe = this.applyStyle(this.box.getElementsByTagName('iframe')[0], this.styles.iframe);
		
		this.acceptButton = this.box.appendChild(this.applyStyle(document.createElement('a'), this.styles.acceptButton));
		this.acceptButton.href = "#accept-background";
		this.acceptButton.onclick = function(){
			$bgpatt_pick._onAccept();
			$bgpatt_pick.hide();
			return false;
		}
		
		this.closeButton = this.box.appendChild(this.applyStyle(document.createElement('a'), this.styles.closeButton));
		this.closeButton.href = "#hide";
		this.closeButton.onclick = function(){
			$bgpatt_pick._onClose();
			$bgpatt_pick.hide();
			return false;
		}
		
		this.loading = this.box.appendChild(this.applyStyle(document.createElement('a'), this.styles.loading));
		this.loading.appendChild(this.applyStyle(document.createElement('img'), this.styles.imgLink)).src = '/images/widgets/pick/logo.png';
		this.loading.appendChild(this.applyStyle(document.createElement('img'), this.styles.imgLink)).src = '/images/widgets/pick/loading.gif';
		this.loading.setAttribute('href','http://bgpatterns.com/');
		this.loading.setAttribute('target','_blank');
		
		this.lock();
	},
	
	loaded: function(){
		this.iframe.style.display = 'block';
		this.loading.style.display = 'none';
	},
	
	lock: function(){
		this.acceptButton.style.display = 'none';
	},
	
	unlock: function(){
		this.acceptButton.style.display = 'block';
	},
	
	applyStyle: function(elm, style){
		for(var p in style)
			elm.style[p] = style[p];
		return elm;
	}
}