document.write("<style>");
document.write("#veil {");
document.write("    position:absolute;");
document.write("	left:0px;");
document.write("	top:expression(document.documentElement.scrollTop+document.documentElement.clientHeight-this.clientHeight);");
document.write("}");
document.write("body > div#veil {");
document.write("    position: fixed;");
document.write("    top:0px;");
document.write("	left:0px;");
document.write("}");
document.write("</style>");
document.write("<div style='width:100%;height:100%;background-color:#000000;filter:alpha(opacity=10);-moz-opacity:.10;opacity:.10;visibility:hidden; display:none; z-index:1' id='veil'></div>");

var aTimer;
var mTimer;
var status;
var slut_tid
var max_bud;
var antal_bud;
var current_time;
var last_bid_name;
var next_bid;
var minimum_bid;
var startprice;
var arrayTimers = new Array();
var array_default_content = new Array();

var receiveReq = getXmlHttpRequestObject();
var receiveReq_online_users = getXmlHttpRequestObject();

function openPopup(path)
{
	window.open(path,"","width=500,height=500,scrollbars=yes");
}

function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		return new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.' +	'Consider upgrading your browser.';
	}
}

function getOnlineUsers()
{
	setTimeout('getOnlineUsers();',30000);
	if (receiveReq_online_users.readyState == 4 || receiveReq_online_users.readyState == 0) 
	{
		receiveReq_online_users.open('GET', 'ajax.php?action=getonline', true);
		receiveReq_online_users.onreadystatechange = handleReceiveOnlineUsers;
		receiveReq_online_users.send(null);
	}	
}

function handleReceiveOnlineUsers() 
{
	if (receiveReq_online_users.readyState == 4) 
	{
		if (receiveReq_online_users.responseText != '') 
		{
			obj = document.getElementById("online_users");
			obj.innerHTML = "<b>Brugere online: " + receiveReq_online_users.responseText + "</b>";
		}
	}
}

function getAuktionStats() 
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{
		var auktion_id = document.getElementById('auktions_id').value;
		receiveReq.open('GET', 'ajax.php?action=getauktionstats&id='+auktion_id, true);
		receiveReq.onreadystatechange = handleReceiveAuktionStats;
		receiveReq.send(null);

	}
}

function handleReceiveAuktionStats() 
{
	if (receiveReq.readyState == 4) {
		if (receiveReq.responseText != '') {
			//alert(receiveReq.responseText);
			var exploded = receiveReq.responseText.split(';');
			status = exploded[0];
			slut_tid = exploded[1];
			max_bud = exploded[2];
			antal_bud = exploded[3];
			current_time = exploded[4];
			last_bid_name = exploded[5];
			next_bid = exploded[6];
			minimum_bid = exploded[7];
			startprice = exploded[8];
			if (document.getElementById('last_bid')) {
				if (last_bid_name != '') {
					document.getElementById('last_bid').innerHTML = max_bud+' DKK';
				}
				else {
					document.getElementById('last_bid').innerHTML = '<i>Ingen bud</i>';
				}
			}

			if (document.getElementById('next_bid')) {
				if (status == "closed") {
					document.getElementById('next_bid').innerHTML = '<i>Auktion afsluttet</i>'
				}
				else {
					if (max_bud*1 >= minimum_bid*1) {
						document.getElementById('next_bid').innerHTML = next_bid+',- DKK (<i>Mindstepris er opnået</i>)'
					}
					else {
						if (max_bud == 0) {
							document.getElementById('next_bid').innerHTML = startprice+',- DKK (<i>Mindstepris endnu ikke opnået</i>)'
						}
						else {
							document.getElementById('next_bid').innerHTML = next_bid+',- DKK (<i>Mindstepris endnu ikke opnået</i>)'
						}
					}
				}
			}
			if (document.getElementById('bud')) {
				if (document.getElementById('bud').value == max_bud) {
					document.getElementById('bud').value = next_bid;
				}
			}
			if (current_time <= slut_tid && status == 'running') {
				clearInterval(mTimer);
				timer(slut_tid);
				clearInterval(aTimer);
				aTimer = setTimeout('getAuktionStats();',5000);
			}
			//auktionen er afsluttet
			else {
				clearInterval(mTimer);
				clearInterval(aTimer);
				if (document.getElementById('question_form')) {
					document.getElementById('question_form').style.visibility = "hidden";
					document.getElementById('question_form').style.display = "none";
				}
				document.getElementById('countdown').innerHTML = 'Afsluttet';
				if (max_bud == 0) {
					document.getElementById('final_text').innerHTML = '<br />Auktionen er afsluttet, og der var ingen vindere';
				}
				else {
					if (max_bud*1 >= minimum_bid*1) {
						document.getElementById('final_text').innerHTML = '<br />Auktionen er afsluttet og vinderen blev <b>'+last_bid_name+'</b> med et bud på <b>'+max_bud+',- DKK</b>';
					}
					else {
						document.getElementById('final_text').innerHTML = '<br /><b>Auktionen er afsluttet, men mindsteprisen blev desværre ikke opnået, så varen blev ikke solgt.</b>';
					}
				}
				if (document.getElementById('bud_form_1')) {
					document.getElementById('bud_form_1').style.visibility = 'hidden';
					document.getElementById('bud_form_1').style.display = 'none';
				}
				if (document.getElementById('bud_form_2')) {
					document.getElementById('bud_form_2').style.visibility = 'hidden';
					document.getElementById('bud_form_2').style.display = 'none';
				}
			}
		}
	}
}

