
$(document).ready(function () {

    if ($('#page_13 #news').length > 0) {
        $('#news').fadeIn(2000)

        $('#news #close').click(function () {

            if ($('#news').css('left') == '-570px') {
                $('#news #close').html("<")
                $('#news').animate({
                    left: 48
                }, 300)
            } else {
                $('#news #close').html(">")
                $('#news').animate({
                    left: -570
                }, 300)
            }

        })

    }


    $('#law').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    });

    footer();
    hoverEffects();

    if ($('#cycle').length > 0) {
        resize();
        slideshow();
        keyprs();
    }

})

$(window).resize(function() {
        footer();
        resize();
});


function footer() {
    var windowHeight = $(window).height();
    var containerHeight = $('#container').height();
    var footerHeight = $('#footer').height();
    var totalHeight = windowHeight - (containerHeight + footerHeight);
    if (totalHeight >= 0) {
        $('#footer').css('top', totalHeight - 8);
    }
    else {
        $('#footer').css('top', 0);
    }
}

function hoverEffects() {
    $('#villaarmena, #fcbk, #twtt, #trdv, #flck').hover(function() {
        $(this).fadeTo(200, '0.8')
    }, function() {
        $(this).fadeTo(200, '1')
    })
}

function resize() {
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var headerHeight = $('#header').height();
    var footerHeight = $('#footer').height();
    var totalHeight = windowHeight - (headerHeight + footerHeight)-15;
    $('#slideshow').css('height', totalHeight);
    
    var ratio = 2.28; //1280x562
    var minWidth = 980;

    var width = totalHeight * ratio;
    var w = windowWidth > minWidth ? windowWidth : minWidth;
    var h = w / ratio;

    $('#slideshow').css('width', w);
    
    $('#cycle img').css('width', w);
    $('#cycle img').css('height', h); //ie8 bug
    
    if (totalHeight > h) {
        $('#cycle img').css('height', totalHeight);
        $('#cycle img').css('width', width);
    }

    $('#navCycle').css('top', totalHeight / 2);

    //single image
    if ($('#cycle img.sigleImg').length > 0) {
        $('#cycle img.sigleImg').show()
        $('#navCycle').hide();
        if ($('#img1Cap').length > 0) {
            $('#capCycle').show().css('opacity', '0.80').css('top', 0)
            $('#capCycle p').html($('#img1Cap').html());
        }
    }
    
}

function slideshow() {
    $('#cycle').cycle({
        fx: 'scrollHorz',
        speed: 900,
        timeout: 7000,
        next: '#prv',
        prev: '#nxt',
        easing:  'easeInOutCubic',
        after: onAfter,
        before: onBefore
    });
}

function keyprs() {

    $("#nxt, #prv").click(function() {
        $('#cycle').cycle('pause');
    })
    
    $(document).keydown(function(e) {
        if (e.keyCode == 37) { //left
            $("#nxt").trigger('click').parent().addClass('trigged');
            return false;
        }
        if (e.keyCode == 39) { //right
            $("#prv").trigger('click').parent().addClass('trigged');
            return false;
        }

        if (e.keyCode == 38) { //top
            $("#openZn").trigger('click').addClass('trigged2');
            return false;
        }
        if (e.keyCode == 40) { //bottom
            $("#closeZn").trigger('click').addClass('trigged2');
            return false;
        }

    });
    
    $(document).keyup(function(e) {
        if (e.keyCode == 37) { //left
            $("#nxt").parent().removeClass('trigged');
            return false;
        }
        if (e.keyCode == 39) { //right
            $("#prv").parent().removeClass('trigged');
            return false;
        }
    })

    
}

function onAfter(curr, next, opts) {
    if ($('#' + this.id + 'Cap').length > 0) {
        $('#capCycle').fadeIn('fast').animate({
            top: '0',
            opacity: 0.80
        }, 200);
        $('#capCycle p').html($('#' + this.id + 'Cap').html());
    }
}

function onBefore(curr, next, opts) {
    $('#capCycle').animate({
        top: '-29',
        opacity: 1
    }, 200);
}

function closecvs() {
        
        $('html, body').css('overflow', 'hidden');
        $('#canvas').animate({
            bottom: '-182'
        }, 200);

       $('#closeZn').hide();
       $('#openZn').show();
       $('#btns').fadeIn();
   }

   function opencvs() {

       $('#canvas').animate({
           bottom: '29'
       }, 200, function() {
            $('html, body').css('overflow', 'auto');
       });

       $('#openZn').hide();
       $('#closeZn').show();
       $('#btns').fadeOut();
   }
