$(function(){
	// hide orderform (degrade gracefully)
	$('#order_form')
		.hide()
		.css({
			'background': '#fff url(/img/order_form_bg.gif) right bottom no-repeat'
		});
		
	// show want_it link	
	$('#i_want_it').css({'visibility':'visible'});
	
	// blur clicked links (avoid ugly border around them)
	$('a, object').focus(function(){
		this.blur();
	});
	
	// lightbox elements
	$('.foto_gallery a, #details_thumbs a').lightbox(); 

		
	// 3d icon
	if($('#view_1').length > 0 && $('#view_2').length > 0){
		var current = 1; // current image number
		var images = 1; // number of images we have
		var timeout = setTimeout("$('#rotator .next').click()",10000);
		$('#rotator .prev').hide();
		$('#rotator').fadeIn(1000);
		for(i = 2; i < 5; i++){
			if($('#view_'+i).length > 0){
				$('#view_'+i).hide();
				images++;
			}
		}
		
		$('#rotator .next').click(function(e){
			clearTimeout(timeout);
			$('img').stop();
			$('#rotator').fadeOut(100);
			$('#view_'+current).fadeOut(function(){
				current++;
				$('#rotator .prev').show();
				if(current == images){
					$('#rotator .next').hide();
				}else{
					$('#rotator .next').show();
				}
				if(current > images){
					current = 1;
				}
				if(current == 1){
					$('#rotator .prev').hide();
				}
				
				$('#view_'+current).fadeIn(function(){
					$('#rotator').fadeIn(100);
					timeout = setTimeout("$('#rotator .next').click()",10000);
				});
			});
			e.preventDefault();
		});
		
		$('#rotator .prev').click(function(e){
			clearTimeout(timeout);
			$('img').stop();			
			$('#rotator').fadeOut(100);
			$('#view_'+current).fadeOut(function(){
				current--;
				if(current == 1){
					$('#rotator .prev').hide();
				}
				if(current != images){
					$('#rotator .next').show();
				}
				$('#view_'+current).fadeIn(function(){
					$('#rotator').fadeIn(100);
					timeout = setTimeout("$('#rotator .next').click()",10000);
				});
			});
			e.preventDefault();
			timeout = setTimeout("$('#rotator .next').click()",10000);
		});
	}
	
	
		// order_form display
	$('#i_want_it').click(function(event){
		clearTimeout(timeout);
		$('#buy_rules').hide(); // will be shown only in reservation mode
		$('#order_form img').show();
		
		// add overlay
		$('body').append('<div id="overlay">');
		if($(window).height() > $('body').height()){
			var he = $(window).height()
		}else{
			var he = $('body').height() 
		}
		$('#overlay')
			.css({
				display: 'none',
				position: 'absolute',
				top: 0,
				left: 0,
				'z-index': 1000,
				background: '#000',
				width: $(window).width(), 
				height: he,
				opacity: '0.8'
			})
			.fadeIn()
			// remove overlay
			.click(function(e){
				$('#overlay, #order_form').fadeOut(function(){
					$('#overlay').remove();
					$('#order_form').remove().appendTo('#text');
					// start timout for rotation
					if($('#view_1').length > 0 && $('#view_2').length > 0){
						timeout = setTimeout("$('#rotator .next').click()",10000);
					}
				});
				e.preventDefault();
			});

		// show orderform
		$('#order_form')
			.remove()
			.appendTo('body')
			.hide()
			.css({
				position: 'absolute',
				top: '40px',
				left:(($('body').width()-580)/2),
				'z-index': 1001,
				padding:'20px'
			})
			.fadeIn(function(){
				// close
				$('a#close')
				.show()
				.click(function(e){
					this.blur();
					$('#overlay, #order_form').fadeOut(function(){
						$('#overlay').remove();
						$('#order_form').remove().appendTo('#text');	
					});
					if($('#view_1').length > 0 && $('#view_2').length > 0){
						timeout = setTimeout("$('#rotator .next').click()",10000);
					}
					e.preventDefault();
				});
				
				// show #buy_rules
				$('select').change(function(){
					$('select > option:selected').each(function(){
						//alert($(this).val());
						if($(this).val() == 3){
							//alert('fuck MS');
							$('#buy_rules').fadeIn().show(); // show() only for ie8
						}else{
							$('#buy_rules').fadeOut();
						}
					});
				}).change();
			});
		event.preventDefault();
	});
});
