var root 	= "/";
var loading = '<div class="loading"></div>';

$(document).ready(function() {
   $('ul#videospy').simpleSpy();
	$('.post').filter(':first').show();$('#sliderpanel .prev a').addClass('disabled');
	var count = $('div.post').size();
	$('#sliderpanel .prev a').click(function() {
		if(!$(this).is('.disabled')) {
		var current = $('.post:visible');
		$('#sliderpanel .next a').removeClass('disabled');
		if($('.post:eq(1)').is(':visible')) {$('#sliderpanel .prev a').addClass('disabled');} current.hide();current.prev().fadeIn();}
		return false;
	});
	$('#sliderpanel .next a').click(function() {
		if(!$(this).is('.disabled')) {
		var current = $('.post:visible');
		$('#sliderpanel .prev a').removeClass('disabled');
		var c = count - 2 ;
		if($('.post:eq('+c+')').is(':visible')) {$('#sliderpanel .next a').addClass('disabled');} current.hide();current.next().fadeIn();}
		return false;
	});
	$('#populars ul.tabs a').click(function() {$('#populars ul.tabs a').removeClass('active');$(this).addClass('active');
		$('#populars ul.news').hide();
		var ul = $(this).parent().attr('id');$('#'+ul+'news').show();
		return false; 
	});
	$("#tabs a").click(function() {$("#tabs a").removeClass("active");$(this).addClass("active");$("#comments>div").hide();
		var div = $(this).attr("rel");		
		$("#" + div).show();
		return false;
	});
	$('#searchstring').focus(function() {if($(this).attr("value") == "Sitede Ara") {$(this).val("");}});
	$('#searchstring').blur(function() {if($(this).attr("value") == "") {$(this).val("Sitede Ara")}});
	
	$("#commentform").submit(function() {
		$("#writecomment").prepend(loading);$("#submit").attr("disabled", true);$("#writecomment .messages").remove();
		var inputs = new Object;$(':input', this).each(function() {inputs[this.name] = this.value;});
		$.ajax({type : "POST",data : inputs,url : root + "tasks.php?task=addcomment",success : function(msg) {
				$(".loading").remove();$(".error").remove();if(msg == "ok") {
					$("#writecomment").prepend('<div class="messages done"><b>Yorumunuz alındı!</b></div>');$("#commentform").remove();
				} else {
					$("#writecomment").prepend(msg); 
				}
				$("#submit").attr("disabled", false);
			}
		});
		return false;
	});
	$('#galleries ul li .galleryinfo').not(':first').hide();$('#galleries li:first').addClass('active');
	$('#galleries ul li a.thumb').click(function() {
		$('#galleries ul li .galleryinfo:visible').hide();$('#galleries li').removeClass('active');
		$(this).next('.galleryinfo').fadeIn();$(this).parent().addClass('active');return false;
		}
	);

	$('#vidboxs li').hover(function() {
		$('.vidinfo').hide();
		$(this).children('.vidinfo').animate({opacity: "show", top: "0"}, "slow");

	}, function() {$('.vidinfo').fadeOut(250);});
	
	$("form#pollform").submit(function() {
		$("#poll").prepend('<div class="loading"></div>');
		$("#poll .errors").remove()
		var vote = "vote=" + $('input[name=vote]:checked').val();
		var poll = $("input[name=pollid]").val();
		$.ajax({
		type : "POST",		
		data : vote,
		url : root + "tasks.php?task=poll_vote&p="+poll,
		success : function(msg) {
		$("#poll .loading").remove();
		if(msg == "ok") {
			$("#poll").html('<div class="messages done">Oyunuz alındı!</div>');
			var results = function() { $("#poll").load(root + "tasks.php?task=poll_results&p=" + poll); }
			setTimeout(results,1000);
		} else {
			$("#poll").prepend(msg); }
		}
			
		});
		return false;
	});
	$("#usermessages a.deletemessage").click(function() {
		var sure = confirm("Silmek istediğinizden emin misiniz?");
		if(sure) {
			$(this).parents('li').children('.m_body').prepend(loading);
			var mid = $(this).attr("rel");
			var message = $(this).parents('li');
			$(this).load(root + "tasks.php?task=deleteusermessage&mid=" + mid);
			$('img.loadingsmall').remove();
			message.animate({ opacity: "hide" }, 1800);
		} else {
			return false;
		}
		return false;
	});
	
	$('#fp_link').click(function() {$('#forgotpassword').slideToggle();return false;});
	$("#fp_form").submit(function() {
		$(".messages").remove();$("#submitp").attr("disabled", true);$(this).prepend(loading);
		var inputs = new Object;$(':input', this).each(function() {inputs[this.name] = this.value;});
		$.ajax({type : "POST",data : inputs,url : root + "tasks.php?task=forgotpass",success : function(msg) {
					$(".loading").remove();$(".error").remove();if(msg == "ok") {
					$(".holder").html('<div class="messages done">Parolanızı yenilemek için gereken bilgiler e-posta adresinize gönderildi!</div>');$("#fp_form").remove();
				} else {
					$(".holder").html(msg); 
				}
				$("#submitp").attr("disabled", false);
			}
		});
		return false;
	});
});

function getAbsoluteTop(objectId) {
	o = document.getElementById(objectId)
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent		oTop += oParent.offsetTop
		o = oParent
	}
	return oTop
}


(function ($) {
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 2;
    interval = interval || 4000;
    
    return this.each(function () {
        // 1. setup
            // capture a cache of all the list items
            // chomp the list down to limit li elements
        var $list = $(this),
            items = [], // uninitialised
            currentItem = limit,
            total = 0, // initialise later on
            height = $list.find('> li:first').height();
            
        // capture the cache
        $list.find('> li').each(function () {
            items.push('<li>' + $(this).html() + '</li>');
        });
        
        total = items.length;
        
        $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
        
        $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();

        // 2. effect        
        function spy() {
            // insert a new item with opacity and height of zero
            var $insert = $(items[currentItem]).css({
                height : 0,
                opacity : 0,
                display : 'none'
            }).prependTo($list);
                        
            // fade the LAST item out
            $list.find('> li:last').animate({ opacity : 0}, 1000, function () {
                // increase the height of the NEW first item
                $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                
                // AND at the same time - decrease the height of the LAST item
                // $(this).animate({ height : 0 }, 1000, function () {
                    // finally fade the first item in (and we can remove the last)
                    $(this).remove();
                // });
            });
            
            currentItem++;
            if (currentItem >= total) {
                currentItem = 0;
            }
            
            setTimeout(spy, interval)
        }
        
        spy();
    });
};
    
})(jQuery);