if ( window.addEventListener ){
	window.addEventListener('load', getLinks, false);
} else {
	window.attachEvent('load', getLinks, false);
}
if ( window.addEventListener ){
	window.addEventListener('load', prep, false);
} else {
	window.attachEvent('load', prep, false);
}

function ToggleRes()
{
	
	if ( document.getElementById('reservation_info') ){
		var res_info = document.getElementById('reservation_info');
		
		if ( document.contact_form.reservation.checked ){
			res_info.style.display = 'block';
		} else {
			res_info.style.display = 'none';
		}

	}
	
}

function prep()
{

	document.onclick = function(e){
		hidemenus()
	}
	
	ToggleRes();
	
	var links = document.getElementsByTagName('a');
	var link_num = links.length;
	for ( var x=0; x < link_num; x++ ){
		if ( links[x].className == 'menu' ){
			links[x].onclick = function(e){
				if (!e) var e = window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				return openClose(this);
			}
		}	
	}
}


function hidemenus()
{
	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' ){
			menus[n].style.display = 'none';
		}
	}
}

function openClose(obj){
	
	var parent = obj.parentNode;
	var childList = parent.getElementsByTagName("ul");
	if (childList[0].style.display == "none" || childList[0].style.display == "" ){
		childList[0].style.display = "block";
	} else {
		childList[0].style.display = "none";
	}
	hideothers(childList[0]);
	return false;
}

function hideothers(obj)
{
	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' && menus[n] != obj ){
			menus[n].style.display = 'none';
		}
	}
}


function getLinks()
{
	if ( document.getElementById('main_nav') ){
		var main_nav = document.getElementById('main_nav');
		var links = main_nav.getElementsByTagName('a');
		var link_num = links.length;
		for ( var x=0; x < link_num; x++ ){
			links[x].onclick = function(){
				return bikePic(this);
			}
		}
	}
}

var flag = 1;
var swtch = 1;
var current_special = null;
function showSpecial(link)
{
	if ( link != current_special ){
		current_special = link;
		var main = document.getElementById('main');
		
		
		if ( document.getElementById('overlay') ){
			var overlay = document.getElementById('overlay');
			var fade = false;
			var fade_pic = true;
		} else {
			//make overlay
			var overlay = document.createElement('div');
			overlay.setAttribute('id', 'overlay');
			var fade = true;
			var fade_pic = false;
		}
		removeChildren(overlay);
		//main.style.overflow = 'hidden';
			
		var picholder = document.createElement('div');
		picholder.setAttribute('id', 'specialPicholder');
		overlay.appendChild(picholder);
		
		var close_btn = document.createElement('img');
		close_btn.setAttribute('src', 'close.jpg');
		close_btn.setAttribute('class', 'close');
		close_btn.setAttribute('title', 'Close');
		close_btn.onclick = function(){
			return hideSpecial(this.parentNode);
		}
		overlay.appendChild(close_btn);
		
		var content = document.getElementById('content');
		content.appendChild(overlay);
		if ( fade ){
			setOpacity(overlay, 0);
			fadein('overlay', 0);
		}
		
		var bike_name = link.firstChild.data;
		var bike_id = link.getAttribute('data-bikeid');
		
		if ( document.getElementById('specialHeading') ){
			var heading = document.getElementById('specialHeading');
		} else {
			var heading = document.createElement('specialHeading');
			heading.setAttribute('id', 'specialHeading');
		}
		
		removeChildren(heading);
		heading.appendChild(document.createTextNode(bike_name));
		heading.appendChild(document.createElement('br'));
		heading.innerHTML += 'click <a href="specs.php?id='+bike_id+'" onclick="specialSpecs('+bike_id+'); return false;" data-bikeid="'+bike_id+'">here</a> for specs';
		
		overlay.appendChild(heading);
		
		if ( document.getElementById('special_main_pic') ){
			var current_pic = document.getElementById('special_main_pic');
			current_pic.parentNode.removeChild(current_pic);
		}
		
		var pic = link.getAttribute('data-picture');
		var bikeimg = new Image();
		bikeimg.onload = function(){
			var photo = document.createElement('img');
			photo.setAttribute('src', 'images/bikes/'+pic);
			photo.setAttribute('id', 'special_main_pic');
			photo.setAttribute('alt', '');
			
			if ( fade_pic == true ){
				setOpacity(photo, 0);
				picholder.appendChild(photo);
				fadein('special_main_pic', 0);
			} else {
				picholder.appendChild(photo);
			}
			
			
		}
		bikeimg.src = 'images/bikes/'+pic;
		bikeimg.setAttribute('alt', '');
	
	}
	return false;
}

function specialSpecs(id)
{
	var url = 'specs.php?id='+id;
	
	var req = makeRequest(url);
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				if ( document.getElementById('specialSpecsheet') ){
					var specsheet = document.getElementById('specialSpecsheet');
				} else {
					var specsheet = document.createElement('div');
					specsheet.setAttribute('id', 'specialSpecsheet');
				}
				document.getElementById('overlay').appendChild(specsheet);
				specsheet.innerHTML = req.responseText;
				specsheet.style.display = 'block';
				
			} else {
				//alert('There was a problem with the request.');
			}
		}
	};
	req.open('GET', url, true);
	req.send(null);
	return false;
}

