function pad2(number)
{
return (number < 10 ? '0' : '') + number  
}

function updateWCTime(todate)
{
	var now      = new Date();
	var kickoff  = Date.parse(todate);
	var diff = kickoff - now;

	var days  = Math.floor( diff / (1000*60*60*24) );
	var hours = Math.floor( diff / (1000*60*60) );
	var mins  = Math.floor( diff / (1000*60) );
	var secs  = Math.floor( diff / 1000 );

	var dd = days;
	var hh = hours - days  * 24;
	var mm = mins  - hours * 60;
	var ss = secs  - mins  * 60;
	
$('#next-round-cd').html('<span>' + pad2(dd) + '</span>:<span>' + pad2(hh) + '</span>:<span>' + pad2(mm) + '</span>:<span>' + pad2(ss) + '</span>');
}

function updateWCTime2(todate) {
	now      = new Date();
	kickoff  = new Date.parse(todate);
	diff = kickoff - now;

	days  = Math.floor( diff / (1000*60*60*24) );
	hours = Math.floor( diff / (1000*60*60) );
	mins  = Math.floor( diff / (1000*60) );
	secs  = Math.floor( diff / 1000 );

	dd = days;
	hh = hours - days  * 24;
	mm = mins  - hours * 60;
	ss = secs  - mins  * 60;
	
	var numbArray 	= new Array();
	
	numbArray[0] = dd.split("", 1);
	numbArray[1] = dd.split("", 1);
	numbArray[2] = hh.split("", 1);
	numbArray[3] = hh.split("", 1);
	numbArray[4] = mm.split("", 1);
	numbArray[5] = mm.split("", 1);
	numbArray[6] = ss.split("", 1);
	numbArray[7] = ss.split("", 1);
	
	var timer;
	timer = '<span>' + pad2(dd) + '</span>:<span>' + pad2(hh) + '</span>:<span>' + pad2(mm) + '</span>:<span>' + pad2(ss) + '</span>';
	
	for (i = 0; i < numbArray.length; i++)
	{
		if (numbArray[i].value !== getElementById(numbArray[i]))
		{
		$('#next-round-cd .'+numbArray[i]).animate({color:'#ffffff'}, 500).html('<span>'+pad2(dd)+'</span>').animate({color:'#252629'}, 500);
		}
		else
		{
		}
	}
}

