var ART_CHILD_WINDOWS = new Array();
var ART_SYSTEM_PROPERTIES = new Array("visible","isVisible","legendIcon","idObject","idLang","idOwner","idCreator","dateUpdate","dateInsert","validFrom","validTo","id","customOrder");



window.onclose = function(){
	opener.alert(54);
}

function isObject(o){
	return typeof o == "object" && o != null;
}
function isFunction(o){
	return typeof o == "function" && o != null;
}
function isString(s){
	return typeof s == "string";
}

function systemInfo(msg){
	if(isFunction(userInfo)){
		userInfo(msg);
	}
}


function isArtObject(object){
	if(typeof object != "object"){console.log("isArtObject: Provided argument is not of 'object' type.");return false;}
	if(object==null){console.log("isArtObject: Provided argument is NULL.");return false;}
	if(typeof object.__art_meta == "undefined"){console.log("isArtObject: There is no meta record in provided object.");return false;}
	if(typeof object.setData != "function"){console.log("isArtObject: There is no setData function  in provided object.");return false;}
	return true;
}

function loading(bool){
	dojo.require("dijit.util.place");
	var img = dojo.byId('art_loading_image');
	if(bool){
		if(typeof img != "object" || img == null){
			img = document.createElement("img");
			img.id = "art_loading_image";
			img.src = ART_THEME_DIR + "default/images/loading.gif";
			dojo.style(img,"position","absolute");
			dojo.style(img,"zIndex",2000);
			//dojo.style(img,"zIndex",2000);
			document.body.appendChild(img);
		}
		centerElement(img);
	}else{
		console.log("stop laoding");
		if(typeof img == "object" && img != null){
			dojo._destroyElement(img);
		}
	}
}

function centerElement(domNode){
	domNode = dojo.byId(domNode);
	var viewport = dijit.util.getViewport();
	var mb = dojo.marginBox(domNode);

	var style = domNode.style;
	style.left = (viewport.l + (viewport.w - mb.w)/2) + "px";
	style.top = (viewport.t + (viewport.h - mb.h)/2) + "px";
}


function openPlugin(pluginName,urlParams,windowParams){
	var url = pluginName;
	var wName = pluginName;
	if(trim(pluginName).substring(0,4).toLowerCase()!="http"){
		url = ART_PLUGIN_URL+pluginName;
	}
	var pString = "?";

	if(typeof(urlParams)=='object'){
		for(var i in urlParams){
			pString+= i+"="+urlParams[i]+"&";
		}
	}

	if(!isObject(windowParams)){
		if(windowParams=="normal"){
			windowParams = {scrollbars: true,menubar: true,directories: false,resizable: true,toolbar:true,location: true,status: true};
		}else{
			windowParams = {};
		}
	}

    var p = dojo.mixin(
        {
            name: 'win' + Math.round(Math.random()*10000000),
            scrollbars: true,
            menubar: false,
            directories: false,
            status: true,
            resizable: true,
            toolbar: false,
            location: false,
            status: false,
            width: 930,
            height: 623,
            left: 10,
            top: 10,
            fullscreen: false,
            center: false
        },
        windowParams
    );

    var confStr = "";
    confStr += "width=" + parseInt(p.width) + ",";
    confStr += "height=" + parseInt(p.height) + ",";
    confStr += "resizable=" + (p.resizable?1:0) + ",";
    confStr += "scrollbars=" + (p.scrollbars?1:0) + ",";
    confStr += "menubar=" + (p.menubar?1:0) + ",";
    confStr += "status=" + (p.status?1:0) + ",";
    confStr += "directories=" + (p.directories?1:0) + ",";
    confStr += "location=" + (p.location?1:0) + ",";
    confStr += "toolbar=" + (p.toolbar?1:0) + ",";
    confStr += p.fullscreen?"fullscreen":"";


	ART_CHILD_WINDOWS[wName] = window.open(url+pString,wName,confStr);
	ART_CHILD_WINDOWS[wName].focus();
	return ART_CHILD_WINDOWS[wName];
}



var ART_WINDOW_X = 0;
var ART_WINDOW_Y = 0;
function openPluginWindowConfiguration(width,height){
	if(typeof(width)=='undefined'){width=930;}
	if(typeof(height)=='undefined'){height=623;}
	var confString = 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=0,menubar=no,';
	confString+= 'scrollbars=no,resizable=yes,left='+ART_WINDOW_X+',top='+ART_WINDOW_Y;
	if(ART_WINDOW_X<300){ART_WINDOW_X+=30;}else{ART_WINDOW_X = 0;}
	if(ART_WINDOW_Y<300){ART_WINDOW_Y+=30;}else{ART_WINDOW_Y = 0;}
	return 	confString;
}


function sortByCustomOrder (a, b) {
    var x = parseInt(a.customOrder);
    var y = parseInt(b.customOrder);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}















