//window.onresize = function(){
//	Fix();
//}

var SITEURL;

function setSiteUrl( url )
{
	SITEURL = url;
}

function getSiteUrl()
{
	return "http://" + SITEURL + "/";	
}

function getSiteUrlPlain()
{
	return "http://" + SITEURL + "/";	
}

function getSiteUrlNoSlash()
{
	return "http://" + SITEURL;	
	
}

function fixPageHeight( )
{

	setTimeout("Fix()",0);

}

function showHide( div )
{
	tmpdiv = document.getElementById( div );
	
	if ( tmpdiv.style.display == 'inline' )
		tmpdiv.style.display = 'none';	
	else
		tmpdiv.style.display = 'inline';	
}

function swapGalleryImg( path, height, counter )
{
	document.getElementById( 'showImage' ).height = height + 'px';
	document.getElementById( 'theimg' ).src = path;
}

function onGalleryImg( counter )
{
	document.getElementById( 'imagex' + counter ).style.border = '2px solid #333333';
}

function outGalleryImg( counter )
{
	document.getElementById( 'imagex' + counter ).style.border = '2px solid #e2e2e2';
}

function gotoPage( e, sitenumber, limit, maxx ) 
{
	var searchText = document.getElementById( sitenumber );
	var urlalias   = document.getElementById( 'urlalias' );
	var limit = document.getElementById( limit );
	var maxxx = document.getElementById( maxx ).value;
	
	if ( searchText.value.length > 0 &&  isInteger( searchText.value ) )
	{
		if ( parseInt( searchText.value ) > 0 && parseInt( searchText.value ) <= maxxx )
		{
			code =  e.keyCode;
			
			if ( code == '13' ) 
			{
			// Enter
				this.window.location = getSiteUrlNoSlash() + urlalias.value + "/(offset)/" + parseInt( limit.value * parseInt( searchText.value )-parseInt( limit.value ));
				
			}
		}
		else
			searchText.value = '';	
			
			
	}
	else
		searchText.value = '';	
}



function Fix()
{
	var wrap 		= document.getElementById('wrap');
	var wrapHeight 	= wrap.offsetHeight+20;
	var winHeight 	= windowHeight();
	
	var marginTop = parseInt( (winHeight-wrapHeight)/2 );
	
	if ( marginTop > 0 )
		wrap.style.marginTop = marginTop + "px";
	
}

function windowHeight()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	
	return myHeight;
}


function swapImg( direction, maxx )
{
	var current = document.getElementById( 'current' ).value;
	
	var newcurrent;
	
	if ( direction == 'prev' )
	{
		if ( current == 0 )
			newcurrent = maxx-1;	
		else
			newcurrent = parseInt( current-1 );
	}
	else
	{
		if ( current == parseInt( maxx-1 ) )
			newcurrent = 0;	
		else
			newcurrent = parseInt( current+1 );
	}
	
	document.getElementById( "current" ).value = newcurrent;
	document.getElementById( "swapimg" + current ).style.display='none';
	document.getElementById( "swapimg" + newcurrent ).style.display='inline';
}

var COUNTER;
var COLORNAME;
var IMAGE;
var GROUPCODE;
var PARENTNODE;
var PRICELISTID;
var CUSTOMERID;

function swapColor( counter, colorname, groupcode, parentnode, image, pricelistid, customerid )
{
	COUNTER = counter;
	COLORNAME = colorname;
	IMAGE = image;
	GROUPCODE = groupcode;
	PARENTNODE = parentnode;
	PRICELISTID = pricelistid;
	CUSTOMERID = customerid;
	
	getSimilar();
}

var http_request = false;
var myFunction = '';
   
function postRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Kan ikke opprette XMLHTTP instansen. Bytt browser');
	 return false;
  }
  
  http_request.onreadystatechange = hentPHP;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function hentPHP() {
  if ( http_request.readyState == 4 ) {
	 if ( http_request.status == 200 ) 
	 {
		//alert(http_request.responseText);
		result = http_request.responseText;
		if ( myFunction == 'getSimilar' )
		{
			document.getElementById( 'selectedcolor' + COUNTER ).innerHTML = COLORNAME;
			str =  '<a href=\"javascript:clickImage(' + COUNTER + ');\">';
			str += '<img src=\"' + getSiteUrlPlain() +  IMAGE + '\">';
			str += '</a>';
			document.getElementById( 'swapimage' + COUNTER ).innerHTML = str;
			
			var splitArr = result.split( ";;" );
			
			removeList( 'products' + COUNTER );

			nodeids = '';
			
			for (i = 0; i < splitArr.length; i++)
			{
				splitTwo = splitArr[i].split( ",," );

				newdiv = document.createElement('div');
				newdiv.className ='itemline';
				
				superSTR = "<div class=\"item item1\">" + splitTwo[0] + "</div>";
				superSTR = superSTR + "<div class=\"item item2\">" + splitTwo[1] + ",-</div>";
				
				/*
				if ( splitTwo[3] == 0 )
					superSTR = superSTR + "<div class=\"item item3\"><span style=\"color: red;\">0</span></div>";
				else
					superSTR = superSTR + "<div class=\"item item3\"><span style=\"color: green;\">" + splitTwo[3] + " stk</span></div>";
				*/
				
				if ( splitTwo[3] == 0 )
					superSTR = superSTR + "<div class=\"item item3\"><span style=\"color: red;\">-</span></div>";
				else
					superSTR = superSTR + "<div class=\"item item3\"><img src=\"http://www.gulvex.no/extension/vztlibrary/design/vztdesign/images/vzt/instock.gif\" /></div>";
				
				superSTR = superSTR + "<div class=\"item item4\"><input type=\"text\" class=\"antbox\" id=\"antbox_" + splitTwo[2] +"\"></div>";
				
				newdiv.innerHTML = superSTR;
				document.getElementById( 'products' + COUNTER ).appendChild( newdiv );
				
				nodeids = nodeids + splitTwo[2] + ";";
			}
			
				document.getElementById( 'nodeids' + COUNTER ).value = nodeids;


		}
		
		if ( myFunction == 'addToBasket' )
		{
			if ( result == 1 )
				showDialog();
		}
		
		
		myFunction = '';
		
	} 
	 else 
	 {
		alert( 'Problemer med å finne fila det postes til.' );
	 }
  }
}
 
