var gadgets={};;
var gadgets=gadgets||{};
if(window.JSON){gadgets.json={parse:function(B){try{return window.JSON.parse(B)
}catch(A){return false
}},stringify:function(B){try{return window.JSON.stringify(B)
}catch(A){return null
}}}
}else{gadgets.json=function(){function f(n){return n<10?"0"+n:n
}Date.prototype.toJSON=function(){return[this.getUTCFullYear(),"-",f(this.getUTCMonth()+1),"-",f(this.getUTCDate()),"T",f(this.getUTCHours()),":",f(this.getUTCMinutes()),":",f(this.getUTCSeconds()),"Z"].join("")
};
var m={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};
function stringify(value){var a,i,k,l,r=/["\\\x00-\x1f\x7f-\x9f]/g,v;
switch(typeof value){case"string":return r.test(value)?'"'+value.replace(r,function(a){var c=m[a];
if(c){return c
}c=a.charCodeAt();
return"\\u00"+Math.floor(c/16).toString(16)+(c%16).toString(16)
})+'"':'"'+value+'"';
case"number":return isFinite(value)?String(value):"null";
case"boolean":case"null":return String(value);
case"object":if(!value){return"null"
}a=[];
if(typeof value.length==="number"&&!value.propertyIsEnumerable("length")){l=value.length;
for(i=0;
i<l;
i+=1){a.push(stringify(value[i])||"null")
}return"["+a.join(",")+"]"
}for(k in value){if(value.hasOwnProperty(k)){if(typeof k==="string"){v=stringify(value[k]);
if(v){a.push(stringify(k)+":"+v)
}}}}return"{"+a.join(",")+"}"
}}return{stringify:stringify,parse:function(text){if(/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/b-u]/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){if ((typeof(text) == 'string') && (text.length > 0))return eval("("+text+")")
}return false
}}
}()
};;

gadgets.hashRpc = function() {
	s = {
		formatUrl: function(caller) {
			return encodeURIComponent("#json-rpc="+gadgets.json.stringify(caller));
		},
		callService: function(method,params) {
			var caller = {
				method: method,
				params: params,
				id: (new Date()).getTime()
			};
			var myUrl = gadgets.hashRpc.formatUrl(caller);
			try {
				parent.setHash(myUrl);
			} catch(e) {
				parent.location.hash = myUrl;
			}
		}
	};
	return s;
}();

oldHeight = false;

gadgets.window = function() {
	s = {
		getViewportDimensions: function() {
			var x,y;
			if (self.innerHeight) {
				// all except Explorer
				x = self.innerWidth;
				y = self.innerHeight;
			} else if (document.documentElement &&
				document.documentElement.clientHeight) {
				// Explorer 6 Strict Mode
				x = document.documentElement.clientWidth;
				y = document.documentElement.clientHeight;
			} else if (document.body) {
				// other Explorers
				x = document.body.clientWidth;
				y = document.body.clientHeight;
			} else {
				x = 0;
				y = 0;
			}
			return {width: x, height: y};

		},
		adjustHeight: function(opt_height, POG) {

			if(typeof(POG) == 'undefined') {
				gadgets.window.adjustHeight(750, true);
				setTimeout(function() { gadgets.window.adjustHeight(opt_height, true) }, 250);
				return true;
			}

			var newHeight = parseInt(opt_height, 10);
			if (isNaN(newHeight)) {
				var vh = gadgets.window.getViewportDimensions().height;
				var body = document.body;
				var docEl = document.documentElement;
				if (document.compatMode === 'CSS1Compat' && docEl.scrollHeight) {
					newHeight = docEl.scrollHeight !== vh ?
					docEl.scrollHeight : docEl.offsetHeight;
				} else {
					var sh = docEl.scrollHeight;
					var oh = docEl.offsetHeight;
					if (docEl.clientHeight !== oh) {
						sh = body.scrollHeight;
						oh = body.offsetHeight;
					}

					if (sh > vh) {
						newHeight = sh > oh ? sh : oh;
					} else {
						newHeight = sh < oh ? sh : oh;
					}
				}
			};

			if ( (typeof(oldHeight) == 'undefined' ) || ( newHeight !== oldHeight ) ) {
				oldHeight = newHeight;
				try {
					gadgets.hashRpc.callService('adjustHeight',[oldHeight]);
				} catch(e) {
					//
				}
			}
		}
	};
	return s;
}();


gadgets.generalServices = function() {
	s = {
		activiePost: function(title,body) {
			gadgets.hashRpc.callService('activePost',[{"title":title,"body":body}]);
		}
	}
	return s;
}();

