if(window.top != window) {
	window.top.location.href = window.location.href;
}

function init_effect_open(a_menu, steps_y, timeout, callback) {
	if(a_menu.slideTimer) return false;
	
	var cur_height = 0;

	a_menu.style.display = 'block';
	
	a_menu.style.height = 'auto';
	
	var max_height = a_menu.clientHeight;
	
	a_menu.style.height = max_height+'px';
	
	max_height -= a_menu.clientHeight - max_height;
	
	if(steps_y == 1) cur_height = max_height;
	
	a_menu.style.overflow='hidden';
	a_menu.style.height = cur_height+'px';
	
	step_height = steps_y > 0 ? max_height/steps_y:max_height;
	
	if(!a_menu.getAttribute('id')) {
		objId = 'tmpObjId_'+Math.random();
		a_menu.setAttribute('id', objId);
	}else{
		objId = a_menu.getAttribute('id');
	}
	
	do_effect_open(objId, cur_height, max_height, timeout, callback);
}

function do_effect_open(objId, cur_height, max_height, timeout, callback) {
	var tmp_div = document.getElementById(objId);
	if(!tmp_div) return false;
	cur_height = cur_height+step_height <= max_height ? cur_height+step_height : max_height;
	
	tmp_div.style.height = cur_height+'px';
	
	tmp_div.scrollTop = max_height;
	
	if (cur_height < max_height) {
		tmp_div.slideTimer = window.setTimeout('do_effect_open(\''+objId+'\', '+cur_height+', '+max_height+', '+timeout+', '+callback+')', timeout);
	}else{
		tmp_div.style.overflow = 'visible';
		if(objId.match(/tmpObjId_/)) tmp_div.setAttribute('id', null);
		try {
			delete tmp_div.slideTimer;
		} catch (e) {
			tmp_div.slideTimer = null;
		}
		if(typeof(callback) == 'function') callback();
	}
}

function init_effect_close(a_menu, steps_y, timeout, callback) {
	if(a_menu.slideTimer) return false;
	
	a_menu.style.display='block';
	a_menu.style.overflow='hidden';

	max_width = 0;
	max_height = 0;
	
	a_menu.orig_height = cur_height = a_menu.clientHeight;
	
	a_menu.style.height = (cur_height > 0 ? cur_height : 0)+'px';
	
	a_menu.orig_height -= a_menu.clientHeight - cur_height;

	a_menu.style.height = (a_menu.orig_height > 0 ? a_menu.orig_height : 0)+'px';

	step_height = steps_y > 0 ? cur_height/steps_y : 0;
	
	if(!a_menu.getAttribute('id')) {
		objId = 'tmpObjId_'+Math.random();
		a_menu.setAttribute('id', objId);
	}else{
		objId = a_menu.getAttribute('id');
	}
	
	do_effect_close(objId, cur_height, max_height, timeout, callback);
}

function do_effect_close(objId, cur_height, max_height, timeout, callback) {
	var tmp_div = document.getElementById(objId);
	if(!tmp_div) return false;
	cur_height = cur_height-step_height >= max_height ? cur_height-step_height:max_height;
	
	tmp_div.scrollTop = tmp_div.orig_height ? tmp_div.orig_height : cur_height;
	
	if (cur_height > 0) {
		tmp_div.style.height = cur_height+'px';
		tmp_div.slideTimer = window.setTimeout('do_effect_close(\''+objId+'\', '+cur_height+', '+max_height+', '+timeout+', '+callback+')', timeout);
	} else {
		tmp_div.style.display = 'none';
		tmp_div.style.overflow = 'visible';
		if(objId.match(/tmpObjId_/)) {
			tmp_div.setAttribute('id', null);
		}
		try {
			delete tmp_div.slideTimer;
		} catch (e) {
			tmp_div.slideTimer = null;
		}
		if(typeof(callback) == 'function') callback();
	}
}

