// Prevent IE error when debugging
if (!window.console) console = {log: function() {}};

// Divs in IE don't auto adjust even if you put height of 100%.  This is to calculate the main content's height on page load and adjust accordingly
function adjustHeight() {
	mainBox = document.getElementById('main');
	//mainMargin = document.getElementById('main_margin');
	leftSide = document.getElementById('left');
	rightSide = document.getElementById('right');
//	divider = document.getElementById('divider');

	mainHeight = Math.max(mainBox.offsetHeight, mainBox.scrollHeight);

	if (leftSide)
		leftHeight = Math.max(leftSide.offsetHeight, leftSide.scrollHeight);
	if (rightSide)
		rightHeight = Math.max(rightSide.offsetHeight, rightSide.scrollHeight);

	if (leftSide)
		maxHeight = Math.max(mainHeight, leftHeight);
	if (rightSide)
		maxHeight = Math.max(mainHeight, rightHeight);

	maxHeight = maxHeight-50;
	maxHeight = maxHeight + 'px';

	if (leftSide)
		leftSide.style.height=maxHeight;
	if (rightSide)
		rightSide.style.height=maxHeight;
}
function toggleElement(elementname) {
    thisItem = document.getElementById(elementname);
    if (thisItem.style.display && thisItem.style.display != 'none') {
        thisItem.style.display = 'none';
    }
    else {
        thisItem.style.display = 'inline';
    }
}
function hideElement(elementname) {
    thisItem = document.getElementById(elementname);
    if (thisItem)
		thisItem.style.display = 'none';
}
function showElement(elementname) {
    thisItem = document.getElementById(elementname);
    if (thisItem)
		thisItem.style.display = 'inline';
}

function toggleBullet(bulletname, bulleton, bulletoff) {
    thisItem = document.getElementById(bulletname);

    if (thisItem.style.listStyleImage=='url(' + bulleton + ')')
        thisItem.style.listStyleImage='url(' + bulletoff + ')';
    else
        thisItem.style.listStyleImage='url(' + bulleton + ')';
}

 function calculateCornerY(floatingMenu) {  
//     if (floatingMenu.targetY != 'center')  
  //       return floatingMenu.shiftY + floatingMenu.targetY;  
   
     var height = parseInt(floatingMenu.offsetHeight);  
     // Handle Opera 8 problems                                   
     var clientHeight =   
         floatingMenu.hasElement && floatingMenu.hasInner  
         && document.documentElement.clientHeight   
             > window.innerHeight  
         ? window.innerHeight  
         : document.documentElement.clientHeight  

alert(floatingMenu.hasElement);   
     var cornerY =  
         floatingMenu.hasElement  
         ? (floatingMenu.hasInner    
            ? pageYOffset  
            : document.documentElement.scrollTop) +   
           (clientHeight - height)/2  
         : document.body.scrollTop +   
           (document.body.clientHeight - height)/2;  
     return cornerY;  
 }

 function calculateYPos(item, startPos) {  
	var pageHeight = document.body.clientHeight;
	var pixelsScrolled = document.body.scrollTop;

     var posY =  startPos + pixelsScrolled;
     return posY;  
 }

function getPos(el) {
    // yay readability
    for (var lx=0, ly=0;
         el != null;
         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return {x: lx,y: ly};
}

// if view is hidden, make it visible.  If visible, make it hidden
function toggleView(div1, div2) {
//  alert(document.getElementById(div1));
    div1view = document.getElementById(div1);
    if (div1view.style.display == 'inline')
        div1view.style.display = 'none';
    else
        div1view.style.display = 'inline';

	if (div2) {
    div2view = document.getElementById(div2);
    if (div2view.style.display == 'none')
        div2view.style.display = 'inline';
    else
        div2view.style.display = 'none';
	}
}
function openFileBrowser() {
    window.open('editor/jscripts/tiny_mce/plugins/filemanager/', 'UploadFile', "height=500,width=700,modal=yes,alwaysRaised=yes");
    return false;
}

function popitup(url, name, options) {
	newwindow=window.open(url,name,options);
	if (window.focus) {newwindow.focus()}
	return false;
}

function limitTextarea(field, statusfield, maxlimit) {
	// trim if too long
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
		statusfield.value = maxlimit - field.value.length;
}
function hideInputTip(inputField, tooltip) {
    if (inputField.value=='')
        showElement(tooltip);
}
    
function toggleNewsArchive(img, monthsdiv) {
    thisItem = document.getElementById(monthsdiv);
    thisImage = document.getElementById(img);
    if (thisItem.style.display == 'none') {
        thisItem.style.display = 'inline';
        thisImage.src = '/images/news_arrow_opened.gif';
    }
    else {
        thisItem.style.display = 'none';
    thisImage.src = '/images/news_arrow_closed.gif';
    }
}

function showDiv(elementname) {
    thisItem = document.getElementById(elementname);
    thisItem.style.display = 'inline';
}
function hideDiv(elementname) {
    thisItem = document.getElementById(elementname);
    thisItem.style.display = 'none';
}

// format a number to have comma separators (e.g. 100000 = 100,000)
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function getQueryVar(variable, link) { 
  var query = parseUrl(link).search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++) { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable) { 
      return pair[1]; 
    } 
  } 
} 
function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

function findDuplicates(thisarray) {
	var values = [];
	thisarray.forEach(function(thisvalue) {
		if (thisvalue) // ignore blanks
		    values.push(thisvalue);
	});

	var uniq = values
	.map(function(name) {
		  return {count: 1, name: name}
	})
	.reduce(function(a, b) {
	  a[b.name] = (a[b.name] || 0) + b.count
	  return a
	}, {})

	var duplicates = Object.keys(uniq).filter(
		function(a) { if (uniq[a] > 1) return a }
	)

	return duplicates;
}
function updatePreviewImage(imageid, imagefield) {
	image = imagefield.value;
	$('#'+imageid).attr('src', '');
	$('#'+imageid).attr('src', image);
}
