/**
 * 
 */
var getComments;
/**
 * jQuery'e bağlı fonksiyonlar
 * jQuery yüklendikten sonra çalıştırılacak 
 */
$(document).ready(function(){
	
	// yorumlar köşe yuvarla
	$("#comment-add").corner("5px");
	$(".info-bar").corner("5px");
	
    /**
     * Yazarlar
     */
    $(".authors-block .authors-slide").jCarouselLite({
        btnNext: ".authors-block .next",
        btnPrev: ".authors-block .prev",
        vertical: true,
        mouseWheel: true,
        scroll: 1,
        visible: 4,
        auto: 3000,
        speed: 500
    });
    
    /**
     * Videolar Kayan
     */
    $(".video-block .videos-slide").jCarouselLite({
        btnNext: ".video-block .b-title .next",
        btnPrev: ".video-block .b-title .prev",
        scroll: 3,
        visible: 3,
        auto: 3000,
        speed: 500
    });
    
    /**
     * Foto Galeriler Kayan
     */
    $(".gallery-block .galleries-slide").jCarouselLite({
        btnNext: ".gallery-block .b-title .next",
        btnPrev: ".gallery-block .b-title .prev",
        scroll: 3,
        visible: 3,
        auto: 3000,
        speed: 500
    });
    
    /**
     * Günün haberleri
     */
	$("#sub-headlines .inner").jCarouselLite({
        btnNext: "#sub-headlines .next",
        btnPrev: "#sub-headlines .prev",
        mouseWheel: true,
        scroll: 1,
        visible: 5,
        auto: 4000,
        speed: 500
    });
    
    /**
     * Kayan son yorumlar
     */
    commentSlide();
    
        /**
     * Yorum ekleme formu
     */
    function commentToggle(){
        var defaultText = 'Bu yazıya yorum gönderin...';
        var inpObj      = $('#comment-text');
        var postObj     = $('#comment-post');
        
        inpObj.html(defaultText);
        
       inpObj.focusin(function(){
           postObj.slideDown('fast');
           inpObj.html('');            
       });
       
       $('#comment-post a.cancel').click(function(){
           postObj.slideUp('fast');
           inpObj.html(defaultText); 
       });
    } commentToggle();
    
    /**
     * Yorum Ekle
     */
    commentAdd = function(){    
        var parent_id = $('#parent_id').val();
        var comment_type = $('#comment_type').val();
        var comment  = $('#comment-text').val();
        var name     = $('#comment-name').val();
        var title     = $('#comment-title').val();
                
        if(comment != '' && name != '' && title != ''){
            $.post('/comment/add', {parent_id: parent_id, comment_type: comment_type, title: title, comment: comment, name: name}, function(result){
                if(result.status){
                    $('#comment-add .comment-form').html('<span class="comment-success">Yorumunuz başarıyla eklendi. Onaylandıktan sonra yayınlanacaktır</span>');
                }else{
                    alert('Yorumunuz Eklenemedi. Hata : '+result.message);
                }
            }, 'json').error(function(err, url, line){ /*alert('Hata Oluştu : ' + err.message+', url : '+url+', satır : '+line);*/ });
        }else {
            $('#comment-add .error').html('Lütfen gerekli alanları doldurun').fadeIn('fast');
        }
    };
    
    /**
     * Yorum Cevapla
     */
    commentReply = function(elm){
        var parent_id = $('#parent_id').val();
        var comment_type = $('#comment_type').val();
        var comment_parent_id = $();
        var comment  = $('#comment-reply #comment-text').val();
        var name     = $('#comment-reply #comment-name').val();
    };
    
    /**
     * Hava durumu ve Döviz Kuru değiştirici
     */
    function currWeatherRotator(){
        this.old = null;
        this.rotate = function(ID){
            if(cwr.old != null){
                $('#header-'+cwr.old).slideUp('slow');
            }
            $('#header-'+ID).slideDown('slow');
            
            cwr.old = ID;
        };
        
        this.swap = function(){
            if(cwr.old == 'weather'){
                cwr.rotate('currency');
            }else{
                cwr.rotate('weather');                
            }
        };
        
        this.timer = setInterval(this.swap, 5000);
    }
    
    var cwr = new currWeatherRotator();
    cwr.rotate('weather');
    
    /**
     * Ana Manşetler
     */
    function mainHeadline(){
        this.old = null;
        this.length = $('#headlines .numbers .no').length;
        this.wfu = new waitForUser(this);
        
        $('#headlines .numbers .no').each(function (index, value) {
                $(value).mouseover(function () {
                    headline.toggle(index);
                    headline.wfu.init();
                });
        });
        
        this.toggle = function(id){
            if(headline.old != null){
                $('#headlines .numbers .no:eq('+headline.old+')').removeClass('selected');
            }
            
            $('#headline_image').attr('src', '/assets/uploads/news/' + imgArr[id] + '.jpg');
            $('#headline_title').html('<a href="' + linkArr[id] + '">'+ titleArr[id] + '</a>');
            $('#headline_description').html('<a href="' + linkArr[id] + '">'+ descArr[id] + '</a>');
            $('#headline_link').attr('href', linkArr[id]);
            
            $('#headlines .numbers .no:eq('+id+')').addClass('selected');
            headline.old = id;
        };
        
        this.swapNext = function(){
            if(headline.old == headline.length-1){
                headline.toggle(0);
            } else {
                headline.toggle(headline.old+1);
            }
        };
    }
    
    var headline = new mainHeadline();
    headline.toggle(0);
    
    /**
     * Yan Manşetler
     */
    function mainHeadline2(){
        this.old = null;
        this.length = $('#headlines2 .numbers .no').length;
        this.wfu = new waitForUser(this);
        
        $('#headlines2 .numbers .no').each(function (index, value) {
                $(value).mouseover(function () {
                    headline2.toggle(index);
                    headline2.wfu.init();
                });
        });
        
        this.toggle = function(id){
            if(headline2.old != null){
                $('#headlines2 .numbers .no:eq('+headline2.old+')').removeClass('selected');
            }
            
            $('#headline-img-2').attr('src', '/assets/uploads/news/' + imgArr2[id] + '.jpg');
            $('#headline-title-2').html('<a href="' + linkArr2[id] + '">'+ titleArr2[id] + '</a>');
            $('#headline-link-2').attr('href', linkArr2[id]);
            
            $('#headlines2 .numbers .no:eq('+id+')').addClass('selected');
            headline2.old = id;
        };
        
        this.swapNext = function(){
            if(headline2.old == headline2.length-1){
                headline2.toggle(0);
            } else {
                headline2.toggle(headline2.old+1);
            }
        };
    }
    
    var headline2 = new mainHeadline2();
    headline2.toggle(0);
    
    /**
     * Ana Manşetler
     */
    function mostReadBlock(){
        this.old = null;
        this.length = $('#block-news-list li').length;
        this.wfu = new waitForUser(this);
        
        $('#block-news-list li').each(function (index, value) {
                $(value).mouseover(function () {
                    mrBlock.toggle(index);
                    mrBlock.wfu.init();
                });
        });
        
        this.toggle = function(id){
            if(mrBlock.old != null){
                $('#block-news-list li:eq('+mrBlock.old+') a').removeClass('over');
            }
            
            var item = newsData[id];
            
            $('#block-news-inner h2').html(item.title);
            $('#block-news-inner .desc').html(item.description);
            $('#block-news-inner a').attr({ href: item.link} );
            $('#block-news-inner img').attr({ src: item.image} );
            
            $('#block-news-list li:eq('+id+') a').addClass('over');
            mrBlock.old = id;
        };
        
        this.swapNext = function(){
            if(mrBlock.old == mrBlock.length-1){
                mrBlock.toggle(0);
            } else {
                mrBlock.toggle(mrBlock.old+1);
            }
        };
    }
    
    var mrBlock = new mostReadBlock();
    mrBlock.toggle(0);
	
    /**
     * wait for user
     */
    function waitForUser(obj){
        this.played = true;
        this.resume = null;
    
        this.init = function(){
            this.played = false;
            clearInterval(this.resume);
            this.resume = setInterval(this.playAgain, 9000);
        };
    
        this.playAgain = function(){
            obj.wfu.played = true;
            clearInterval(obj.wfu.resume);
        };
    }
    
    /**
     * Timer
     */
    function siteTimer(){
        this.swap = function(){
            if(headline.wfu.played){
                headline.swapNext();
            }
            if(headline2.wfu.played){
                headline2.swapNext();
            }
        };
        this.slider = setInterval(this.swap, 5000);
    }
    var timer = new siteTimer();
});
    
