function ShowHideForm(name)
{
	div = document.getElementById(name);
	if(div.style.display == 'none' || div.style.display == "")
		div.style.display = 'block';
	else
		div.style.display = 'none';
}

var the_page = null;
var the_territory = null;

function start_music() {
	var so2 = new SWFObject('player.swf','player','0','0','9');
	so2.addParam('allowscriptaccess', 'always');
	so2.addParam('loop', 'true');
	so2.addVariable('repeat', 'always');
	so2.addVariable('file', 'sounds/background.mp3&autostart=true');
	so2.write('music');
	var buff = jwplayer('player').getBuffer();
	var d = jwplayer('player').getDuration();
	var s = Math.floor(Math.random()*d*buff/150);
	jwplayer('player').seek(s);
}

var curr_ball_anim = null;

function start(page, territory) {
	start_music();
	curr_ball_anim = new balls_anim();
	the_page = page;
	the_territory = territory;
	if (page == 'gallery') {
		gallery1 = new gallery('gallery', 0);
		gallery1.load_smalls();
	}
	if (page == 'index') {
		var menu = document.getElementById('entryMenu');
		var imgs = menu.getElementsByTagName('img');
		map_anim1 = new map_anim(imgs[1], 1);
		map_anim2 = new map_anim(imgs[2], 2);
		map_anim3 = new map_anim(imgs[3], 3);
		map_anim4 = new map_anim(imgs[4], 4);
		map_anim5 = new map_anim(imgs[5], 5);
		map_anim6 = new map_anim(imgs[6], 6);
		map_anim7 = new map_anim(imgs[7], 7);
		map_anim8 = new map_anim(imgs[8], 8);
		map_anim6.ball_start();
	} else {
		var menu = document.getElementById('menu');
		var imgs = menu.getElementsByTagName('img');
		for (var i = 0; i < imgs.length; i++) {
			imgs[i].selected = false;
			imgs[i].current = false;
			imgs[i].loaded = false;
		}
		var img1 = null;
		if (page == 'main') {
			img1 = imgs[6];
		} else if (page == 'aboutUs') {
			img1 = imgs[5];
		} else if (page == 'gallery') {
			img1 = imgs[4];
		} else if (page == 'prices') {
			img1 = imgs[3];
		} else if (page == 'specialOffers') {
			img1 = imgs[2];
		} else if (page == 'contact') {
			img1 = imgs[1];
		} else if (page == 'rent') {
			img1 = imgs[0];
		}
		img1.current = true;
		var w1 = img1.width;
		var h1 = img1.height;
		var src = img1.src;
		src = src.substr(0, src.lastIndexOf('.')) + 'b' + '.png';
		img1.style.zIndex = '1';
		img1.onload = new Function('adjust_menu(this,'+w1+','+h1+')');
		img1.src = src;
		img1.onclick = '';
		if (territory == 'NS') {
			img1 = imgs[11];
		} else if (territory == 'NS2') {
			img1 = imgs[10];
		} else if (territory == 'NS3') {
			img1 = imgs[9];
		} else if (territory == 'KR') {
			img1 = imgs[8];
		} else if (territory == 'LIM') {
			img1 = imgs[7];
		} else {
			return;
		}
		img1.current = true;
		w1 = img1.width;
		h1 = img1.height;
		src = img1.src;
		src = src.substr(0, src.lastIndexOf('.')) + 'b' + '.png';
		img1.style.zIndex = '1';
		img1.onload = new Function('adjust_menu(this,'+w1+','+h1+')');
		img1.src = src;
		img1.onclick = '';
	}
}

function adjust_menu(img, w1, h1) {
	var w2 = img.width;
	var h2 = img.height;
	var left = img.style.marginLeft;
	left = parseInt(left.substr(0,left.length-2)) - parseInt((w2-w1)/2+.5);
	var top = img.style.marginTop;
	top = parseInt(top.substr(0,top.length-2)) - parseInt((h2-h1)/2+.5);
	img.style.marginLeft = left + 'px';
	img.style.marginTop = top + 'px';
	img.neww = w2;
	img.newh = h2;
	img.newleft = img.style.marginLeft;
	img.newtop = img.style.marginTop;
	img.loaded = true;
}

