//ʵΨһʶ
Native.UID = 1;

/*
Ψһʶ
ĿʹúǺʹĿԭռʱ,ÿִзʱж
*/
var $uid = (Browser.Engine.trident) ? function(item){
 return (item.uid || (item.uid = [Native.UID++]))[0];
} : function(item){
 return item.uid || (item.uid = Native.UID++);
};

//Window,windowչ
var Window = new Native({

 //,Ϊ$typeṩ׼ж
 name: 'Window',

 //չԭ
 legacy: (Browser.Engine.trident) ? null: window.Window,

 initialize: function(win){
  //Ψһʶ
  $uid(win);
  if (!win.Element){
   win.Element = $empty;
   if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2
   win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {};
  }
  return $extend(win, Window.Prototype);
 },

 //ͬʱԵǰwindowչ
 afterImplement: function(property, value){
  window[property] = Window.Prototype[property] = value;
 }

});

Window.Prototype = {$family: {name: 'window'}};

new Window(window);

//Windowչ
Window.implement({

 //Ԫ,ȡDOMĿݷʽ,ͬʱȡĶElement
 $: function(el, notrash){
  if (el && el.$family && el.uid) return el;
  var type = $type(el);

  //߼ŵķ,$.string,$.element,$.object$.textnode
  return ($[type]) ? $[type](el, notrash, this.document) : null;
 },

 //XPath﷨ȡDOM,Elements
 $$: function(selector){
  if (arguments.length == 1 && typeof selector == 'string') return this.document.getElements(selector);
  var elements = [];
  //ά
  var args = Array.flatten(arguments);
  for (var i = 0, l = args.length; i < l; i++){
   var item = args[i];
   switch ($type(item)){
    case 'element': item = [item]; break;
    case 'string': item = this.document.getElements(item, true); break;
    default: item = false;
   }
   if (item) elements.extend(item);
  }
  return new Elements(elements);
 },

 getDocument: function(){
  return this.document;
 },

 getWindow: function(){
  return this;
 }

});

//$,ΪstringʱĴ
$.string = function(id, notrash, doc){
 id = doc.getElementById(id);
 return (id) ? $.element(id, notrash) : null;
};

//ѾΪDOMʱĴ
$.element = function(el, notrash){
 $uid(el);
 if (!notrash && !el.$family && !(/^object|embed$/i).test(el.tagName)){
  var proto = Element.Prototype;
  for (var p in proto) el[p] = proto[p];
 };
 return el;
};

//ΪobjectʱĴ
$.object = function(obj, notrash, doc){
 if (obj.toElement) return $.element(obj.toElement(doc), notrash);
 return null;
};

//ΪıڵʱĴ,ʹ$argumentsֱӷصһ
$.textnode = $.whitespace = $.window = $.document = $arguments(0);

