function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

var easing = 0.18;
var zoomInterval = 0; 
var mouseX = 0;
var mosueY = 0;
var sHeight;
var elements;
var elements2;
var elements3;
var scrollBorder;
var scrollBorder2;
var scrollBorder3;
var thumbsPos = new Array();
var thumbsPosDown = new Array();
var thumbsPos2 = new Array();
var thumbsPosDown2 = new Array();
var thumbsPos3 = new Array();
var thumbsPosDown3 = new Array();
var imgAmount;
var imgAmount2;
var imgAmount3;
var i = 0;
var idc = 0;
var idc2 = 0;
var idc2 = 0;
var delay = 0;
var rollOverInetrval;
var hideInterval;
var str =  new String(window.location);
var nr;
var issueNumber;
var pageNumber;
var images;
var images2;
var images3;
var scrollHeight; 
var scrollHeight2;
var scrollHeight3;
var no_list_shift = false;


function initShafts()
{
	sHeight = 92;
	elements = 0;
	idc = 0;
	
	if(shaft_length > 0) {
		elements = countElements("thumbnails-mask","img");
		scrollHeight = sHeight * elements ;
	}
	
	if (elements > 1) {
		YAHOO.util.Event.removeListener("up-arrow", "click");
		YAHOO.util.Event.removeListener("down-arrow", "click");
		YAHOO.util.Event.addListener("up-arrow", "click", moveDown);
		YAHOO.util.Event.addListener("down-arrow", "click", moveUp);
		countsImages ('thumbnails-mask');
	} else {
		YAHOO.util.Event.removeListener("up-arrow", "click");
		YAHOO.util.Event.removeListener("down-arrow", "click");
	}
}

function initGrips()
{
	sHeight = 92;
	idc2 = 0;
	
	elements2 = countElements("thumb-mask2","img");
	scrollHeight2 = sHeight * elements2 ;
		
	if (elements2 > 1) {
		YAHOO.util.Event.removeListener("up-arrow2", "click");
		YAHOO.util.Event.removeListener("down-arrow2", "click");
		YAHOO.util.Event.addListener("up-arrow2", "click", moveDown2);
		YAHOO.util.Event.addListener("down-arrow2", "click", moveUp2);
		countsImages2 ('thumb-mask2');
	} else {
		YAHOO.util.Event.removeListener("up-arrow2", "click");
		YAHOO.util.Event.removeListener("down-arrow2", "click");
	}

    if (jQuery('#build_image_ag.build_active').length) {
        setTimeout("preloadBuildAG();", 500);
    }
}

function initFerrules()
{
	sHeight = 92;
	idc3 = 0;
	
	elements3 = countElements("thumb-mask3","img");
	scrollHeight3 = sHeight * elements3 ;
		
	if (elements3 > 1) {
		YAHOO.util.Event.removeListener("up-arrow3", "click");
		YAHOO.util.Event.removeListener("down-arrow3", "click");
		YAHOO.util.Event.addListener("up-arrow3", "click", moveDown3);
		YAHOO.util.Event.addListener("down-arrow3", "click", moveUp3);
		countsImages3 ('thumb-mask3');
	} else {
		YAHOO.util.Event.removeListener("up-arrow3", "click");
		YAHOO.util.Event.removeListener("down-arrow3", "click");
	}
}

function countsImages (element_id)
{
	thumbsPos = new Array();
	thumbsPosDown = new Array();
	images = document.getElementById(element_id).getElementsByTagName('img');

	imgAmount = (images.length -1);
	idcc = (images.length -1);
	
	scrollBorder = (sHeight * 3 ) - (sHeight*images.length);

	for(var d=0;d<images.length;d++)
	{
		thumbsPos.push(sHeight*(d-1));
		
		thumbsPosDown.unshift(sHeight*(d-1));
		
		document.getElementById(images[d].id).style.top = thumbsPos[d]+'px';
		YAHOO.util.Event.addListener(images[d].id, "click", moveUp);
	}
}

function countsImages2 (elements_id)
{
	thumbsPos2 = new Array();
	thumbsPosDown2 = new Array();
	images2 = document.getElementById(elements_id).getElementsByTagName('img');
	imgAmount2 = (images2.length -1);
	idcc2 = (images2.length -1);
	
	scrollBorder2 = (sHeight * 3 ) - (sHeight*images2.length);

	for(var d=0;d<images2.length;d++)
	{
		thumbsPos2.push(sHeight*(d-1));
		
		thumbsPosDown2.unshift(sHeight*(d-1));
		
		document.getElementById(images2[d].id).style.top = thumbsPos2[d]+'px';
		YAHOO.util.Event.addListener(images2[d].id, "click", moveUp2);
	}
}

