/*!
 * dsHistory, v1-beta5 $Rev$
 * Revision date: $Date$
 * Project URL: http://code.google.com/p/dshistory/
 * 
 * Copyright (c) Andrew Mattie (http://www.akmattie.net)
 * Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
 * THIS IS FREE SOFTWARE, BUT DO NOT REMOVE THIS COMMENT BLOCK
 */
var dsHistory=function(){function F(){var c=b?z():0;var d=C();if(!u&&(c<f||g!=d&&k[k.length-2]==d&&!a.IE)){o=true;t=false;if(g!=d&&k[k.length-2]==d||a.IE){l=l.concat(k.splice(k.length-1,1));if(a.IE){if(w.deferProcessing){window.setTimeout(function(){window.location.hash=k[k.length-1]},10)}else{window.location.hash=k[k.length-1]}}D();j=g}if(m.length>1){m[m.length-2](v?"back":{calledFromHistory:true,direction:"back"});n=n.concat(m.splice(m.length-1,1))}}else if(o&&!t&&(c>f||g!=d&&l[l.length-1]==d&&!a.IE)){u=false;if(g!=d&&l[l.length-1]==d||a.IE){if(a.IE)window.location.hash=l[l.length-1];D();j=g;k=k.concat(l.splice(l.length-1,1))}n[n.length-1](v?"forward":{calledFromHistory:true,direction:"forward"});m=m.concat(n.splice(n.length-1,1))}f=c}function E(d){var e=b?z():0;var f,h;if(k.length>0&&a.Gecko||a.WebKit||!b&&z()>0){if(g==""&&k.length>1){window.location.hash="_";g=C(true);k.push(g)}else if(g!=""||a.WebKit){f=m.splice(m.length-1,1)[0];window.location.hash=g+String(k.length);k.push(g+String(k.length));window.location.hash=g==""?"-":g;k.push(g==""?"-":g);m.push(function(a){if(v?a:a.direction=="back"){t=true;window.history.back()}else{u=true;window.history.forward()}});m.push(f)}return}if(e==0&&(k.length==(d?1:0)&&!a.IE||k.length==2&&a.IE)&&m.length<=1){A(1)}else{if(c)document.getElementById("dsHistoryFrame").src="data:,"+String(e+1);else{p.document.open();p.document.write(String(e+1));p.document.close()}}}function D(){w.QueryElements={};if(window.location.hash==""||window.location.hash=="#")return;var a=window.location.hash.substring(1).split("&");var b;for(i=0,len=a.length;i<len;++i){b=a[i].split("=");w.QueryElements[B(b[0])]=b.length==2?B(b[1]):""}g=C(true)}function C(a){var b=window.location.hash;if(!a&&b==lastRawHash)return g;lastRawHash=b;var c=b.substring(1).split("&");var d;if(c.length>9){var e=[];for(var f=0,i=c.length;f<i;++f){hashSplit=c[f].split("=");e.push(h(B(hashSplit[0]))+(hashSplit.length==2?"="+h(B(hashSplit[1])):""))}d=e.join("&")}else{d="";for(var f=0,i=c.length;f<i;++f){hashSplit=c[f].split("=");d+=(f==0?"":"&")+h(B(hashSplit[0]))+(hashSplit.length==2?"="+h(B(hashSplit[1])):"")}}return d}function B(a){if(d){var b=window.decodeURIComponent;B=function(a){return b(a)}}else{B=function(a){return a}}return B(a)}function A(a){if(c){A=function(a){p.document.body.textContent=String(a)}}else{A=function(a){p.document.body.innerText=String(a)}}A(a)}function z(){if(!b){z=function(){return 0}}else if(c){z=function(){return p.document.body?parseInt(p.document.body.textContent):0}}else{z=function(){return parseInt(p.document.body.innerText)}}return z()}function y(a,b,c){if(typeof c!="undefined"){return function(d){a.call(b||window,c,d)}}else{return function(c){a.call(b||window,c)}}}function x(){window.clearInterval(s);p=null;m=null}var a=function(){var a=window.navigator.userAgent;var b=!!(window.attachEvent&&!window.opera&&a.indexOf("Opera")==-1);return{IE:b,IE6:b&&a.indexOf("MSIE 6")!=-1,IE7:b&&a.indexOf("MSIE 7")!=-1,Opera:!!window.opera&&a.indexOf("Opera")!=-1,WebKit:a.indexOf("AppleWebKit/")>-1,Gecko:a.indexOf("Gecko")>-1&&a.indexOf("KHTML")==-1}}();var b=a.IE||a.Gecko;var c=a.Gecko;var d=a.IE||a.WebKit;var e=15;var f=0;var g=lastRawHash="";var h=window.encodeURIComponent;var j=initialHash=C(true);var k=[];var l=[];var m=[];var n=[];var o=false;var p;var q;var r=[];var s;var t,u;var v=false;var w;w={QueryElements:{},deferProcessing:false,initialize:function(a){if(typeof a=="function")a()},addFunction:function(c,d,e){if(b&&(!p||!p.document||!p.document.body)){r.push({type:arguments.callee,fnc:c,scope:d,objectArg:e});return}o=false;n=[];l=[];if(a.IE)k.push(C());m.push(y(c,d,e));E()},setQueryVar:function(a,b){var c,d;var e;a=String(a);b=String(typeof b=="undefined"?"":b);c=h(a);d=h(b);if(j=="#"||j==""||j.indexOf("#_serial")==0){if(d!="")j="#"+c+"="+d;else j="#"+c}else{if(typeof this.QueryElements[a]!="undefined"&&b!=""){e=j.search(c+"\\b");j=j.substr(0,j.indexOf(c)+c.length+1)+d+j.substr(j.indexOf(c)+c.length+1+String(h(this.QueryElements[a])).length)}else if(typeof this.QueryElements[a]=="undefined"){if(b=="")j+="&"+c;else j+="&"+c+"="+d}}this.QueryElements[a]=b;if(k>1&&k[k.length-2]==j)j+="&_serial="+k.length;else if(j.indexOf("_serial")!=-1)this.removeQueryVar("_serial")},removeQueryVar:function(a){if(!this.QueryElements[a]&&a!="_serial")return;var b,c,d;if(this.QueryElements[a]=="")b=h(a);else b=h(a)+"="+h(this.QueryElements[a]);c=j.indexOf(b);if(j[c-1]=="&"){b="&"+b;c--}j=j.substr(0,c)+j.substr(c+b.length);if(j[0]=="&")j=j.substr(1,j.length-1);delete this.QueryElements[a];if(j=="#"||j=="")j="_serial="+k.length},bindQueryVars:function(c,d,e,f){if(b&&(!p||!p.document||!p.document.body)){r.push({type:arguments.callee,fnc:c,scope:d,objectArg:e});return}if(C()==j.replace("#","")&&m.length>0)return false;if(this.deferProcessing&&!f){var h=arguments.callee;window.setTimeout(function(){h(c,d,e,true)},10);return}o=false;n=[];l=[];if(k.length==0&&m.length>0&&!a.IE)k.push(C());window.location.hash=j;g=C(true);k.push(g);m.push(y(c,d,e));if(a.IE)E(true);D()},setFirstEvent:function(a,b,c){if(m.length>0)m[0]=y(a,b,c)},setUsingStringIndicators:function(){v=true}};if(b){if(c)document.write('<iframe id="dsHistoryFrame" name="dsHistoryFrame" style="display:none" src="data:,0"></iframe>');else document.write('<iframe id="dsHistoryFrame" name="dsHistoryFrame" style="display:none" src="javascript:document.open();document.write(\'0\');document.close();"></iframe>');p=window.frames["dsHistoryFrame"];if(!p||!p.document||!p.document.body){q=window.setInterval(function(){p=window.frames["dsHistoryFrame"];if(p&&p.document&&p.document.body){window.clearInterval(q);s=window.setInterval(F,e);for(i=0,len=r.length;i<len;++i){var a=r[i];a.type(a.fnc,a.scope,a.objectArg)}r=null}},50)}else{s=window.setInterval(F,e)}}else{s=window.setInterval(F,e)}if(a.IE||a.WebKit)k.push(initialHash);D();if(window.addEventListener)window.addEventListener("unload",x,false);else if(window.attachEvent)window.attachEvent("onunload",x);return w}()
