function moveArrow(offVal) {
	if(offVal!='') $('navBox').morph({'background-position': offVal+'px 25px'});
	else $('navBox').morph({'background-position': $('original_offset').value+'px 25px'});
}
function runSlideShow() {
	var id_string = $('id_string').value.split('#');
	var nextid = parseInt($('currentID').value) + 1;
	if(nextid==(parseInt($('total').value) + 1)) nextid = 1;
	$('currentID').value = nextid;
	$('frontImg').style.backgroundImage = $('backImg').style.backgroundImage;
	$('frontImg').fade('show');
	$('backImg').style.backgroundImage = 'url('+id_string[nextid]+')';
	setTimeout("performSwap()",$('timeout').value);
}
function performSwap() {
	$('frontImg').fade('out');
	setTimeout("runSlideShow()",600);
}
function showLabel(id) {
	$('text'+id).style.display = '';
	$('text'+id).fade(0.9);
}
function hideLabel(id) {
	$('text'+id).fade('out');
}
function hideTextBoxes(cntr) {
	for(var t=1;t<=parseInt(cntr);t++) {
		$('text'+t).fade('hide');
	}
}
function setHeightWidth() {
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
  	}
	$('boxCheck').style.height = yWithScroll+'px';
}
function closeItemDescription() {
	$('boxCheck').fade('out');
	$('changeBox').fade('out');
	document.body.style.height = '';
	setTimeout("$('boxCheck').style.display = 'none';$('changeBox').style.display = 'none';",600);
}

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

var http = getHTTPObject();

