function openSubmenu(el){
	$('.submenu').each( function(i) {
		if($(this).prev().hasClass("active")){
			$(this).hide("fast");
			$(this).prev().removeClass("active");
		}
	});
	
	$(el).next().show("fast", function() {
		makeSubmenu();
  });
	$(el).addClass("active");
}

function moveListDown(el){
	if($(el).hasClass('unactive') == false && $(el).hasClass('disabled') == false){
		var currentPos = $(el).prev().children().css('top');
		var movePos = parseInt(currentPos.slice(0, currentPos.length - 2));
		
		if($(el).prev().children().height() + movePos - 100 <= $(el).prev().height() + 20){
			currentPos = $(el).prev().height() - $(el).prev().children().height();
			$(el).addClass('unactive');
		}
		else{
			currentPos = movePos - 100;
		}
		
		$(el).prev().children().animate({
		top: currentPos
	  }, 500, function() {
		$(el).removeClass('disabled');
	  });
	  
	  $(el).addClass('disabled');
	  $(el).parent().children(".move-up").removeClass('unactive');
	  }
}

function moveListUp(el){
	if($(el).hasClass('unactive') == false && $(el).hasClass('disabled') == false){
		var currentPos = $(el).next().children().css('top');
		var movePos = parseInt(currentPos.slice(0, currentPos.length - 2));
		
		if(movePos + 100 >= -10){
			
			currentPos = '0';
			$(el).addClass('unactive');
		}
		else{
			currentPos = movePos + 100;
		}
		
		$(el).next().children().animate({
		top: currentPos
	  }, 500, function() {
		$(el).removeClass('disabled');
	  });
	  
	  $(el).addClass('disabled');
	  $(el).parent().children(".move-down").removeClass('unactive');
	}
	
}

function makeSubmenu(){
	$('.submenu').each( function(i) {
		if($(this).children(".submenu-box").children("ul").height() < 190){
			$(this).children("a").hide();
		}
		else{
			$(this).children("a").show("slow");
			
			var toppos = 0;
			
			if($(this).find('.active').position()){
				toppos = $(this).find('.active').position().top;
			}
			
			if($(this).children(".submenu-box").children().height() - $(this).children(".submenu-box").height() <= toppos){
				$(this).children(".submenu-box").children("ul").css('top', ($(this).children(".submenu-box").height() - $(this).children(".submenu-box").children().height())+'px');
				$(this).children(".move-down").addClass('unactive');
			}
			else{
				$(this).children(".submenu-box").children("ul").css('top', -toppos+'px');
			}
			
			if(parseInt($(this).children(".submenu-box").children("ul").css('top')) == 0){
				$(this).children(".move-up").addClass('unactive');
			}
			else{
				$(this).children(".move-up").removeClass('unactive');
			}
		}
	});
}

function  makePanorama(){
    mph = $('#main-photo').height();
	if(mph && mph < 200){
		
		$.ajax({
		  url: "/content/index/image-thumb?path="+photosrc+"&width=100000&height=400",
		  cache: false,
		  
		  success: function(src){
			$('.opt1').css("background-image", "url('/"+src+"')");
			
			$('#navigate').show();
			
			panoH = (400 * $('#main-photo').width()) / mph;
			PhotoNav.init('navigate', 700, panoH, false);
		  }
		});
	}
 
}

function send(){
	
	var tempString = '';
	
	if($('#f1').val() == ""){
		tempString += 'Put your e-mail\n';
	}
	if($('#f1').val() != "" && !validMail($('#f1').val())){
		tempString += 'Incorrect e-mail address\n';
	}
	if($('#f3').val() == ""){
		tempString += 'Put your message\n';
	}
	if($('#f4').val() == ""){
		tempString += 'Put code from image';
	}
	
	if(tempString == ''){
		$('#contact_form').submit();
	}
	else{
		alert(tempString);
	}
}

function validMail(mail){
	var emailReg = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
	if(!emailReg.test(mail)) {
		return false;
	}
	return true;
}