function commentSlide(){
    var first = 0;
    var speed = 700;
    var pause = 4000;
    var css = 'odd';
    function removeFirst(){
        first = $('ul#last-comments li:first').html();
        css = $('ul#last-comments li:first').attr('class');
        $('ul#last-comments li:first')
        .animate({opacity: 0}, speed)
        .slideUp('slow', function() {$(this).remove();});
        addLast(first, css);
    }
    function addLast(first, css){
        last = '<li style="display:none" class="'+css+'">'+first+'</li>';
        $('ul#last-comments').append(last);
        $('ul#last-comments li:last')
        .animate({opacity: 1}, speed)
        .fadeIn('fast');
    }
    interval = setInterval(removeFirst, pause);
} 

/**
 * Video Oylaması yap
 * 
 * @param id
 * @param vid
 * @param rate
 * @param isReadOnly
 */
function videoRate(id, vid, rate, isReadOnly){
    
    if(isReadOnly == undefined) isReadOnly = false;
    $('#'+id).raty({
        half: true,
        start: rate,
        readOnly: isReadOnly,
        path: '/assets/images/rating/',
        click: function(score, evt){}
    });
};


/**
 * Yorumları getir
 * 
 * @param int parent_id
 * @param int type
 */
function getComments(parent_id, comment_type, limit){
    $('#comments-list').html('<div class="loading">Yorumlar Yükleniyor... Lütfen bekleyiniz...</div>').show();
    $.post('/comment/list', {parent_id: parent_id, comment_type: comment_type, limit: limit}, function(result){
        $('#comments-list').html(result).show();
    }).error(function(err, url, line){ alert('Hata Oluştu : ' + err.message+', url : '+url+', satır : '+line); });
};