var numx; 
var node_id;
   
function getSimilar() 
{
	siteURL = getSiteUrl();
	myFunction = 'getSimilar';
	var poststr = "GROUPCODE=" + encodeURI( GROUPCODE ) + "&COLOR=" + encodeURI( COLORNAME ) + "&PARENT=" + encodeURI( PARENTNODE ) + "&CUSTOMERID=" + encodeURI( CUSTOMERID )+ "&PRICELISTID=" + encodeURI( PRICELISTID );
	postRequest( siteURL + "layout/set/ajax/vztlibrary/getsimilar/", poststr);

}

function removeList( div )
{
	hasSearchResult = 0;
	selected = 0;

	result = document.getElementById(div);
	
	if ( result.hasChildNodes() )
	{
		while ( result.childNodes.length >= 1 )
		{
			result.removeChild( result.firstChild );       
		} 
	}	
}

var basketAntallTOT;
var posCounter;

function addToBasket( counter )
{
	basketAntallTOT = 0;
	posCounter = counter;
	
	myFunction = 'addToBasket';
	
	nodeIDS = document.getElementById( 'nodeids' + counter ).value;
	
	splitted = nodeIDS.split( ";" );
	var orderStr = '';
	var ok = 0;
	
	for (i = 0; i < splitted.length-1; i++)
	{
		nodeID = splitted[i];
		
		antall = document.getElementById( 'antbox_' + nodeID ).value;

		if ( isInteger( antall ) )
		{
			// OK! Samle opp alle som er OK og legg i basket
			//alert( nodeID + " - - - " + antall );
			orderStr = orderStr + nodeID + ",," + antall + ";;";
			
			basketAntallTOT = basketAntallTOT + parseInt( antall );
			
			ok = 1;
		}
	}
	
	if ( ok == 1 )
	{
		var poststr = "orderStr=" + encodeURI( orderStr );
		postRequest( getSiteUrl() + "layout/set/ajax/vztlibrary/addtobasket/", poststr);
	}
	else
		alert( "Tast inn et gyldig antall" );
	
}

function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
	 var c = s.charAt(i);

	 if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function showDialog()
{
	/*document.getElementById( 'tot' ).innerHTML = basketAntall;*/
	document.getElementById( 'totant' ).innerHTML = parseInt( basketAntallTOT );
	
	var oldnumitems = document.getElementById( 'numitems' ).innerHTML;
	var newsumitems = parseInt( oldnumitems ) + parseInt( basketAntallTOT );
	document.getElementById( 'numitems' ).innerHTML = newsumitems;

	if ( parseInt( basketAntallTOT ) == 1 )
	{
			
	}
	else
		document.getElementById( 'flertall' ).innerHTML = "er" ;
	
		
	
	posLink = document.getElementById( 'link' + posCounter );

	var x = findPosX( posLink );
	var y = findPosY( posLink );
	
	newY = y-50;
	newX = x-50;
	
	var layer = document.getElementById( 'addToBasketLayer' );

	layer.style.left = newX + "px";
	layer.style.top = newY + "px";
	showHide( 'addToBasketLayer' );
	vzttimeout = setTimeout(fadeOut, 2000);

}

function setOpacity(level) 
{
	element = document.getElementById( 'addToBasketLayer' );

	if ( level < 0.1 )
	{
		clearTimeout(vzttimeout);
		element.style.display = 'none';
		newOpac();
	}
	else
	{
		element.style.opacity = level;
		element.style.MozOpacity = level;
		element.style.KhtmlOpacity = level;
		element.style.filter = "alpha(opacity=" + (level * 100) + ");";
	}
}


function fadeOut() {
	for (i = 0; i <= 1; i += (1 / 20)) {
		setTimeout("setOpacity(" + (1 - i) + ")", i * 1000);
	}
}

function newOpac()
{
	element = document.getElementById( 'addToBasketLayer' );
	element.style.opacity = 1;
	element.style.MozOpacity = 1;
	element.style.KhtmlOpacity = 1;
	element.style.filter = "alpha(opacity=100);";		
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}


function clickImage( num )
{
	var swapimg = document.getElementById( 'swapimage' + num );
	var child = swapimg.getElementsByTagName('img');
	
	var src;
	var newsrc;

	for (var i = 0; i < child.length; i++)
	{
		src = child[i].src;
		if ( src.search( '_medium' ) != -1 )
			newsrc = src.replace('_medium', '');
		else
			newsrc = src.replace('.jpg', '_medium.jpg');

		child[i].src = newsrc;

		break;
	}
	
	

	

	
}
