/*
 * FIREBUG (Firefox only) hookup to console
 * 
 * STUB...for all browsers. Firefox impl should implement real hook.
 */

function log()
{
	;
}

/*
 * Given id to move, target dropId/offset, and a move function, animate the move.
 */
function moveEffect(moveId, dropId, dropOffset, fMove)
{
//	Effect.Fade(subject, { afterFinish: 
//		new Effect.ScrollTo(a, { afterFinish: function() {
//			fMove();
//			Effect.BlindDown(subject);
//		} );
//	}});
	Effect.Fade(moveId, { afterFinish: function () {
		fMove();
		new Effect.ScrollTo(dropId, { offset: dropOffset, afterFinish: function() {
			Effect.BlindDown(moveId);
		} } );
	}});
	
}
function removeElement(subject)
{
	var s = $(subject);

	Effect.Fade(subject, { afterFinish: function () {
		s.parentNode.removeChild(s);		
	} });
	return true;
}

function moveElementFirst(subject)
{
	var s = $(subject);
	var a = s.parentNode.firstChild;
	
	if (a==s)
		return false;
	moveEffect(subject, a, 0, function () {
		s.parentNode.removeChild(s);
		a.parentNode.insertBefore(s, a)
	});
	return true;
}
function moveElementLast(subject)
{
	var s = $(subject);
	var a = s.parentNode.lastChild;
	
	if (a==s)
		return false;
	moveEffect(subject, a, a.height, function () {
		s.parentNode.removeChild(s);
		a.parentNode.appendChild(s);
	});
	return true;
}
function moveElementBefore(subject)
{
	var s = $(subject);
	var a = s.previousSibling;
	
	if (a==null)
		return false;
	moveEffect(subject, a, 0, function () {
		s.parentNode.removeChild(s);
		a.parentNode.insertBefore(s, a)
	});
	return true;
}
function moveElementAfter(subject)
{
	var s = $(subject);
	var a = s.nextSibling;
	
	if (a==null)
		return false;
	moveEffect(subject, a, a.height, function () {
		s.parentNode.removeChild(s);
		if (a.nextSibling!=null)
			a.parentNode.insertBefore(s, a.nextSibling);
		else
			a.parentNode.appendChild(s);
	});
	return true;
}

/* Generic switching command (DISPLAY)
 */
function switch_on_off(onId, offId, displaytype)
{
	if (onId==offId)
		return;
	if (!displaytype)
		displaytype = 'block';
	var onObj = document.getElementById(onId);
	var offObj = document.getElementById(offId);
	
	if (onObj)
		onObj.style.display = displaytype;
	if (offObj)
		offObj.style.display = 'none';
}

/* Generic switching command (VISIBILITY)
 */
function switch_on_off_vis(onId, offId)
{
	if (onId==offId)
		return;
	var onObj = document.getElementById(onId);
	var offObj = document.getElementById(offId);	
	if (onObj)
		onObj.style.visibility = 'visible';
	if (offObj)
		offObj.style.visibility = 'hidden';
}

/* Generic background switching command
 */
function switch_background_on_off(onId, onValue, offId, offValue)
{
	if (onId==offId)
		return;
	var onObj = document.getElementById(onId);
	var offObj = document.getElementById(offId);
	
	if (onObj)
		xBackground(onObj,onValue);
	if (offObj)
		xBackground(offObj,offValue);
}

function cl(nId, sId, wh)
{
	var p = 's='+sId+'&wh='+wh+'&n='+nId+'&u=na';
	
	new Ajax.Request('V', { method: 'get', parameters: p });
}

function label(nid, sid, name, value)
{
	var p = 'function=label&sid='+sid+'&nid='+nid+'&name='+name+'&value='+value;
	
	new Ajax.Request('sapi.do', { method: 'get', parameters: p });
}

function changeScrapClass(sid, name)
{
	var p = 'function=setScrapClass&sid='+sid+'&name='+name;
	
	new Ajax.Request('sapi.do', { method: 'get', parameters: p });
}

function enableRedband()
{
	var p = 'function=enableRedband';
	
	new Ajax.Request('/my/api.do', { method: 'get', parameters: p });
}

function jsEnable(checksum)
{
	var p = 'function=jsEnable&x='+checksum;
	
	new Ajax.Request('/my/api.do', { method: 'get', parameters: p });
}

function setSessionProperty(name, value)
{
	var p = 'function=setSessionProperty&x='+name+'&v='+value;
	
	new Ajax.Request('/my/api.do', { method: 'get', parameters: p });
}

