var centeringW = 1020;
var centeringH = 592;
var backOffsetX = 335;
var backOffsetY = 280;
var changePosition = function()
{
	var windowH = $(window).height();
	var windowW = $(window).width();

	if ( selected == 'Home' ){
		var menuOffsetX = 296;
	} else {
		var menuOffsetX = 0;
	}

	$('#bodybackground').css('top', ((windowH-centeringH)/2-backOffsetY));
	$('#bodybackground').css('left', ((windowW-centeringW)/2-backOffsetX));
	$('#menubackground').css('top', ((windowH-centeringH)/2-backOffsetY));
	$('#menubackground').css('left', ((windowW-centeringW)/2-backOffsetX+menuOffsetX));

	$('#content').css('top', (windowH-centeringH)/2);
	$('#content').css('left', (windowW-centeringW)/2);
	$('#menu').css('top', (windowH-centeringH)/2);
	$('#menu').css('left', (windowW-centeringW)/2+menuOffsetX);

	if($(".dekor").length > 0){
		$('.dekor').css('top', (windowH-centeringH)/2-129);
		$('.dekor').css('left', (windowW-centeringW)/2+menuOffsetX+307);
	}
};
var getMenuPosition = function()
{
	$('#menu .container').height();
};
var action = false;
var mc = null;
var openMenu = function(){
	if($(this).attr('selected')=='selected') return true;
	else{
		$('a.title[selected=selected]').removeAttr('selected');
	}
	var offset = $(this).attr('rel');

	if ( action == true ){//|| mc.css('margin-top') == offset+'px'
		return false;
	}
	action = true;

	$('#menu').removeClass('over');

	$(this).attr('selected', 'selected');
	$('a.title[selected!=selected]').each(function(){
		moveMenu();
		$(this).removeClass($(this).attr('class').substr(0, 1)+'_over');
		$(this).next().slideUp(500);
	});

	var ez = $(this);
	var sint = setInterval(moveMenu, 20);

	mc.animate({
		marginTop: offset
	}, 500, undefined, function(){
		$('#menu').addClass('over');
		opened = ez;
		action = false;
		clearInterval(sint);
		sint = undefined;
	});

	$(this).next().slideDown(500, function(){
		var p = $(this).prev();
		clearInterval(sint);
		p.css('margin-left', 0);
		p.addClass(p.attr('class').substr(0, 1)+'_over');
		if($.browser.msie){
			$('#logo').removeClass().addClass(ez.attr('class').substr(0, 1));
		}else{
			$('#logo').parent().fadeOut(300, function(){
				$('div',$(this)).removeClass().addClass(ez.attr('class').substr(0, 1));
				$(this).fadeIn(300);
			});
		}
	});
};
var moveMenu = function(){
	$('a.title').each(function(){
		$(this).css('margin-left', Math.pow($(this).position().top-285, 2)/1000+25);
	});
	$('#icons').each(function(){
		$(this).css('margin-left', Math.pow($(this).position().top-285, 2)/1000);
	});
};

function dekor(){
	this.page = 0;
	this.count = 0;
	this.init = function(){
		if($(".dekor").length > 0){
			this.count = $('.dekor').length;
			setInterval(function(){
				$($('.dekor')[this.page]).fadeOut(350);
				this.page = this.page + 1;
				if(this.page>this.count-1){
					this.page = 0;
				}
				$($('.dekor')[this.page]).fadeIn(350);
			}.bind(this),5000);
		}
		$(".dekor").hide();
		$(".dekor:first").show();
	};
}
$(window).resize(changePosition);
$(document).ready(changePosition);
$(document).ready(function(){
	mc = $('#menu .container');
	$('a.title').each(function(){
		$(this).mouseover(openMenu);
		$(this).next().removeClass('hide').hide();
	});
	$('div#menu-'+selected).prev().trigger('mouseover');
	var d = new dekor();
	d.init();
});
$(document).ready(moveMenu);

var allowLightBox = true;
$(document).ready(function(){
	gallery();
	faq();
	askForm();
	scroll();
});

function gallery(){
	if ( $("#gallery").length > 0 && !$("#gallery").hasClass("noscript")  ){
		if(allowLightBox){
			$('#gallery div.galleryContent a').lightBox();
		}
		var wrapperWidth = $('div.galleryContent').length * $('div.galleryContent:first').width();
		var posLeft = 0;
		var posStep = $('div.galleryContent:first').width();
		var prev = $('#gallery a.prev');
		var next = $('#gallery a.next');

		$('#galleryWrapper').css('width', wrapperWidth);

		$('a.next').click(function(){
			if (posLeft != ( posStep - wrapperWidth ) ) {
				posLeft = posLeft - posStep;
				$("#galleryWrapper").animate({
					left: posLeft
				}, { "duration": "slow", "easing": "easeInQuad" });

				if ( posLeft + posStep >= 0) prev.css('display', 'block');
				if ( posLeft == ( posStep -wrapperWidth )) next.css('display', 'none');
			}

		});

		if(wrapperWidth == posStep || wrapperWidth == 0) next.css('display', 'none');

		$('a.prev').click(function(){

			if ( (posLeft + posStep) <= 0) {
				posLeft = posLeft + posStep;
				$("#galleryWrapper").animate({
					left: posLeft
				}, { "duration": "slow", "easing": "easeInQuad" });

				if ( posLeft == 0) prev.css('display', 'none');
				if ( posLeft >= ( posStep -wrapperWidth )) next.css('display', 'block');
			}
		});
	}
}



