var URL = '/';

function jcarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

$(function () {
    $("a[rel^='gallery']").prettyPhoto({
        theme:'dark_square',
        showTitle: false
    });

    $('.slideshow').cycle();
    
    $('#stage').cycle({
      timeout: 6000
    });
    

    $('#campaign ul').jcarousel({
        scroll: 1,
        auto: 5,
        wrap: 'circular',
        initCallback: jcarousel_initCallback
    });
    
    $('#header ul:first li').mouseenter(function () {
      var submenu = $('ul', this);
      if (submenu.length === 0) {
        return false;
      }  
      var submenuRight = submenu.parent().offset().left + submenu.outerWidth();
      var menuRight = $('#header ul:first').children('li:last').offset().left + $('#header ul:first').children('li:last').outerWidth();      
      var diff = submenuRight - menuRight;
      
      if (diff > 0) {
        submenu.css('left', -diff);
      }
    });

    
    $('#models .category').parent().mouseenter(function () {
      var models = $('.models', this);
      var modelsRight = models.parent().offset().left + models.outerWidth();
      var menuRight = $('#models').offset().left + $('#models').outerWidth();
      var diff = modelsRight - menuRight;
      if (diff > 0) {
        models.css('left', -diff);
      }
    });
    

    $("a[id^='pool-']").click(function () {
        var a, id;
        a = $(this)
        id = parseInt(a.attr('id').split('-')[1], 10);

        $.post(URL + 'poll/vote.json', {id: id}, function (data) {
            if (data.error === undefined) {
                var results = a.parent().parent().find('.pool-result');
                var votes = parseInt(results.text(), 10);
                results.text(votes + 1);
            } else {
                alert('V tejto ankete ste už hlasovali!');
            }
        }, 'json');
    });

/*
    $("div[id^='news-']").click(function () {
        var id = parseInt($(this).attr('id').split('-')[1], 10);
        $.ajax({
            url: URL +'cms/newsDetail',
            type: 'get',
            data: {id: id},
            dataType: 'html',
            success: function (data) {
                $("#popup").html(data);
            }
        });

        $("body").append('<div id="popup"><div class="loading">Načítavam...</div></div>');

        $("#popup").dialog({
            bgiframe: true,
            resizable: false,
            modal: true,
            width: 900,
            height: 600,
            dialogClass: 'dialog',
            close: function(event,ui){
                $("#popup").remove();
            }
        });
    });
*/
    $("#cars .car").click(function () {
        var href = $('a', this).attr('href');

        window.location = href;
    });
});
