$(document).ready(function(){
	if($('.lnws').length > 0) {
		$('.lnws').cycle({
			fx:     'scrollVert',
			speed:   1000,
			timeout: 6000,
			prev:   '#prev', 
			next:   '#next',
			pause: 1
		});
	}
	if($('#hd_img').length > 0 || $('.levs').length > 0) {
		$('#hd_img,.levs').cycle();
	}
	
	if($("a.fancybox").length > 0) {
		$("a.fancybox").fancybox({
			'zoomOpacity'			: true,
			'overlayShow'			: true,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500,				 
			'overlayOpacity'		: .8,
			'hideOnContentClick' 	: false,
			'centerOnScroll' 		: true
	
		});
	}
	
		// main navigation
	if($("#mnv ul").length > 0) {		
		$("#mnv ul").hide();		
		$("#mnv li").hover(function() {		
			var active = $(this).hasClass("act");
			// add another class to indicate
			// that the button had class assigned
			if(active) {
				$(this).addClass("had");
			} else {
				$(this).addClass("act");
			}
			$(this).find("ul").stop(true, true).fadeIn("fast");		
		}, function() {
			// if button had the new class assigned
			// then remove it and also remove active
			if(!$(this).hasClass("had")) {				
				$(this).removeClass("act");	
			} else {
				$(this).removeClass("had");
			}
			$(this).find("ul").stop(true, true).fadeOut("fast");
		});		
	}
	
	
	
	$('#submit_comment').live('click', function() {
		var comment = $('#comment').val();
		if (comment !== '') {
			$.post('/discussion-board-comment.html', { comment : comment }, function(data) {
				if (!data.error) {
					var cln = $('#comment').closest('form').clone();
					$('#comment').closest('form').fadeOut(300, function() {
						$(this).replaceWith($(data.message).hide().fadeIn(300, function() {
							var elem = $(this);
							setTimeout(function() { 
								elem.fadeOut(300, function() {
									location.reload();
									//$(this).replaceWith($(cln).hide().fadeIn(300));
								}); 
							}, 2000);
						}));
					});
				}
			}, 'json');
		}
		return false;
	});

	$('.reload').live('click', function() {
		location.reload();
	});
	
	$('.send_link').live('click', function() {
		var target = $(this).attr('id');
		$('.'+target).siblings('form').hide();
		$('.'+target).toggle();	
		return false;	
	});
	
	
	$('.submit_comment').live('click', function() {
		var tar = $(this).prev('textarea');
		var comment = tar.val();
		var identity = $(this).attr('rel').split('_');
		if (comment !== '' && identity.length > 0) {
			switch(identity[0]) {
				case 'reply':
				$.post('/discussion-board-reply.html', { comment : comment, master : identity[1] }, function(data) {
					if (!data.error) {
						var cln = tar.closest('form').clone();
						tar.closest('form').fadeOut(300, function() {
							$(this).replaceWith($(data.message).hide().fadeIn(300, function() {
								var elem = $(this);
								setTimeout(function() { 
									elem.fadeOut(300, function() {
										location.reload();
										//$(this).replaceWith($(cln).hide());
									}); 
								}, 2000);
							}));
						});
					}
				}, 'json');
				break;
				case 'private':
				$.post('/discussion-board-private-comment.html', { comment : comment, parent : identity[1] }, function(data) {
					if (!data.error) {
						var cln = tar.closest('form').clone();
						tar.closest('form').fadeOut(300, function() {
							$(this).replaceWith($(data.message).hide().fadeIn(300, function() {
								var elem = $(this);
								setTimeout(function() { 
									elem.fadeOut(300, function() {
										$(this).replaceWith($(cln).hide());
									}); 
								}, 2000);
							}));
						});
					}
				}, 'json');
				break;
			}			
		}
		return false;
	});


});
