/*
 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.com/yui/license.html
 version: 3.3.0
 build: 3167
 */
YUI.add('history-base',function(Y){var Lang=Y.Lang,Obj=Y.Object,GlobalEnv=YUI.namespace('Env.History'),YArray=Y.Array,doc=Y.config.doc,docMode=doc.documentMode,win=Y.config.win,DEFAULT_OPTIONS={merge:true},EVT_CHANGE='change',SRC_ADD='add',SRC_REPLACE='replace';function HistoryBase(){this._init.apply(this,arguments);}
Y.augment(HistoryBase,Y.EventTarget,null,null,{emitFacade:true,prefix:'history',preventable:false,queueable:true});if(!GlobalEnv._state){GlobalEnv._state={};}
function _isSimpleObject(value){return Lang.type(value)==='object';}
HistoryBase.NAME='historyBase';HistoryBase.SRC_ADD=SRC_ADD;HistoryBase.SRC_REPLACE=SRC_REPLACE;HistoryBase.html5=!!(win.history&&win.history.pushState&&win.history.replaceState&&('onpopstate'in win||Y.UA.gecko>=2));HistoryBase.nativeHashChange=('onhashchange'in win||'onhashchange'in doc)&&(!docMode||docMode>7);Y.mix(HistoryBase.prototype,{_init:function(config){var initialState;config=this._config=config||{};initialState=this._initialState=this._initialState||config.initialState||null;this.publish(EVT_CHANGE,{broadcast:2,defaultFn:this._defChangeFn});if(initialState){this.add(initialState);}},add:function(){var args=YArray(arguments,0,true);args.unshift(SRC_ADD);return this._change.apply(this,args);},addValue:function(key,value,options){var state={};state[key]=value;return this._change(SRC_ADD,state,options);},get:function(key){var state=GlobalEnv._state,isObject=_isSimpleObject(state);if(key){return isObject&&Obj.owns(state,key)?state[key]:undefined;}else{return isObject?Y.mix({},state,true):state;}},replace:function(){var args=YArray(arguments,0,true);args.unshift(SRC_REPLACE);return this._change.apply(this,args);},replaceValue:function(key,value,options){var state={};state[key]=value;return this._change(SRC_REPLACE,state,options);},_change:function(src,state,options){options=options?Y.merge(DEFAULT_OPTIONS,options):DEFAULT_OPTIONS;if(options.merge&&_isSimpleObject(state)&&_isSimpleObject(GlobalEnv._state)){state=Y.merge(GlobalEnv._state,state);}
this._resolveChanges(src,state,options);return this;},_fireEvents:function(src,changes,options){this.fire(EVT_CHANGE,{_options:options,changed:changes.changed,newVal:changes.newState,prevVal:changes.prevState,removed:changes.removed,src:src});Obj.each(changes.changed,function(value,key){this._fireChangeEvent(src,key,value);},this);Obj.each(changes.removed,function(value,key){this._fireRemoveEvent(src,key,value);},this);},_fireChangeEvent:function(src,key,value){this.fire(key+'Change',{newVal:value.newVal,prevVal:value.prevVal,src:src});},_fireRemoveEvent:function(src,key,value){this.fire(key+'Remove',{prevVal:value,src:src});},_resolveChanges:function(src,newState,options){var changed={},isChanged,prevState=GlobalEnv._state,removed={};if(!newState){newState={};}
if(!options){options={};}
if(_isSimpleObject(newState)&&_isSimpleObject(prevState)){Obj.each(newState,function(newVal,key){var prevVal=prevState[key];if(newVal!==prevVal){changed[key]={newVal:newVal,prevVal:prevVal};isChanged=true;}},this);Obj.each(prevState,function(prevVal,key){if(!Obj.owns(newState,key)||newState[key]===null){delete newState[key];removed[key]=prevVal;isChanged=true;}},this);}else{isChanged=newState!==prevState;}
if(isChanged){this._fireEvents(src,{changed:changed,newState:newState,prevState:prevState,removed:removed},options);}},_storeState:function(src,newState){GlobalEnv._state=newState||{};},_defChangeFn:function(e){this._storeState(e.src,e.newVal,e._options);}},true);Y.HistoryBase=HistoryBase;},'3.3.0',{requires:['event-custom-complex']});
