


// FORM AUTOVALUES
jQuery(document).ready(function($){

	function populateElement(selector, defvalue) {
		$(selector).focus(function() {
			if ($(selector).val() == 'Website') {
				$(selector).val('http://');
			} else if ($(selector).val() == defvalue) {
				$(selector).val('');
			}
		});

		$(selector).blur(function() {
				if ($.trim($(selector).val()) == '') {
						$(selector).val(defvalue);
				}
		});
	};

	$('form').find('input[type="text"], input[type="password"], textarea').each(function() {
				populateElement($(this), $(this).val());
			}
	);

});
//

// FORM SUBMIT + VALIDATE
jQuery(document).ready(function($){
	function validateNoempty(text) {
		if (!text || text.length < 3) { return false;	}

		return true;
	}

	function validateText(text) {
		if (!validateNoempty(text)) { return false; }

		var re_text = /[^A-Za-z0-9\s]/;
		if (text.match(re_text) != null ) { return false; }
		return true;
	}

	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }

		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}

	$('.js-proceed').click( function(event) {
			event.preventDefault();
			error = false;

			$(this).parents('form').children('fieldset').children('input[type="text"], textarea').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}

				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-mail')) {
					if (!validateEmail($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				}
			});

			if (!error) {
				var node = $(this).parents('.js-hidebox');
				node.slideUp('slow');
				$(this).parents('form').submit();
			}

			//alert( (error)?'error' : 'no' ) ;
			return false;
		}
	);

	var _char_count = 500;

	$('textarea').keydown( function() {
			var node = $(this).parents('form').children('.w-left').
												 children('.w-action').children('.js-count-char').children('em');

			if ($(this).val().length >= _char_count) {
				$(this).val( $(this).val().substr(0, _char_count) );
			}

			node.html( _char_count - $(this).val().length );

		}
	);

});
//

// TOP MENU
jQuery(document).ready(function($){

	$('ul.js-automenu').children('li').hover(
		function() {
			$(this).children('ul').show('fast');
		},
		function() {
			$(this).children('ul').hide('fast');
		}
	);

});
//

// ADD COMMENT ON PICTURE
jQuery(document).ready(function($){
	$('a.js-add-comment').click( function(event) {
			//var node = $(this).parents('.x-post').next('.x-comment').children('.f-add-comment'); // common
			var node = $(this).parents('.x-post').children('.x-comment').children('.f-add-comment'); // custom

			var tarea = node.children('form').children('.w-right').children('.js-ta-comm')

			var ax = 'js-add-comment js-img-id-';
			var k = $(this).attr('class');
			k = '{img}' + k.substr(ax.length, 255) + '{/img}';

			tarea.val( tarea.val() + k );

			if (node.css('display') == 'none') {
				node.slideDown('fast');
			}
		}
	);

});
//













// SHOW post
jQuery(document).ready
(
 
 function($){
	$('a.js-show-post').click( function(event) 
		{
			
			
			var node = $(this).parents('.b-post').children('.b-post-content');
			var nodeview = $(this).parents('.b-post').children('.b-titlebox').children('.b-titlebox-open');
			//var nodehide = $(this).parents('.b-com-view').children('.js-phide');
			
			//var node = $(".b-com-viewer"); // custom
			
			if(node.css('display') == 'none')
			{
				
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
				//nodehide.css("display","block");
				
			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'slow');
				nodeview.css("display","block");
				//nodehide.css("display","none");
			}

			
			
		}
	);

  }
);
//


//INITIATE SHOW POST

jQuery(document).ready
(
 function($) 
 {

setTimeout( 
 function(){


			var count=$('.b-post').length;

			var node=$('.b-post-content');
			var nodeview=$('.b-titlebox-open');

			if(count<2)
			{

				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
	
			}
			

  }
  ,300);
 }
);
//





// SHOW ADD COMMENT BOX
jQuery(document).ready
(
 function($){
	$('.js-add').click( function(event) 
		{			
			var node = $(this).parents('.b-post').children('.b-post-content').children('.add-commentbox');
			var nodeview = $(this).parents('.b-com-add').children('.js-view');
			var nodehide = $(this).parents('.b-com-add').children('.js-hide');
			
			//var node = $(".b-com-viewer"); // custom
			
			if(node.css('display') == 'none')
			{
				
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
				nodehide.css("display","block");
				
			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'fast');
				nodeview.css("display","block");
				nodehide.css("display","none");
			}	
		}
	);

  }
);
//