function countsImages3 (elements_id)
{
	thumbsPos3 = new Array();
	thumbsPosDown3 = new Array();
	images3 = document.getElementById(elements_id).getElementsByTagName('img');
	imgAmount3 = (images3.length -1);
	idcc3 = (images3.length -1);
	
	scrollBorder3 = (sHeight * 3 ) - (sHeight*images3.length);

	for(var d=1;d<=images3.length;d++)
	{
		if (d == images3.length) {
			thumbsPos3.push(sHeight*(-1));
			thumbsPosDown3.unshift(sHeight*(-1));			
		} else {
			thumbsPos3.push(sHeight*(d-1));
			thumbsPosDown3.unshift(sHeight*(d-1));
		}
		
		document.getElementById(images3[d-1].id).style.top = thumbsPos3[d-1]+'px';
		YAHOO.util.Event.addListener(images3[d-1].id, "click", moveUp3);
	}
}

function countElements(container_id, tag)
{	
	var container = document.getElementById(container_id);
	if (container == null) return 0;
	var elem_count = container.getElementsByTagName(tag).length;
	return elem_count;
}
 
function moveDown(no_list_shift, e)
{		
	if(idc>0)
	{
		idc--;		
	}		
	else
	{
		idc=images.length-1;
	}	
	var tempPoss = thumbsPos.shift();
	thumbsPos.push(tempPoss);
		document.getElementById(images[idc].id).style.top = -92+'px';	
	
	if (no_list_shift != true) {
		i = document.getElementById('shaft');
		if (i.selectedIndex == 0) {
			i.selectedIndex = i.options.length-1;
		} else {
			i.selectedIndex--;
			updatePrice();
		}
	}
	no_list_shift = false;
	
	for(var d=0;d<images.length;d++)
	{
		if(thumbsPos[d] == 0 || thumbsPos[d] == 92) {		
			var move = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			move.animate();	
		} else {
			document.getElementById(images[d].id).style.top = thumbsPos[d]+'px';
		}
		//var moves = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
		//moves.animate();
	} 
 }
 

function moveUp(no_list_shift, e) 
{ 	
	if(idc <= imgAmount) {
	if(document.getElementById(images[idc].id).offsetTop<92) 
	{		
		document.getElementById(images[idc].id).style.top = 1012+'px';
	}
	if(idc<imgAmount)
	{						
		idc++;
	}
	else 
	{
		idc=0;
	}
		
	var tempPos = thumbsPos.pop();
	thumbsPos.unshift(tempPos);
	
	if (no_list_shift != true) {
		i = document.getElementById('shaft');
		if (i.selectedIndex == i.options.length-1) {
			i.selectedIndex = 0;
		} else {
			i.selectedIndex++;
			updatePrice();
		}
	}
	no_list_shift = false;
	
	for(var d=0;d<images.length;d++)
	{	
		if(thumbsPos[d] == 0 || thumbsPos[d] == -92) {
			var move = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			move.animate();	
		} else {
			document.getElementById(images[d].id).style.top = thumbsPos[d]+'px';
		}
		//var move = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
		//move.animate();	
	}
	}
}

