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'});
	}
}
var theDrag = '';
var box_width = 0;
var box_height = 0;
var set_height = 0;
function getImgAtt(att) {
	var details = $('zoomImg').value.split("#");
	var width = details[2];
	var height = details[1];
	var large_src = details[0];
	var src_details = details[0].split(".");
	var imageID = src_details[0].split("/");
	imageID = imageID[(imageID.length - 1)].substr(1);
	var filext = src_details[1];
	var medium_src = src_details[0]+"m."+src_details[1];
	var thumb_src = src_details[0]+"t."+src_details[1];
	var ratio = (set_height / height);
	switch(att) {
		case 'imageID': return imageID; break;
		case 'filext': return filext; break;
		case 'width': return width; break;
		case 'height': return height; break;
		case 'lge_src': return large_src; break;
		case 'med_src': return medium_src; break;
		case 'thb_src': return thumb_src; break;
		case 'ratio': return ratio; break;
	}
}
function loadImage(imageID,filext,width,height) {
	$('zoomImg').value = 'uploads/c'+imageID+'.'+filext+'#'+height+'#'+width;
	var left_dist = Math.round((box_width - (getImgAtt('width') * getImgAtt('ratio')))/2);
	var top_dist = left_dist;
	box_height = (left_dist * 2) + set_height;
	hideDetailsDivFast();
	$('main_img').style.cursor = 'pointer';
	$('main_div').style.height = box_height+'px';
	$('details_div').style.top = (box_height - 34)+'px';
	$('main_img').style.height = set_height+'px';
	$('main_img').style.top = top_dist+'px';
	$('main_img').style.left = left_dist+'px';
    $('main_img').addEvent('dblclick', zoomInSetup);
    if($('details_div').getStyle('display')=='none') $('details_div').setStyle('display','block');
    if($('main_img').getStyle('display')=='none') {
    	$('main_img').addEvent('load', function() {
	    	$('main_img').setStyle('display','block');
    	});
    }
	$('main_img').src = 'uploads/c'+imageID+'m.'+filext;
}
function zoomInSetup() {
   	$('main_img').removeEvents('load');
	$('main_img').addEvent('load', zoomIn);
	$('main_img').src = getImgAtt('lge_src');
}
function zoomIn() {
    $('main_img').removeEvents('dblclick');
    $('main_img').removeEvents('load');
	var new_width = getImgAtt('width');
	var new_height = getImgAtt('height');
	var start_x = Math.round((new_width - box_width) / 2);
	var start_y = Math.round((new_height - box_height) / 2);
	$('main_img').set('morph',{duration: 800, onComplete: setUpDrag});
	$('main_img').morph({height: new_height+'px', top: '-'+start_y+'px', left: '-'+start_x+'px'});
}
function setUpDrag() {
	var xlimit = 0 - (getImgAtt('width') - box_width);
	var ylimit = 0 - (getImgAtt('height') - box_height);
	$('main_img').style.cursor = 'move';
    theDrag = $('main_img').makeDraggable({
    	limit: {x:[xlimit,0],y:[ylimit,0]},
    	preventDefault: true
    });
    $('main_img').addEvent('dblclick', zoomOut);
}
function zoomOut() {
	theDrag.detach();
	$('main_img').style.cursor = 'pointer';
	var top_dist = Math.round((box_height - set_height)/2);
	var left_dist = Math.round((box_width - (getImgAtt('width') * getImgAtt('ratio')))/2);
	$('main_img').set('morph',{duration: 800, onComplete: zoomOutComplete});
	$('main_img').morph({height: set_height+'px', top: top_dist+'px', left: left_dist+'px'});
}
function zoomOutComplete() {
	$('main_img').src = getImgAtt('med_src');
    $('main_img').removeEvents('dblclick');
    $('main_img').removeEvents('load');
    $('main_img').addEvent('dblclick', zoomInSetup);
}
function initializeSettings() {
	if($('main_img')) {
		$('main_img').addEvent('dblclick', zoomInSetup);
		box_width = parseInt($('main_div').getStyle('width'));
		box_height = parseInt($('main_div').getStyle('height'));
		set_height = parseInt($('main_img').getStyle('height'));
		loadImage(getImgAtt('imageID'),getImgAtt('filext'),getImgAtt('width'),getImgAtt('height'));
	}
}
window.onload = function() {
	initializeSettings();
	loadClickEvents();
	setTimeout("initTrans();",7000);
}
function showDetailsDiv() {
	var imageID = getImgAtt('imageID');
	var http = getHTTPObject();
	http.open("GET", "includes/get_tails.php?imageID="+imageID, true);
	http.onreadystatechange = function(http) {
		if(this.readyState==4) {
			var textout = this.responseText.split('#@#');
			$('dress_name').innerHTML = textout[0].toUpperCase();
			if(textout.length>1) {
				var colours = textout[1].split("#!#");
				$('dress_colours').innerHTML = '';
				var cntr = 0;
				for(var t=0;t<colours.length;t++) {
					var colour_split = colours[t].split("(-)");
					var div = document.createElement('div');
					div.className = 'colour_box';
					div.style.backgroundColor = '#'+colour_split[1];
					div.innerHTML = '&nbsp;';
					$('dress_colours').appendChild(div);
					var div = document.createElement('div');
					div.className = 'colour_label';
					div.innerHTML = colour_split[0].toUpperCase();
					$('dress_colours').appendChild(div);
                    //added
					var div = document.createElement('div');
					div.className = 'colour_filler';
					div.innerHTML = '&nbsp;';
					$('dress_colours').appendChild(div);
                    /*
					if(cntr==1) {
						var div = document.createElement('div');
						div.className = 'colour_filler';
						div.innerHTML = '&nbsp;';
						$('dress_colours').appendChild(div);
					}
					cntr++;
					if(cntr==2) cntr = 0;
                    */
				}
			} else $('dress_colours').innerHTML = '<b>NO COLOUR INFORMATION AVAILABLE</b>';
			var div_height = $('details_div').offsetHeight;
			$('details_div').set('morph',{
				duration: 800,
				onComplete: function() {
					$('details_link').innerHTML = 'HIDE DRESS DETAILS';
					$('details_link').onclick = hideDetailsDiv;
				}
			});
			$('details_div').morph({top: (box_height - div_height)+'px'});
		}	
	};
	http.send(null);
}
function hideDetailsDiv() {
	$('details_div').set('morph',{
		duration: 800,
		onComplete: function() {
			$('details_link').innerHTML = 'SHOW DRESS DETAILS';
			$('details_link').onclick = showDetailsDiv;
		}
	});
	$('details_div').morph({top: (box_height - 34)+'px'});
}
function hideDetailsDivFast() {
	$('details_link').innerHTML = 'SHOW DRESS DETAILS';
	$('details_link').onclick = showDetailsDiv;
}
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;
}
function initTrans() {
	setTimeout("initTrans();",10000);
	var imgs_array = $('imgs_avail').value.split("#");
	var next_key = parseInt($('next_key').value);
	var delay = 200;
	for(var t=1;t<=9;t++) {
		$('ss_img'+t).removeEvents('click');
		$('ss_img'+t).setStyle('opacity',0);
		var img_src = $('ss_img'+t).src;
		$('ss_div'+t).setStyle('background-image','url('+img_src+')');
		$('ss_img'+t).src = 'uploads/c'+imgs_array[next_key];
		$('ss_img'+t).set('morph',{duration: 1000});
		setTimeout("doTrans('"+t+"');",delay);
		delay += 200;
		next_key += 1;
		if(typeof(imgs_array[next_key])=='undefined'||imgs_array[next_key]=='') next_key = 0;
	}
	$('next_key').value = next_key;
}
function doTrans(imgCode) {
	$('ss_img'+imgCode).morph({opacity: 1});
	var imgID = $('ss_img'+imgCode).src.split("/c");
	imgID = imgID[1].split("t");
	imgID = imgID[0];
	$('ss_img'+imgCode).imgID = imgID;
	$('ss_img'+imgCode).addEvent('click',function() { window.location.href = 'index.php?pageID=S&imageID='+this.imgID; });
}
var hoverTimeout = 0;
function loadClickEvents() {
	var t = 1;
	while($('img'+t)) {
		$('img'+t).addEvent('mousemove', function(event) {
			clearTimeout(hoverTimeout);
			if(!$('tooltip')) {
				var div = document.createElement("div");
				div.id = 'tooltip';
				div.className = 'tooltip';
				$(document.body).appendChild(div);
				$('tooltip').setStyle('opacity', 0);
				$('tooltip').setStyle('display', 'block');
				$('tooltip').set('morph',{duration:200});
			}
			$('tooltip').setStyle('left',(event.page.x + 15));
			$('tooltip').setStyle('top',(event.page.y + 15));
			$('tooltip').innerHTML = this.alt.toUpperCase();
			$('tooltip').morph({opacity: 0.75});
		});
		$('img'+t).addEvent('mouseout', function() {
			hoverTimeout = setTimeout("$('tooltip').morph({opacity: 0});",500);
		});
		t++;
	}
}