var map_anim = function(button, n) {

	this.growing = false;
	this.move_down = false;
	this.wcoeff = 1.2;
	this.button = button;
	this.n = n;
	this.ball_coeff = 2;
	this.ball_delay = 100;

	this.map_over2 = function() {
		var w = this.button.offsetWidth;
		var h = this.button.offsetHeight;
		var left = this.button.style.marginLeft;
		left = parseInt(left.substr(0,left.length-2));
		var top = this.button.style.marginTop;
		top = parseFloat(top.substr(0,top.length-2));
		if (!this.growing) {
			this.button.oldw = w;
			this.button.oldh = h;
			this.button.old_left = left;
			this.button.old_top = top;
		} else {
			top = this.button.top;
		}
		w = w + 2;
		left--;
		top = top - h/parseFloat(w);
		if (w >= this.wcoeff * this.button.oldw) {
			this.growing = false;
			if (this.move_down) {
				this.map_out2();
			}
		} else {
			this.growing = true;
			this.button.style.width = w + 'px';
			this.button.style.marginLeft = left + 'px';
			this.button.style.marginTop = parseInt(top+.5) + 'px';
			this.button.top = top;
			setTimeout('map_anim'+this.n+'.map_over2()', 20);
		}
	}
	
	this.map_over = function() {
		if (!this.growing && !this.move_down) {
			this.map_over2();
		}
	}
	
	this.map_out2 = function() {
		var w = this.button.offsetWidth;
		var h = this.button.offsetHeight;
		var left = this.button.style.marginLeft;
		left = parseInt(left.substr(0,left.length-2));
		var top = this.button.top;
		w = w - 2;
		left++;
		top = top + h/parseFloat(w);
		if (w <= this.button.oldw) {
			this.button.style.width = this.button.oldw + 'px';
			this.button.style.marginLeft = this.button.old_left + 'px';
			this.button.style.marginTop = this.button.old_top + 'px';
			this.move_down = false;
		} else {
			this.move_down = true;
			this.button.style.width = w + 'px';
			this.button.style.marginLeft = left + 'px';
			this.button.style.marginTop = parseInt(top+.5) + 'px';
			this.button.top = top;
			curr_map = this;
			setTimeout('map_anim'+this.n+'.map_out2()', 20);
		}
	}
	
	this.map_out = function() {
		if (!this.growing && !this.move_down) {
			this.map_out2();
		} else if (this.growing) {
			this.move_down = true;
		}
	}
	
	this.ball_start = function() {
		var w = this.button.offsetWidth;
		this.button.oldw = w;
		this.ball_up();
	}
	
	this.ball_up = function() {
		var w = this.button.offsetWidth;
		var h = this.button.offsetHeight;
		var left = this.button.style.marginLeft;
		left = parseInt(left.substr(0,left.length-2));
		var top = this.button.style.marginTop;
		top = parseFloat(top.substr(0,top.length-2));
		w = w + 2;
		left--;
		top--;
		this.button.style.width = w + 'px';
		this.button.style.marginLeft = left + 'px';
		this.button.style.marginTop = top + 'px';
		if (w >= this.ball_coeff * this.button.oldw) {
			setTimeout('map_anim'+this.n+'.ball_down()', 2*this.ball_delay);
			var n2 = (this.n + 1) % 3 + 6;
			setTimeout('map_anim'+n2+'.ball_start()', 2*this.ball_delay);
		} else {
			setTimeout('map_anim'+this.n+'.ball_up()', this.ball_delay);
		}
	}
	
	this.ball_down = function() {
		var w = this.button.offsetWidth;
		var h = this.button.offsetHeight;
		var left = this.button.style.marginLeft;
		left = parseInt(left.substr(0,left.length-2));
		var top = this.button.style.marginTop;
		top = parseFloat(top.substr(0,top.length-2));
		w = w - 2;
		left++;
		top++;
		this.button.style.width = w + 'px';
		this.button.style.marginLeft = left + 'px';
		this.button.style.marginTop = top + 'px';
		if (w > this.button.oldw) {
			setTimeout('map_anim'+this.n+'.ball_down()', this.ball_delay);
		}
	}

}

var menu_selected = null;

function get_closest_n(menu, e) {
	var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	var imgs = menu.getElementsByTagName('img');
	var closest = -1;
	var mind = 10000;
	for (var i = 0; i < imgs.length; i++) {
		var top = imgs[i].offsetTop;
		var h = imgs[i].offsetHeight;
		var d = Math.abs(top + h/2 - y);
		if (d < mind) {
			mind = d;
			closest = i;
		}
	}
	return closest;
}

function menu_click(menu, e) {
	if (menu_selected == null) {
		return;
	}
	var page = the_page;
	var territory = the_territory;
	if (menu_selected == 0) {
		page = 'rent';
	} else if (menu_selected == 1) {
		page = 'contact';
	} else if (menu_selected == 2) {
		page = 'specialOffers';
	} else if (menu_selected == 3) {
		page = 'prices';
	} else if (menu_selected == 4) {
		page = 'gallery';
	} else if (menu_selected == 5) {
		page = 'aboutUs';
	} else if (menu_selected == 6) {
		page = 'main';
	} else if (menu_selected == 7) {
		territory = 'LIM';
	} else if (menu_selected == 8) {
		territory = 'KR';
	} else if (menu_selected == 9) {
		territory = 'NS3';
	} else if (menu_selected == 10) {
		territory = 'NS2';
	} else if (menu_selected == 11) {
		territory = 'NS';
	}
	if (page == 'rent' && territory != 'NS') {
		page = 'main';
	}
	location.href='index.php?teritoryID='+territory+'&pageName='+page;
}

