var fc;

$(document).ready(function() {

    // SLIDER
    $('.images').cycle();
    $('.slider a').wrapInner('<span />');
    $('a.link').wrapInner('<span />');


    // ACCORDION
    $("#accordion > li > div").click(function(){
        $(this).next().slideToggle(300);
    });
    
    
    $('#accordion ul').hide();
    $('#accordion ul.active').show();
    
    $("#content .tog").click(function(){
        console.log('remove');
        $(this).parent().removeClass('content-block-hide');
    });
    
    $('#accordion ul li.active').parents('#content').find('.content-block').addClass('content-block-hide');  
    $('#accordion ul li.active').parents('.content-block:first').removeClass('content-block-hide');  

    initFancyIframe($);    
    initFancy($);    

    // GALLERY
    var start_gallery_index = 0;
    if($('.ad-thumb-list li.active').length) {
        start_gallery_index = $('.ad-thumb-list li').index($('.ad-thumb-list li.active'));
    }
    
    var galleries = $('.ad-gallery').adGallery({
        loader_image: 'sources/img/loader.gif',
        height: 500, 
        slideshow: false,
        description_wrapper: false,
        display_next_and_prev: true,
        enable_keyboard_move: true,
        display_back_and_forward:false,
        start_at_index: start_gallery_index ,
        description_wrapper: $('#descriptions')
    });
    
  
    $('#tabs div.tab').hide(); // Hide all divs
    $('#tabs div.tab:first').show(); // Show the first div
    $('#tabs .select ul li:first').addClass('active'); // Set the class of the first link to active
    $('#tabs .table ul li ul li:nth-child(2)').addClass('active');
    $('#tabs .table ul.popis li:nth-child(2)').addClass('active');
    $('#tabs .select ul li a').click(function(){ //When any link is clicked*/


        $('#tabs .select ul li').removeClass('active'); // Remove active class from all links
        $(this).parent().addClass('active'); //Set clicked link class to active
        var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
        $('#tabs div.tab').hide(); // Hide all divs
        $(currentTab).show(); // Show div with id equal to variable currentTab
        return false;
        
    });

    $('#tabs .select ul li #t1').click(function(){
        $('#tabs .table ul li ul li').removeClass('active');
        $('#tabs .table ul.popis li').removeClass('active');
      
        $('#tabs .table ul li ul li:nth-child(2)').addClass('active');
        $('#tabs .table ul.popis li:nth-child(2)').addClass('active');
    });

    $('#tabs .select ul li #t2').click(function(){
        $('#tabs .table ul li ul li').removeClass('active');
        $('#tabs .table ul.popis li').removeClass('active');
      
        $('#tabs .table ul li ul li:nth-child(3)').addClass('active');
        $('#tabs .table ul.popis li:nth-child(3)').addClass('active');
    });

    $('#tabs .select ul li #t3').click(function(){
        $('#tabs .table ul li ul li').removeClass('active');
        $('#tabs .table ul.popis li').removeClass('active');
      
        $('#tabs .table ul li ul li:nth-child(4)').addClass('active');
        $('#tabs .table ul.popis li:nth-child(4)').addClass('active');
    });



    $("ul.row1,ul.row2").each(function(){
        var max_height = 0;
        $(this).children('li').each(function(){
            if($(this).height()>max_height) {
                max_height = $(this).height();
            }
        })
        $(this).children('li').height(max_height);
    })



});

var timeout    = 0;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)

    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function()
{
    $('#jsddm > li').bind('mouseover', jsddm_open)
    $('#jsddm > li').bind('mouseout',  jsddm_timer)
});

document.onclick = jsddm_close;




function initFancy($) {
    var th = $("a[rel=popup],.fancy");
    if (th.length == 0) return;
    var fancyboxShowsUp = true;
    $("#fancybox-footer").hide();
    $.getScript('sources/js/popup.js', function() {
       fc = th.fancybox({
            'padding' 				: 0,
            'transitionIn'			: 'none',
            'transitionOut'			: 'none',
            'overlayOpacity'                    : 0.9,
            'overlayColor'                      : '#000',
            'centerOnScroll'                    : true,
            'autoScale'                         : true,
            'titlePosition'                     : 'inside',
            'hideOnOverlayClick'                : false,
            'showNavArrows'			: true,
            'autoDimensions'                    : true,
            'scrolling'                         : 'no',
            'titleFormat'			: function(title, currentArray, currentIndex, currentOpts)
            {
                $("#fancybox-footer-fb").html('');
                if($(currentArray[currentIndex]).data('title') != undefined) {
                    $("#fancybox-footer-fb").html($(currentArray[currentIndex]).data('title'));
                }
                return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            },

            'onStart'               : function(selectedArray, selectedIndex, selectedOpts)
            {
                $("#fancybox-footer").fadeIn();
                $("#fancybox-footer-counter").html((selectedIndex + 1) + ' / ' + selectedArray.length);
                fancyboxShowsUp = false;
                
            },

            'onClosed'              : function ()
            {
                
                $("#fancybox-footer").hide();
                fancyboxShowsUp = true;
            }
        });
    });

}
function initFancyIframe($) {
    var th = $(".videoframe");
    if (th.length == 0) return;
    var fancyboxShowsUp = true;
    $("#fancybox-footer").hide();
    $.getScript('sources/js/popup.js', function() {
       fc = th.fancybox({
            'type' : 'iframe',
            'width' : 933,
            'height': 525,
            'padding' 				: 0,
            'transitionIn'			: 'none',
            'transitionOut'			: 'none',
            'overlayOpacity'                    : 0.9,
            'overlayColor'                      : '#000',
            'centerOnScroll'                    : true,
            'autoScale'                         : true,
            'titlePosition'                     : 'inside',
            'hideOnOverlayClick'                : false,
            'showNavArrows'			: true,
            'autoDimensions'                    : true,
            'scrolling'                         : 'no',
            'titleFormat'			: function(title, currentArray, currentIndex, currentOpts)
            {
                $("#fancybox-footer-fb").html('');
                if($(currentArray).data('title') != undefined) {
                    $("#fancybox-footer-fb").html($(currentArray).data('title'));
                }
                return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            },

            'onStart'               : function(selectedArray, selectedIndex, selectedOpts)
            {
                $("#fancybox-footer").fadeIn();
                $("#fancybox-footer-counter").html((selectedIndex + 1) + ' / ' + selectedArray.length);
                fancyboxShowsUp = false;
                
            },

            'onClosed'              : function ()
            {
                
                $("#fancybox-footer").hide();
                fancyboxShowsUp = true;
            }
        });
    });

}