// SHOW ADD COMMENT BOX w/o animation
jQuery(document).ready
(
 function($){
	$('.js-add-wo').click( function(event) 
		{			
			var node = $(this).parents('.b-post').children('.b-post-content').children('.add-commentbox');
			
			if(node.css('display') == 'none')
			{			
				node.animate({height:'show',opacity:'show'}, 'slow');	
			}
			else
			{
				node.animate({height:'hide',opacity:'hide'}, 'fast');
			}	
		}
	);

  }
);
//











// SEND TO A FRIEND BOX
jQuery(document).ready
(
 function($){
	$('.js-send').click( function(event) 
		{			
			var node = $(this).parents('.b-post').children('.b-post-content').children('.send-commentbox');
			var nodeview = $(this).parents('.b-com-send').children('.js-view');
			var nodehide = $(this).parents('.b-com-send').children('.js-hide');
			
			//var node = $(".b-com-viewer"); // custom
			
			if(node.css('display') == 'none')
			{
				
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
				nodehide.css("display","block");
				
			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'fast');
				nodeview.css("display","block");
				nodehide.css("display","none");
			}	
		}
	);

  }
);
//








// SEND TO A FRIEND BOX w/o animation
jQuery(document).ready
(
 function($){
	$('.js-send-wo').click( function(event) 
		{			
			var node = $(this).parents('.b-post').children('.b-post-content').children('.send-commentbox');
			
			if(node.css('display') == 'none')
			{			
				node.animate({height:'show',opacity:'show'}, 'slow');	
			}
			else
			{
				node.animate({height:'hide',opacity:'hide'}, 'fast');
			}	
		}
	);

  }
);
//















// SHOW COMMENTS
jQuery(document).ready
(
 function($){
	$('a.js-show-comments').click( function(event) 
		{
			
			
			var node = $(this).parents('.b-com-box').children('.b-com-viewer');
			var nodeview = $(this).parents('.b-com-view').children('.js-view');
			var nodehide = $(this).parents('.b-com-view').children('.js-hide');
			
			//var node = $(".b-com-viewer"); // custom
			
			
			if(node.css('display') == 'none')
			{
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
				nodehide.css("display","block");	
			
				node.children('.b-com-scroll').jScrollPane();
			
			
			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'fast');
				nodeview.css("display","block");
				nodehide.css("display","none");
			}

			
			
		}
	);

  }
);
//






// SHOW COMMENTS w/o animation
jQuery(document).ready
(
 function($){
	$('a.js-show-comments-wo').click( function(event) 
		{
			
			
			var node = $(this).parents('.b-com-box').children('.b-com-viewer');
			
			if(node.css('display') == 'none')
			{
				node.animate({height:'show',opacity:'show'}, 'slow');
			}
			else
			{
				node.animate({height:'hide',opacity:'hide'}, 'fast');
			}

			
			
		}
	);

  }
);
//











// SHOW SNEAK PEAK
jQuery(document).ready
(
 function($){
	$('a.js-show-sneak-peak').click( function(event) 
		{
			
			
			var node1 = $(this).parents('#sneak-peak').children('#sneak-peak-content');
			var node2 = $(this).parents('#sneak-peak-content').children('.sp-im-con');
			var nodeview = $(this).parents('.sp-im-con').children('.js-view');
			var nodehide = $(this).parents('.sp-im-con').children('.js-hide');
					
			if(node2.css("top")!="0px")
			{
				node1.animate({height:'375px'}, 'slow');
				node2.animate({top:'0px'}, 'slow');
				nodeview.css("display","none");
				nodehide.css("display","block");
			}
			else
			{
				node1.animate({height:'110px'}, 'slow');
				node2.animate({top:'-105px'}, 'slow');	
				nodeview.css("display","block");
				nodehide.css("display","none");
			}

			
			
		}
	);

  }
);
//











// ARCHIVES SCROLLER up
jQuery(document).ready
(
 function($){
	$('a.js-arc-up').click( function(event) 
		{
			var step=36;

			
			var node = $(this).parents('#archives').children('.arc-holder').children('.arc-scr');
			var h=node.innerHeight();
			var top=node.css("top").replace("px","");
			var topv=top*1;
			var go_to_h=topv-step;
			
			var fl=top/step - Math.floor(top/step); 
			
			if((h>(-go_to_h)) && fl=="0")
			{
				go_to_h=go_to_h+"px";
				node.animate({top:go_to_h}, 'slow');
			}
			
			
		}
	);

  }
);
//









// ARCHIVES SCROLLER down
jQuery(document).ready
(
 function($){
	$('a.js-arc-down').click( function(event) 
		{
			var step=36;

			
			var node = $(this).parents('#archives').children('.arc-holder').children('.arc-scr');
			var h=node.innerHeight();
			var top=node.css("top").replace("px","");
			var topv=top*1;
			var fl=top/step - Math.floor(top/step); 
			var go_to_h=topv+step;
			if(top<0 && fl=="0")
			{
				go_to_h=go_to_h+"px";
				node.animate({top:go_to_h}, 'slow');
			}
			
			
		}
	);

  }
);
//

























