var floorIds, floorPercs, initPhase, rePhase, Steps, stepMultiplier, activeTip;
var slider = new Array();
var floorTip = new Array();
var tileHeight = 48;
var skip = 6;

window.addEvent('domready', function() {
	
	if(Browser.Platform.ios == true || Browser.Platform.android == true || Browser.Platform.webos == true || Browser.Platform.other == true) {
		document.body.className = 'ios';
		if($('collectionWrapper')) {
			$('collectionWrapper').className = 'collectionIos';
			$('arrUp').addEvent('mousedown',function() {
				var scrollVal = $('collectionContainer').getScroll();
				var scrollY = scrollVal.y;
				scrollY = scrollY - (tileHeight * skip);
				if(scrollY < 0) { scrollY = 0; }
				$('collectionContainer').scrollTo(scrollVal.x, scrollY);
			});
			$('arrDown').addEvent('mousedown',function() {
				var scrollSize = $('collectionContainer').getScrollSize();
				var scrollVal = $('collectionContainer').getScroll();
				var scrollY = scrollVal.y;
				scrollY = scrollY + (tileHeight * skip);
				if(scrollY > scrollSize.y) { scrollY = scrollSize.y; }
				$('collectionContainer').scrollTo(scrollVal.x, scrollY);
			});
		}
		var v_css  = document.createElement('link');
		v_css.rel = 'stylesheet'
		v_css.type = 'text/css';
		v_css.href = 'includes/styles_mobile.css';
		document.getElementsByTagName('head')[0].appendChild(v_css);
	} 
	
	
	if ($('Steps')) {
	
		Steps = parseInt($('Steps').value);
		stepMultiplier = parseInt(100 / Steps);
		floorIds = $('Ids').value.split(',');
		floorPercs = $('Percs').value.split(',');
		initPhase = true;
		rePhase = false;
		$$('div.slider').each(function(el, i) {
			var perc = parseInt(floorPercs[i],10);
			slider[i] = new Slider(el, el.getElement('.knob'), {
				steps: Steps,
				snap: false,
				wheel: false,
				onChange: function() {
					if (rePhase == false) {
						floorPercs[i] = this.step * stepMultiplier;
						$('floorPerc' + i).innerHTML = floorPercs[i];
						setPercs(i);
					}
				},
				onComplete: function() {
					if (rePhase == false) {
						checkPercs(i);
					}
				}
			}).set(perc / stepMultiplier);
		});
		
		$$('.sliderContainer .sliderArrowLeft').each(function(el, i) {
			el.style.display = 'block';
			el.addEvent('click', function() {
				var currentStep = parseInt(slider[i].step,10);
				var newStep = currentStep - 1;
				if(newStep < 0) {
					newStep = 0;	
				}
				slider[i].set(newStep);
			});
		});
		
		$$('.sliderContainer .sliderArrowRight').each(function(el, i) {
			el.style.display = 'block';
			el.addEvent('click', function() {
				var currentStep = parseInt(slider[i].step,10);
				var newStep = currentStep + 1;
				if(newStep > Steps) {
					newStep = Steps;	
				}
				slider[i].set(newStep);
			});
		});
		
		$$('a.floorTip').each(function(el, i) {
			floorTip[i] = new Tips(el, {
				offset: {'x': 16, 'y': 5},
				className: 'collectionTip',
				showDelay: 0,
				hideDelay: 0,
				title: function() {
					var tmp = el.title;
					tmp = '<strong>' + tmp + '</strong>';
					return tmp;
				},
				onShow: function(tip) {
					tip.setStyle('display', 'block');
					activeTip = i;
				},
				onHide: function(tip) {
					tip.setStyle('display', 'none');
					activeTip = 0;
				}
			});
		});
		
		$$('a.floorTipRight').each(function(el, i) {
			var fTip = new Tips(el, {
				offset: {'x': 16, 'y': 5},
				className: 'collectionTip',
				showDelay: 0,
				hideDelay: 0
			});
		});
		
		initPhase = false;
		
	}
	
	
});

function checkToolTips () {
	if (activeTip > 0) {
		$(floorTip[activeTip]).setStyle('display', 'none');
		activeTip = 0;
	}
}

function checkPercs(id) {
	if (initPhase) return;
	var total = parseInt(floorPercs[id]);
	for (var x = 0; x < floorIds.length; x++) {
		if (x != id) total += parseInt(floorPercs[x]);
	}
	if (total != 100) {
		if (floorIds.length == 1) {
			floorPercs[0] = 100;
			$('floorPerc' + 0).innerHTML = floorPercs[0];
			slider[0].set(floorPercs[0] / stepMultiplier);
		} else {
			var dif = 100 - total;
			for (var x = 0; x < floorIds.length; x++) {
				if (x != id && floorPercs[x] > 0) {
					floorPercs[x] += dif;
					$('floorPerc' + x).innerHTML = floorPercs[x];
					slider[x].set(floorPercs[x] / stepMultiplier);
					//break;
				}
			}
		}
	}
	$('Percs').value = floorPercs.join(',');
}

function setPercs(id) {
	if (initPhase) return;
	rePhase = true;
	var restValue = 0, restCount = 0;
	for (var x = 0; x < floorIds.length; x++) {
		if (x != id) {
			restValue += parseInt(floorPercs[x] / stepMultiplier);
			restCount += 1;
		}
	}
	var total = restValue + parseInt(floorPercs[id] / stepMultiplier);
	var multiplier = total > parseInt(100 / stepMultiplier)?-1:1;
	var dif = Math.abs(parseInt(100 / stepMultiplier) - total) / restCount;
	total = parseInt(floorPercs[id] / stepMultiplier);
	for (var x = 0; x < floorIds.length; x++) {
		if (x != id) {
			var tmpPerc = parseInt(floorPercs[x] / stepMultiplier) + parseInt(dif * multiplier);
			if (tmpPerc < 0) tmpPerc = 0;
			total += tmpPerc;
			floorPercs[x] = tmpPerc * stepMultiplier;
			$('floorPerc' + x).innerHTML = floorPercs[x];
			slider[x].set(floorPercs[x] / stepMultiplier);
		}
	}
	rePhase = false;
	$('submitButton').setStyle('visibility', 'visible');
}

function addFloor(id) {
	var allow = true;
	for (var x = 0; x < floorIds.length; x++) {
		if (parseInt(id) == parseInt(floorIds[x])) allow = false;
	}
	if (allow) {
		var Ids = $('Ids').value + ',' + id;
		$('Ids').value = Ids;
		$('form1').submit();
	}
	return false;
}

function removeFloor(id) {
	if (floorIds.length == 1) return;
	var Ids = '';
	for (var x = 0; x < floorIds.length; x++) {
		if (x != id) {
			if (Ids != '') Ids += ',';
			Ids += floorIds[x];
		}
	}
	$('Ids').value = Ids;
	$('form1').submit();
	return false;
}

function selectBackground(id) {
	$('roomId').value = id;
	$('form1').submit();
	return false;
}

function submitForm() {
	$('submitter').value = 'Wacht...';
	$('submitter').disabled = true;
	$('form1').submit();
	return false;
}