function update_branches()
{
	var confirmDiv = document.getElementById('confirm-location');
	var mapHolderDiv = document.getElementById('custom-position-map-div'), bb;
	
	if(mapHolderDiv && mapHolderDiv.style.display == 'none' && bBox && bBox['lat1'] && bBox['lng1'] && bBox['lat2'] && bBox['lng2']) {
		bb = bBox['lat1']+','+bBox['lng1']+','+bBox['lat2']+','+bBox['lng2'];
	}else if(mapHolderDiv && mapHolderDiv.style.display == 'block') {
		bb = vigmap.getBounds();
		bb = bb.getSouthWest().toUrlValue()+','+bb.getNorthEast().toUrlValue();
	}else {
		return false;
	}
	
	var urls = self.location.pathname.toString().replace(new RegExp("/[^/]*$", 'gi'), '/').split('/');
	
	for(var a in urls) {
		urls[a] = encodeURI(urls[a]);
	}
	
	var url = self.location.protocol+'//'+self.location.hostname+urls.join('/')+(confirmDiv && confirmDiv.ll ? 'll='+confirmDiv.ll.getLatLng().toUrlValue()+'&':'')+'bb='+bb+'&format=json';
	
	Request.sendPOST(url, null, function (response) {
		if(!response || !response.responseText || response.responseText.match(/<!DOC/))
			return false;
			
		if(!vimmgr) {
			vimmgr = new MarkerManager(vigmap);
		}

		var cIcon = new GIcon(G_DEFAULT_ICON);
		cIcon.image = '/i/icons/currencyexchange.png';
		cIcon.shadow = '';
		cIcon.iconSize = new GSize(32, 37);
		
		var coords = JSON.parse(response.responseText);
		
		var points = Array();
		for(i in coords) {
			var LatLng = new GLatLng(coords[i]['lat'], coords[i]['lng']);
			if(!loadedPoints[LatLng.toUrlValue()]) {
				loadedPoints[LatLng.toUrlValue()] = true;
				points.push(new GMarker(LatLng, {title: coords[i]['objname'], draggable: false, icon: cIcon}));
				
			}
		}
		
		if(points.length > 0) vimmgr.addMarkers(points, 3, 19);
		
		if(confirmDiv.ll) vigmap.removeOverlay(confirmDiv.ll);
		vimmgr.refresh();
		if(confirmDiv.ll) vigmap.addOverlay(confirmDiv.ll);
	});
}

function toggleCheckboxes(curVal, tObjs, showVal) {
	if(typeof(curVal) == 'undefined' || typeof(tObjs) == 'undefined' || !tObjs.length || typeof(showVal) == 'undefined')
		return false;
	
	var displayVal = curVal != showVal ? 'none' : 'inline';
	for(var i in tObjs) {
		if(tObjs[i] && tObjs[i].style) tObjs[i].style.display = displayVal;
	}
}

function randomNum(Min, Max) {
	Min = Max ? Min : 0;
	Max = Max ? Max : Min;
	return Math.floor((Max-(Min-1))*Math.random()) + Min;
}

function objLength(obj) {
	var count = 0;
	for(var c in obj) count++;
	
	return count;
}

// ========== REQUEST ====================================================================================================

var Request = new Object();

Request.send = function(url, method, callback, data, urlencoded) {
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var readychange = function() {
		if (req.readyState == 4) {// only if req shows "loaded"
			if (req.status < 400) {// only if "OK"
				if (method=="POST") {
					callback(req);
					delete callback;
				} else {
					callback(req,data);
					delete callback;
				}
			} else if (typeof req == "undefined" || typeof req.status == "undefined") {
				// don't do anything. user has navigated away
				delete callback;
			} else if (req.status == 401) { // unauthorized
				callback(req);
				delete callback;
			} else if (req.status == 404) {
				// should not happen, but ignore it for now
			} else {
				switch(req.status) {
					// windows error codes
					case 12002: // server timeout
					case 12029: case 12030: case 12031: // dropped connection
					case 12152: // connection closed by server
					case 13030:
						//alert("There was a network problem. Please reload the page.");
						break;
					case 500: case 503:
						//alert("There was an internal server error. Please try later.");
						break;
					default:
						//alert("There was a problem loading data:" + "\nstatus: " + req.status+ "/" + req.statusText + "\n" + url);
				}
				delete callback;
			}
		}
	};
	function do_request() {
		if (method=="POST") {
			req.open("POST", url, true);
			if (urlencoded) req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.onreadystatechange = readychange;
			req.send(data);
		} else {
			req.open("GET", url, true);
			req.onreadystatechange = readychange;
			req.send(null);
		}
	};
	do_request();
	return req;
}

Request.sendRawPOST = function(url, data, callback) {
	Request.send(url, "POST", callback, data, false);
}
Request.sendPOST = function(url, data, callback) {
	Request.send(url, "POST", callback, data, true);
}
Request.sendGET = function(url, callback, args) {
	return Request.send(url, "GET", callback, args);
}

function addViEventListener(obj, eventName, handler, capture) {
	if (obj.attachEvent) {
		obj.attachEvent('on' + eventName, handler);
	}else if(obj.addEventListener){
		obj.addEventListener(eventName, handler, capture);
	}
}