function menu_show_all(menu, e) {
	menu_selected = get_closest_n(menu, e);
	var imgs = menu.getElementsByTagName('img');
	menu_show(imgs[menu_selected]);
}

function menu_show(button) {
	var img1 = button;
	var menu = document.getElementById('menu');
	var buttons = menu.getElementsByTagName('img');
	for (var i = 0; i < buttons.length; i++) {
		if (!buttons[i].current) {
			buttons[i].style.zIndex = '0';
		}
	}
	if (!button.current) {
		img1.style.zIndex = '2';
	}
}

function menu_hide(menu) {
	var buttons = menu.getElementsByTagName('img');
	for (var i = 0; i < buttons.length; i++) {
		if (!buttons[i].current) {
			buttons[i].style.zIndex = '0';
		}
	}
	menu_selected = null;
}

var balls_anim = function() {

	this.relocate2 = function(img, offsetx, offsety, maxx, maxy) {
		var x = Math.floor(Math.random()*maxx) + offsetx;
		var y = Math.floor(Math.random()*maxy) + offsety;
		img.style.marginLeft = x + 'px';
		img.style.marginTop = -y + 'px';
		var w = parseInt(img.width * (1 -  .3 * y / 335));
		var h = img.height * w / img.width;
		img.style.width = w + 'px';
		img.style.height = h + 'px';
	}
	
	this.areas = new Array(
		20, -20, 140, 200, 
		-60, 140, 75, 95, 
		185, 10, 170, 130, 
		375, 95, 135, 40, 
		470, 115, 120, 20, 
		150, 335, 40, 25, 
		765, 230, 35, 105, 
		710, 290, 80, 45
	);
	this.nareas = parseInt(this.areas.length / 4);
	this.arean = 0;
	
	this.relocate = function(img) {
		var arean = this.arean;
		this.arean = (this.arean + 3) % this.nareas;
		this.relocate2(img, this.areas[4*arean], this.areas[4*arean+1], this.areas[4*arean+2], this.areas[4*arean+3]);
	}

	var balls = document.getElementById('balls');
	var imgs = balls.getElementsByTagName('img');
	var l = imgs.length;
	for (var i = 0; i < l; i++) {
		this.relocate(imgs[i]);
		imgs[i].jump = 0;
		for (var j = 0; j < 4; j++) {
			var newimg = document.createElement('img');
			newimg.src = imgs[i].src;
			newimg.style.position = 'absolute';
			newimg.style.width = imgs[i].width + 'px';
			newimg.style.height = imgs[i].height + 'px';
			newimg.jump = 0;
			this.relocate(newimg);
			balls.appendChild(newimg);
		}
	}
	this.iteration = 0;
	
	this.animate = function() {
		this.iteration++;
		if (this.iteration == 10) {
			this.iteration = 0;
			var n = Math.floor(Math.random()*imgs.length);
			if (imgs[n].jump == 0) {
				imgs[n].jump = 1;
				imgs[n].h = Math.floor(Math.random()*80) + 20;
				var top = imgs[n].style.marginTop;
				top = parseInt(top.substr(0, top.length-2));
				imgs[n].oldtop = top;
			}
		}
		for (var i = 0; i < imgs.length; i++) {
			if (imgs[i].jump > 0) {
				if (imgs[i].jump >= 100) {
					imgs[i].style.marginTop = imgs[i].oldtop + 'px';
					imgs[i].jump = 0;
				} else {
					var top = imgs[i].oldtop - parseInt(Math.sin(Math.PI*imgs[i].jump/100) * imgs[i].h + .5);
					imgs[i].style.marginTop = top + 'px';
					if (navigator.appName == 'Microsoft Internet Explorer') {
						imgs[i].jump += 3;
					} else {
						imgs[i].jump++;
					}
				}
			}
		}
		if (navigator.appName == 'Microsoft Internet Explorer') {
			setTimeout('curr_ball_anim.animate()', 1);
		} else {
			setTimeout('curr_ball_anim.animate()', 8);
		}
	}
	
	this.animate();
	
}

var music_on = true;

function music_pause(button) {
	var img = button.getElementsByTagName('img')[0];
	if (music_on) {
		jwplayer('player').pause();
		img.src = 'images/nosound.png';
		music_on = false;
	} else {
		jwplayer('player').play();
		img.src = 'images/sound.png';
		music_on = true;
	}
}

