﻿
var seek4 = {
	debug: true,

	ajax: {
		defaults: {
			type: "GET",
			async: false
		},

		GetData: function(_url, _opt) {
			if (typeof (_opt) == "undefined") _opt = {};
			_opt = this.mapDefaults(_opt);
			_opt.url = _url;
			if (!_opt.async){
				try{
					eval( $J.ajax(_opt).responseText );
				}catch(exception){
					seek4.debugAlert(exception.message);
					AJAXResponse = null;
				}
				
				if (AJAXResponse == null) {
					seek4.debugAlert("Uncachable exception after ajax request. Possible 500.");
					return null;
				} else {
					return AJAXResponse;
				}
			}else{
				$J.ajax(_opt);
			}
		},
		mapDefaults: function(_o) {
			if (typeof (_o.type) == "undefined") _o.type = this.defaults.type;
			if (typeof (_o.async) == "undefined") _o.async = this.defaults.async;
			return _o;
		},
		GetHTML : function(_url, _opt){
			if (typeof (_opt) == "undefined") _opt = {};
			_opt = this.mapDefaults(_opt);
			_opt.url = _url;
			if (!_opt.async)
				return $J.ajax(_opt).responseText;
			$J.ajax(_opt);
		}
	},

	debugAlert: function(_message) {
		if (this.debug) alert(_message);
	},

	submit: function(_commandName, _commandValue, _formId, _url) {
		var ht = "<input type='hidden' name='commandName' value='" + _commandName + "'>";
		ht += "<input type='hidden' name='commandValue' value='" + _commandValue + "'>";
		var form = null;
		if (typeof (_formId) != "undefined" && _formId != null) form = $J("#" + _formId);
		if (form == null || form.length == 0) form = $J($J("form")[0]);
		if (typeof (form[0].submit) != "function") {
			$J("body").append("<form id='form1' name='form1' method='post'></form>");
			form = $J("#form1");
		}
		if (typeof (_url) != "undefined" && _url != null) form.attr("action", _url);
		form.append(ht);
		form[0].submit();
	},

	PrepareQueryString: function(_obj) {
		var res = "";
		for (prop in _obj) {
			res += prop + "=" + _obj[prop] + "&";
		}
		if (res.length>0) res = res.substring(0, res.length-1);
		return res;
	},

	SetCookie: function(name, value, minutes) {
		if (minutes) {
			var date = new Date();
			date.setTime(date.getTime() + (minutes * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";

	},

	GetCookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	},

	getScroll: function() {
		var scrOfX = 0, scrOfY = 0;
		if (typeof (window.pageYOffset) == 'number') {
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return { left: scrOfX, top: scrOfY };

	},

	setScrollX: function(left) {
		if (typeof (window.pageXOffset) == 'number') {
			window.pageXOffset = left;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			document.body.scrollLeft = left;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			document.documentElement.scrollLeft = left;
		}
	},

	setScrollY: function(top) {
		if (typeof (window.pageYOffset) == 'number') {
			var x = getScroll().left;
			window.scrollTo(x, top);
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			document.body.scrollTop = top;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			document.documentElement.scrollTop = top;
		}
	},

	getClientSize: function() {
		var myWidth = 0, myHeight = 0;
		if (typeof (window.innerWidth) == 'number') {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return { width: myWidth, height: myHeight };
	},
	
	WaitSign : {
		signs : [],
		parent : null,
		//		sign :{
//			id : "",
//			container:null,
//			selector:"",
//			element : null,
//			css : "",
//			init : function(_id, _selector, _o){
//				_o = typeof(_o)=="undefined"?{}:_o;
//				this.id = id;
//				this.css = typeof(_o.css)!="undefined"?_o.css:"";
//				this.selector = selector;
//				this.container = $(selector);
//				var ht = "<div id='#"+_id+"' class='wait-sign "+this.css+"'>&nbsp;</div>";
//				this.container.append(ht);
//				this.element = $J('#'+_id);
//			}
//		},
		
		show : function(id, forceCreate, _o){
			_o = typeof(_o)=="undefined"?{}:_o;
			if (forceCreate || $J("#"+id+"_wait").length==0) this.signs[id] = this.create(id,_o);
			this.signs[id].css("height", $J("#"+id).height());
			var d = typeof(_o.delay)!="undefined"?_o.delay:0;
			this.signs[id].show(d);
		},
		hide:function(id, delay){
			var d = typeof(delay)!="undefined"?delay:0;
			$J("#"+id+"_wait").hide(d, function(){$J("#"+id+"_wait").remove();});
		},
		create : function(id, _o){
			var container = $J("#"+id);
			container.css("position","relative");
			var css = typeof(_o.css)!="undefined"?_o.css:"";
			var ht="<div id='"+id+"_wait' class='wait-sign "+css+"'>&nbsp;</div>";
			container.append(ht);
			var el = $J("#"+id+"_wait");
			return el;
		}
		
		
	},
	
	UrlHash : {
		params : {},
		
		init : function(){
			this.deserialize();
		},
		
		Set : function(_name, _value){
			this.params[_name] = _value;
			this.updateWindowUrl(this.serialize());
		},
		
		Get : function(_name){
			if (typeof (this.params[_name]) != "undefined") return this.params[_name];
			return null;
		},
		
		Remove : function(_name){
			this.params[_name] = null;
		},
		
		deserialize : function(){
			var pairs = window.location.hash.substring(1).split("&");
			var l = pairs.length;
			for (var i=0;i<l;i++){
				var pair = pairs[i].split("=");
				this.params[pair[0]]=pair[1];
			}
		},
		
		serialize : function(){
			var res = "";
			for (var name in this.params){
				if (typeof(this.params[name]) != "function" && this.params[name] != null){
					res += name +"="+this.params[name]+"&";
				}
			}
			if (res[res.length-1] == "&") res = res.substr(0, res.length-1);
			return res;
		},
		updateWindowUrl : function(hash){
			window.location.hash = hash;
		}
	},
	
	map : {
		create : function(_container, _center, _zoom){
			var map = new GMap2(_container);
			if (typeof(_center) != "undefined")
				map.setCenter(_center, _zoom);
			map.enableScrollWheelZoom();
			map.addControl(new GLargeMapControl3D());

			// Add GHierarchicalMapTypeControl
			map.addMapType(G_PHYSICAL_MAP);
			var hControl = new GHierarchicalMapTypeControl();
			hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
			map.addControl(hControl);
			return map;
  		}
  		
	},
	
	convert : {
		metersToMiles : function(val){
			return val / 1609.344;
		},
		metersToKm : function(val){
			return val / 1000;
		}
	}
};

$(document).ready(function(){
	seek4.UrlHash.init();
});

Array.prototype.removeEmptyElements = function() {
	var i=0;
	while (i <this.length){
		if (this[i] == null){
			this.splice(i,1);
			continue;
		}
		i++;
	}
}

Array.prototype.contains = function(val){
		for (var i=0;i<this.length;i++){
			if (this[i] == val) return true;
		}
		return false;
}
Array.prototype.remove = function(val){
	var i=0;
	while (i <this.length){
		if (this[i] == val){
			this.splice(i,1);
			continue;
		}
		i++;
	}
}