/* jQuery plugins */

//COOKIES
jQuery.cookie=function(a,b,c){if(typeof b!='undefined'){c=c||{};if(b===null){b='';c.expires=-1}var d='';if(c.expires&&(typeof c.expires=='number'||c.expires.toUTCString)){var e;if(typeof c.expires=='number'){e=new Date();e.setTime(e.getTime()+(c.expires*24*60*60*1000))}else{e=c.expires}d='; expires='+e.toUTCString()}var f=c.path?'; path='+(c.path):'';var g=c.domain?'; domain='+(c.domain):'';var h=c.secure?'; secure':'';document.cookie=[a,'=',encodeURIComponent(b),d,f,g,h].join('')}else{var j=null;if(document.cookie&&document.cookie!=''){var k=document.cookie.split(';');for(var i=0;i<k.length;i++){var l=jQuery.trim(k[i]);if(l.substring(0,a.length+1)==(a+'=')){j=decodeURIComponent(l.substring(a.length+1));break}}}return j}};

//AJAX+HISTORY
jQuery.extend({historyCurrentHash:undefined,historyCallback:undefined,historyIframeSrc:undefined,historyInit:function(a,b){jQuery.historyCallback=a;if(b)jQuery.historyIframeSrc=b;var c=location.hash.replace(/\?.*$/,'');jQuery.historyCurrentHash=c;if(jQuery.browser.msie){if(jQuery.historyCurrentHash==''){jQuery.historyCurrentHash='#'}jQuery("body").prepend('<iframe id="jQuery_history" style="display: none;"'+(jQuery.historyIframeSrc?' src="'+jQuery.historyIframeSrc+'"':'')+'></iframe>');var d=jQuery("#jQuery_history")[0];var e=d.contentWindow.document;e.open();e.close();e.location.hash=c}else if(jQuery.browser.safari){jQuery.historyBackStack=[];jQuery.historyBackStack.length=history.length;jQuery.historyForwardStack=[];jQuery.lastHistoryLength=history.length;jQuery.isFirst=true}jQuery.historyCallback(c.replace(/^#/,''));setInterval(jQuery.historyCheck,100)},historyAddHistory:function(a){jQuery.historyBackStack.push(a);jQuery.historyForwardStack.length=0;this.isFirst=true},historyCheck:function(){if(jQuery.browser.msie){var a=jQuery("#jQuery_history")[0];var b=a.contentDocument||a.contentWindow.document;var c=b.location.hash.replace(/\?.*$/,'');if(c!=jQuery.historyCurrentHash){location.hash=c;jQuery.historyCurrentHash=c;jQuery.historyCallback(c.replace(/^#/,''))}}else if(jQuery.browser.safari){if(jQuery.lastHistoryLength==history.length&&jQuery.historyBackStack.length>jQuery.lastHistoryLength){jQuery.historyBackStack.shift()}if(!jQuery.dontCheck){var d=history.length-jQuery.historyBackStack.length;jQuery.lastHistoryLength=history.length;if(d){jQuery.isFirst=false;if(d<0){for(var i=0;i<Math.abs(d);i++)jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop())}else{for(var i=0;i<d;i++)jQuery.historyBackStack.push(jQuery.historyForwardStack.shift())}var e=jQuery.historyBackStack[jQuery.historyBackStack.length-1];if(e!=undefined){jQuery.historyCurrentHash=location.hash.replace(/\?.*$/,'');jQuery.historyCallback(e)}}else if(jQuery.historyBackStack[jQuery.historyBackStack.length-1]==undefined&&!jQuery.isFirst){if(location.hash){var c=location.hash;jQuery.historyCallback(location.hash.replace(/^#/,''))}else{var c='';jQuery.historyCallback('')}jQuery.isFirst=true}}}else{var c=location.hash.replace(/\?.*$/,'');if(c!=jQuery.historyCurrentHash){jQuery.historyCurrentHash=c;jQuery.historyCallback(c.replace(/^#/,''))}}},historyLoad:function(a){var b;a=decodeURIComponent(a.replace(/\?.*$/,''));if(jQuery.browser.safari){b=a}else{b='#'+a;location.hash=b}jQuery.historyCurrentHash=b;if(jQuery.browser.msie){var c=jQuery("#jQuery_history")[0];var d=c.contentWindow.document;d.open();d.close();d.location.hash=b;jQuery.lastHistoryLength=history.length;jQuery.historyCallback(a)}else if(jQuery.browser.safari){jQuery.dontCheck=true;this.historyAddHistory(a);var e=function(){jQuery.dontCheck=false};window.setTimeout(e,200);jQuery.historyCallback(a);location.hash=b}else{jQuery.historyCallback(a)}}});

//scrollTo plugin
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

//sprintf
function sprintf(){var p=/%%|%(\d+\$)?([-+#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;var a=arguments,i=0,format=a[i++];var q=function(a,b,c,d){var e=(a.length>=b)?'':Array(1+b-a.length>>>0).join(c);return d?a+e:e+a};var r=function(a,b,c,d,e){var f=d-a.length;if(f>0){if(c||!e){a=q(a,d,' ',c)}else{a=a.slice(0,b.length)+q('',f,'0',true)+a.slice(b.length)}}return a};var s=function(a,b,c,d,e,f,g){var h=a>>>0;c=c&&h&&{'2':'0b','8':'0','16':'0x'}[b]||'';a=c+q(h.toString(b),f||0,'0',false);return r(a,c,d,e,g)};var t=function(a,b,c,d,e){if(d!=null){a=a.slice(0,d)}return r(a,'',b,c,e)};var u=function(b,c,d,e,_,f,g){if(b=='%%')return'%';var h=false,positivePrefix='',zeroPad=false,prefixBaseX=false;for(var j=0;d&&j<d.length;j++)switch(d.charAt(j)){case' ':positivePrefix=' ';break;case'+':positivePrefix='+';break;case'-':h=true;break;case'0':zeroPad=true;break;case'#':prefixBaseX=true;break}if(!e){e=0}else if(e=='*'){e=+a[i++]}else if(e.charAt(0)=='*'){e=+a[e.slice(1,-1)]}else{e=+e}if(e<0){e=-e;h=true}if(!isFinite(e)){throw new Error('sprintf: (minimum-)width must be finite');}if(!f){f='fFeE'.indexOf(g)>-1?6:(g=='d')?0:void(0)}else if(f=='*'){f=+a[i++]}else if(f.charAt(0)=='*'){f=+a[f.slice(1,-1)]}else{f=+f}var k=c?a[c.slice(0,-1)]:a[i++];switch(g){case's':return t(String(k),h,e,f,zeroPad);case'c':return t(String.fromCharCode(+k),h,e,f,zeroPad);case'b':return s(k,2,prefixBaseX,h,e,f,zeroPad);case'o':return s(k,8,prefixBaseX,h,e,f,zeroPad);case'x':return s(k,16,prefixBaseX,h,e,f,zeroPad);case'X':return s(k,16,prefixBaseX,h,e,f,zeroPad).toUpperCase();case'u':return s(k,10,prefixBaseX,h,e,f,zeroPad);case'i':case'd':{var l=parseInt(+k);var m=l<0?'-':positivePrefix;k=m+q(String(Math.abs(l)),f,'0',false);return r(k,m,h,e,zeroPad)}case'e':case'E':case'f':case'F':case'g':case'G':{var l=+k;var m=l<0?'-':positivePrefix;var n=['toExponential','toFixed','toPrecision']['efg'.indexOf(g.toLowerCase())];var o=['toString','toUpperCase']['eEfFgG'.indexOf(g)%2];k=m+Math.abs(l)[n](f);return r(k,m,h,e,zeroPad)[o]()}default:return b}};return format.replace(p,u)}

