/**
 *----------------------------------------------------------*-----------------*
 *     Web Project Manager                                  |   frontSystem   |
 *----------------------------------------------------------*-----------------*
 *     @category frontSystem
 *     @package javascript
 *     @version 0.1 [2008/11/29]
 *
 *     @author Tomáš Režňák <tomas.reznak@web-project-manager.com>
 *     @copyright Copyright © 2008, Tomáš Režňák
 *     @link http://www.web-project-manager.com/
 *----------------------------------------------------------------------------*
 *     definition of general javascript functions
 */


/*
 * Virtualis scripts
 */
 
/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = "";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bexternal\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				objWarningText = document.createElement("em");
				objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
		//		link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

addEvent(window, 'load', getNewWindowLinks);



/*
 * HTML object manipulation functions / START
 */
 
/**
 * get HTML element defined by id
 *
 * @return obj    HTML element
 */
function element(id) {
  var obj = document.getElementById(id);
  
  return obj;
}
 
/**
 * hide HTML element
 *    
 * @param mixed obj    object or object's id
 * @return void
 */
function elementHide(obj) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  obj.style.display = "none";
}

/**
 * show HTML element
 *    
 * @param mixed obj            object or object's id
 * @param boolean is_inline    [optional] if element is inline, pass true as second parameter
 * @return void
 */
function elementShow(obj, is_inline) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }

  obj.style.display = (is_inline ? "inline" : "block");
}


/**
 * switch element class name - if element has className class_1, replace it with class_2 and vice versa
 *    
 * @param mixed obj         object or object's id
 * @param string class_1    name of class 1
 * @param string class_2    name of class 2
 * @return void
 */
function switchClass(obj, class_1, class_2) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  if (obj.className == "") {    // class name empty -> set class name from class_1
    obj.className = class_1;
  } else {
    if (obj.className.indexOf(class_1) != -1) {    // class_1 exists -> replace with class_2
      obj.className = obj.className.replace(class_1, class_2);
    } else {
      if ((obj.className.indexOf(class_2) != -1) && (class_2 != "")) {    // class_2 exists -> replace with class_1
        obj.className = obj.className.replace(class_2, class_1);
      } else {    // if not found, add class_1 to existing
        obj.className = obj.className + " " + class_1;
      }
    }
  }
}

/**
 * get cookie value
 *         
 * @param string name    cookie name
 * @return mixed         cookie value if cookie set, undefined otherwise
 */
function getCookie(name) {
  var t;
  var search = name + "=";
  
  if (document.cookie.length) {
    var start = document.cookie.indexOf(search);
    if (start != -1) {
      start += search.length;
      end = document.cookie.indexOf(";", start);
      
      if (end == -1) {
        end = document.cookie.length;
      }
      
      t = unescape(document.cookie.substring(start, end));
    }
  }
  
  return t;
}

/**
 * set cookie value
 *         
 * @param string name            cookie name
 * @param string value           cookie value
 * @param integer days_expire    [optional] number of days after which cookie will expire
 * @return void
 */
function setCookie(name, value, days_expire) {
	if (days_expire) {
		var expires = new Date();
		expires.setTime(expires.getTime() + 1000*60*60*24*days_expire);
	}
	
	document.cookie = name + "=" + escape(value) + (days_expire == null ? "" : (";expires=" + expires.toGMTString())) + ";path=/";
}

/*
 * HTML object manipulation functions / END
 */


/*
 * card functions / START
 */
 
/**
 * display user card
 *
 * @param integer id         user id  
 * @param object obj         reference object used for card positioning
 * @param string template    card template which generates card content
 * @param string script      URL leading to ajax script processing requests
 * @return void
 */
function cardShowUser(id, obj, template, script) {
  var width = "300";    // approximate card width (in pixels)
  var pos = elementPosition(obj);
  var left, top;
  if ((pos.x) > (width + 5)) {
    left = (pos.x - width);
  } else {
    left = pos.x;
  }
  top = pos.y;

  var ajax = new traxi();
  ajax.action_script = script;
  ajax.execute_response = true;
  ajax.set("action", "card");
  ajax.set("type", "user");
  ajax.set("id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();	  
}

/**
 * hide card (typically when user clicks on 'close' link within the card)
 *    
 * @return void
 */
function cardClose() {
  var obj = element("card_div");
  if (obj) {
    obj.parentNode.removeChild(obj);
  }
}

/**
 * calculate element absolute position
 *    
 * @param object obj     reference object used for positioning
 * @param string type    [optional] type of coordinates to be returned
 *                         - 'bottomright' or not set: coordinates of bottom right corner
 *                         - 'topleft': coordinates of top left corner
 * @return object        element position (x, y)
 */
function elementPosition(obj, type) {
  var pos = {x:0, y:0};
	
  if (type == null) {
    type = "bottomright";
  }
	
  pos.x = obj.offsetLeft
  pos.y = obj.offsetTop
	
  if (type == "bottomright") {
    pos.x += obj.offsetWidth;
    pos.y += obj.offsetHeight;
  }

  while((obj = obj.offsetParent) != null) {
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
  }
	
  return pos;
}

/*
 * card functions / END
 */
 

/* 
 * tab functions / START
 */

/**
 * hide tab
 *    
 * @param string tab_name    name of tab to hide
 * @return void
 */
function tabHide(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementHide(tab);
  }
}

/**
 * show tab
 *    
 * @param string tab_name    name of tab to show
 * @return void
 */
function tabShow(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementShow(tab, true);
  }
}

/**
 * change active tab to tab_name
 *    
 * @param string tab_name    name of tab which should be set as active
 * @return void
 */
function tabChange(tab_name) {
	elementHide("tab_" + active_tab);
	element("tabname_" + active_tab).className = "";
	
	elementShow("tab_" + tab_name);
	element("tabname_" + tab_name).className = "active";
	active_tab = tab_name;
}

/*
 * tab functions / END
 */