function faq(){
	if ($("#faq").length > 0) {

		var item = null;

		$("#faq div.item").click(

			function(){
				if ( item == null){
					$(this).find("div.question").addClass("questionActive");
					$(this).find("div.answer").slideDown(500);
					item = $(this).attr("id");
					return;
				}

				if ( item != null && item != $(this).attr("id") ){
					$("#faq div.item[id="+item+"]").find("div.answer").slideUp(500);
					$("#faq div.item[id="+item+"]").find("div.question").removeClass("questionActive");

					$(this).find("div.question").addClass("questionActive");
					$(this).find("div.answer").slideDown(500);
					item = $(this).attr("id");
					return;
				}

				if ( item == $(this).attr("id") ){
					$(this).find("div.answer").slideUp(500);
					$(this).find("div.question").removeClass("questionActive");
					item = null;
					return;
				}
			}
		);
	}
}

function askForm(){
	if ($('#askForm').length > 0) {

		var lease 	= $('#lease');
		var buy   	= $('#buy');
		var lbInput = $('#leaseBuyInput');
		var spInput = $('#spaceInput');
		var flInput = $('#floorInput');
		var reset	= $('#reset');
		var slider1	= $('#slider1');
		var slider2	= $('#slider2');

		lease.click(
			function(){
			$(this).addClass('active');
			buy.removeClass('active');
			lbInput.attr('value','lease');
			return false;
		});

		buy.click(
			function(){
			$(this).addClass('active');
			lease.removeClass('active');
			lbInput.attr('value','buy');
			return false;
		});

		reset.click(
			function(){
			lease.addClass('active');
			buy.removeClass('active');
			lbInput.attr('value','lease');
			$("#slider1").slider('option', 'value', 1);
			$("#slider1 a").html('1<sup>&#160;sqm</sup>');
			spInput.attr('value','1');
			$("#slider2").slider('option', 'value', 1);
			$("#slider2 a").html('1<sup>&#160;st</sup>');
			flInput.attr('value','1');
		});

		function setFloor(value){
			if (value == 1){ floor = 'st'; }
			else if (value == 2) { floor = 'nd'; }
			else if (value == 3) { floor = 'rd'; }
			else { floor = 'th'; }
			return floor;
		}

		var area = data[floor]['area'];
		var step = data[floor]['step'];
		var maxImum = Math.ceil( area / step) * step;
		$("#slider1").slider({
			step: step,
			min: 0,
			max: Math.ceil(area / step) * step,
			value: 0,
			animate: true,
			slide: function(event, ui){
				var val = ui.value ==  maxImum ? area : ui.value;
				$(this).find('a').html( val + '<sup>&#160;sqm</sup>' );
				spInput.attr('value', val);
			}
		});

		value1 = $("#slider1").slider('option', 'value');
		$("#slider1 a").html(value1 + '<sup>&#160;sqm</sup>');


		$("#slider2").slider({
			step: 1,
			min: 0,
			max: data.length - 1,
			value: floor,
			animate: true,
			slide: function(event, ui){
				$(this).find('a').html( ui.value + '<sup>&#160;' + setFloor(ui.value) + '</sup>' );
				flInput.attr('value', ui.value);
				$("#slider1").slider('destroy');
				var area = data[ui.value]['area'];
				var step = data[ui.value]['step'];
				var maxImum = Math.ceil( area / step) * step;
				$("#slider1").slider({
					step: step,
					min: 0,
					max: maxImum,
					value: 0,
					animate: true,
					slide: function(event, ui){
						var val = ui.value == maxImum ? area : ui.value;
						$(this).find('a').html( val + '<sup>&#160;sqm</sup>' );
						spInput.attr('value', val);
					}
				});

				value1 = $("#slider1").slider('option', 'value');
				$("#slider1 a").html(value1 + '<sup>&#160;sqm</sup>');
			}
		});

		value2 = $("#slider2").slider('option', 'value');
		$("#slider2 a").html(value2 + '<sup>&#160;' + setFloor(value2) + '</sup>');


	}
}

function scroll(){
	if($('.textContainer').length > 0 && $('.textContent').height()>$('.textContainer').height()){
		$('.textContainer:first').jScrollPane();
	}
}