function rateScrap(f, sid, value)
{
	var p = 'function=rateScrap&sid='+sid+'&value='+value;
	
	//AJAX.rateScrap(function(scrap) { base2_box_ajax_pre(scrap,PREFIX); },CURRENT_SCRAP_ID,0);
	new Ajax.Request('sapi.do', { method: 'get', parameters: p, onSuccess: f });
}

function cullScrap(f, sid, value)
{
	var p = 'function=cullScrap&sid='+sid+'&value='+value;

	//AJAX.cullScrap(cull_ajax,$s.scrapId,'no');
	new Ajax.Request('sapi.do', { method: 'get', parameters: p, onSuccess: f });
}

function addToDashboard(dashboardId, gid)
{
	var p = 'function=addToDashboard&dashboardId='+dashboardId+'&gid='+gid;

	//AJAX.addToDashboard(-1, $guide.guideId);
	new Ajax.Request('api.do', { method: 'get', parameters: p, onSuccess: f });
}

function removeFromDashboard(dashboardId, gid)
{
	var p = 'function=removeFromDashboard&dashboardId='+dashboardId+'&gid='+gid;

	//AJAX.addToDashboard(-1, $guide.guideId);
	new Ajax.Request('api.do', { method: 'get', parameters: p, onSuccess: f });
}

function reorderDashboard(dashboardId, gid, reorder)
{
	var p = 'function=reorderDashboard&dashboardId='+dashboardId+'&gid='+gid+'&reorder='+reorder;

	//AJAX.addToDashboard(-1, $guide.guideId);
	new Ajax.Request('api.do', { method: 'get', parameters: p, onSuccess: f });
}

function reorderDashboardAll(reorder)
{
	var p = 'function=reorderDashboardAll&reorder='+reorder;

	//AJAX.addToDashboard(-1, $guide.guideId);
	new Ajax.Request('api.do', { method: 'get', parameters: p, onSuccess: f });
}

function loadpanel(uri,params,panelId)
{
	menu_on('loading-popup', panelId); 
	new Ajax.Request(uri, { method: 'get', parameters: params, onSuccess: function(t) { 
			menu_off('loading-popup'); 
			$(panelId).innerHTML = t.responseText;
			}});
}

function updateDivWithTransition(onId, offId)
{
  new Effect.Opacity(offId,
    { duration: 1.0, 
      transition: Effect.Transitions.sinoidal, 
      from: 1.0, to: 0.1,
       afterFinish: function() {
      		switch_on_off(onId, offId);
  			new Effect.Opacity(onId, { duration: 1.0,
  				transition: Effect.Transitions.sinoidal, 
      			from: 0.1, to: 1.0 });
  }});
}

function updateBackgroundWithTransition(id, value)
{
  new Effect.Opacity(id,
    { duration: 1.0, 
      transition: Effect.Transitions.sinoidal, 
      from: 1.0, to: 0.1,
       afterFinish: function() {
		  attempt1='good';
		  try {document.getElementById(id).style.direction='ltr';}
		  catch (err) {attempt1='bad';};
		  if (attempt1=='good') {
  			xBackground(id,null,value);
  			new Effect.Opacity(id, { duration: 1.0,
  				transition: Effect.Transitions.sinoidal, 
      			from: 0.1, to: 1.0 });
      		}
  }});
}

function updateValueWithTransition(id, value)
{
  new Effect.Opacity(id,
    { duration: 1.0, 
      transition: Effect.Transitions.sinoidal, 
      from: 1.0, to: 0.1,
       afterFinish: function() {
		  attempt1='good';
		  try {document.getElementById(id).style.direction='ltr';}
		  catch (err) {attempt1='bad';};
		  if (attempt1=='good') {
		  			DWRUtil.setValue(id, value);
		  			new Effect.Opacity(id, { duration: 1.0,
		  				transition: Effect.Transitions.sinoidal, 
		      			from: 0.1, to: 1.0 });
		      }
  }});
}

// BASE2 Boxscore Callback from AJAX with instance of the scrap that was modified
function base2_box_ajax(scrap)
{
	base2_box_ajax_pre(scrap,'');
}
function base2_box_ajax_pre(scrap,PREFIX)
{
  attempt1='good';
  try {document.getElementById(PREFIX+'sr'+scrap.scrapId).style.direction='ltr';}
  catch (err) {attempt1='bad';};
  if (attempt1=='good') {
	updateValueWithTransition(PREFIX+'sr'+scrap.scrapId, scrap.communityVotes+' vote(s)');
	updateBackgroundWithTransition(PREFIX+'base2_rating_'+scrap.scrapId, scrap.ratingImg);
	updateValueWithTransition(PREFIX+'HovStar'+scrap.scrapId, scrap.ratingTierDesc);  
	Effect.Fade(PREFIX+'rating_'+scrap.scrapId, { afterFinish: function () {
		Effect.Appear(PREFIX+'voted_'+scrap.scrapId);
	}});
  }
	if (PREFIX=='popup') {attempt='good';
	 try {document.getElementById('sr'+scrap.scrapId).style.direction='ltr';}
	 catch (err) {attempt='bad';};
	 if (attempt=='good') {base2_box_ajax_pre(scrap,'');};
	};
	try {updateBackgroundWithTransition('s12_'+scrap.scrapId,'/img/'+scrap.ratingTier+'star12.png');}
	 catch (err) {};
}