function loadItemDescription(itemID) {
	http.open("GET", "includes/get_item.php?itemID="+itemID, true);
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

var ie5 = (document.all && document.getElementById) ? true : false;
function useHttpResponse() {
	if(http.readyState==4) {
		// 0 - itemID   1 - description_img src   2 - item_name   3 - item_wood   4 - item_finishes   5 - other items   6 - items_options_table
		var textout = http.responseText.split("@#@");
        $('description_img').src = textout[1];
        $('item_name').innerHTML = textout[2];
        $('item_wood').innerHTML = textout[3];
        $('item_finishes').innerHTML = textout[4];
		$('item_link').itemID = textout[0];
		$('item_link').onclick = function() { print_PDF(this.itemID); }

		if(textout[5]!='') {
			$('other_items_row1').style.display = '';
			$('other_items_row2').style.display = '';
			var target = $('other_items');
			target.innerHTML = '';
			var other_items = textout[5].split('(-)');
            div_cnt = 0;
			for(var t=0;t<other_items.length;t++) {
                if(div_cnt==0) {
                    spaceDiv = document.createElement("div");
                    spaceDiv.innerHTML = " ";
            	    target.appendChild(spaceDiv);
                }
				var parts = other_items[t].split('||');
				var img = document.createElement("img");
				img.itemID = parts[0];
				img.style.cursor = 'hand';
				img.style.cursor = 'pointer';
				img.onclick = function() { $('changeBox').fade('out');setTimeout("loadItemDescription('"+this.itemID+"')",500); }
				img.src = parts[1];
				img.align = 'top';
				img.vspace = 5;
				img.hspace = 5;
            	spaceDiv.appendChild(img);
                div_cnt++;
                if(div_cnt==6) div_cnt = 0;
			}
		} else {
			$('other_items_row1').style.display = 'none';
			$('other_items_row2').style.display = 'none';
		}

		if(textout[6]!='') {
			var target = $('items_options_table');
		    if(ie5) { if(target.hasChildNodes()) while (target.childNodes.length>1) target.removeChild(target.lastChild); }
		    else { if(target.hasChildNodes()) while (target.childNodes.length>2) target.removeChild(target.lastChild); }
			var items_options = textout[6].split('(-)');
			for(var t=0;t<items_options.length;t++) {
				var parts = items_options[t].split('||');
				var tr = document.createElement("tr");
				for(var w=0;w<parts.length;w++) {
					var td = document.createElement("td");
					td.align = 'center';
					if(w>0) td.style.borderLeft = '1px solid #DDDDDD';
					if(w>1) td.innerHTML = parts[w]+'mm';
					else td.innerHTML = parts[w];
					tr.appendChild(td);
				}
				target.appendChild(tr);
			}
		}
		setTimeout("showDescription()",100);
	}
}

function showDescription() {
	var obj1 = $('boxCheck');
	var obj2 = $('changeBox');
	obj2.style.visibility = 'hidden';
	obj2.style.display = 'block';
	var div_width = parseInt(obj2.style.width);
    var div_height = parseInt(obj2.offsetHeight);
	var page_width = parseInt(document.body.offsetWidth);
	var page_height = parseInt(document.body.offsetHeight);
	obj1.style.height = page_height+'px';
	obj1.style.width = page_width+'px';

	if(div_height>(page_height-60)) {
		page_height = div_height + 60;
        document.body.style.height = page_height+'px';
		obj1.style.height = page_height+'px';
	}

    var leftOff = Math.round((page_width - div_width)/2);
    var topOff = Math.round((page_height - div_height)/2) + document.body.scrollTop;
	obj2.style.top = topOff+'px';
	obj2.style.left = leftOff+'px';
	obj1.style.display = 'block';
	obj1.fade(0.4);
	obj2.style.display = 'block';
	obj2.fade('in');
}

function print_PDF(itemID) {
	var url = 'print_item.php?id='+itemID;
	window.open(url, "item",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,modal=yes,close=no,resizable=1,width=800,height=600,left=100,top=100');
}
function loadGallery(id,width,height,comment,ext) {
	var page_width = parseInt(document.body.clientWidth);
	var page_length = parseInt(document.body.scrollHeight);
	var page_height = parseInt(document.body.clientHeight);
    if(page_length<page_height) page_length = page_height;

	var scroll = document.body.scrollTop;

	var obj = document.getElementById('image_div');
	var div_width = parseInt(obj.style.width);
	var div_height = parseInt(obj.style.height);

	var prevleftOff = Math.round((page_width - div_width)/2);
	var newleftOff = Math.round((page_width - (width))/2);
	var prevtopOff = Math.round((page_height - div_height)/2) + scroll;
	var newtopOff = Math.round((page_height - (height))/2) + scroll;

	$('image_div').style.display = 'block';
	$('blackout').style.display = 'block';
	$('blackout').style.width = page_width+'px';
	$('blackout').style.height = page_length+'px';
	$('image_div').style.top=prevtopOff+'px';
	$('image_div').style.left=prevleftOff+'px';
	$('actual_img').src = 'uploads/c'+id+'.'+ext;
	$('comment_img').innerHTML = comment;
	$('actual_img').fade('hide');
	$('comment_img').fade('hide');
	$('blackout').morph({'opacity': 0.85});
	$('image_div').morph({'opacity': 1});
	setTimeout("$('image_div').morph({'left': '"+newleftOff+"px','width': '"+width+"px'});",500);
	setTimeout("$('image_div').morph({'top': '"+newtopOff+"px','height': '"+(height + 45)+"px'});",1000);
	setTimeout("$('actual_img').morph({'opacity': 1});",1500);
	setTimeout("$('comment_img').morph({'opacity': 1});",1500);
}

function closeGallery(width,height) {
	$('blackout').morph({'opacity': 0,'moz-opacity': 0});
	$('image_div').morph({'opacity': 0});
	setTimeout("$('blackout').style.display = 'none';",500);
	setTimeout("$('image_div').style.display = 'none';",500);
	setTimeout("$('image_div').style.width = '"+width+"px';",500);
	setTimeout("$('image_div').style.height = '"+height+"px';",500);
	setTimeout("$('image_div').style.background = '#FFFFFF url(images/loading.gif) no-repeat scroll top left';",500);
	setTimeout("$('actual_img').src = '';",500);
	setTimeout("$('comment_img').innerHTML = '';",500);
}
function adjustGallery() {
	if($('image_div').style.display=='block') {
		var height = $('image_div').offsetHeight;
		var scroll = document.body.scrollTop;
		var page_height = parseInt(document.body.clientHeight);
		var newtopOff = Math.round((page_height - (height + 20))/2) + scroll;
    	$('image_div').morph({'top': newtopOff+'px'});
	}
}
function resizeGallery() {
	if($('image_div').style.display=='block') {
		var page_width = parseInt(document.body.clientWidth);
		var page_length = parseInt(document.body.scrollHeight);
		var page_height = parseInt(document.body.clientHeight);
	    if(page_length<page_height) page_length = page_height;
       	$('blackout').style.width = page_width+'px';
		$('blackout').style.height = page_length+'px';

		var height = $('image_div').offsetHeight;
		var width = $('image_div').offsetWidth;
		var scroll = document.body.scrollTop;
		var newtopOff = Math.round((page_height - (height + 20))/2) + scroll;
		var newleftOff = Math.round((page_width - (width + 20))/2);
    	$('image_div').morph({'top': newtopOff+'px','left': newleftOff+'px'});
	}
}