var someStuff = {
	fancyboxFormatTitle	: function (title, currentArray, currentIndex, currentOpts) {
		title = title.split('|');
		if(title[0] || title[1]) {
			return '<div id="fancybox-title-db">' + (title[0] ? '<div class="image-caption">' + title[0] + '</div>' : '') + (title[1] ? '<div class="image-author">' + title[1] + '</div>' : '') + '</div>';
		}
	}
}

var rating = {
	rateArticle : function (rating, t) {
		$('.article-social .voting a').
			unbind('click').
			click(function (){
				return false;
			});
			
		var id = window.location.href.split('-');
		var storage = "a"+id;
		
		if($.Storage.get(storage) == null) {
			if(rating > 0) {
				$(t).siblings().text('+'+(parseInt($(t).siblings().text())+1));
			} else {
				$(t).siblings().text((parseInt($(t).siblings().text())-1));
			}
			
			$.Storage.set(storage, "1");
			$.post('/rating/article/'+(rating > 0 ? 'plus' : 'minus')+'/'+parseInt(id[id.length-1]));
		}
		return false;
	},
	rateComment : function (rating, t) {
		$(t).parent().find('a').
			unbind('click').
			click(function (){
				return false;
			});
			
		var id = $(t).parents(".comment").first().data("comment").id;
		var storage = "c"+id;
		
		if($.Storage.get(storage) == null) {
			if(rating > 0) {
				$(t).find('span').text('+'+(parseInt($(t).find('span').text())+1));
			} else {
				$(t).find('span').text((parseInt($(t).find('span').text())-1));
			}
			
			$.Storage.set(storage, "1");
			$.post('/rating/comment/'+(rating > 0 ? 'plus' : 'minus')+'/'+parseInt(id));
		}
		return false;
	}
}

$(function() {
	$('a[rel*=external]').attr('target', '_blank');

	$('.list div.comment').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

    $(".comment-form, .comment-answer").append('<input type="hidden" name="spamtest" value="spamtest" />');

	$('p.buried a').click(function(e) {
		e.preventDefault();
		comment = $(this).parents('li');
		comment.slideUp(function() {
			$('p.buried', comment).hide();
			comment.removeClass('buried').slideDown();
		});
	});

	if($('.open-article a.fancybox').get().length > 0) {
		$('.open-article a.fancybox').fancybox({hideOnContentClick:true, title: $('.open-article .image-caption').text()+'|'+$('.open-article .image-author').text(), titlePosition : 'inside', titleFormat: someStuff.fancyboxFormatTitle});
	}

	$('#db-newsletter').change(function() {
		if ($(this).is(':checked'))
			$('.db-newsletter-choices input').attr('disabled', '');
		else
			$('.db-newsletter-choices input').attr('checked', '').attr('disabled', 'disabled');
	});

	$('li.comment-answer a').click(function(e) {
		e.preventDefault();
		$(this).parent().addClass('active');
		if (!$(this).parents('.related').next().hasClass('comment-answer')) {
			commentForm = $('form.comment-answer.hidden').clone();
//			commentForm.submit(function(e) {
//				e.preventDefault();
//				$(this).slideUp(function() {
//					$(this).remove();
//				});
//			});

            commentForm.find(".parent").val($(this).parents(".comment").first().data("comment").id);
			commentForm.insertAfter($(this).parents('.related')).removeClass('hidden');
		}
	});

	$('.label-inside').live('focus', function() {
		$(this).addClass('focused').attr('alt', $(this).val());
		if (!$(this).hasClass('changed')) {
			$(this).val('');
		}
	});

	$('.label-inside').live('blur', function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('alt')).removeClass('focused');
		} else {
			$(this).addClass('changed');
		}
	});

	if($('.article-social .voting').get().length > 0) {
		$('.article-social .voting a.up').click(function(){rating.rateArticle(+1,$(this));return false;});
		$('.article-social .voting a.down').click(function(){rating.rateArticle(-1,$(this));return false;});
	}

	if($('#comments ul.list div.voting').get().length > 0) {
		$('#comments ul.list div.voting a.up').click(function(){rating.rateComment(+1,$(this));return false;});
		$('#comments ul.list div.voting a.down').click(function(){rating.rateComment(-1,$(this));return false;});
	}

	$(
        '.video-article #footer-container, ' +
        '.video-article #header-container, ' +
        '.video-article .wide .category-news, ' +
        '.video-article .wide .comments-form, ' +
        '.video-article .wide .article-comments, ' +
        '.video-article .side .video-categories, ' +
        '.video-article .side .without-tie'
    ).hover(function() {
        if (!$(this).hasClass("open-article")){
		    $(this).animate({opacity: 1}, 200);
        }
	}, function() {
        if (!$(this).hasClass("open-article")){
		    $(this).animate({opacity: .3}, 200);
        }
	});

    if($('.actual').get().length > 0) {
        $('.actual a.next').click(function (){
            var count = $('.actual .article').get().length;

            if(count < 4) {
                return false;
            }

            $('.actual .article.hidden:first').removeClass("hidden");
            $('.actual .article[class!=hidden]:first').addClass("hidden").insertAfter($(".actual .article:last"));

            return false;
        });

        $('.actual a.prev').click(function (){
            var count = $('.actual .article').get().length;

            if(count < 4) {
                return false;
            }

            $('.actual .article.hidden:last').removeClass("hidden").insertBefore($(".actual .article:first"));
            $('.actual .article:eq(4)').addClass("hidden");

            return false;
        });
    }
});

function draugiemSay(title, url, titlePrefix) {
	window.open(
		'http://www.draugiem.lv/say/ext/add.php?title='+encodeURIComponent(title)+
		'&link=' + encodeURIComponent(url) +
		'&titlePrefix=' + encodeURIComponent(titlePrefix),
		'',
		'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
	);
	return false;
}
