
// Java script functions used for Product page

var picturePath=new Array();
var pictureDesc=new Array();
var activeThumb=new Array();
var activeProd=-1;
var activeImage=-1;
var centerInterval;


function Buy(art)
{
	var qty;
	if(art!=null)
	{
		qty=GetValue(art);
	}
	else
	{
		// Snabbköp
		art=GetValue("fast_art").toUpperCase();
		qty=GetValue("fast_nbr");
	}
	if(art=="")
	{
		alert("Ogiltig artikell!");
		return;
	}
	if(qty>1000)
	{
		alert("Du kan lägga till max 1000 artiklar!");
		return;
	}
	Add2Cart(art, qty);
}

function Add2Cart(Sart, qty)
{
	//alert(Sart+' Antal: '+qty);
	var theForm=document.cartForm;
	theForm.id.value=Sart;
	theForm.qty.value=qty;
	theForm.act.value="add";
	theForm.action=location.href;
	theForm.submit();
}

function UpdateArt(art)
{
	// Update quantity
	var qty=GetValue('qty_'+art);
	var theForm=document.cartForm;
	theForm.id.value=art;
	theForm.qty.value=qty;
	theForm.act.value="update";
	theForm.action=location.href;
	theForm.submit();
}

function TabortArt(art)
{
	// Update quantity
	var theForm=document.cartForm;
	theForm.id.value=art;
	theForm.act.value="delete";
	theForm.action=location.href;
	theForm.submit();
}

function GetValue(str)
{
	return document.getElementById(str).value;
}

function LoadPicture(prodIdx,imageIdx)
{
	// Cleear any old Centerdialog
	clearInterval(centerInterval);
	activeProd=prodIdx;
	activeImage=imageIdx;
	// Print image tag into the popup div
	GetElem("productImageObj").innerHTML=('<img id="productImage" style="cursor: pointer; height: 480px;" title="Klicka för att stänga bilden" onload="ShowPicture()" onclick="HidePicture()" src="/images/products/'+picturePath[prodIdx][imageIdx]+'\">');
	// Update image description
	GetElem("productImageTitle").innerHTML=((pictureDesc[prodIdx][imageIdx]!="")?pictureDesc[prodIdx][imageIdx]:"&nbsp;");
	SetCursor('wait');
}


function ShowPicture()
{
	var imageWidth;
	var activeArrows=0;
	
	// Product image is loaded, make it visible
	// Start center dialog
	imageWidth=GetElem("productImage").width;
	CenterDialog("pictureDiv",imageWidth);
	centerInterval=setInterval("CenterDialog('pictureDiv',"+imageWidth+")",1000);
	SetCursor('default');
	if(activeImage>0)
	{
		// Not first picture, show prev arrow
		ShowElement("prevArrow");
		activeArrows++;
	}
	else
	{
		HideElement("prevArrow");
	}
	if(activeImage<(picturePath[parseInt(activeProd)].length-1))
	{
		// No last picture, show next arrow
		ShowElement("nextArrow");
		activeArrows++;
	}
	else
	{
		HideElement("nextArrow");
	}
	
	if(activeArrows==0)
	{
		GetElem("prevArrow").style.display="none";
		GetElem("nextArrow").style.display="none";
	}
	else
	{
		GetElem("prevArrow").style.display="";
		GetElem("nextArrow").style.display="";
	}
	// Make div visible
	ShowElement("pictureDiv");
	ShowElement("pictureDivInner");
}

function HidePicture()
{
	// Remove image tag from popup div
	GetElem("productImageObj").innerHTML=('&nbsp;');
	//GetElem("productImageTitle").innerHTML=('&nbsp;');
	// Hide div
	HideElement("pictureDiv");
	HideElement("pictureDivInner");
	HideElement("prevArrow");
	HideElement("nextArrow");
	clearInterval(centerInterval);
	activeProd=-1;
	activeImage=-1;
	SetCursor('default');
}

function PrevThumb(prodIdx)
{
	var prodId=parseInt(prodIdx);
	if(activeThumb[prodId]<=0)
	{
		return;
	}
	// Show next arrow
	ShowElement("nextArrow_"+prodId);
	activeThumb[prodId]--;
	if(activeThumb[prodId]<=0)
	{
		// First picture, hide prev arrow
		HideElement("prevArrow_"+prodId);
	}
	ChangeThumb(prodId, activeThumb[prodId]);
}

function NextThumb(prodIdx)
{
	var prodId=parseInt(prodIdx);
	if(activeThumb[prodId]>=(picturePath[prodId].length-1))
	{
		return;
	}
	// Show prev arrow
	ShowElement("prevArrow_"+prodId);
	activeThumb[prodId]++;
	if(activeThumb[prodId]>=(picturePath[prodId].length-1))
	{
		// Last picture, hide next arrow
		HideElement("nextArrow_"+prodId);
	}
	ChangeThumb(prodId, activeThumb[prodId]);
}