function timer(endStamp) {
	//endStamp = document.getElementById('endstamp').value;
	if (endStamp < current_time) {
		document.getElementById('countdown').innerHTML = '<font color="#FF0000">Udløbet!</font>';
		//		document.getElementById('budform').style.visiblity = 'hidden';
		//		document.getElementById('budform').style.display = 'none';
		clearInterval(mTimer);
	}
	else {
		var diff = endStamp-current_time;
		var count_dage = 0;
		var count_timer = 0;
		var count_minutter = 0;
		var count_sekunder = 0;
		// dage
		if (diff%86400 > 0) {
			count_dage = Math.floor(diff/86400);
			diff = diff%86400;
		}
		else {
			count_dage = Math.floor(diff/86400);
		}
		// timer
		if (diff%3600 > 0) {
			count_timer = Math.floor(diff/3600);
			diff = diff%3600;
		}
		else {
			count_timer = Math.floor(diff/3600);
		}
		// minutter/sekunder
		if (diff%60 > 0) {
			count_minutter = Math.floor(diff/60);
			count_sekunder = diff%60;
		}
		else {
			count_minutter = Math.floor(diff/60);
		}
		if (count_dage == 0 && count_timer == 0 && count_minutter == 0 && count_sekunder == 0) {
			clearInterval(aTimer);
			getAuktionStats();
		}
		current_time++;
		document.getElementById('countdown').innerHTML = count_dage+'d. '+count_timer+'t. '+count_minutter+'m. '+count_sekunder+'s.';
		clearInterval(mTimer);
		mTimer = setTimeout('timer('+endStamp+');',1000);
	}
}

function countDown(endStamp, id) 
{	
	//endStamp = document.getElementById('endstamp').value;
	if (endStamp < current_time) {
		document.getElementById(id).innerHTML = '<font color="#FF0000">Udløbet!</font>';
		//		document.getElementById('budform').style.visiblity = 'hidden';
		//		document.getElementById('budform').style.display = 'none';
		clearInterval(arrayTimers[id]);
	}
	else 
	{
		var diff = endStamp-current_time;
		var count_dage = 0;
		var count_timer = 0;
		var count_minutter = 0;
		var count_sekunder = 0;
		// dage
		if (diff%86400 > 0) {
			count_dage = Math.floor(diff/86400);
			diff = diff%86400;
		}
		else {
			count_dage = Math.floor(diff/86400);
		}
		// timer
		if (diff%3600 > 0) {
			count_timer = Math.floor(diff/3600);
			diff = diff%3600;
		}
		else {
			count_timer = Math.floor(diff/3600);
		}
		// minutter/sekunder
		if (diff%60 > 0) {
			count_minutter = Math.floor(diff/60);
			count_sekunder = diff%60;
		}
		else {
			count_minutter = Math.floor(diff/60);
		}
		if (count_dage == 0 && count_timer == 0 && count_minutter == 0 && count_sekunder == 0) {
			clearInterval(aTimer);
//			getAuktionStats();
		}
		
		obj = document.getElementById(id);
		obj.innerHTML = count_dage+'d. '+count_timer+'t. '+count_minutter+'m. '+count_sekunder+'s.';
		
		clearInterval(arrayTimers[id]);
		arrayTimers[id] = setTimeout('countDown("'+ endStamp +'","'+ id +'");',1000);
	}
}

