
$(document).ready(function() {
	Orientate.init();
	CloseMenu.init();
	HomePage.init();
});

function attachAllEvents() {
	Fieldsets.init();	
	Spinner.init();
	CheckboxSwitch.init();
}


var Orientate = {
	init: function() {
		var currentWidth = 0;
		if(window.innerWidth != currentWidth) {   
			currentWidth = window.innerWidth;
			currentHeight = window.innerHeight;
			var orient = currentWidth < currentHeight ? "profile" : "landscape";
			document.body.setAttribute("orient", orient);
			setTimeout(scrollTo, 100, 0, 1);
		}
	}
}

var Fieldsets = {
	init: function() {
		$('.planform, .timeform, .searchform').each(function() {			
			$(this).find('fieldset:not(:first)').addClass('closed').find('span.input, div').hide();
			
			$(this).find('legend a').bind('focus click active', function() {				
				$(this).parents('fieldset').find('span, div').slideDown('fast').end().removeClass('closed').siblings(':visible').addClass('closed').find('span, div').slideUp('fast');
				return false;									
			});
		});
		$('fieldset legend').each(function() {
			$(this).find('a').width($(this).parent().width());
		});
	}
};

var Spinner = {
	init: function() {
		$('.buttonup').each(function() {
			$(this).css('display', 'inline-block');
			$(this).bind('click', function() {
				thisInput = $(this).siblings(':next').find('input[type=text]:first');
				val = parseInt(thisInput.val());
				max = parseInt(thisInput.attr('max'));
				step = parseInt(thisInput.attr('step'));
				if(val<max) {
					thisInput.val(val + step);
					$(this).parent('div').find('button:last').removeClass('disabled').attr('disabled', '');
				}
				val = parseInt(thisInput.val());
				if(val==max) {
					$(this).addClass('disabled').attr('disabled', 'disabled');
					thisInput.val(max);
				}
				return false;	
			});
		});

		$('.buttondown').each(function() {
			$(this).css('display', 'block');
			$(this).bind('click', function() {
				thisInput = $(this).siblings(':prev').find('input[type=text]:first');
				val = parseInt(thisInput.val());
				min = parseInt(thisInput.attr('min'));
				step = parseInt(thisInput.attr('step'));

				if(val>min) {
					thisInput.val(val - step);
					$(this).parent('div').find('button:first').removeClass('disabled').attr('disabled', '');
				}
				val = parseInt(thisInput.val());
				if(val<min || val==min) {
					$(this).addClass('disabled').attr('disabled', 'disabled');
					thisInput.val(min);
				}
				return false;	
			});
		});

		$('.spinner').each(function() {		
			thisInput = $(this).find('input');
			thisInput.bind('blur', function() {
				val = parseInt($(this).val());
				min = parseInt($(this).attr('min'));
				max = parseInt($(this).attr('max'));
				if(val<=min) {
					$(this).parents('div').find('button:last').addClass('disabled').attr('disabled', 'disabled');
					$(this).parents('div').find('button:first').removeClass('disabled').attr('disabled', '');
					$(this).val(min);
				} else if (val>=max) {
					$(this).parents('div').find('button:first').addClass('disabled').attr('disabled', 'disabled');
					$(this).parents('div').find('button:last').removeClass('disabled').attr('disabled', '');
					$(this).val(max);
				}
			});
		});
	}
};

var CloseMenu = {
	init: function() {
		$('#topmenu').click(function() {
			$(this).parent().toggleClass('open');
		});
		$('.topnavigation .last ul li a').click(function() {
			$(this).parent().parent().parent().toggleClass('open');
		});
	}
}

var CheckboxSwitch = {
	init: function() {
		$('.switch > label').each(function(){
			$(this).addClass("jsCheckbox");
			if($(this).next().is(':checked')) {
				$(this).addClass('checked');
			}
			$(this).click(function() {
				$(this).toggleClass("checked");
				if($(this).next().is(':checked')) {
					$(this).next().attr('checked', false);
				} else {
					$(this).next().attr('checked', true);
				}
			});
			$(this).next().hide();
		});
	}
};

var HomePage = {
	init: function() {
		$('#visual img').attr('src', $('#visual img').attr('src') + '&width=' + $('#visual').width());
		$('#visual img').attr('width', $('#visual').width());
		procent = ($('#visual').width()/640);
		newHeight = Math.round(procent*250);
		$('#visual img').attr('height', newHeight);
	}
};


