var XHTMLNS = "http://www.w3.org/1999/xhtml";

var ie = false;

function createElement(element) {
	if(typeof document.createElementNS != 'undefined') {
		return document.createElementNS(XHTMLNS, element)
	}
	else if (typeof document.createElement != 'undefined') {
		return document.createElement(element)
	}
	else {
		return false;
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload
	
	if(typeof window.onload != 'function') {
		window.onload = func
	}
	else {
		if(typeof func == 'function') {
			window.onload = function() {
				func()
				oldonload()
			}
		}
		else {
			window.onload = function() {
				eval(func)
				oldonload()
			}
		}
	}
}

function cancelDefault(e) {
	if(e.cancelable) {
		e.preventDefault()
	}
	else if(document.all) {
		event.returnValue = false
	}
	else {
		alert('not cancelable')
	}
}

function getTarget(e) {
	if(window.event && window.event.srcElement) {
		r = window.event.srcElement
	}
	else if (e && e.target) {
		r = e.target
	}

	return r
}

function addEvent(obj,evType,fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType,fn,false)
		return true
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent('on'+evType, fn)
		
		ie = true;
		
		if(!r) {
			alert('cannot add event!');
		}
		
		return r
	}
	else {
		alert('cannot add event!')
		return false
	}
}

function removeEvent(obj,evType,fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(evType,fn,false)
		return true
	}
	else if (obj.detachEvent) {
		var r = obj.detachEvent('on'+evType, fn)
		return r
	}
	else {
		return false
	}
}

document.getElementsByClassName = function(class_name) {
	var all_obj, ret_obj = new Array(), j = 0, strict = 0, a = 1

	if (document.getElementsByClassName.arguments.length > 1)
		strict = ( document.getElementsByClassName.arguments[1] ? 1 : 0 )

	if (document.all) {
		all_obj = document.all
	}
	else if (document.getElementsByTagName && !document.all) {
		all_obj = document.getElementsByTagName("*")
	}

	for (i = 0; i < all_obj.length; i++) {
		if (
			(' ' + all_obj[i].className + ' ').toLowerCase().match(
				new RegExp (( strict ? '^ ' + class_name + ' $' : '^.* ' + class_name + ' .*$').toLowerCase(),'g' )
			)
		 ) {
			ret_obj[j++] = all_obj[i]
		}
	}

	return ret_obj
}

function getParent(el, pTagName) {
	if(el == null) {
        	return null;
	}
	else if (el.nodeType == 1 && elementCompare(el.tagName,pTagName)) {
		return el;
	}
	else {
		return getParent(el.parentNode, pTagName);
	}
}

function elementCompare(a,b) {
	a = a.toLowerCase() // Gecko bug, supposed to be uppercase
	b = b.toLowerCase()

	if((a == b) || (a == "html:" + b)) { // opera has html: in front of elements
		return true
	}
	else {
		return false
	}
}

var position = 1;

var itemCount = 32;

var hold = false;

function previousPhotoSlide(e) {
	cancelDefault(e);
	
	if(hold) {
		return;
	}
	
	if(position > 1 && itemCount > 4) {
		position--;
		continueSlide();
		
		if(position > 1 && itemCount > 4) {
		}
		else {
			document.getElementById('photoSlidePrevious').style.background = 'transparent';
			// hide previous
		}
		
		if((itemCount - position + 1) > 4) {
			document.getElementById('photoSlideNext').style.background = 'url(http://huntervetscycling.ocoth.net/media/images/photoslide/right.gif) center center no-repeat';
			// show next
		}
	}
}
function nextPhotoSlide(e) {
	cancelDefault(e);
	
	if(hold) {
		return;
	}
	
	if(itemCount > 4 && (itemCount - position + 1) > 4) {
		position++;
		continueSlide();
		
		if((itemCount - position + 1) == 4) {
			// hide next
			document.getElementById('photoSlideNext').style.background = 'transparent';
		}
		
		if(position > 1 && itemCount > 4) {
			// show previous
			document.getElementById('photoSlidePrevious').style.background = 'url(http://huntervetscycling.ocoth.net/media/images/photoslide/left.gif) center center no-repeat';
		}
	}
}

