//** show/hide any page element 
function hideme(myname){
	$('#'+myname).animate({marginTop: '-350px', opacity: 0}, 400);}
function showme(myname){
	$('#'+myname).animate({marginTop: '10px', opacity: 0.65}, 'slow');}

// page scroll script  
function scrollit(container){
	destination = $("#" + container).offset().top;
	$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollTop: destination}, 800);
}


/***** jQuery onLoad scripts *****/ 
$(document).ready(function() {


// for portfolio - images scrolling initialization
	$(".scrollable").scrollable();

// call the images description function for portfolio 
	window.setTimeout("showdes();", 2500);
	
	
	showform(); // call the forms overlay
	fancyimg(); // Fancybox for Portfolio images
	mycounter(); // call the text-area counter


// show the portfolio albums description 
$(".alb").hover(
	function(){
	$(this).animate({marginTop:"-125px"}, 400);}, 
	function(){
	$(this).animate({marginTop:"-30px"}, 250);}
);



$.ajaxSetup({
	cache: false,
	type: "get",
 });


});
/***** end of jQuery onLoad scripts *****/ 


// Overlay for the forms 
function showform(){
$(".myform[rel]").overlay({
	top: '20px',
	speed: 'slow',
	mask: {
		color: '#000',
		loadSpeed: 200,
		opacity: 0.55
	}
});
}

// Fancybox for article images
function fancyimg(){
		function formatTitle(title){
			return "<div id='p-title' style='background-image:url(img/bg.png);'>" + (title && title.length ? title : '') + '</div>';}
	$("a.group, a.gal").fancybox({
		'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 
		'speedIn' : 600, 'speedOut' : 300,
		'changeSpeed' : 600, 'changeFade' : 600, 
		'padding' : 3, 'margin' : 10,
		'autoScale' : false, 'overlayOpacity' : 0.55, 'overlayColor' : '#000',
		'titlePosition' : 'over', 'titleFormat' : formatTitle,
		'onComplete' : function() {
			$("#fancybox-title").delay(2000).slideUp('slow');
			$("#fancybox-wrap").hover(function() {
			$("#fancybox-title").slideDown('slow');
			}, function() {
			$("#fancybox-title").slideUp('slow');});
		},
	});
}

// AJAX sending any text form 
function sendme(myform, sender, refcom, refid){
	mydata = $('#'+myform).serializeArray(); // get the data from the form

$.ajax({
  url: sender,
  data: mydata,
  type: "post",
  beforeSend: function(){
	$("#mynote").html("<p style='text-align:center;'><img src='js/loading.gif' style='margin:10px;'><br><br>Отправка запроса...</p>");
	showme("bignote");
	},
  success: function(a){
	$("#mynote").html(a);
	window.setTimeout("hideme('bignote');", 2000);
	   // clear the form
		var allels = document.forms[myform].elements; 
		for(var i=0; i < 3; i++){
			allels[i].value = "";
			if(allels[i].name=='nm'){allels[i].value = "Ваше имя";}
			if(allels[i].name=='em'){allels[i].value = "Email";}
		}
	if(refcom){comrefresh(refcom, refid);} // refresh page part if requested
	},
  error: function(){
	$("#mynote").html("<p>Извините, мы не смогли отправить Ваш запрос. Попробуйте, пожалуйста, позднее.</p>").fadeTo("slow", 0.65);
	window.setTimeout("hideme('bignote');", 2000);
	$.fancybox.close();
	}
});
return false;
}



//** textarea characters/words counter
function mycounter(){
$('.txt-area, #txt-area').keyup(function() {
var maxcar = 1000, mincar = 30; // max and warning numbers

// limit textarea
if($(this).val().length > maxcar) {
	$(this).val($(this).val().substr(0, maxcar));
}

// find and show the counter
var counter = $(this).parent().attr('id');
var counter = $('#'+counter+'2');
counter.val(maxcar - $(this).val().length);

// change the style if warning number is reached
if(counter.val() <= mincar){
	counter.css({
		fontWeight: 'bold', 
		color: 'red'
	});
}else{
	counter.css({
		fontWeight: 'normal', 
		color: 'black'
	});
}
});
} //** end of textarea counter