$(function()
{

$(".coc").focus(function()
{
	if (this.value == this.defaultValue)
	{
	this.value = '';
	return false;
	}
}).blur(function()
{
	if (!$.trim(this.value))
	{
	this.value = '';
	return false;
	}
});

$(".show-rest-field").focus(function()
{
$(this).next(".show-rest-box").slideDown("fast");
return false;
}).blur(function()
{
	if (!$.trim(this.value))
	{
	$(this).next(".show-rest-box").slideUp("fast");
	return false;
	}
});

$(".action-button-plus").click(function()
{
var div = $(this).closest(".post").find(".post-open-box");

	var startHeight;
	var endHeight;
	var firstHeight;
	
	if (div.css("z-index") == 'auto')
	{
	var fef = div.height();
	div.attr({style:"z-index:"+fef});
	firstHeight = div.height();
	}
	else
	{
	firstHeight = div.css("z-index");
	}
	
	startHeight = div.height();
	div.css({height:'auto'});
	endHeight = div.height();

	if (startHeight !== endHeight)
	{
	div.height(startHeight).animate({height: endHeight, paddingTop:5, paddingBottom:5});
	}
	else
	{
	div.height(endHeight).animate({height: firstHeight, paddingTop:0, paddingBottom:0});
	}
	
return false;
});


$(".comment-link").click(function()
{
	if ($(this).closest(".post-actions").nextAll(".feedbacklist").find(".comment_box").is(":hidden"))
	{
	$(this).closest(".post-actions").nextAll(".feedbacklist").find(".comment_box").slideDown("fast");
	}
	else
	{
	$(this).closest(".post-actions").nextAll(".feedbacklist").find(".comment_box").slideUp("fast");
	}
return false;
});

$(".follow-notmember").click(function()
{
	if ($(".startfollow").is(":hidden"))
	{
	$(".startfollow").slideDown("fast");
	}
	else
	{
	$(".startfollow").slideUp("fast");
	}
});

$(".comment-not-member").click(function()
{
$(this).closest(".post-actions").nextAll(".commentform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".likeform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".dislikeform-not-member").slideUp("fast");

	if ($(this).closest(".post-actions").nextAll(".commentform-not-member").is(":hidden"))
	{
	$(this).closest(".post-actions").nextAll(".commentform-not-member").slideDown("fast");
	}
	else
	{
	$(this).closest(".post-actions").nextAll(".commentform-not-member").slideUp("fast");
	}
return false;
});

$(".like-not-member").click(function()
{
$(this).closest(".post-actions").nextAll(".commentform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".likeform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".dislikeform-not-member").slideUp("fast");

	if ($(this).closest(".post-actions").nextAll(".likeform-not-member").is(":hidden"))
	{
	$(this).closest(".post-actions").nextAll(".likeform-not-member").slideDown("fast");
	}
	else
	{
	$(this).closest(".post-actions").nextAll(".likeform-not-member").slideUp("fast");
	}
return false;
});

$(".dislike-not-member").click(function()
{
$(this).closest(".post-actions").nextAll(".commentform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".likeform-not-member").slideUp("fast");
$(this).closest(".post-actions").nextAll(".dislikeform-not-member").slideUp("fast");

	if ($(this).closest(".post-actions").nextAll(".dislikeform-not-member").is(":hidden"))
	{
	$(this).closest(".post-actions").nextAll(".dislikeform-not-member").slideDown("fast");
	}
	else
	{
	$(this).closest(".post-actions").nextAll(".dislikeform-not-member").slideUp("fast");
	}
return false;
});

$(".post_feedback").click(function()
{
var email_value = $(".email").val();
var name_value 	= $(".name").val();

var dataString 	= 'email=' + email_value + '&name='+ name_value;
$(this).closest(".feedbackitem").nextAll("ul.feedbacklist").prepend('<li class="feedbackitem"><span class="comment-arrow"></span>Uppdaterat</li>');
$(this).closest(".feedbackitem").nextAll(".comment_box ul.feedbacklist li.feedbackitem:first").slideDown("slow");
/*$(this).closest(".feedbackitem").nextAll("ul.feedbacklist li.feedbackitem:first").slideDown("slow");*/
/*
	$.ajax({
	type	: "POST",
	url		: "/actions/post_feedback.php",
	data	: dataString,
	cache	: false,
	success	: function(html)
		{
		$(this).closest(".feedbackitem").nextAll("ul.feedbacklist").prepend(html);
		$(this).closest(".feedbackitem").nextAll("ul.feedbacklist li.feedbackitem:first").slideDown("slow");
		document.getElementById('content').value='';
		document.getElementById('content').focus();
		}
	});
	*/
return false;
});

$(".add-spec").click(function()
{
var row = $(this).closest('li.links-fileds-colum').find('.arrangableNodes > tbody > tr:last').index()+1;
var prefix = $(this).closest('li.links-fileds-colum').find('table.arrangableNodes').attr("id");

var content = '<tr><td><div class="spec-item-box"><div class="link-item-attr"><input type="text" name="'+prefix+'['+row+'][name]" value="Namn" class="textfield supersmall-field" /></div><div class="spec-open open-more">Ner</div><div class="spec-more-content more-box open"><div class="link-item-attr"><input type="text" name="'+prefix+'['+row+'][headline]" value="Rubrik" class="textfield supersmall-field" /></div><div class="link-item-attr"><input type="text" name="'+prefix+'['+row+'][url]" value="URL" class="textfield supersmall-field" /></div>';
	if (prefix == 'bc')
	{
	content += '<div class="link-item-attr"><input type="file" name="'+prefix+'['+row+'][image]" class="textfield supersmall-field" /></div>';
	}
content += '<div class="link-item-attr"><a href="#" class="delete-spec">Ta bort</a></div></div></div></td></tr>';

$(this).closest('li.links-fileds-colum').find('table.arrangableNodes > tbody:last').append(content);
return false;
});

$('.open-more').live('click', function()
{
$(this).next('.more-box').slideToggle('fast');
return false;
});

$('.delete-spec').live('click', function()
{
$(this).closest('tr').remove();
return false;
});

});

function markText(id)
{
var myField = document.getElementById(id);
	if (myField.value == myField.defaultValue)
	{
	myField.focus();
	var valueLenght = myField.value.length;
		//IE support
		if (document.selection)
		{
		var sel = document.selection.createRange();
			if (myField.createTextRange)
			{
			var range = myField.createTextRange();
			/*alert(valueLenght);*/
			range.collapse(true);
			range.moveStart('character',0); 
			range.moveEnd('character',(valueLenght)); 
			range.select(); 
			}
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == '0')
		{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.setSelectionRange(0,valueLenght);
		}
	}
}

function insertAtCursor(myField, myValue)
{
	//IE support
	if (document.selection)
	{
	myField.focus();
	sel = document.selection.createRange();
	sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0')
	{
	var startPos = myField.selectionStart;
	var endPos = myField.selectionEnd;
	myField.value = myField.value.substring(0, startPos)
	+ myValue
	+ myField.value.substring(endPos, myField.value.length);
	}
	else
	{
	myField.value += myValue;
	}
}

function doGetCaretPosition(ctrl)
{
var CaretPos = 0;
	// IE Support
	if (document.selection) 
	{
	ctrl.focus();
	var Sel = document.selection.createRange ();
	var Sel2 = Sel.duplicate();
	Sel2.moveToElementText(ctrl);
	var CaretPos = -1;
		while(Sel2.inRange(Sel))
		{
		Sel2.moveStart('character');
		CaretPos++;
		}
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
	{
	CaretPos = ctrl.selectionStart;
	}
return (CaretPos);
}

function insertTag(tagCode)
{
var myField = document.form.page_content;
var myTagOpen = '[' + tagCode + ']';
var myTagClose = '[/' + tagCode + ']';
var pos = doGetCaretPosition(myField);
var pos = Math.floor(myTagOpen.length + pos);

	//IE support
	if (document.selection)
	{
	myField.focus();
	var sel = document.selection.createRange();

	sel.text = myTagOpen + sel.text + myTagClose;

		if (myField.createTextRange)
		{
		var range = myField.createTextRange(); 
		range.collapse(true);
		range.moveStart('character',pos); 
		range.moveEnd('character',0); 
		range.select(); 
		}
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0')
	{
	mozWrap(myField, myTagOpen, myTagClose); 
		if (myField.setSelectionRange)
		{
		myField.focus();
		myField.setSelectionRange(pos,pos); 
		}
	}
	else
	{
	myField.value += myTagOpen + myTagClose;
	}
}

// From http://www.massless.org/mozedit/ 
function mozWrap(txtarea, open, close) 
{ 
   var selLength = txtarea.textLength; 
   var selStart = txtarea.selectionStart; 
   var selEnd = txtarea.selectionEnd; 
   if (selEnd == 1 || selEnd == 2) 
      selEnd = selLength; 
   var s1 = (txtarea.value).substring(0,selStart); 
   var s2 = (txtarea.value).substring(selStart, selEnd) 
   var s3 = (txtarea.value).substring(selEnd, selLength); 
   txtarea.value = s1 + open + s2 + close + s3; 
   return; 
}


<!-- 
// bbCode control by 
// subBlue design 
// www.subBlue.com 

// Startup variables
var imageTag = false; 
var theSelection = false; 

// Check for Browser & Platform for PC & IE specific bits 
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html 

var clientPC = navigator.userAgent.toLowerCase(); // Get client info 
var clientVer = parseInt(navigator.appVersion); // Get browser version 

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1) 

&& (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1) 

&& (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1)); 

var is_moz = 0; 

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1)); 
var is_mac = (clientPC.indexOf("mac")!=-1); 