function moveDown2(no_list_shift)
{	
	if(idc2>0)
	{
		idc2--;		
	}		
	else
	{
		idc2=imgAmount2;
	}
	
	var tempPoss2 = thumbsPos2.shift();
	thumbsPos2.push(tempPoss2);
	document.getElementById(images2[idc2].id).style.top = -92+'px';	
		
	if (no_list_shift != true) {
		i = document.getElementById('grip');
		if (i.selectedIndex == 0) {
			i.selectedIndex = i.options.length-1;
		} else {
			i.selectedIndex--;
			updatePrice();
		}
	}
	no_list_shift = false;
	
	for(var d=0;d<images2.length;d++)
	{
		if(thumbsPos2[d] != 0 && images2.length > 2) {		
			document.getElementById(images2[d].id).style.top = '-92px';
		} else {
			var moves = new YAHOO.util.Anim(images2[d].id, {top: {to: thumbsPos2[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			moves.animate();
		}
		//var moves = new YAHOO.util.Anim(images2[d].id, {top: {to: thumbsPos2[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
		//moves.animate();
	} 
}
 

function moveUp2(no_list_shift) 
{ 		
	if(document.getElementById(images2[idc2].id).offsetTop<92) 
	{		
		document.getElementById(images2[idc2].id).style.top = 1012+'px';
	}
	if(idc2<imgAmount2)
	{						
		idc2++;
	}
	else 
	{
		idc2=0;
	}
		
	var tempPos = thumbsPos2.pop();
	thumbsPos2.unshift(tempPos);
	
	if (no_list_shift != true) {
		i = document.getElementById('grip');
		if (i.selectedIndex == i.options.length-1) {
			i.selectedIndex = 0;
		} else {
			i.selectedIndex++;
			updatePrice();
		}
	}
	no_list_shift = false;
	for(var d=0;d<images2.length;d++)
	{	
		if(thumbsPos2[d] != 0) {		
			document.getElementById(images2[d].id).style.top = thumbsPos2[d]+'px';
		} else {
			var move = new YAHOO.util.Anim(images2[d].id, {top: {to: thumbsPos2[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			move.animate();	
		}
		//var move = new YAHOO.util.Anim(images2[d].id, {top: {to: thumbsPos2[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
		//move.animate();	
	}
}

function moveDown3(no_list_shift) {	
	if(idc3>0) {
		idc3--;		
	} else {
		idc3=imgAmount3;
	}
	
	var tempPoss3 = thumbsPos3.shift();
	thumbsPos3.push(tempPoss3);
	document.getElementById(images3[idc3].id).style.top = -92+'px';	
		
	if (no_list_shift != true) {
		i = document.getElementById('ferrule');
		if (i.selectedIndex == 0) {
			i.selectedIndex = i.options.length-1;
		} else {
			i.selectedIndex--;
			updatePrice();
		}
	}
	no_list_shift = false;
	
	for(var d=0;d<images3.length;d++)
	{
		if(document.getElementById(images3[d].id).offsetTop - thumbsPos3[d] > 92 && images3.length > 2) {		
			document.getElementById(images3[d].id).style.top = '-92px';
		} else {
			var moves = new YAHOO.util.Anim(images3[d].id, {top: {to: thumbsPos3[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			moves.animate();
		}
	} 
}
 

function moveUp3(no_list_shift) { 		
	//if(document.getElementById(images3[idc3].id).offsetTop == -92) {		
	//	document.getElementById(images3[idc3].id).style.top = 1012+'px';
	//}
	if(idc3<imgAmount3) {						
		idc3++;
	} else {
		idc3=0;
	}
		
	var tempPos = thumbsPos3.pop();
	thumbsPos3.unshift(tempPos);
	
	if (no_list_shift != true) {
		i = document.getElementById('ferrule');
		if (i.selectedIndex == i.options.length-1) {
			i.selectedIndex = 0;
		} else {
			i.selectedIndex++;
			updatePrice();
		}
	}
	no_list_shift = false;
	for(var d=0;d<images3.length;d++) {	
		if(document.getElementById(images3[d].id).offsetTop == -92) {		
			document.getElementById(images3[d].id).style.top = 1012+'px';
		} else {
			var move = new YAHOO.util.Anim(images3[d].id, {top: {to: thumbsPos3[d]}} ,  0.6 ,YAHOO.util.Easing.easeBoth); 		
			move.animate();	
		}
	}
}

function moveGripTo(element_id) 
{ 	
	var direction = "";
	var current = 0;
	var offset = 0;

	for(var d=0;d<images2.length;d++) {
		if (document.getElementById(images2[d].id).style.top == '0px'){
			direction = "up";
			current = d;
		}
		if (element_id == document.getElementById(images2[d].id).id){
			direction = "down";
			offset = d;
		}
	}
	spaces = Math.abs(current - offset);

	if (direction == "up") {
		for (var i = 1; i<=spaces; i++) {
			if (i == spaces) moveDown2(true);
			else {
				var tempPos = thumbsPos2.shift();
				thumbsPos2.push(tempPos);
			}
		}
	} else {
		for (var i = 1; i<=spaces; i++) {
			if (i == spaces) moveUp2(true);
			else {
				var tempPos = thumbsPos2.pop();
				thumbsPos2.unshift(tempPos);
			}
		}
	}
}

function moveShaftTo(lmnt_id) 
{ 	
	var dir = "";
	var curnt = 0;
	var oset = 0;
	var elementid = 's_'+ lmnt_id;
	if (images != undefined) {
		for(var d=0;d<images.length;d++) {
			if (document.getElementById(images[d].id).style.top == '0px'){
				dir = "up";
				curnt = d;
			}
			if (elementid == document.getElementById(images[d].id).id){
				dir = "down";
				oset = d;
			}
		}
		spcs = Math.abs(curnt - oset);
		if (dir == "up") {
			for (var i = 1; i<=spcs; i++) {
				if (i == spcs) moveDown(true);
				else {
					var tempPoss2 = thumbsPos.shift();
					thumbsPos.push(tempPoss2);
				}
			}
		} else {
			for (var i = 1; i<=spcs; i++) {
				if (i == spcs) moveUp(true);
				else {
					var tempPos = thumbsPos.pop();
					thumbsPos.unshift(tempPos);
				}
			}
		}
	}
}

function moveFerruleTo(lmnt_id) 
{ 	
	var dir = "";
	var curnt = 0;
	var oset = 0;
	var elementid = 'f_'+ lmnt_id;
	if (images3 != undefined) {
		for(var d=0;d<images3.length;d++) {
			if (document.getElementById(images3[d].id).style.top == '0px'){
				dir = "up";
				curnt = d;
			}
			if (elementid == document.getElementById(images3[d].id).id){
				dir = "down";
				oset = d;
			}
		}
		spcs = Math.abs(curnt - oset);
		if (dir == "up") {
			for (var i = 0; i<spcs; i++) {
				moveDown3(true);
			}
		} else {
			for (var i = 0; i<spcs; i++) {
				moveUp3(true);
			}
		}
	}
}