// BASE2 Review Callback from AJAX with instance of the scrap that was modified
function base2_review_ajax(scrap)
{
	base2_review_ajax_pre(scrap,'');
}
function base2_review_ajax_pre(scrap,PREFIX)
{
  attempt1='good';
  try {document.getElementById(PREFIX+'sr'+scrap.scrapId).style.direction='ltr';}
  catch (err) {attempt1='bad';};
  if (attempt1=='good') {
	  updateValueWithTransition(PREFIX+'sr'+scrap.scrapId, scrap.communityVotes+' vote(s)');
	  updateBackgroundWithTransition(PREFIX+'base2_rating_'+scrap.scrapId,scrap.ratingImg);
	  updateValueWithTransition(PREFIX+'HovStar'+scrap.scrapId, scrap.ratingTierDesc);  
	  if (scrap.userRatingTier=='r-1') {scrap.userRatingTier='r0'};
	  updateBackgroundWithTransition(PREFIX+'userrating_'+scrap.scrapId,'/img/'+scrap.userRatingTier+'-arrow.png');
	  }
  if (PREFIX=='popup') {attempt2='good';
	try {document.getElementById('sr'+scrap.scrapId).style.direction='ltr';}
	catch (err) {attempt2='bad';};
	if (attempt2=='good') {base2_review_ajax_pre(scrap,'');};
  };
  try {updateBackgroundWithTransition('s12_'+scrap.scrapId,'/img/'+scrap.ratingTier+'star12.png');}
    catch (err) {};
}

function idIn(e, id)
{
	if (e.id == id)
		return true;
	for (var c = e.firstChild; c; c = c.nextSibling) 
	{
    	if (c.nodeType == 1) 
    	{
    		if (idIn(c, id))
	    		return true;
    	}
	}
	return false;
}

function findMenu(triggerId)
{
	if (!COMMON_POPUP.isOpen)
		return COMMON_POPUP;
	if (COMMON_POPUP.triggerId==triggerId)
		return null;
	if (!idIn(COMMON_POPUP.menu, triggerId))
		/* Trigger was not in the popup, so restart the popup with new root */
		return COMMON_POPUP;
	if (!COMMON_SUBPOPUP.isOpen)
		return COMMON_SUBPOPUP;
	if (COMMON_SUBPOPUP.triggerId==triggerId)
		return null;
	alert('no menus available');
	return null;
}

// Callback from AJAX with instance of the scrap that was modified
function cull_ajax(scrap)
{
  //DWRUtil.setValue('sr'+scrap.scrapId, scrap.score);
  //document.getElementById('voted_'+scrap.scrapId).style.display='block';
  document.getElementById('cull_'+scrap.scrapId).style.display='none';
}
function cull_ajax_popup()
{
  document.getElementById('bpquestion').style.display='none';
  document.getElementById('bphelp').style.display='none';
  document.getElementById('bpquestionmore').style.display='block';    
  setTimeout("document.getElementById('betapopup').style.display='none'",4000);
}
function fadediv(id)
{
  attempt1='good';
  try {document.getElementById(id).style.direction='ltr';}
  catch (err) {attempt1='bad';};
  if (attempt1=='good') {
	  document.getElementById(id).style.visibility='visible';    
//		new Effect.Opacity(id, { duration: 1.0,
//			transition: Effect.Transitions.sinoidal, 
//			from: 0.1, to: 1.0 });
      }
}
function overvis(overid)
{
	document.getElementById(overid).style.overflow='visible';
	document.getElementById(overid).style.zIndex='1000';
}
function overhide(overid)
{
	document.getElementById(overid).style.overflow='hidden';
	document.getElementById(overid).style.zIndex='1'
}

/*
 * Doesn't work in IE7 reliably yet
 */
function addGlobalEventListener(evtType, callback)
{
	if (typeof(window.addEventListener) != 'undefined') {
	  window.addEventListener(evtType, callback, false);
	}
	if (typeof(document.addEventListener) != 'undefined') {
	  document.addEventListener(evtType, callback, false);
	}
	if (typeof(window.attachEvent) != 'undefined') {
	  window.attachEvent('on'+evtType, callback);
	}
}
	