function addWms(){
	var pane = showDialog(ART_PLUGIN_URL+"dojo/addWms.dhtml","Add WMS (web map service)");
	pane.show();
}

function showFloatingWindow(contentUrl,isModal,title){
	var div = document.createElement('div');
	document.body.appendChild(div);
	var params =  {
		resizable: "true",
		constrainToContainer: "true",
		hasShadow: "true",
		displayMinimizeAction: "true",
		displayMaximizeAction: "true",
		displayCloseAction: "true",
		id: "modal_addWms",
		title: title
	};

	var widgetName = "FloatingPane";
	if(isModal){widgetName = "ModalFloatingPane";}
	var pane = dojo.widget.createWidget(widgetName,params,div);
	//alert(contentUrl);
	pane.setUrl(contentUrl);
	pane.domNode.style.width = "300px";
	pane.domNode.style.height = "300px";
	pane.show();
	return pane;
}



function showDialog(contentUrl,title){
	dojo.require("dijit.Dialog");
	dojo.require("dijit.form.Textbox");
	dojo.require("dijit.form.Button");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("dojo.parser");

	var divTest = document.createElement('div');
	//divTest.style.display = "none";
	divTest.id = "testId";
	document.body.appendChild(divTest);

	var pane = new dijit.TooltipDialog({
			id: "modal_addWms",
			title: title
		},dojo.byId("testId"));

	pane.setHref(contentUrl);
	return pane;
}



function getGeoCoordX(x,minGeoX,maxGeoX,width_in_px){
	var dgx = maxGeoX-minGeoX;
	var dx = parseInt(width_in_px);
	x = parseInt(x);
	return round(minGeoX+(x*(dgx/dx)),5);
}

function getGeoCoordY(y,minGeoY,maxGeoY,height_in_px){
	var dgy = maxGeoY-minGeoY;
	var dy = parseInt(height_in_px);
	y = parseInt(y);
	return round(maxGeoY+(y*(-1*(dgy/dy))),5);
}


function getFormValues(obj,idPrefix){
	var reply = new Array();
	obj = dojo.byId(obj);
	if(!isObject(obj)){
		console.log("Provided form with id ["+obj+"] not found xxx.");
		return false;
	}
	var dbg = false;
	var value = null;
	var key = null;

	if(obj.tagName=="INPUT"){
		switch(obj.type){
			case "radio":{
				if(typeof obj.name !="undefined"){
					key = obj.name;
					var inputs = document.getElementsByName(trim(obj.name));
					for(var i=0;i<inputs.length;i++){
						if(typeof inputs[i]=="object"&&inputs[i]!=null){
							//console.debug(inputs[i] + " ID: "+inputs[i].id+ " TYPE: "+inputs[i].type);
							if(inputs[i].type=="radio"){
								//console.debug(obj+ " -- " + obj.name);
								if(inputs[i].checked){
									value = inputs[i].value;
									break;
								}else{
									value = 0;
								}
							}
						}
					}
				}
				break;
			}

			case "checkbox":{
				value = obj.checked?1:0;
				key = obj.id;
				break;
			}
			default:{
				if(typeof obj.value!="undefined"){
					value = obj.value;
				}
//				var di = dijit.byId(obj.id);
//				if(isObject(di)){
//					value = di.getValue();
//				}
				key = obj.id;
				break;
			}
		}
	}else if(obj.tagName=="SELECT"){
		key = obj.id;
		value = select_getValue(obj);
	}else if(obj.tagName=="TEXTAREA"){
		key = obj.id;
		value = obj.value;
	}else{
		if(typeof obj.childNodes == "object"){
			for(var i in obj.childNodes){
				if(isObject(obj.childNodes[i])){
					var arr = getFormValues(obj.childNodes[i],idPrefix);
					for(var j in arr){
						reply[j] = arr[j];
					}
				}
			}
		}
	}

	if(key!=null){
		if(isString(idPrefix)){
			if(startsWith(key+"_",idPrefix)){
				var parts = key.split("_");
				delete parts[0];
				key = parts.join("_").substring(1);
			}
		}
		reply[key] = value;
	}
	return reply
}




