/*
 * Copyright (c) 2007-2008 Stardock Systems, Inc.
 * Author: Andrew Powell, 10.30.07
 * Version 1.01 ~ 01.03.08
 * Version 1.1 ~ 07.25.08
*/

Sd.UI = Class.create();

// Creates a copy of the original, changes properties to disable the button, inserts it before the original.
// This prevents IE from stopping the postback, and it looks perty.
Sd.UI.disableButton = function(button, abspos, hide)
{
	abspos = (abspos != null) ? abspos : true;
	hide = (hide != null) ? hide : false;
	
	var copy = $new('input');
	var type = button.type.toLowerCase();
	
	if(button.src) copy.src = button.src;
	copy.type = type;
	copy.disabled = true;
	copy.className = button.className;	
	copy.className += ' disabled';
	copy.value = button.value;

	if(!button.hide) Sd.Proto.extend(button);		
	if(type == 'button' || type == 'submit')
		button.hide();
	else
	{
		if(abspos)
		{
			copy.style.position = 'absolute';
			copy.style.left = button._bounds().x + 'px';		
			copy.style.top = button._bounds().y + 'px';
		}
		hide ? button.hide() : button.invisible();
	}
		
	button.parentNode.insertBefore(copy, button);
}

Sd.UI.noEnter = function(e)
{
	if(!e && window.event) e = window.event;
	if(!e) return true;
	var key = e.keyCode ? e.keyCode : e.which;
	var result = key != 13;
	
	if(result == false)
	{
	  e.cancel = true;
	  e.returnValue = false;
	  if(e.preventDefault) e.preventDefault();
	}
	
	return result;	
}

//
// Retrieves a tinymce editor based on the partial id of the generated textarea.
// Andrew Powell 06.24.08
//
Sd.UI.getTinyEditor = function(partialId)
{
   var textarea = $id(partialId, null, 'textarea');
   if(!textarea) return;
   var editor = tinyMCE.editors[textarea.id];
   if(!editor) return;
   
   return editor;
};

//
// Determines if there is valid content within an editor.
//
//
Sd.UI.tinyValid = function(id)
{
	 var editor = Sd.UI.getTinyEditor(id);
	 if(!editor) return false;
   var content = editor.getContent();
   if(!content || content.trim() == '')	return false;
   
   return true;	
};

//
// Retrieves the selected text from a page.
// Andrew Powell 06.24.08
//
Sd.UI.getSelection = function(clear)
{
   var text;
   clear  = clear ? clear : true;
   if (document.getSelection)
   { 
      text = document.getSelection().toString();
      if(text && !(text == '')) window.getSelection().collapseToStart();
   }
   else if (document.selection && document.selection.createRange)
   {
      var range = document.selection.createRange();
      text = range.text;
      range.collapse();
      range = null;
   }
   if(!text) text = '';
   return text;
};

Sd.UI.fixAvatar = function(img)
{
	img.prevSrc = img.src;
   img.src = '/images/space.gif';
   img.className += ' noavatar';
};

Sd.UI.fixPersonal = function(img)
{
	img.prevSrc = img.src;   
   if(img.src.indexOf('none') > 0)
     img.className = 'noimage';
   else
     img.className = 'imageerror';
     
   img.src = '/images/space.gif';
};

Sd.UI.fixDesktop = function(img)
{
	img.prevSrc = img.src;	
   if(img.src.indexOf('none') > 0)
     img.className = 'noimage';
   else
     img.className = 'imageerror';
     
   img.src = '/images/space.gif';
};

Sd.UI.Tabs = Class.create();
Sd.UI.Tabs.prototype = {}

