

// -------------------------------------------------
// Sidebar follows results list (category, serp, video covers templates)

function sideboxTopOffset() {
    return ($('.sidebar-facets').offset().top - 20) - parseFloat($('.sidebar-facets').css('margin-top').replace(/auto/, 0));
}

function fixCategoryBox() {
    if ($('.sidebar-facets').length) {
        var msie6 = $.browser == 'msie' && $.browser.version < 7;
        if (!msie6) {
            var top = sideboxTopOffset();
            $(window).scroll(function() {
               sidebarFollowingBox(top); 
            });
        };
    };
};

function sidebarFollowingBox(top) {
    var y = $(window).scrollTop();
    if (y >= top) {
        
        maxOffsetTop = parseInt(top + $('#content .grid').height() - $('.sidebar-facets').height() - 120);
        
        if ( y > maxOffsetTop) { // 36px is top and bottom padding of #content .grid
            $('.sidebar-facets').addClass('absolute');
            $('.sidebar-facets').removeClass('fixed');
        } else {
            $('.sidebar-facets').slideDown();
            $('.sidebar-facets').addClass('fixed');
            $('.sidebar-facets').removeClass('absolute');
        }
        
    } else {
        $('.sidebar-facets').removeClass('fixed');
        $('.sidebar-facets').removeClass('absolute');
    };
};



// -------------------------------------------------
// More results big button (Video covers templates)

$(function() {
    $('.covers .more-link .button, .the-results .more-link .button').click(function() {
        var element = $(this);
        var params = element.attr('href').slice(element.attr('href').indexOf('?') + 1);
        
        if (params.indexOf('type=video') != -1) {
            var typeUrl = '/templates/snippets/video-covers.php';
            var targetContainer = '.covers .more-link';
        }
        
        if (params.indexOf('type=magazin') != -1) {
            var typeUrl = '/templates/snippets/serp-magazine.php';
            var targetContainer = '.the-results .more-link';
        }
        
        if (params.indexOf('type=tv') != -1) {
            var typeUrl = '/templates/snippets/serp-tv.php';
            var targetContainer = '.the-results .more-link';
        }
        
        $.ajax({
            type: 'GET',
            url: typeUrl,
            data: params,
            cache: false,
            success: function(html){
                $(html).fadeIn().insertBefore($(targetContainer));
                var top = sideboxTopOffset();
                sidebarFollowingBox(top);
            }
        });
        
        // presložiti parametre da dohvaćaju novi upit, npr:
        // var new_params = '?sort=&category=&item_from=30&item_to=40&how_much=7';
        
        var new_params = params;
        element.attr('href', new_params);
        
        return false;
    });
});



// -------------------------------------------------
// Smooth scroll within the same page