/** Updates the current unix timestamp, adding a second every second **/
function currentTimeUpdate()
{
	current_time++;
	setTimeout('currentTimeUpdate();',1000);
}

/** Corrects the height of the 3 menus to be as high as the tallest (IE bug) **/
function correctTDHeight()
{
	var obj_td1 = document.getElementById("td_left_middle");
	var obj_td2 = document.getElementById("td_middle_middle");
	var obj_td3 = document.getElementById("td_right_middle");
	
	/** We want the max height to be at least 600 px **/
	var max_height = 600;
	
	/** We find the max height **/
	if(obj_td1.clientHeight > max_height)
	{
		max_height = obj_td1.clientHeight;
	}
	
	if(obj_td2.clientHeight > max_height)
	{
		max_height = obj_td2.clientHeight;
	}
	
	if(obj_td3.clientHeight > max_height)
	{
		max_height = obj_td3.clientHeight;
	}
	
	/** We set all td heights to the max height **/
	obj_td1.style.height = max_height + "px";
	obj_td2.style.height = max_height + "px";
	obj_td3.style.height = max_height + "px";	
}

/** Changes the content of the provided id **/
function changeContent(id, content)
{
	obj = document.getElementById(id);
	obj.innerHTML = content;
}

function changeContentDefault(id)
{
	obj = document.getElementById(id);
	obj.innerHTML = array_default_content[id];
}

//																						
// Gallery 
//																						
var gallery = new Array();
var count = -1;			
function swapPicBig(position,id) 
{
	var imgnum = gallery.length;
	if(typeof(id) != "undefined") 
	{
		count = id;
	}
	
	if(position == "back") 
	{
		if(count > 0) 
		{
			count = count-1;
		}
		else
		{
			count = imgnum-1;
		}
	}
	
	if(position == "forward") 
	{
		if(count < imgnum-1) 
		{
			count = count+1;
		}
		else
		{
			count = 0;
		}
	}
	
	if(count < imgnum && count > -1) 
	{
		var gris = getScrollY() + (windowSizeY()/2);
		
		var imgheight = gallery[count][7];
		var imgwidth = gallery[count][6];
		var imgtext = gallery[count][8];
		
		if(windowSizeY() < 500) 
		{
			var size = windowSizeY()*10/100;
			
			var maxHeight = 400; 
			if(gallery[count][7]*1 > maxHeight) {
				var tmp = gallery[count][7]*1 / maxHeight;
				
				imgwidth = gallery[count][6]*1 / tmp
				imgheight = maxHeight;
				imgtext = "This image have been resized due to small screen resolution";
			}
		}
		else if(getWindowWidth() <= imgwidth)
		{
			imgwidth = getWindowWidth()-100;
			imgheight = "";
		}
		else 
		{
			var size = windowSizeY()*10/100;
		}
		
//		document.getElementById("btn_bck_img").src = "images/icon_back.gif";
//		document.getElementById("btn_back").href = "javascript:swapPicLoad(\'back\', \'true\');";
//		document.getElementById("btn_back").style.cursor = "pointer";
//		document.getElementById("btn_back").title = "Tilbage";
//		document.getElementById("btn_back").alt = "Tilbage";
//
//		document.getElementById("btn_fwd_img").src = "images/icon_forward.gif";
//		document.getElementById("btn_forward").href = "javascript:swapPicLoad(\'forward\', \'true\');";
//		document.getElementById("btn_forward").style.cursor = "pointer";
//		document.getElementById("btn_forward").title = "Næste";
//		document.getElementById("btn_forward").alt = "Næste";
		
		var current = count;
		current +=1;
		document.getElementById("gallery_outerPicture").style.display = "inline"			
		document.getElementById("gallery_innerPicture").innerHTML = "<img width=\'" + imgwidth + "\'  id=\'gallery_picture\' src=\'" + gallery[count][5] + "\' border=\'0\' alt=\'" + imgtext + "\' title=\'" + imgtext + "\' >";
		document.getElementById("gallery_pictureText").style.width = imgwidth +"px";
		document.getElementById("gallery_pictureText").style.color = "#565656";
		document.getElementById("gallery_outerPicture").style.width = imgwidth +"px";
		document.getElementById("gallery_innerPicture").style.width = imgwidth +"px";
		document.getElementById("gallery_pictureText").innerHTML = "(" + current + "/" + imgnum + ") " + gallery[count][1];
		if(imgheight != "")
		{
			document.getElementById("gallery_innerPicture").style.height = imgheight +"px";
		}
		document.getElementById("gallery_outerPicture").style.left = ((getWindowWidth()/2)-(document.getElementById("gallery_outerPicture").style.width.replace("px", "")/2))+"px"; 
		document.getElementById("gallery_outerPicture").style.zIndex = "99000";
		document.getElementById("gallery_outerPicture").style.top = (((gris)-(document.getElementById("gallery_innerPicture").style.height.replace("px", "")/2))-60*1)+"px";
		if(imgheight == "")
		{
			document.getElementById("gallery_outerPicture").style.top = "336.5px";
		}
//		document.getElementById("gallery_outerPicture").style.bottom = size +"px"
	}
}