Sd.UI.Tabs.select = function(tab)
{
   var prevTab = Sd.UI.Tabs.prevTab;
   if(prevTab)
   {
      prevTab.className = prevTab.className.replace(/\sselected/, '');
      Sd.UI.Tabs.setSiblings(prevTab, false);
      
      var prevTabId = prevTab.id.replace(/_Tab/, '');
      var prevPageId = '_Page' + prevTabId;
      var prevPage = $id(prevPageId);
      prevPage.className = prevPage.className.replace(/\sselected/, '');
      
   }
   
   tab.className += ' selected';
   
   Sd.UI.Tabs.prevTab = tab;

   Sd.UI.Tabs.setSiblings(tab, true);
   
   var tabId = tab.id.replace(/_Tab/, '');
   var pageId = '_Page' + tabId;
   var page = $id(pageId);
   if(page) page.className += ' selected';            
   
}

Sd.UI.Tabs.setSiblings = function(tab, add)
{
   var nodes = Sd.UI.Tabs.getNodes(tab.parentNode);
   var nodeIndex = Sd.UI.Tabs.getNodeIndex(tab, nodes);
    
   var prev = ((nodeIndex > 0 && nodeIndex < nodes.length) ? nodes[nodeIndex - 1] : null);
   var next = ((nodeIndex >= 0 && nodeIndex < nodes.length - 1) ? nodes[nodeIndex + 1] : null);
      
   if(!prev && !next) return;
   
   var prevValid = (prev && prev.className && prev.className.indexOf('tab') == 0);
   var nextValid = (next && next.className && next.className.indexOf('tab') == 0);
    
   if(prevValid == false && nextValid == false) return;

   if(add == true)
   {
      if(prev) prev.className += ' prevt';
      if(next) next.className += ' nextt';
   }
   else
   {
      if(prev) prev.className = prev.className.replace(/\sprevt/g, '');
      if(next) next.className = next.className.replace(/\snextt/g, '');
   }
   
}        

Sd.UI.Tabs.getNodes = function(parent)
{
   var nodes = [];
   for(var i = 0; i < parent.childNodes.length; i++)
   {
      if(parent.childNodes[i].tagName && parent.childNodes[i].tagName.toLowerCase() == 'div') nodes[nodes.length] = parent.childNodes[i];
   }
   
   return nodes;
}

Sd.UI.Tabs.getNodeIndex = function(tab, nodes)
{
   for(var i = 0; i < nodes.length; i++)
   {
      if(nodes[i] == tab) return i;
   }
   
   return -1;
}

Sd.UI.Mask = Class.create();
Sd.UI.Mask.index = 0;
Sd.UI.Mask.create = function(item, ani)
{
	Sd.Proto.extend(item);
	var b = item._bounds();

	var div = $new('div', '_ItemMask' + Sd.UI.Mask.index, 'itemmask');
	div.style.height = b.height + 'px';
	div.style.width = b.width + 'px';
	div.style.position = 'absolute';
	div.style.left = b.left + 'px';
	div.style.top = b.top + 'px';
	div.style.zIndex = Math.max(100, (item.style.zIndex + 1));

	if(ani)
	{
	  var waitani = $new('div', '', 'wait');
	  div.appendChild(waitani);
	}
	document.body.appendChild(div);

	Sd.UI.Mask.index ++;
	
	return div.id;
}

Sd.UI.Mask.destroy = function(maskId)
{
	var mask = $id(maskId);
	if(!mask) return;
	document.body.removeChild(mask);
}

Sd.UI.Mask.waitMask = function(item, message, spaceloc)
{
   var maskId = Sd.UI.Mask.create(item);
   var mask = $id(maskId);
   var div = $new('div', null, 'waitmask');
   var img = $new('img');
   var divText = $new('div', null, 'text');
   
   img.src = spaceloc ? spaceloc : '/images/Base/ajax.loading.gif';
   divText.innerHTML = 'Please wait...';

   div.appendChild(img);
   div.appendChild(divText);

   if(message) 
   {
   	var divMsg = $new('div', null, 'message');
   	divMsg.innerHTML = message;
    div.appendChild(divMsg);
   }

   mask.appendChild(div);
   
   var maskBounds = mask._bounds();
   var divBounds = div._bounds()
   
   div.style.marginTop = ((maskBounds.height - divBounds.height) /2) + 'px';
   
   return maskId;
};