function hideSpecial(obj)
{
	current_special = null; 
	var main = obj.parentNode;
	main.removeChild(obj);
	//var obj_id = obj.getAttribute('id');
	//fadeout(obj_id, 100, removeSpecial);
}

function removeSpecial()
{
	var overlay = document.getElementById('overlay');
	overlay.parentNode.removeChild(obj);
}

function bikePic(link)
{
	
	if ( flag == 1 ){
		flag = 0;
		var main = document.getElementById('main');
		var picholder = document.getElementById('picholder');
		if (swtch == 1){
			var current_slide = 'main_pic';
			var new_slide = 'main_pic2';
			swtch = 2;
		} else {
			var current_slide = 'main_pic2';
			var new_slide = 'main_pic';
			swtch = 1;
		}
		
		//change heading
		if ( document.getElementById('special') ){
			var heading = document.getElementById('special');
			
		} else {
			var heading = document.createElement('p');
			heading.setAttribute('id', 'special');
			main.appendChild(heading);
		}
		
		var bike_name = link.firstChild.data;
		var bike_id = link.getAttribute('data-bikeid');
		removeChildren(heading);
		heading.appendChild(document.createTextNode(bike_name));
		heading.appendChild(document.createElement('br'));
		heading.innerHTML += 'click <a href="specs.php?id='+bike_id+'" onclick="getSpecs(this); return false;" data-bikeid="'+bike_id+'">here</a> for specs';
		
		var pic = link.getAttribute('data-picture');
		var bikeimg = new Image();
		bikeimg.onload = function(){
			
			var imgheight = bikeimg.height;
			var imgwidth = bikeimg.width;
			
			var biketop = Math.round((460 - imgheight) / 2);
			var bikeleft = Math.round((700 - imgwidth) / 2);
			
			var photo = document.createElement('img');
			photo.setAttribute('src', 'images/bikes/'+pic);
			photo.setAttribute('id', new_slide);
			photo.setAttribute('alt', '');
			
			photo.style.top = biketop+'px';
			photo.style.left = bikeleft+'px';
			
			setOpacity(photo, 0);
			picholder.appendChild(photo);
			fadeout(current_slide, 0, removeBike);
			fadein(new_slide, 0);
		}
		bikeimg.src = 'images/bikes/'+pic;
		bikeimg.setAttribute('alt', '');
	}
	
	return false;
}

function getSpecs(link)
{
	var bike_id = link.getAttribute('data-bikeid');
	var url = 'specs.php?id='+bike_id;

	var req = makeRequest(url);
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				if ( document.getElementById('specsheet') ){
					var specsheet = document.getElementById('specsheet');
				} else {
					var specsheet = document.createElement('div');
					specsheet.setAttribute('id', 'specsheet');
					document.getElementById('main').appendChild(specsheet);
				}
				
				specsheet.innerHTML = req.responseText;
				showSpec();
				
			} else {
				//alert('There was a problem with the request.');
			}
		}
	};
	req.open('GET', url, true);
	req.send(null);
	return false;
}


function showSpec()
{
	if (document.getElementById('specsheet')){
		var specsheet = document.getElementById('specsheet');
		specsheet.style.display = 'block';	
	}
}

function hideSpec(obj)
{
	obj.parentNode.style.display = 'none';
}

function makeRequest(url) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		/*if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}*/
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	return http_request;
}

function populateCart(req)
{
	if (req.readyState == 4) {
		if (req.status == 200) {

			var specsheet = parent.document.getElementById('quick_view_cart');
			quick_view_cart.innerHTML = req.responseText;
		} else {
			//alert('There was a problem with the request.');
		}
	}
}

function fadein(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		
		if (opacity == 0){
			obj.style.visibility = "visible";
		}
		
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 20;
			window.setTimeout("fadein('"+objId+"',"+opacity+")", 50);
		} else {
			flag = 1;
		}
	}
}

function fadeout(objId, opacity, callback) {
	if (document.getElementById) {
		obj = document.getElementById(objId);

		if (opacity <= 0){
			setOpacity(obj, opacity);
			if ( isFunction(callback) ){
				callback(objId);
			}
		} else if (opacity > 0) {
			setOpacity(obj, opacity);
			opacity = opacity - 20;
			window.setTimeout("fadeout('"+objId+"',"+opacity+")", 50);
		}
	}
}


function removeBike(objId)
{
	var obj = document.getElementById(objId);
	obj.parentNode.removeChild(obj);
	flag = 1;
}

function setOpacity(obj, opacity) {
	//opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function removeChildren(obj)
{
	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.lastChild);
	}
}

function isFunction(possibleFunction) {
  return (typeof(possibleFunction) == typeof(Function));
}