var slideInterval = null;

function continueSlide() {
	destination = calculateEndPoint();
	
	current = parseInt(document.getElementById('photoSlide').getElementsByTagName('ul')[0].style.marginLeft);
	
	//alert('c' + current + ', d' + destination);
	
	if(slideInterval) {
		if(destination == current) {
			clearInterval(slideInterval);
			
			slideInterval = null;
			//alert('stop!');
			hold = false;
		}
		else {
			difference = current - destination;
			
			otherDifference = (-5 + -121 * (position - 2)) - current;
			
			if(difference >= -2 && difference <= 2) {
				changeRate = 1
			}
			else if(difference >= -8 && difference <= 8) {
				changeRate = 2
			}
			else if(difference >= -20 && difference <= 20) {
				changeRate = 3
			}
			else {
				changeRate = 4
			}
			
			if(otherDifference >= -2 && otherDifference <= 2) {
				changeRate = 1
			}
			else if(otherDifference >= -8 && otherDifference <= 8) {
				changeRate = 2
			}
			else if(otherDifference >= -20 && otherDifference <= 20) {
				changeRate = 3
			}
			
			if(current > destination) {
				document.getElementById('photoSlide').getElementsByTagName('ul')[0].style.marginLeft = (current - changeRate) + 'px';
			}
			else {
				document.getElementById('photoSlide').getElementsByTagName('ul')[0].style.marginLeft = (current + changeRate) + 'px';
			}
		}
	}
	else {
		hold = true;
		slideInterval = window.setInterval("continueSlide()", 25);
	}
}

function showPhotograph(e) {
	triggerer = getTarget(e);
	
	locationToOpen = triggerer.src.replace(/width=105&height=70/, "wrap=true");
	
	widthOfOpen = 680;
	heightOfOpen =  680;
	
	topDimension = ((window.screen.height - heightOfOpen) / 2);
	
	if(topDimension < 0) {
		topDimension = 0;
	}
	
	leftDimension = ((window.screen.width - widthOfOpen) / 2);
	
	if(leftDimension < 0) {
		leftDimension = 0;
	}
	
	window.open(locationToOpen, 'viewPhotograph', "top=" + topDimension + ",left=" + leftDimension + ",width=" + widthOfOpen + ",height=" + heightOfOpen + ",menubar=no,location=no,resizable=yes,scrollbars=no,status=yes");
}