/**
 * 520x400 Popup Pencere
 * 
 * @param url
 * @param target
 * @returns {Boolean}
 */
function popup520x400(url, target)
{
    var width = 520;
    var height = 400;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    
    window.open(url, target, 'width='+width+', height='+height+', top=' + top + ', left=' + left + ', toolbar=0, location=0, directories=0, status=1, menuBar=0, scrollBars=1, resizable=0');
    return false;
}

/**
 * Popup Pencere
 * 
 * @param url
 * @param target
 * @param width
 * @param height
 * @returns {Boolean}
 */
function popup(url, target, width, height)
{
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    
    window.open(url, target, 'width='+width+', height='+height+', top=' + top + ', left=' + left + ', toolbar=0, location=0, directories=0, status=1, menuBar=0, scrollBars=1, resizable=0');
    return false;
}

/**
 * Sosyal paylaşım sitelerinde paylaşım linkleri
 * 
 * @date: 13.03.2011
 */
function addSharingLinks()
{
    var _title  = document.title;
    var _url    = document.location;
    
    var links = '<a href="http://www.facebook.com/sharer.php?u='+ _url +'" rel="nofollow" title="Facebook "target="_blank"><img src="' + BASE_URL + 'assets/images/social/facebook.png" alt="Facebook" /><span>Facebook</span></a>'
              + '<a href="http://del.icio.us/post?url='+ _url +'&amp;title='+ _title +'" rel="nofollow" title="Del.icio.us "target="_blank"><img src="' + BASE_URL + 'assets/images/social/delicious.png" alt="Delicious" /><span>Delicious</span></a>'
              + '<a href="http://twitter.com/home?status='+ _url +'" rel="nofollow" title="Twitter "target="_blank"><img src="' + BASE_URL + 'assets/images/social/twitter.png" alt="Twitter" /><span>Twitter</span></a>'
              + '<a href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk='+ _url +'&amp;title='+ _title +'" rel="nofollow" title="Google "target="_blank"><img src="' + BASE_URL + 'assets/images/social/buzz.png" alt="Buzz" /><span>Buzz</span></a>'
              + '<a href="http://digg.com/submit?url='+ _url +'&amp;title='+ _title +'" rel="nofollow" title="Digg "target="_blank"><img src="' + BASE_URL + 'assets/images/social/digg.png" alt="Digg" /><span>Digg</span></a>'
              + '<a href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+ _url +'&amp;t='+ _title +'" rel="nofollow" title="Yahoo! MyWeb "target="_blank"><img src="' + BASE_URL + 'assets/images/social/yahoo.png" alt="Yahoo" /><span>Yahoo</span></a>'
              //+ '<a href=""><img src="' + BASE_URL + 'assets/images/social/reddit.png" alt="Reddit" /><span>Reddit</span></a>'
              //+ '<a href=""><img src="' + BASE_URL + 'assets/images/social/myspace.png" alt="MySpace" /><span>MySpace</span></a>'
              + '<a href="http://friendfeed.com/share?url='+ _url +'&title='+_title+'" target="_blank"><img src="' + BASE_URL + 'assets/images/social/friendfeed.png" alt="FriendFeed" /><span>FriendFeed</span></a>';
              //+ '<a href=""><img src="' + BASE_URL + 'assets/images/social/stumbleupon.png" alt="StumbleUpon" /><span>stumbleupon</span></a>';
    
    document.write('<div class="social-links">' + links + '</div>');                   
}

/**
 * Gazeteyi aç
 * 
 * @param url
 * @param name
 * @returns {Boolean}
 */
function goNewspaper(url, name)
{
    if(name == "")
        return false;
    
    window.open(url  + name + "-gazetesi.html", "", "");
    return true;
}

/**
 * Burç
 * 
 * @param name
 * @returns {Boolean}
 */
function goBurc(name)
{
    if(name == "")
        return false;
    
    popup520x400('http://www.astroloji.org/yildizfali/'+name+'.asp', '');
}

/**
 * font boyutunu değiştir
 *
 * @param boolean isUp
 * @return bool
 */
var fontSize = 15;
function changeFontSize(isUp)
{
    var doc = document.getElementById('detail');
    
    if(isUp == true)
    {
        fontSize = fontSize + 1;
        if(fontSize >= 24)
            fontSize = 24;
    }
    else
    {
        fontSize = fontSize - 1;
        if(fontSize <= 12)
            fontSize = 12;
    }
    doc.style.fontSize = (fontSize)+"px";
    
    return false;
}