function PrevImage()
{
	// Show next arrow
	ShowElement("nextArrow");
	activeImage--;
	if(activeImage<=0)
	{
		// First picture, hide prev arrow
		HideElement("prevArrow");
	}
	LoadPicture(activeProd, activeImage);
}

function NextImage()
{
	// Show prev arrow
	ShowElement("prevArrow");
	activeImage++;
	if(activeImage>=(picturePath[parseInt(activeProd)].length-1))
	{
		// Last picture, hide next arrow
		HideElement("nextArrow");
	}
	LoadPicture(activeProd, activeImage);
}

function ChangeThumb(prodIdx, imageIdx)
{
	document.getElementById('thumb_'+prodIdx).innerHTML='<img style="cursor: pointer; border-style: solid; border-width: 1px; border-color: #808080; height: 96px;" title="Klicka för större bild" src="/images/thumbs/'+picturePath[prodIdx][imageIdx]+'" onclick="LoadPicture(\''+prodIdx+'\',\''+imageIdx+'\')">';
}

function SetCursor(cursor)
{
	// Set cursor
	document.body.style.cursor = cursor;
}

function GetElem(obj) 
{ 
	return document.getElementById(obj); 
}

function HideElement(obj)
{
	GetElem(obj).style.visibility="hidden";
}

function ShowElement(obj)
{
	GetElem(obj).style.visibility="visible";
}

function CenterDialog(ObjStr,picWidth)
{
	var crossobj,iebody;
	//define universal reference to "staticcontent"
	crossobj=document.getElementById(ObjStr);
	//define reference to the body object in IE
	iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	//define universal dsoc left point
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	//define universal dsoc top point
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	//define universal dsoc left point
	var w=document.all? document.body.clientWidth : window.innerWidth;
	//define universal dsoc top point
	var h=document.all? document.body.clientHeight : window.innerHeight;

	//if the user is using IE 4+ or Firefox/ NS6+
	if (document.all||document.getElementById)
	{
		crossobj.style.left=(parseInt(dsocleft)+((parseInt(w)-picWidth)/2))+"px";
		crossobj.style.top=(parseInt(dsoctop)+((parseInt(h)-480)/2))+"px";
	}
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
//var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
//if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
//document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
//var tempX = 0
//var tempY = 0
//var oldBubble=null;
//var oldRow=null;
//var oldArrow=null;

// Main function to retrieve mouse x-y pos.s
//Finds the absolute position based on the client window

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function PositionBubble(bubbletext,obj)
{
	var bubObj=document.getElementById("bubble");
	document.getElementById("bubblecontent").innerHTML=bubbletext;
	var pos=findPos(obj);
	bubObj.style.display="";
	var px=pos[0]-67;
	var py=pos[1]-bubObj.offsetHeight;
	bubObj.style.left=px+"px";
	bubObj.style.top=py+"px";
}

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	}
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY
	return true
}

function PositionBubbleOld(bubbletext)
{
	var bubObj=document.getElementById("bubble");
	document.getElementById("bubblecontent").innerHTML=bubbletext;
	bubObj.style.display="";
	var px=tempX-70;
	var py=tempY-bubObj.offsetHeight+5;
	bubObj.style.left=px+"px";
	bubObj.style.top=py+"px";
}

function PositionBubbleNoTxt(advanceId)
{
	var bubObj=document.getElementById("advance"+advanceId);
	var activeRow=document.getElementById("activeRow"+advanceId);
	var activeArrow=document.getElementById("setArrowPos"+advanceId);

	if(oldBubble!=null) oldBubble.style.display="none";
	if(oldRow!=null) oldRow.className="menu_adv_out";
	if(oldArrow!=null) oldArrow.className="bs_setArrowOutpos";

	if(oldBubble==bubObj)
	{
		oldBubble=null;
		oldRow=null;
		oldArrow=null;
		return;
	}

	bubObj.style.display="";
	activeRow.className="menu_adv_over";
	oldBubble=bubObj;
	oldRow=activeRow;
	oldArrow=activeArrow;
}

function AdvIn(nr)
{
	var obj1=document.getElementById("activeRow"+nr);
	var obj2=document.getElementById("setArrowPos"+nr);
	if(obj1!=oldRow)
	{
		obj1.className="menu_adv_over";
		obj2.className="bs_setArrowPos";
	}
}

function AdvOut(nr)
{
	var obj1=document.getElementById("activeRow"+nr);
	var obj2=document.getElementById("setArrowPos"+nr);
	if(obj1!=oldRow)
	{
		obj1.className="menu_adv_out";
		obj2.className="bs_setArrowOutpos";
	}
}

function HideBubble()
{
	var bubObj=document.getElementById("bubble");
	bubObj.style.display="none";
}

