/*
	General Leapfrog js-library for frontend
	might be replaced later by a framework
*/

leapfrog = this;
leapfrog.log = function (logTxt) {
	var console = 	document.getElementById('errorConsole');
	console.innerHTML = console.innerHTML + logTxt + '<br />';
}

/*
	function to attach a function to an event
	param: 	obj - js object
			evType - event type as string
			fn - function as js-function
	example: addEvent(window, 'load', foo)
*/

var popupWin;
 
function popUp(url,w,h,target,windowOptions) { 
            if ((!target)||(target=="")) target="_blank"; 
            if ((!windowOptions)||(windowOptions=="")) windowOptions= 'scrollbars,titlebar,resizable'; 
            popupWin = window.open(url,target,'width='+w+',height='+h+','+windowOptions); 
            setTimeout("popupWin.focus();",250); 
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

//addEvent(window, "load", initBreadcrumbs);
addEvent(window, "load", makeButtons);

// introducting css helper functions as class structure
css = this;
/*
	function css.myClass
	to handle className settings to an object
	param:	action - action to process as string (swap, add, remove, check)
			obj - DOM object to manipulate the className setting
			class1 - classname as string
			class2 - classname as string (only used for swap)

	actions:check - returns true, if class1 is already set on obj.className
			add - adds class1 to obj.className
			remove - removes class1 from obj.className
			swap - replace class1 and class2
*/
css.myClass = function (action, obj, class1, class2) {
	switch (action){
    case 'check':
      return new RegExp('\\b'+class1+'\\b').test(obj.className);
    break;
    case 'add':
      if(css.myClass('check',obj,class1)) break; // class already set 
      if(typeof obj.className == 'undefined') { // no class set
      	obj.className=class1;
      } else {
      	obj.className+=obj.className?' '+class1:class1;
	  }
    break;
    case 'remove':
      var rep=obj.className.match(' '+class1)?' '+class1:class1;
      obj.className=obj.className.replace(rep,'');
    break;
    case 'swap':
      o.className=!css.myClass.check(obj,class1)?obj.className.replace(class2,class1): o.className.replace(class1,class2);
    break;
  }
}

css.myClass.check = function (obj, class1) {
	return css.myClass ('check', obj, class1, null);
}

css.myClass.add = function (obj, class1) {
	css.myClass ('add', obj, class1, null);
}

css.myClass.remove = function (obj, class1) {
	css.myClass ('remove', obj, class1, null);
}

css.myClass.swap = function (obj, class1, class2) {
	css.myClass ('swap', obj, class1, class2);
}




function hasClass (o, c) {
      return new RegExp('\\b'+c+'\\b').test(o.className);
}




/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res))
    	return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return (	hasClass(nod, 'skip') || // explictly skip
  			nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}


/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}



/*	moves breadcrumb element if in a 2 column layout */
function initBreadcrumbs()
{
	var page = document.getElementById("page");
	if (page) {
		var children = page.childNodes;
		for (var i = 0; i <children.length ; i++) {
			var child = children.item(i);
			if (css.myClass.check(child, 'col_2')&&(page.className!='productDetail')&&(page.className!='productDetailSearch')) { 
				var firstChild = first_child(child);
				var breadcrumbs = document.getElementById("breadcrumbs");
				if (breadcrumbs) {child.insertBefore( breadcrumbs, firstChild ); }
			}
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
	var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