// Helpline contents
b_help = "Fet text: **text**"; 
i_help = "Länk till uppslagsord: [[Utombordare]]"; 

// Definiera bbCode-taggar
bbcode = new Array(); 
bbtags = new Array(
		  '**'
		, '**'
	
		, '[['
		, ']]'
		
		, '=='
		, '=='
		
		, '[[Fil:'
		, ']]'
);
imageTag = false; 

// Shows the help contents in the helpline window 
function helpline(help)
{ 
document.form.helpbox.value = eval(help + "_help"); 
}

// Replacement for arrayname.length property 
function getarraysize(thearray)
{
	for (i = 0; i < thearray.length; i++)
	{
	if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null)) 
	return i; 
	}
return thearray.length; 
} 

// Replacement for arrayname.push(value) not implemented in IE until version 5.5 
// Appends element to the array 
function arraypush(thearray,value)
{ 
thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5 
// Removes and returns the last element of an array 
function arraypop(thearray)
{ 
thearraysize = getarraysize(thearray); 
retval = thearray[thearraysize - 1]; 
delete thearray[thearraysize - 1]; 
return retval; 
} 

function bbstyle(bbnumber)
{ 
var txtarea = document.form.page_content; 

donotinsert = false; 
theSelection = false;
bblast = 0; 

	if (bbnumber == -1)
	{ // Stäng alla öppna taggar och standard knappnamnen
		while (bbcode[0])
		{ 
		butnumber = arraypop(bbcode) - 1; 
		txtarea.value += bbtags[butnumber + 1]; 
		buttext = eval('document.form.addbbcode' + butnumber + '.value'); 
		eval('document.form.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"'); 
		}
	imageTag = false; // All tags are closed including image tags :D 
	txtarea.focus(); 
	return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
	{
	theSelection = document.selection.createRange().text; // Hämta text-markeringen
	
		if (theSelection)
		{ 
		// Lägg till taggar runt markeringen
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1]; 
		txtarea.focus(); 
		theSelection = ''; 
		return;
		} 
	
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) 
	{
	mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]); 
	return; 
	}

	// Find last occurance of an open tag the same as the one just clicked 
	for (i = 0; i < bbcode.length; i++)
	{
		if (bbcode[i] == bbnumber+1)
		{
		bblast = i; 
		donotinsert = true; 
		} 
	} 

	// Stäng alla öppna taggar upp till den just klickade & standard knapp namnen 
	if (donotinsert)
	{
		while (bbcode[bblast])
		{ 
		butnumber = arraypop(bbcode) - 1; 
		txtarea.value += bbtags[butnumber + 1]; 
		buttext = eval('document.form.addbbcode' + butnumber + '.value'); 
		eval('document.form.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"'); 
		imageTag = false; 
		} 
	/*txtarea.focus(); */
	return;
	}
	else
	{
	// ÖPPNA TAG:
	
		if (imageTag && (bbnumber != 14))
		{
		// Stänger bild-tag innan vi lägger till annan
		txtarea.value += bbtags[15]; 
		lastValue = arraypop(bbcode) - 1;   // Remove the close image tag from the list 
		document.form.addbbcode14.value = "Img";   // Return button back to normal state 
		imageTag = false; 
		} 
	
	// Öppna tag:
	insertAtCursor(document.forms['form'].elements['page_content'],bbtags[bbnumber]);
	
	/*txtarea.value += bbtags[bbnumber];*/
	if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag 
	arraypush(bbcode,bbnumber+1); 
	eval('document.form.addbbcode'+bbnumber+'.value += "*"'); 
	//      alert(eval('document.form.addbbcode' +bbnumber+ '.value')); 
	txtarea.focus();
    var startPos = txtarea.selectionStart;
    var endPos = txtarea.selectionEnd;
	txtarea.setSelectionRange(startPos,endPos);
	return false;
	}

}

function PopUp(url, name, width, height)
{
window.open(url, name, "channelmode=no, directories=no, fullscreen=no,"+" height="+height+", location=no, menubar=no,"+" resizable=no, scrollbars=yes, status=no, width="+width+", left= "+(((screen.width-width)/2)-15)+", top= "+(screen.height-height)/3);
return false
}

function showImage(ImageName)
{
var imgMain = document.getElementById("ImgMain");
	if(ImageName)
	{
	imgMain.src = ImageName;
	}
}
