/* 
 * KLASA koja predstavlja site_map
 */

site_map_obj = function (container_id, p_height) {
	// general properties
	this.contents = '';
	this.browser = (navigator.appName == 'Microsoft Internet Explorer') ? 'IE' : (navigator.appName == 'Opera') ? 'OP' : 'FF';
	this.browser_ver = navigator.appVersion;
	this.cont_id = container_id;
	this.height = p_height;
	
	// timer properties
	this.scroll_speed = 25;
	this.scroll_time = null;
	this.animation = false;
	
	// position properties
	this.resize = 0;
	this.resize_leap = 80;
	this.resize_leap_slow = 20;
	this.cont_center = 0;
	
	this.trans = 0;
	this.trans_leap = 20;
	this.trans_leap_slow = 4;

	this.min_size = 0;
	this.max_size = this.height + 30;
	this.min_resize = 0;
	this.max_resize = 940;
	this.min_trans = 0;
	this.max_trans = 100;
	this.extended = false;
	this.widened = true;
	this.opaque = true;

	this.size = 0;
	this.size_leap = this.max_size / 10;
	this.size_leap_slow = this.max_size / 40;

	// setting the container properties
	this.setContProps();
	this.setTransparency(70);
	
	// setting the container functions
	this.setFunctions();
}

/* CONTAINER properties functions */
site_map_obj.prototype.setContProps = function () {
	var elem = document.getElementById(this.cont_id);

	// creating a contents window
	var elem_cont = document.getElementById(this.cont_id + "_cont")
	
	// centering the window
	if (this.browser == 'IE') {
		elem.style.left = (document.body.clientWidth / 2) - (974 / 2) + "px";
		elem_cont.style.left = (document.body.clientWidth / 2) + "px";
		this.cont_center = (document.body.clientWidth / 2);
	}
	else {
		elem.style.left = (document.documentElement.clientWidth / 2) - (974 / 2) + "px";
		elem_cont.style.left = (document.documentElement.clientWidth / 2) + "px";
		this.cont_center = (document.documentElement.clientWidth / 2);
	}
	
	// sizing it's windows
	var elem_data = document.getElementById(this.cont_id + "_cont_data");
	elem_cont.style.height = this.height + "px";
	elem_data.style.height = this.height + "px";
	
	return;
}

site_map_obj.prototype.setTransparency = function (percentage) {
	var elem = document.getElementById(this.cont_id);

	if (this.browser == 'IE') {
		elem.filters.alpha.opacity = percentage;
	}
	else {
		elem.style.opacity = percentage / 100;
	}
	return;
}

site_map_obj.prototype.setFunctions = function () {
	var elem = document.getElementById(this.closer_id);
	var me = this;

	if (!elem){
		return;
	}
	if (this.browser == 'IE') {
		elem.attachEvent("onclick", function() { me.hide(); });
	}
	else {
		elem.addEventListener("click", function() { me.hide(); }, false);
	}
	return;
}

/* OBJECT class functions */
site_map_obj.prototype.show = function () {
	document.getElementById(this.cont_id).style.display = 'block';
	if (!this.animation) {
		this.start_scroll(1);
	}
	return;
}

site_map_obj.prototype.hide = function () {
	if (!this.animation) {
		this.start_trans(-1);
	}
	return;
}

site_map_obj.prototype.start_scroll = function (direction) {
	this.animation = true;
	if (this.browser == 'IE'){
		/* IE */
		var _tmp_this = this;
		this.scroll_time = setInterval(function () {_tmp_this.scroll_wnd.apply(_tmp_this, [direction]);}, this.scroll_speed);
	}
	else{
		/* FF */
		this.scroll_time = setInterval(function(openMe) {openMe.scroll_wnd(direction);}, this.scroll_speed, this);
	}
	return;
}

site_map_obj.prototype.scroll_wnd = function (direction) {
	var elem = document.getElementById(this.cont_id);
	var elem_cont = document.getElementById(this.cont_id + "_cont");

	// distance calculation
	if (direction > 0 ) {
		// show
		if (this.size >= this.max_size) {
			this.size = this.max_size;
			clearInterval(this.scroll_time);
		
			this.extended = true;
		}
		else if (this.size >= this.max_size * 0.7 ) {
			this.size += direction * this.size_leap_slow;
		}
		else {
			this.size += direction * this.size_leap;
		}
	}
	else {
		// hide
		if (this.size <= this.min_size) {
			this.size = this.min_size;
			clearInterval(this.scroll_time);
			elem.style.display = 'none';
		}
		else if (this.size <= this.max_size * 0.3 ) {
			this.size += direction * this.size_leap_slow;
		}
		else {
			this.size += direction * this.size_leap;
			this.extended = false;
			this.widened = true;
			this.opaque = true;
		}
	}
	
	// positioning the window
	elem.style.height = this.size + "px";
	
	// starting the contents window animation
	if (this.extended) {
		elem_cont.style.display = 'block';
		if (this.browser == 'IE'){
			/* IE */
			var _tmp_this = this;
			this.scroll_time = setInterval(function () {_tmp_this.cont_resize.apply(_tmp_this, [direction]);}, this.scroll_speed);
		}
		else{
			/* FF */
			this.scroll_time = setInterval(function(openMe) {openMe.cont_resize(direction);}, this.scroll_speed, this);
		}
	}
	return;
}