$(function() {
    $('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});



// -------------------------------------------------
// replace ugly generic checkboxes and radio buttons

var setupLabel = function() {
    if ($('.label-check input').length) {
        $('.label-check').each(function(){ 
            $(this).removeClass('c-on');
        });
        $('.label-check input:checked').each(function(){ 
            $(this).parent('label').addClass('c-on');
        });                
    };
    if ($('.label-radio input').length) {
        $('.label-radio').each(function(){ 
            $(this).removeClass('r-on');
        });
        $('.label-radio input:checked').each(function(){ 
            $(this).parent('label').addClass('r-on');
        });
    };
};


// -------------------------------------------------
// Space shuttle carousel

(function($) {

    $.fn.cn_crossFade = function(options) {
        
        var defaults = {			
    		speed: 1000,
            pause: 3000
    	};
	
        var options = $.extend(defaults, options);
        
        this.each(function(){
            
            // ul
            var obj = $(this);
        
            function cn_crossSlide(x) { 
            
                $('li.slideshow-item:animated', obj).stop();
    
                var iteLen = $('li.slideshow-item', obj).length;
                var indLen = $('.indicators li').length;
    
                var toIndex = parseInt(x);
                var nextIndex = toIndex != (iteLen - 1) ? parseInt(toIndex + 1) : 0;
                var prevIndex = toIndex != 0 ? parseInt(toIndex - 1) : parseInt(iteLen - 1);
                
                var prevObj = $('li.slideshow-item', obj).get(prevIndex);
                var currentObj = $('li.slideshow-item', obj).get(toIndex);
                
                $(prevObj).fadeOut('fast');
                $('.ambilite').fadeOut('slow', function(){
                    
                    if ($(currentObj).hasClass('ad')) {
                        $('.slideshow-controls, .primary-video .desc, .editors-pick, #promo-channel').fadeOut();
                    } else {
                        if ($('.slideshow-controls:hidden, .primary-video .desc:hidden, .editors-pick:hidden, #promo-channel:hidden').length) {
                            $('.slideshow-controls, .primary-video .desc, .editors-pick, #promo-channel').fadeIn();
                        };
                    };

                    $(currentObj).fadeIn('fast');
                    $('.ambilite').css('background-color', '#' + $(currentObj).attr('rel'));
                    $('.ambilite').fadeIn('slow');                
                    cn_updateIndicators(toIndex);
                    
                });
                
                crossSlideTimeout = setTimeout(function(){
    				cn_crossSlide(nextIndex);
    			}, options.pause);
    
            }

            function cn_updateIndicators(x) {
                $('.indicators li').each(function(){
                    $(this).removeClass('current');
                });
                $($('.indicators li').get(x)).addClass('current');
            }
            
            var items = $('li.slideshow-item', obj);
            items.hide();
            var parent = $(obj).parent();
            parent.append('<div class="slideshow-controls"><div class="inner"></div></div>');
            $('.slideshow-controls .inner', parent).append('<ul class="indicators"></ul>');
            //$('.slideshow-controls .inner', parent).append('<ul class="slideshow-prev-next"><li class="prev-slide"><a href="#" onclick="alert(); return false;">«</a></li><li class="next-slide"><a href="#" onclick="alert(); return false;">»</a></li></ul>');
            $('.slideshow-controls .inner', parent).append('<ul class="slideshow-prev-next"><li class="prev-slide"><a href="#" onclick="return false;">«</a></li><li class="next-slide"><a href="#" onclick="return false;">»</a></li></ul>');
            
            items.each(function(i){
                //$('.indicators', parent).append('<li><a href="#" onclick="alert(' + i + '); return false;">' + (i+1) + '</a></li>');
                $('.indicators', parent).append('<li><a href="#" onclick="return false;">' + (i + 1) + '</a></li>');
               });
            items.first().show();
            $($('.indicators li').get(0)).addClass('current');
      
            $('.indicators li a').each(function(index) {
            	jQuery(this).click(function() {
            		clearTimeout(crossSlideTimeout);
            		cn_crossSlide(index);
            	});
            });
            crossSlideTimeout = setTimeout(function(){
    			cn_crossSlide(1);
    		}, options.pause);    
        });
    };
    
})(jQuery);



// -----------------------------------
// Display instant notifications (without reloading the page)

var notificationTimeout = null;

var update_event = function(action, on) {
    clearTimeout(notificationTimeout);
    $.ajax({
        type: 'get',
        url: '/templates/user/user-update.php',
        data: 'action=' + action + '&on=' + on,
        success: function(msg){
            $('#feedback:animated').stop();
            $('#feedback').fadeIn(200);
            $('#feedback .inner').html(msg);
            notificationTimeout = setTimeout(function(){
                $('#feedback').fadeOut(200);
            }, 1000);
        }
    });
};



// -----------------------------------
// Select or unselect all checkboxes in a given object
// with a single click

var select_deselect_all_checkboxes = function(obj, bool) {
    var obj = (typeof(obj) == undefined) ? $('body') : obj;
    $("input[type='checkbox']:not([disabled='disabled'])", obj).attr('checked', bool);
    if (bool == true) {
        if ($.browser.msie) { 
            $('.single-show').removeClass('dim');
        } else {
            $('.single-show').fadeTo('fast', 1);
        }
    } else {
        if ($.browser.msie) { 
            $('.single-show').addClass('dim');
        } else {
            $('.single-show').css('opacity', '0.2');
        }
    }
};


