var Site = (function() {
	
	function initialise() {
		bindPreScroll();
		bindPortfolioImages();
		bindSearch();
	}
	
	function bindPreScroll() {
		var pres = document.getElementsByTagName('pre');
		if(pres.length > 0) {
			var i = 0, x = pres.length, activePre, currentScrollLeft, mousePosX, mousePosY, tooltip;
			
			for(i; i < x; i++) {
				pres[i].parentNode.getElementsByTagName('p')[0].getElementsByTagName('a')[0].onclick = function() {
					Modal.setContent(new CopyCode(this.parentNode.parentNode.getElementsByTagName('textarea')[0].innerHTML)).setClass('copyCode').bindClose(document.getElementById('close')).show();
					return false;
				}
				/*
				if(pres[i].scrollWidth > 748) {
					pres[i].style.overflow = 'hidden';
					pres[i].className += ' scrollable';
					
					if(!tooltip) {
						tooltip = document.createElement('div');
						tooltip.id = 'tooltip';
						tooltip.innerText = tooltip.textContent = 'Click and drag to scroll';
						document.body.appendChild(tooltip);
					}
					
					pres[i].onscroll = function(e) {
						console.log(e);
					}
					
					pres[i].onmousedown = function(e) {
						e = e || window.event;
						if(e.button != 2) {
							activePre = this;
							currentScrollLeft = this.scrollLeft;
							mousePosX = (e.pageX || (e.clientX + document.documentElement.scrollLeft));

							document.onmousemove = function(e) {
								if(activePre) {
									e = e || window.event;
									activePre.scrollLeft = (currentScrollLeft - ((e.pageX || (e.clientX + document.documentElement.scrollLeft)) - mousePosX));
								}
							}

							document.onselectstart = function() {
								return false;
							}

							document.onmouseup = function() {
								activePre = null;
								document.onmousemove = null;
								document.onselectstart = null;
							}
							
							return false;
						}
					}
					
					pres[i].onmousemove = function(e) {
						if(tooltip) {
							e = e || window.event;
							tooltip.style.top = (e.pageY || (e.clientY + document.documentElement.scrollTop)) + 5 + 'px';
							tooltip.style.left = (e.pageX || (e.clientX + document.documentElement.scrollLeft)) + 5 + 'px';
							tooltip.style.display = 'block';
						}
					}

					pres[i].onmouseout = function() {
						if(tooltip) {
							tooltip.style.display = 'none';
						}
					}
				}*/
			}
		}
	}

	function bindPortfolioImages() {
		var portfolioItems = document.getElementById('portfolioItemList');
		if(portfolioItems != undefined) {
			var items = portfolioItems.getElementsByTagName('li'),
			link, i = 0, x = items.length;
			for(i; i < x; i++) {
				link = items[i].getElementsByTagName('a')[0];
				if(link != undefined) {
					link.onclick = function() {
						Modal.setContent(new ModalImage(this.href)).show();
						return false;
					}
				}
			}
		}
		portfolioItems = document.getElementById('portfolioItems');
		if(portfolioItems != undefined) {
			document.getElementById('postContent').getElementsByTagName('a')[0].onclick = function() {
				Modal.setContent(new ModalImage(this.href)).show();
				return false;
			}
		}
	}
	
	function bindSearch() {
		var search = document.getElementById('search'),
		searchInput = search.getElementsByTagName('input')[0];
		
		search.onsubmit = function() {
			if(searchInput.value == '' || searchInput.value == 'Search') {
				return false;
			}
		}
		
		searchInput.onfocus = function() {
			search.className = 'focus';
			if(this.value == 'Search') {
				this.value = '';
			}
		}
		
		searchInput.onblur = function() {
			search.className = '';
			if(this.value == '') {
				this.value = 'Search';
			}
		}
	}
	
	return function() {
		initialise();
	 }
	
})();

