function isInt(input){
	return typeof(input)=='number'&&parseInt(input)==input;
}

function maxRandom(max){
	var num = Math.round(Math.random()*10);
	if(num <= max && num != 0){
		return num;
	}else{
		return maxRandom(max);
	}
}

var timeout; // timeout holder (global)
var home_time = 4000;
var other_time = 6000;

$(function(){	
	$('a').click(function(){
		this.blur();
	});

	$('a.lightbox').lightbox();

	$('select').sb({fixedWidth:true});

	//
	// fake hover (over image anchors set via html style attr)
	$('#navi ul.nav_1 > li.active > a').css('background-position','0 -36px');
	$('#navi ul.nav_1 > li.active > a').hover(function(){
			$(this).css('background-position','0 bottom');
		},function(){
			$(this).css('background-position','0 -36px');
		});
	$('#navi ul.nav_1 > li:not(.active) > a').hover(function(){
		$(this).css('background-position','0 -18px');
	},function(){
		$(this).css('background-position','0 0');
	});

	$('#childs_list a').hover(function(){
		$(this).css('background-position','0 -18px');
	},function(){
		$(this).css('background-position','0 0');
	});

	////////////////////////////////////////////////////////////////////////////////////////
	// y shape anim
	var ytm = setTimeout(function(){
		$('#slogan img').fadeOut(2500);
		clearTimeout(ytm);
	}, 5000);
	var ytm2 = setTimeout(function(){
		$('#y_img').fadeIn(2500);
		clearTimeout(ytm2);
	}, 6000);

	////////////////////////////////////////////////////////////////////////////////////////
	// contacts
	if($('body').hasClass('galleryLayout')){
			$('#submit_btn').hide();
			$('#filters_form select').change(function(){
				$('#filters_form').submit();
			});

		$('#filters_form').submit(function(e){
			
			
		});
	}

	////////////////////////////////////////////////////////////////////////////////////////
	// contacts
	if($('body').hasClass('contactLayout')){
		var  contacts = function(){
			var tm = 400;
			$('#contacts').css({
				'top':'276px',
				'height':'130px',
				'width': '290px'
			});
			$('#contacts .contact').css({
				'display':'none',
				'position': 'absolute',
				'top': 0}
			);
			$('.contactLayout a.contact_link').click(function(e){
				if(!$(this).hasClass('active')){
					// y shape
					if($(this).hasClass('city')){
						var city = $(this).attr('href').substr(1);
						if($('#y_img').is(':hidden') || $('#y_img').is(':animated')){ // before #slogan fadedOut
							$('#slogan').fadeOut(function(){
								$('#y_img img').hide();
								$('#y_img').show();
								if(city != 'contact-haag'){
									$('#y_img .'+city).fadeIn();
								}else{
									$('#y_img .mountain').fadeIn();
								}
							});
						}else{
							if($('#y_img .'+city).is(':hidden') || city == 'contact-haag'){
								$('#y_img img:not(:hidden)').fadeOut(function(){
									if(city != 'contact-haag'){
										$('#y_img .'+city).fadeIn();
									}else{
										$('#y_img .mountain').fadeIn();
									}
								});
							}
						}
					}
					// text
					$('#contacts .contact:not(:hidden)').fadeOut(tm);
					$($(this).attr('href')).delay(0.9*tm).fadeIn(tm);
					$('.contactLayout a.contact_link').removeClass('active');
					$(this).addClass('active');
				}
			});
			var current =	window.location.hash;
			if(current != ''){
				$.fx.off = true;
				$('.'+current.substr(1)).click();
				$.fx.off = false;
			}else{ // prague is default ;)
				$.fx.off = true;
				$('.contact-prague').click();
				$.fx.off = false;
			}
		}();
	}


	////////////////////////////////////////////////////////////////////////////////////////
	//services
	if($('body').hasClass('servicesLayout')){
		var  fullServicesList = function(){
			$('#all_services, #all_services img').hide();
			//show hide full list of services
			$('#full_list').click(function(e){
				e.preventDefault();
				$('#all_services').css('top','130px').fadeIn(0,function(){
					$('#all_services img').fadeIn(function(){
						$(this).click(function(){
							$(this).fadeOut()
						})
						}).css('cursor','pointer');
					});
				});
		}();
	}


	////////////////////////////////////////////////////////////////////////////////////////
	//clients
	if($('body').hasClass('clientsLayout')){
		var  clients = function(){
			var tm = 400;
			$('.client').hide().css({
				'position': 'absolute',
				'top': '130px'
			});
			$('.map').show();

			$('a.client_link').click(function(e){
			if(!$(this).hasClass('active')){
					$($(this).attr('href')).delay(0.9*tm).fadeIn(tm);
					$('a.client_link').removeClass('active');
					$(this).addClass('active');
					$('.client:not(:hidden)').fadeOut(tm);
				}
			});
			var current =	window.location.hash;
			if(current != ''){
				$('.'+current.substr(1)).click();
			}
		}();
	}






	////////////////////////////////////////////////////////////////////////////////////////
	// home 
	if($('body').hasClass('homeLayout')){

		//////////////////////////////////////////////////////////////////////////////////////
		// presentation
		$('#pres').click(function(){
			var he = $(window).height();
			var res_time = 30000;
			var sertimeout; // timeOut
			var sertm = false; // is sertimeout set?
			clearTimeout(timeout);
			$('body').append('<div id="overlay">');
			$('body').append('<div id="over_rotator"><a class="next" href="#"></a><a class="prev" href="#"></a></div>');
			$('body').height(he).css('overflow','hidden');
			$('#over_rotator a').css({
					'top': 0,
					'height': $(window).height()*0.85,
					'width': parseInt($(window).width()/5)
			});

			// return everything back to where it started
			var finish = function(){
				$('#overlay').fadeOut(function(){
					$('#pres_imgs').hide().remove().appendTo('#panel');
					$('#overlay').remove();
					$('#over_rotator').remove();
					$('body').height('auto').css('overflow','visible');
					if(sertm == true){
						clearTimeout(sertimeout)
						sertm = false;
					}
					resetImgs();
					$('#pres_imgs, #pdf_down').css('display','none');
					timeout = setTimeout("$('#rotator .next').click()",home_time);

				});
			}
			
			var scaleImgs = function(){
				$('#pres_imgs > img').each(function(){
					$(this).attr('height',$(window).height());
				});
			}

			var resetImgs = function(){
				$('#pres_imgs > img').each(function(){
					$(this).css('opacity',1);
				});
			}

			var scaleOverlay = function(){
				$('#overlay')
					.width($(window).width())
					.height($(window).height()+30);
			}

			var scaleRotator = function(){
				$('#over_rotator a').css({
					'height': $(window).height()*0.85
				});
			}

			$('#overlay')
				.css({
					display: 'none',
					position: 'absolute',
					top: 0,
					left: 0,
					'z-index': 1000,
					background: '#fff',
					width: $(window).width(),
					height: 20+he,
					opacity: '1'
				})
				.fadeIn(function(){
					// move imgs to overlay
					$('#pres_imgs').remove().appendTo('#overlay');
					$('#pdf_down').remove().appendTo('body');
					$('#pres_imgs, #pdf_down').css('display','block');
					scaleImgs();

					$(window).resize(function(){
						scaleImgs();
						scaleOverlay();
						scaleRotator();
					})

					// image rotation
					if($('.view_1').length > 0 && $('.view_2').length > 0){
						
						var current = 1; // current image number
						var images = 0; // number of images we have
						sertimeout = setTimeout("$('#over_rotator .next').click()",res_time);
						sertm = true;
						for(i = 1; i < 60; i++){
							if($('.view_'+i).length > 0){
								$('.view_'+i).hide();
								images++;
							}
						}

						$('.view_'+current).fadeIn(500);

						$('#over_rotator .next').click(function(e){
							//alert('click');
							clearTimeout(sertimeout);
							sertm = false;
							$('img').stop();
							$('.view_'+current).fadeOut(500,function(){
								current++;
								if(current > images){
									finish();
								}
								$('.view_'+current).fadeIn(500,function(){
									sertimeout = setTimeout("$('#over_rotator .next').click()",res_time);
									sertm = true;
								});
							});
							e.preventDefault();
						});

						$('#over_rotator .prev').click(function(e){
							clearTimeout(sertimeout);
							sertm = false;
							$('img').stop();
							$('.view_'+current).fadeOut(500,function(){
								current--;
								if(current == 0){ // jump back to the last one
									current = images;
								}
								$('.view_'+current).fadeIn(500,function(){
									sertimeout = setTimeout("$('#over_rotator .next').click()",res_time);
									sertm = true;
								});
							});
							e.preventDefault();
						});

					}

				})
				.click(function(e){
					finish();
					e.preventDefault();
				});

				



		});





		var intro = function(){
			$('#intro_overlay').css({
				'display':'block',
				'height':$('body').height()
			});
			$('#intro').show()
		}();	

		// images rotation
		var current = 1; // current image number
		var images = 0; // number of images we have
		var starts = new Array(); // array of images that can be used as first
		var index = 0; // starts index
		var set_size = 2
		timeout = setTimeout("$('#rotator .next').click()",home_time);
		for(i = 1; i < 60; i++){
			if($('#view_'+i).length > 0){
				++images;
				$('#view_'+i).hide();
				if(images == 1 || isInt((images-1)/set_size)){
					++index;
					starts[index] = images;
				}
			}
		}
		if(images > set_size){ // set random start
			current = starts[maxRandom(index)];
		}
		$('#view_'+current).show();
		$('#rotator .next').click(function(e){
			clearTimeout(timeout);
			$('img').stop();
			$('#view_'+current).fadeOut(function(){
				current++;
				if(current > images){
					current = 1;
				}
				$('#view_'+current).fadeIn(function(){
					timeout = setTimeout("$('#rotator .next').click()",home_time);
				});
			});
			e.preventDefault();
		});
	}else{
	// nonhome image rotation
		if($('#animation').hasClass('clients')){ // temporary bullshit to change logos faster
			other_time = other_time/2;
		}
		if($('#view_1').length > 0 && $('#view_2').length > 0){
			var current = 1; // current image number
			var images = 0; // number of images we have
			var timeout = setTimeout("$('#rotator .next').click()",other_time);

			for(i = 1; i < 60; i++){
				if($('#view_'+i).length > 0){
					$('#view_'+i).hide();
					images++;
				}
			}
			current = maxRandom(images);			
			$('#view_'+current).fadeIn();

			$('#rotator .next').click(function(e){
				clearTimeout(timeout);
				$('img').stop();
				$('#view_'+current).fadeOut(function(){
					current++;
					if(current > images){
						current = 1;
					}
					$('#view_'+current).fadeIn(function(){
						timeout = setTimeout("$('#rotator .next').click()",other_time);
					});
				});
				e.preventDefault();
			});

			$('#rotator .prev').click(function(e){
				clearTimeout(timeout);
				$('img').stop();
				$('#view_'+current).fadeOut(function(){
					current--;
					if(current == 0){ // jump back to the last one
						current = images;
					}
					$('#view_'+current).fadeIn(function(){
						timeout = setTimeout("$('#rotator .next').click()",other_time);
					});
				});
				e.preventDefault();
				timeout = setTimeout("$('#rotator .next').click()",other_time);
			});
		}
	}

	//2011 xmess
	$('#x_skip_intro').click(function(){
		endIntro();
	});

});





//////////////////////////////////////////////////////////////////////////////////////////
// waiting for flash to call..
function endIntro(){
	$(function(){
		$('#intro').hide();
		$('#intro_overlay').fadeOut(500,function(){
			clearTimeout(timeout);
			timeout = setTimeout("$('#rotator .next').click()",5000);
		});
	}
)}