site_map_obj.prototype.start_resize = function (direction) {
	this.animation = true;
	if (this.browser == 'IE'){
		/* IE */
		var _tmp_this = this;
		this.scroll_time = setInterval(function () {_tmp_this.cont_resize.apply(_tmp_this, [direction]);}, this.scroll_speed);
	}
	else{
		/* FF */
		this.scroll_time = setInterval(function(openMe) {openMe.cont_resize(direction);}, this.scroll_speed, this);
	}
	return;
}

site_map_obj.prototype.cont_resize = function (direction) {
	var elem = document.getElementById(this.cont_id + "_cont");

	// width calculation
	if (direction > 0 ) {
		// widen
		if (this.resize >= this.max_resize) {
			this.resize = this.max_resize;
			clearInterval(this.scroll_time);
		
			this.widened = true;
			
			// start displaying contents
			if (this.browser == 'IE'){
				/* IE */
				var _tmp_this = this;
				this.scroll_time = setInterval(function () {_tmp_this.cont_trans.apply(_tmp_this, [direction]);}, this.scroll_speed);
			}
			else{
				/* FF */
				this.scroll_time = setInterval(function(openMe) {openMe.cont_trans(direction);}, this.scroll_speed, this);
			}
		}
		else if (this.resize >= this.max_resize * 0.9 ) {
			this.resize += direction * this.resize_leap_slow;
		}
		else {
			this.resize += direction * this.resize_leap;
		}
	}
	else {
		// narrow
		if (this.resize <= this.min_resize) {
			this.resize = this.min_resize;
			clearInterval(this.scroll_time);
		
			this.widened = false;
		}
		else if (this.resize <= this.max_resize * 0.1 ) {
			this.resize += direction * this.resize_leap_slow;
		}
		else {
			this.resize += direction * this.resize_leap;
		}
	}
	
	// positioning the window
	elem.style.width = this.resize + "px";
	elem.style.left = this.cont_center - this.resize / 2 + 'px';

	// starting the contents window animation
	if (!this.widened) {
		elem.style.display = 'none'
		if (this.browser == 'IE'){
			/* IE */
			var _tmp_this = this;
			this.scroll_time = setInterval(function () {_tmp_this.scroll_wnd.apply(_tmp_this, [direction]);}, this.scroll_speed);
		}
		else{
			/* FF */
			this.scroll_time = setInterval(function(openMe) {openMe.scroll_wnd(direction);}, this.scroll_speed, this);
		}
	}
	return;
}

site_map_obj.prototype.start_trans = function (direction) {
	this.animation = true;
	if (this.browser == 'IE'){
		/* IE */
		var _tmp_this = this;
		this.scroll_time = setInterval(function () {_tmp_this.cont_trans.apply(_tmp_this, [direction]);}, this.scroll_speed);
	}
	else{
		/* FF */
		this.scroll_time = setInterval(function(openMe) {openMe.cont_trans(direction);}, this.scroll_speed, this);
	}
	return;
}

site_map_obj.prototype.cont_trans = function (direction) {
	var elem = document.getElementById(this.cont_id + "_cont_data");
	var elem_cont = document.getElementById(this.cont_id + "_cont");
	
	// width calculation
	if (direction > 0 ) {
		// more opaque
		if (this.trans >= this.max_trans) {
			this.trans = this.max_trans;
			clearInterval(this.scroll_time);
		
			this.animation = false;
			this.opaque = true;
		}
		else if (this.trans >= this.max_trans * 0.8 ) {
			this.trans += direction * this.trans_leap_slow;
		}
		else {
			this.trans += direction * this.trans_leap;
		}
	}
	else {
		// less opaque
		if (this.trans <= this.min_trans) {
			this.trans = this.min_trans;
			clearInterval(this.scroll_time);
		
			this.animation = false;
			this.opaque = false;
		}
		else if (this.trans <= this.max_trans * 0.1 ) {
			this.trans += direction * this.trans_leap_slow;
		}
		else {
			this.trans += direction * this.trans_leap;
		}
	}
	
	// positioning the window
/*	elem.style.width = this.resize + "px";
	elem.style.left = this.cont_center - this.resize / 2 + 'px';*/
	if (this.browser == 'IE') {
		elem.filters.alpha.opacity = this.trans;
	}
	else {
		elem.style.opacity = this.trans / 100;
	}

	// starting the contents window animation
	if (!this.opaque) {
		elem_cont.style.display = 'block'
		if (this.browser == 'IE'){
			/* IE */
			var _tmp_this = this;
			this.scroll_time = setInterval(function () {_tmp_this.cont_resize.apply(_tmp_this, [direction]);}, this.scroll_speed);
		}
		else{
			/* FF */
			this.scroll_time = setInterval(function(openMe) {openMe.cont_resize(direction);}, this.scroll_speed, this);
		}
	}
	return;
}
