//--------------------------------------
//               PowerUI
//
//        For documentation or 
//    if you have any issues, visit
//        powerUI.kulestar.com
//
//    Copyright  2013 Kulestar Ltd
//          www.kulestar.com
//--------------------------------------

using System;
using Dom;


namespace Css{
	
	/// <summary>
	/// Matches if it's got no siblings of the same element type.
	/// <summary>

	sealed class OnlyOfType:CssKeyword{
		
		public override string Name{
			get{
				return "only-of-type";
			}
		}
		
		public override SelectorMatcher GetSelectorMatcher(){
			return new OnlyOfTypeMatcher();
		}
		
	}
	
	/// <summary>
	/// Handles the matching process for :first-child.
	/// </summary>
	
	sealed class OnlyOfTypeMatcher:LocalMatcher{
		
		public override bool TryMatch(Dom.Node node){
			
			if(node==null){
				return false;
			}
			
			// Get parent:
			Node parent=node.parentNode;
			
			if(parent==null || parent.childNodes_==null){
				return true;
			}
			
			// Get the list of siblings:
			NodeList kids=parent.childNodes_;
			
			// Get the elements tag:
			string type=(node as Element).Tag;
			
			// Compare to each sibling:
			for(int i=0;i<kids.length;i++){
				
				Element kid=kids[i] as Element;
				
				if(kid==node || kid==null){
					continue;
				}
				
				if(kid.Tag==type){
					return false;
				}
				
			}
			
			return true;
			
		}
		
	}
	
}