// COMMENTS BLOCK
jQuery(document).ready(function($){

	$('.js-hide').click( function(event) {
			event.preventDefault();
			$(this).parents('.js-hidebox').slideUp('fast');
		}
	);

	$('a[href="#js-add-comment"]').click( function(event) {
			event.preventDefault();

			var node = $(this).parents('.x-comment').children('.f-add-comment');
			node.slideToggle('fast');
		}
	);

	$('a[href="#js-send-mail"]').click( function(event) {
			event.preventDefault();

			var node = $(this).parents('.x-comment').children('.f-send-to-friend');
			node.slideToggle('fast');
		}
	);

});
//

// COMMENTS SCROLLER
jQuery(document).ready(function($){
	var scroll_inc, scroll_offs, scroll_speed, scroll_busy;

	scroll_busy = false;
	scroll_inc = 28; /* line-height */
	scroll_speed = 333;

	//var debug = document.getElementById('debug');
	//debug.innerHTML = scroll_node.css('top');

	$('a.js-scrolldown').click(function(event) {
			event.preventDefault();
			if (scroll_busy) { return; }

			var scroll_offs = $('.x-comment .w-scroll').height() - $('.w-hide').height() - scroll_inc;

			var	scroll_node = $(this).parents('.w-action').prev('.w-hide').children('.w-scroll');

			if (parseInt(scroll_node.css('top')) >= -scroll_offs) {
				scroll_busy = true;
				scroll_node.stop();

				scroll_move = Math.floor( Math.abs(parseInt(scroll_node.css('top'))) + scroll_inc);

				scroll_node.animate(
					{ top: -scroll_move + 'px' },
					scroll_speed,
					function() { scroll_busy = false; }
				);

			} else {
				scroll_busy = false;
				scroll_node.stop();
			}
		}
	);

	$('a.js-scrollup').click(function(event) {
			event.preventDefault();
			if (scroll_busy) { return; }

			var	scroll_node = $(this).parents('.w-action').prev('.w-hide').children('.w-scroll');

			if (parseInt(scroll_node.css('top')) <= -1) {
				scroll_busy = true;
				scroll_node.stop();

				scroll_move = Math.floor( parseInt(scroll_node.css('top')) + scroll_inc);

				scroll_node.animate(
					{ top: scroll_move + 'px' },
					scroll_speed,
					function() { scroll_busy = false; }
				);

			} else {
				scroll_busy = false;
				scroll_node.stop();
			}

		}
	);

});
//

// AUX
jQuery(document).ready(function($){

	// open link in another window
	$('a.js-el').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	// ie6 picture hover
	$('div.js-fixHover').each( function() {
		$(this).hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	});

});







// MOUSE WHEEL & SLIDERS CONTROL
jQuery(document).ready(function($) {
	if ($.browser.mozilla) {
      _wheel_coef = 50;
	} else if($.browser.safari) {
			_wheel_coef = 40;
	}	else {
		_wheel_coef = 45;
	}

	// mouse wheel over the comments area
	$('.w-comment-list').bind('mousewheel', function(event, delta) {
			var node = $(this).find('.w-hide');
			if ( node.attr('scrollHeight') > node.outerHeight() + 2 ) {
				var speed = node.height() / node.attr('scrollHeight') * (_wheel_coef);

				var nv = node.scrollTop() - delta * speed;
				node.scrollTop(nv);

				var s_hande =  node.scrollTop() / ( node.attr('scrollHeight') -  node.height() ) * 100;
				$(this).parents('.x-comment').find('.slide-comment').slider('option', 'value', -s_hande );

				return false;
			}

			return true;
	});
	//

	// slide comments
	$('.slide-comment').each(function() {
		$(this).slider({
			animate: false,
			orientation: "vertical",
			min: -100,
			max: 0,
			slide: function(event, ui) {
				var node = $(this).parents('.x-comment').find('.w-hide');

				if ( node.attr('scrollHeight') > node.outerHeight() + 2 ) {
					var ratio = node.attr("scrollHeight") - node.height();
					node.attr( { scrollTop: -ui.value * (ratio / 100) } );
				} else {
					return false;
				}

				return true;
			}
		});

	});

});
//




//
jQuery(document).ready(function($) {
	$('.send-friend-link').click(function(event) {
		event.preventDefault();
		$(this).closest('form').submit();
	});
});




