//Javascripts

$(document).ready(function(){
	correctArticleImages();
});

function correctArticleImages(){
	$('.articlethumb120').each(function(){
		var imgh = parseInt($(this).height());
		var imgw = parseInt($(this).width());
		var diff = 0;
		if(imgh > 90){
			diff = imgh/2 - 45;
			$(this).css('margin-top','-'+diff+'px');
		} else if( imgh < 90){
			diff = imgw/2 - 45;
			$(this).css('margin-top',0).css('width', 'auto').css('height','90px').css('margin-left','-'+($(this).width()-120)/2+'px');
		}
	});
}

function setNicEdit(){
	var texteditor = new nicEditor({iconsPath : 'images/nicEditorIcons.gif', buttonList : ['bold','italic','underline','left','center','right','justify','indent','outdent','html','image','link']}).panelInstance('blog_textdiv');
}

function setAdblockAlt(){
	var div = document.getElementById('topbanner');
	if(div.getElementsByTagName('IFRAME')[0] == null){
		document.getElementById('blockedalt').style.display = "block";
	}	   
}

function highlightRow(obj){
	var node = obj;
	while(node.tagName != 'TR'){
		node = node.parentNode;
	}
	if(obj.checked == true){
		node.className = 'checked';
	} else {
		node.className = '';
	}
}

var  TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function  animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

/*
$(document).ready(function() {
	
	//new nicEditor().panelInstance('area1');
	//new nicEditor({fullPanel : true}).panelInstance('area2');
	new nicEditor({iconsPath : 'images/nicEditorIcons.gif', buttonList : ['bold','italic','underline','left','center','right','justify','indent','outdent','html','image','link']}).panelInstance('blog_textarea');
	//new nicEditor({buttonList : ['fontSize','bold','italic','underline','strikeThrough','subscript','superscript','html','image']}).panelInstance('area4');
	//new nicEditor({maxHeight : 100}).panelInstance('area5');
});
//bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });*/


function show(oid){
	document.getElementById(oid).style.display = 'block';
}

function hide(oid){
	document.getElementById(oid).style.display = 'none';
}

function SelectAll(obj)
{
    obj.focus();
    obj.select();
}

function checkvalue(id,string){
	var obj = document.getElementById(id);
	if(obj.value == ''){
		alert("Required Field:"+string+", is empty");
		return false;
	}
	return true;
}

function useLink(obj){
	window.location = obj.getAttribute('href');
}

function newTab(obj){
	if(obj.getAttribute('href') != ''){
		window.open(obj.getAttribute('href'),"_blank");
		return false;
	}
}

function countCommentChars(){
	var string = $('#commentContentTA').val();
	var totalChars = string.length;
	var remaining = 500 - totalChars;
	$('#charnum').html(remaining);
	if(totalChars >= 500){
		$('#commentContentTA').val(string.substr(0,500))
		$('#charnum').html('0');
	}
}

function updateTv(id){
	var url = 'icons/article_icon_'+id+'.jpg';
	$('#tv_blackout').css('background-image', 'url(' + url + ')');
}