function showElement()
{
	if(document.getElementById("gallery_outerPicture").style.visibility=="visible") {
		opacity('veil', 70, 0, 700); 
		document.getElementById("gallery_outerPicture").style.visibility="hidden";
	}
	else {  
		document.getElementById("veil").style.visibility="visible"; 
		document.getElementById("veil").style.display="inline"; 
		opacity('veil', 1, 70, 700);
		document.getElementById("gallery_outerPicture").style.visibility="visible"
	} 
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i-=5) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i+=5)
        {  	
        	if(i == 66) 
        	{		
        		setTimeout("changeOpac(" + i + ",'" + id + "',1)",(timer * speed));
        	}
        	else
        	{
            	setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
        	}
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id, goal) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    if (opacity == 0) {
    	document.getElementById(id).style.visibility = 'hidden';
    }
    if(goal == 1) {
		document.getElementById("gallery_outerPicture").style.visibility="visible"; 
		document.getElementById("btn_close").focus();
		document.getElementById("gallery_outerPicture").style.position = 'absolute';
		document.getElementById("gallery_outerPicture").style.left = ((getWindowWidth()/2)-(document.getElementById("gallery_outerPicture").style.width.replace('px', '')/2))+'px';
		document.getElementById("gallery_outerPicture").style.zIndex = '90000'; 		
    }
}

function windowSizeY() {

  var myWidth = 0, myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {

    //Non-IE

    myWidth = window.innerWidth;

    myHeight = window.innerHeight;

  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

    //IE 6+ in 'standards compliant mode'

    myWidth = document.documentElement.clientWidth;

    myHeight = document.documentElement.clientHeight;

  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

    //IE 4 compatible

    myWidth = document.body.clientWidth;

    myHeight = document.body.clientHeight;

  }

  return myHeight;

}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}


function getScrollY() {

  var scrOfX = 0, scrOfY = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {

    //Netscape compliant

    scrOfY = window.pageYOffset;

    scrOfX = window.pageXOffset;

  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

    //DOM compliant

    scrOfY = document.body.scrollTop;

    scrOfX = document.body.scrollLeft;

  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

    //IE6 standards compliant mode

    scrOfY = document.documentElement.scrollTop;

    scrOfX = document.documentElement.scrollLeft;

  }

  return scrOfY;

}

function moveSellerInfoInView()
{
	updateWindowDimensions();
	
	var elementHeight = getObjectDimensions(getElement("infobox_dynamic_content_positioner")).height;
	var containerOffset = baseFindPosY(getElement("infobox_dynamic_content"));
	var newMarginTop = window_scroll_y-containerOffset;
	
	if(window_scroll_y > containerOffset+10 && newMarginTop > 0)
	{
		newMarginTop += 115;
//		alert("elementHeight: "+elementHeight+"\n"+"window_height: "+window_height+"\n"+"window_scroll_y: "+window_scroll_y+"\n"+"contationer pos: "+containerOffset+"\n"+"new margin: "+newMarginTop+"\n");
		getElement("infobox_dynamic_content_positioner").style.marginTop = newMarginTop+"px";
	}
}
