// függőségek
if ("undefined" == typeof AITIA) {
	alert("AITIA include missing!");
}
if ("undefined" == typeof AITIA_BROWSER) {
	alert("AITIA_BROWSER include missing!");
}



// többszörös include
if ("undefined" != typeof AITIA_STYLE) {
	 alert("AITIA_STYLE multiple insert!");
}

AITIA_STYLE = 1;

Aitia.Style = {};

////TODO benchmark: regexp vs split array
Aitia.Style.hasClass = function(el,className) {
  if ( el && typeof el.className!='undefined' ) {
		//var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
		var re = new RegExp("(^|\\s)" + className + "(\\s|$)","gi");
	
		return re.test(el.className);
	} else {
		return false;
	}
} // hasClass


Aitia.Style.addClass = function(el, className) {
  if (null == el.className || "undefined" == typeof el.className || "" == el.className) {  //opera miatt
    el.className = className;
  } else if (false == this.hasClass(el, className)) {
    el.className += ' ' + className;
  }  
} // addClass

Aitia.Style.removeClass = function(el, className) {
  if (!(el && el.className)) {
    return;
  }
  var cls = el.className.split(' ');
  var ar = new Array();
  for (var i = cls.length; i > 0;) {
    if (cls[--i] != className) {
      ar[ar.length] = cls[i];
    }
  }
  el.className = ar.join(' ');
} // removeClass


Aitia.Style.removeStyle = function(el,attr) {
  if (el && el.style) {
    if (Aitia.Browser.ieDom) {
      el.style.removeAttribute(attr);  //IE
    } else {
      el.style.removeProperty(attr);
    }
  }
} // removeStyle


Aitia.Style.getCssRuleBySelectorFromSheet = function(sheet,selector) {
  if (Aitia.Browser.nsDom) {
    var ruleList = sheet.cssRules;

    for (var j = 0; j < ruleList.length; ++j) {
      //    alert("selector:"+ruleList[j].selectorText);
      if (ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector) {
        return ruleList[j];
      } else if (ruleList[j].type == CSSRule.IMPORT_RULE) {
        //         alert("import!!:"+ruleList[j].href);
        var ret = this.getCssRuleBySelectorFromSheet(ruleList[j].styleSheet,selector);
        if (ret != null) {
          return ret;
        }
      }
    }
  } else if (Aitia.Browser.ieDom) {
    //elobb az importalt sheet-et keressuk
    for (var j = 0; j < sheet.imports.length; ++j) {
      var ret = this.getCssRuleBySelectorFromSheet(sheet.imports[j],selector);
      if (ret != null) {
        return ret;
      }
    }
    var ruleList = sheet.rules;
    for (var j = 0; j < ruleList.length; ++j) {
//    alert(ruleList[j].selectorText);
      if (ruleList[j].selectorText == selector) {
        return ruleList[j];
      }   
    }
  
  }
  return null;
} // getCssRuleBySelectorFromSheet

//ez a style objektumot adja vissza, 
//lehet, hogy a cssrule-t kellene
Aitia.Style.getCssRuleBySelector = function( selector ) {
  var sheetList = document.styleSheets;
  var ruleList;
  var i, j;


  if (sheetList) {
    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i = sheetList.length-1; i >= 0; --i) {
      var ret = this.getCssRuleBySelectorFromSheet(sheetList[i],selector);
      if (ret != null) {
        return ret;
      }
    }
  }
  return null;
} // getCssRuleBySelector


//node.style -ebben az van, ami inline, vagy js-ből lett beállítva
Aitia.Style.getStyle = function(el,attr,num) {
  var value = el.style[this.toCamelCase(attr)];
  if ("" == value) {
    if (document.defaultView) {
      value = document.defaultView.getComputedStyle(el, "").getPropertyValue(attr);
    } else if (el.currentStyle) {
      value = el.currentStyle[this.toCamelCase(attr)];
    }
  }
  if (num) {
    value = parseInt(value);
    if (isNaN(value)) {
      value = 0;
    }
  }
  return value;
} // getStyle

Aitia.Style.toCamelCase = function( sInput ) {
  var sArray = sInput.split('-');
  if(1 == sArray.length) {
    return sArray[0];
  }
  var ret = sArray[0];
  for(var i = 1; i < sArray.length; ++i){
    var s = sArray[i];
    ret += s.charAt(0).toUpperCase() + s.substring(1)
  }
  return ret;
} // toCamelCase

Aitia.Style.fromCamelCase = function(camel) {
  return camel.replace(/([A-Z])/g,"-$1").toLowerCase();
} // fromCamelCase



Aitia.Style.camelize = function(currentElement) {
	var parts = currentElement.split('-'), len = parts.length;
	if (len == 1) return parts[0];

	var camelized = currentElement.charAt(0) == '-'
	? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
	: parts[0];

	for (var i = 1; i < len; i++)
	camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);

	return camelized;
}

Aitia.Style.capitalize = function(currentElement) {
	return currentElement.charAt(0).toUpperCase() + currentElement.substring(1).toLowerCase();
}

Aitia.Style.getComputedStyle = function(currentElement, style, currentWindow) {
	currentWindow = typeof currentWindow!='undefined' && currentWindow ? currentWindow : window;
	
	if ( Aitia.Browser.ieVersion>0 ) {
		style = (style=='float' || style=='cssFloat') ? 'styleFloat' : this.camelize(style);
		var value = currentElement.style[style];
		
		if ( !value && currentElement.currentStyle ) {
			value = currentElement.currentStyle[style];
		}

		if ( style=='opacity' ) {
			if ( value = (this.getComputedStyle(currentElement,'filter',currentWindow) || '').match(/alpha\(opacity=(.*)\)/) ) {
				if ( value[1] ) {
					return parseFloat(value[1]) / 100;
				}
			}
			
			return 1.0;
		}

		if ( value=='auto' ) {
			if ( (style=='width' || style=='height') && (this.getComputedStyle(currentElement,'display')!='none') ) {
				return currentElement['offset' + this.capitalize(style)] + 'px';
			}
			
			return null;
		}
		
		return value;
	} else if (currentWindow.document.defaultView.getComputedStyle) {
		style = (style=='float' || style=='styleFloat') ? 'cssFloat' : this.camelize(style);
		var value = currentElement.style[style];
		
		if ( !value || value=='auto' ) {
			// document.defaultView - window
			var css = currentWindow.document.defaultView.getComputedStyle(currentElement, null);
			value = css ? css[style] : null;
		}
		
		if ( style=='opacity' ) {
			return value ? parseFloat(value) : 1.0;
		}
		
		return value=='auto' ? null : value;
	}
	
	return null;
}


// a float értékét más attrubutmmal lehet elérni, böngészőktől függően.
function cssFloatStr()
{
	return (document.documentElement.style.cssFloat === undefined ? "styleFloat" : "cssFloat");
}