//function getFormValues(formNode,idPrefix){
//	var obj;
//	var reply = new Array();
//	if(typeof formNode=="object"){
//		obj = formNode;
//	}else if(typeof formNode=="string"){
//		obj = document.getElementById(formNode);
//	}
//
//	var dbg = false;
//	/*
//	if(typeof obj == "object"){
//		if(obj != null){
//			if(typeof obj.id != "undefined"){
//				console.debug(obj);
//				if(obj.id == "McfaAddress_street_1"){
//					console.debug(obj);
//					dbg = true;
//				}
//			}
//		}
//	}*/
//
//	if(typeof obj=="object" && obj!=null){
//		if(typeof obj.type != "undefined"&&obj.id!=""){
//			var value = null;
//			var key = null;
//			switch(obj.type){
//				case "radio":{
//					if(typeof obj.name !="undefined"){
//						key = obj.name;
//						var inputs = document.getElementsByName(obj.name);
//						//dumpArray(inputs);
//						for(var i=0;i<inputs.length;i++){
//							if(typeof inputs[i]=="object"&&inputs[i]!=null){
//								//console.debug(inputs[i] + " ID: "+inputs[i].id+ " TYPE: "+inputs[i].type);
//								if(inputs[i].type=="radio"){
//									//console.debug(obj+ " -- " + obj.name);
//									if(inputs[i].checked){
//										value = inputs[i].value;
//										break;
//									}else{
//										value = 0;
//									}
//								}
//							}
//						}
//					}
//					break;
//				}
//
//				case "checkbox":
//				{
//					if(obj.checked){
//						value = 1;
//					}else{
//						value = 0;
//					}
//					key = obj.id;
//					break;
//				}
//				default:{
//					if(typeof obj.value!="undefined"){
//						value = obj.value;
//					}
//					key = obj.id;
//					break;
//				}
//			}
//			if(key!=null){
//				reply[key] = value;
//			}
//		}else{
//
//			if(typeof obj.childNodes == "object"){
//				for(var i in obj.childNodes){
//					var arr = getFormValues(obj.childNodes[i],idPrefix);
//					for(var j in arr){
//						reply[j] = arr[j];
//					}
//				}
//			}
//		}
//	}else{
//		//alert("provided argument is not an object");
//	}
//	return reply
//}


function isEmail(argvalue) {
  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
  var retSize = customSplit(argvalue, "@", "arrayString");

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }

  return true;

}

function isURL(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  return true;

}


function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);

}
function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}
function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}


function artCloneObject(obj,recursive){
	var newObj = new Object();
	for(var i in obj){
		if(i=="__art_children"){
			newObj[i] = new Array();
			if(recursive==true){
				for(var j in obj[i]){
					if(isArtObject(obj[i][j])){
						newObj[i][obj[i][j].idObject] = artCloneObject(obj[i][j],recursive);
					}
				}
			}
		}else if(!isObject(obj[i])&&!isFunction(obj[i])){
			newObj[i] = obj[i];
		}
	}
	newObj.id = -1;
	newObj.idObject = -1;
	return newObj;
}



function round(value,precision,constantPrecision){
	precision = parseInt(precision);
	var c = Math.pow(10,precision);
	var result = Math.round(value*c)/c;
	if(typeof constantPrecision!="undefined"){
		if(constantPrecision==true){
			var p = result.toString();
			p = p.split(".");
			var suffix = "";
			if(typeof p[1] =="undefined"){
				suffix = ".";
				for(i = 0;i<precision;i++){
					suffix+="0";
				}
			}else{
				var lDiff = precision - p[1].length;
				for(i = 0;i<lDiff;i++){
					suffix+="0";
				}
			}
			result = result.toString()+suffix;
		}
	}
	return result
}

//getArea (vraci plochu oblasti vymezene polem bodu v parametru [{x:,y:},{x:,y:}....])
function getArea(coordinates){
	area = 0;
	if (coordinates.length > 2){
		coordinates.unshift(coordinates[coordinates.length-1]);
		coordinates.push(coordinates[1]);
		for (i=1;i<coordinates.length-1;i++){
			xn   = coordinates[ i ].x;
			ynp1 = coordinates[i+1].y;
			ynm1 = coordinates[i-1].y;
			area += (xn*(ynp1 - ynm1))/2;
		}
		coordinates.shift();
		coordinates.pop();
	}
	return Math.abs(area);
}

//getPerimeter (vraci obvod polygonu v oblasti vymezene polem bodu v parametru [{x:,y:},{x:,y:}....])
function getPerimeter(coordinates){
	perimeter = 0;
	if (coordinates.length > 2){
		coordinates.push(coordinates[0]);
		for (i=0;i<coordinates.length-1;i++){
			x1 = coordinates[ i ].x;
			x2 = coordinates[i+1].x;
			y1 = coordinates[ i ].y;
			y2 = coordinates[i+1].y;
			perimeter += Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
		}
		coordinates.pop();
	}
	return perimeter;
}

//getLineLength (vraci delku linie zadanou polem bodu v parametru [{x:,y:},{x:,y:}....])
function getLineLength(coordinates){
	perimeter = 0;
	if (coordinates.length > 1){
		for (i=0;i<coordinates.length-1;i++){
			x1 = coordinates[ i ].x;
			x2 = coordinates[i+1].x;
			y1 = coordinates[ i ].y;
			y2 = coordinates[i+1].y;
			perimeter += Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
		}
	}
	console.log(perimeter);

	return perimeter;
}