Sd.UI.Dialog = Class.create();
Sd.UI.Dialog.type = {small: 0, large: 1};
Sd.UI.Dialog.prototype =
{
   initialize: function(id, type, buttons, x, y, caption, content, onClose, zIndex)
   {
      switch(type)
      {
         case Sd.UI.Dialog.type.small:
            this.className = 'dialog';
            break;
         case Sd.UI.Dialog.type.large:
            this.className = 'dialoglg';
            break;
      }
      
      this.id = id;
      this.content = content;
      this.caption = caption;
      this.x = x;
      this.y = y;
      this.onClose = onClose;
      this.initButtons = buttons;
      this.zIndex = zIndex;
      
      this.render();
   },
   
   render: function()
   {
      var dialog = $new('div', this.id, this.className);
      var top = $new('div', null, 'top');
      var caption = $new('div', null, 'caption');
      var mid = $new('div', null, 'mid');
      var content = $new('div', null, 'content');
      var bot = $new('div', null, 'bot');
      var buttons = $new('div', '_Buttons', 'buttons');
    
      if(this.caption) 
      {
      	if(typeof this.caption == 'string')
      		caption.innerHTML = this.caption;
      	else
      		caption.appendChild(this.caption);
      }    
    
      top.appendChild(caption);    
      mid.appendChild(content);
      if(this.content) 
      {
      	if(typeof this.content == 'string')
      		content.innerHTML = this.content;
      	else
      		content.appendChild(this.content);
      }
    
      this.buttons = buttons;  
      this.addButtons(this.initButtons);
      
      if(this.buttons._children().length > 0) bot.appendChild(buttons);
    
      dialog.appendChild(top);
      dialog.appendChild(mid);
      dialog.appendChild(bot);
      if(this.x) dialog.style.left = this.x + 'px';
      if(this.y) dialog.style.top = this.y + 'px';
     	if(this.zIndex) dialog.style.zIndex = this.zIndex;
     
      this.content = content;
      this.dialog = dialog;
      dialog.dialog = this;
      
      document.body.appendChild(dialog);
   },
   
   close: function()
   {
      if(this.maskId && this.maskId.length > 0) Sd.UI.Mask.destroy(this.maskId);
      this.maskId = null;
      document.body.removeChild(this.dialog);
      if(this.onClose) this.onClose();
   },
   
   disable: function()
   {
      this.maskId = Sd.UI.Mask.waitMask(this.content);
      var c = this.buttons._children();
      for(var i=0;i<c.length;i++)
         Sd.UI.disableButton(c[i], false, true);
   },
   
   clearContent: function()
   {
      this.content._children().clear();
   },

   clearMask: function()
   {
      if(this.maskId && this.maskId.length > 0) Sd.UI.Mask.destroy(this.maskId);
      this.maskId = null;
   },
   
   clearButtons: function()
   {
      this.buttons._children().clear();
   },
   
   addContent: function(content)
   {
      this.content.appendChild(content);
   },
   
   addButtons: function(buttons)
   {
   		if(!buttons) return;
      for(var button in buttons)
      {
         var b = $new('input', null, button);
         b.type = 'image';
         b.src = '/images/base/space.gif';
         b.buttonClick = buttons[button];
         b.onclick = function(){ if(this.buttonClick){ this.buttonClick(); }; return false; };
         b.dialog = this;
         this.buttons.appendChild(b);
      }   
   },
   
   addResult: function(result, message)
   {
      var res = $new('div', null, 'result');
      var msg = $new('span');
      var cheeky = $new('span', null, ((result == true) ? 'score' : 'fail'));
      var img = $new('img');
      img.src = '/images/base/space.gif';

      cheeky.appendChild(img);
      cheeky.innerHTML = (result == true) ? 'Huzzah! ' : 'Oh Bother. ';
      msg.innerHTML = message;
      
      res.appendChild(cheeky);
      res.appendChild(msg);
      
      this.addContent(res);
   }
}