function createPhotoSlide() {
	if(document.getElementById('photoSlide')) {
		previousLink = createElement('a');
		previousLink.setAttribute('href', '#');
		previousLink.setAttribute('id','photoSlidePrevious');
		addEvent(previousLink, 'click', previousPhotoSlide);
		
		document.getElementById('photoSlide').appendChild(previousLink);
		
		viewPort = createElement('div');
		viewPort.className = 'viewPort';
		
		album = createElement('ul');
		album.className = 'album';
		
				item61 = createElement('li');
		image61 = createElement('img');
			image61.setAttribute('width', '105');
			image61.setAttribute('height', '70');
			image61.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/61?width=105&height=70');
			image61.setAttribute('alt', '');
		item61.appendChild(image61);
		addEvent(image61, 'click', showPhotograph);
		album.appendChild(item61);
				item62 = createElement('li');
		image62 = createElement('img');
			image62.setAttribute('width', '105');
			image62.setAttribute('height', '70');
			image62.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/62?width=105&height=70');
			image62.setAttribute('alt', '');
		item62.appendChild(image62);
		addEvent(image62, 'click', showPhotograph);
		album.appendChild(item62);
				item63 = createElement('li');
		image63 = createElement('img');
			image63.setAttribute('width', '105');
			image63.setAttribute('height', '70');
			image63.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/63?width=105&height=70');
			image63.setAttribute('alt', '');
		item63.appendChild(image63);
		addEvent(image63, 'click', showPhotograph);
		album.appendChild(item63);
				item64 = createElement('li');
		image64 = createElement('img');
			image64.setAttribute('width', '105');
			image64.setAttribute('height', '70');
			image64.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/64?width=105&height=70');
			image64.setAttribute('alt', '');
		item64.appendChild(image64);
		addEvent(image64, 'click', showPhotograph);
		album.appendChild(item64);
				item65 = createElement('li');
		image65 = createElement('img');
			image65.setAttribute('width', '105');
			image65.setAttribute('height', '70');
			image65.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/65?width=105&height=70');
			image65.setAttribute('alt', '');
		item65.appendChild(image65);
		addEvent(image65, 'click', showPhotograph);
		album.appendChild(item65);
				item66 = createElement('li');
		image66 = createElement('img');
			image66.setAttribute('width', '105');
			image66.setAttribute('height', '70');
			image66.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/66?width=105&height=70');
			image66.setAttribute('alt', '');
		item66.appendChild(image66);
		addEvent(image66, 'click', showPhotograph);
		album.appendChild(item66);
				item67 = createElement('li');
		image67 = createElement('img');
			image67.setAttribute('width', '105');
			image67.setAttribute('height', '70');
			image67.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/67?width=105&height=70');
			image67.setAttribute('alt', '');
		item67.appendChild(image67);
		addEvent(image67, 'click', showPhotograph);
		album.appendChild(item67);
				item70 = createElement('li');
		image70 = createElement('img');
			image70.setAttribute('width', '105');
			image70.setAttribute('height', '70');
			image70.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/70?width=105&height=70');
			image70.setAttribute('alt', '');
		item70.appendChild(image70);
		addEvent(image70, 'click', showPhotograph);
		album.appendChild(item70);
				item71 = createElement('li');
		image71 = createElement('img');
			image71.setAttribute('width', '105');
			image71.setAttribute('height', '70');
			image71.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/71?width=105&height=70');
			image71.setAttribute('alt', '');
		item71.appendChild(image71);
		addEvent(image71, 'click', showPhotograph);
		album.appendChild(item71);
				item73 = createElement('li');
		image73 = createElement('img');
			image73.setAttribute('width', '105');
			image73.setAttribute('height', '70');
			image73.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/73?width=105&height=70');
			image73.setAttribute('alt', '');
		item73.appendChild(image73);
		addEvent(image73, 'click', showPhotograph);
		album.appendChild(item73);
				item74 = createElement('li');
		image74 = createElement('img');
			image74.setAttribute('width', '105');
			image74.setAttribute('height', '70');
			image74.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/74?width=105&height=70');
			image74.setAttribute('alt', '');
		item74.appendChild(image74);
		addEvent(image74, 'click', showPhotograph);
		album.appendChild(item74);
				item75 = createElement('li');
		image75 = createElement('img');
			image75.setAttribute('width', '105');
			image75.setAttribute('height', '70');
			image75.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/75?width=105&height=70');
			image75.setAttribute('alt', '');
		item75.appendChild(image75);
		addEvent(image75, 'click', showPhotograph);
		album.appendChild(item75);
				item76 = createElement('li');
		image76 = createElement('img');
			image76.setAttribute('width', '105');
			image76.setAttribute('height', '70');
			image76.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/76?width=105&height=70');
			image76.setAttribute('alt', '');
		item76.appendChild(image76);
		addEvent(image76, 'click', showPhotograph);
		album.appendChild(item76);
				item77 = createElement('li');
		image77 = createElement('img');
			image77.setAttribute('width', '105');
			image77.setAttribute('height', '70');
			image77.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/77?width=105&height=70');
			image77.setAttribute('alt', '');
		item77.appendChild(image77);
		addEvent(image77, 'click', showPhotograph);
		album.appendChild(item77);
				item78 = createElement('li');
		image78 = createElement('img');
			image78.setAttribute('width', '105');
			image78.setAttribute('height', '70');
			image78.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/78?width=105&height=70');
			image78.setAttribute('alt', '');
		item78.appendChild(image78);
		addEvent(image78, 'click', showPhotograph);
		album.appendChild(item78);
				item79 = createElement('li');
		image79 = createElement('img');
			image79.setAttribute('width', '105');
			image79.setAttribute('height', '70');
			image79.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/79?width=105&height=70');
			image79.setAttribute('alt', '');
		item79.appendChild(image79);
		addEvent(image79, 'click', showPhotograph);
		album.appendChild(item79);
				item80 = createElement('li');
		image80 = createElement('img');
			image80.setAttribute('width', '105');
			image80.setAttribute('height', '70');
			image80.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/80?width=105&height=70');
			image80.setAttribute('alt', '');
		item80.appendChild(image80);
		addEvent(image80, 'click', showPhotograph);
		album.appendChild(item80);
				item81 = createElement('li');
		image81 = createElement('img');
			image81.setAttribute('width', '105');
			image81.setAttribute('height', '70');
			image81.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/81?width=105&height=70');
			image81.setAttribute('alt', '');
		item81.appendChild(image81);
		addEvent(image81, 'click', showPhotograph);
		album.appendChild(item81);
				item82 = createElement('li');
		image82 = createElement('img');
			image82.setAttribute('width', '105');
			image82.setAttribute('height', '70');
			image82.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/82?width=105&height=70');
			image82.setAttribute('alt', '');
		item82.appendChild(image82);
		addEvent(image82, 'click', showPhotograph);
		album.appendChild(item82);
				item83 = createElement('li');
		image83 = createElement('img');
			image83.setAttribute('width', '105');
			image83.setAttribute('height', '70');
			image83.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/83?width=105&height=70');
			image83.setAttribute('alt', '');
		item83.appendChild(image83);
		addEvent(image83, 'click', showPhotograph);
		album.appendChild(item83);
				item84 = createElement('li');
		image84 = createElement('img');
			image84.setAttribute('width', '105');
			image84.setAttribute('height', '70');
			image84.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/84?width=105&height=70');
			image84.setAttribute('alt', '');
		item84.appendChild(image84);
		addEvent(image84, 'click', showPhotograph);
		album.appendChild(item84);
				item85 = createElement('li');
		image85 = createElement('img');
			image85.setAttribute('width', '105');
			image85.setAttribute('height', '70');
			image85.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/85?width=105&height=70');
			image85.setAttribute('alt', '');
		item85.appendChild(image85);
		addEvent(image85, 'click', showPhotograph);
		album.appendChild(item85);
				item86 = createElement('li');
		image86 = createElement('img');
			image86.setAttribute('width', '105');
			image86.setAttribute('height', '70');
			image86.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/86?width=105&height=70');
			image86.setAttribute('alt', '');
		item86.appendChild(image86);
		addEvent(image86, 'click', showPhotograph);
		album.appendChild(item86);
				item87 = createElement('li');
		image87 = createElement('img');
			image87.setAttribute('width', '105');
			image87.setAttribute('height', '70');
			image87.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/87?width=105&height=70');
			image87.setAttribute('alt', '');
		item87.appendChild(image87);
		addEvent(image87, 'click', showPhotograph);
		album.appendChild(item87);
				item88 = createElement('li');
		image88 = createElement('img');
			image88.setAttribute('width', '105');
			image88.setAttribute('height', '70');
			image88.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/88?width=105&height=70');
			image88.setAttribute('alt', '');
		item88.appendChild(image88);
		addEvent(image88, 'click', showPhotograph);
		album.appendChild(item88);
				item89 = createElement('li');
		image89 = createElement('img');
			image89.setAttribute('width', '105');
			image89.setAttribute('height', '70');
			image89.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/89?width=105&height=70');
			image89.setAttribute('alt', '');
		item89.appendChild(image89);
		addEvent(image89, 'click', showPhotograph);
		album.appendChild(item89);
				item90 = createElement('li');
		image90 = createElement('img');
			image90.setAttribute('width', '105');
			image90.setAttribute('height', '70');
			image90.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/90?width=105&height=70');
			image90.setAttribute('alt', '');
		item90.appendChild(image90);
		addEvent(image90, 'click', showPhotograph);
		album.appendChild(item90);
				item91 = createElement('li');
		image91 = createElement('img');
			image91.setAttribute('width', '105');
			image91.setAttribute('height', '70');
			image91.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/91?width=105&height=70');
			image91.setAttribute('alt', '');
		item91.appendChild(image91);
		addEvent(image91, 'click', showPhotograph);
		album.appendChild(item91);
				item92 = createElement('li');
		image92 = createElement('img');
			image92.setAttribute('width', '105');
			image92.setAttribute('height', '70');
			image92.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/92?width=105&height=70');
			image92.setAttribute('alt', '');
		item92.appendChild(image92);
		addEvent(image92, 'click', showPhotograph);
		album.appendChild(item92);
				item93 = createElement('li');
		image93 = createElement('img');
			image93.setAttribute('width', '105');
			image93.setAttribute('height', '70');
			image93.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/93?width=105&height=70');
			image93.setAttribute('alt', '');
		item93.appendChild(image93);
		addEvent(image93, 'click', showPhotograph);
		album.appendChild(item93);
				item94 = createElement('li');
		image94 = createElement('img');
			image94.setAttribute('width', '105');
			image94.setAttribute('height', '70');
			image94.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/94?width=105&height=70');
			image94.setAttribute('alt', '');
		item94.appendChild(image94);
		addEvent(image94, 'click', showPhotograph);
		album.appendChild(item94);
				item95 = createElement('li');
		image95 = createElement('img');
			image95.setAttribute('width', '105');
			image95.setAttribute('height', '70');
			image95.setAttribute('src', 'http://huntervetscycling.ocoth.net/media/photographs/95?width=105&height=70');
			image95.setAttribute('alt', '');
		item95.appendChild(image95);
		addEvent(image95, 'click', showPhotograph);
		album.appendChild(item95);
				
		viewPort.appendChild(album);
		
		document.getElementById('photoSlide').appendChild(viewPort);
		
		nextLink = createElement('a');
		nextLink.setAttribute('href', '#');
		nextLink.setAttribute('id','photoSlideNext');
		addEvent(nextLink, 'click', nextPhotoSlide);
		
		document.getElementById('photoSlide').appendChild(nextLink);
		
		document.getElementById('photoSlide').getElementsByTagName('ul')[0].style.marginLeft = '-5px';
		
		if(itemCount <= 4) {
			// hide next
			document.getElementById('photoSlideNext').style.background = 'transparent';
		}
	}
}

function calculateEndPoint() {
	return -5 + -121 * (position - 1);
}

addLoadEvent(createPhotoSlide);

//ON LOAD
	//get total item count


// scroll to: margin-left = -5 - 121(position - 1)

//ON SCROLLTO
// if initial, get position and check position and calculate scroll to point, start scrolling, trigger interval
// else check if scroll needed, scroll, else, cancel interval
// if last item, remove right arrow
// if first item, remove left arrow


/*
	CREATE:

	<a href="#" class="previous"></a>
	<div class="viewport">
		<ul class="album">
			<li><img src="" width="" height="" alt="" /></li>
			<li>Test 2</li>
			<li>Test 3</li>
			<li>Test 4</li>
			<li>Test 5</li>
			<li>Test 6</li>
			<li>Test 7</li>
			<li>Test 8</li>
			<li>Test 9</li>
			<li>Test 10</li>
		</ul>
	</div>
	<a href="#" class="next"></a>
*/
