var galleryScrollHeight = 350;

function checkScrollUp(){
  var top = $('.gallery-scroll').scrollTop();
  if (top <= 0){
    $('.arrow-up').css('opacity', 0.4);
  } else {
    $('.arrow-up').css('opacity', 1);
  }
}

function checkScrollDown(){
  var top = $('.gallery-scroll').scrollTop();
  if (top >= $('.gallery-scroll-content').height() - galleryScrollHeight){
    $('.arrow-down').css('opacity', 0.4);
  } else {
    $('.arrow-down').css('opacity', 1);
  }
}


$(document).ready(function(){
  $("a[href^='http']").each(function(){
      if ($(this).attr('href').substr(0, 17) != 'http://www.brinox'){
          $(this).attr('target','_blank');
      }     
  });
  $('ul.left-menu > li').find('ul,hr').hide();
  $('ul.left-menu .current').parent().find('ul,hr').show();

  
    $("#eumap area").click(function() {
        $(".map").hide();

        if ($(this).attr("href").substring(0, 1) == "#") {
            $(".map-" + $(this).attr("href").substring(1)).show();
        } else {
            return true;
        }
        return false;
    });
    $(".map").hide();


  /*
  $('.category .category-title').click(function(){
    $('.sitemap-menu-ul', this).hide();
    $('.sitemap-menu').addClass('hover');
    $('.sitemap-menu-ul', this).fadeIn(1500);
    return false;
  });
*/

  $('.program-menu-root > a').bind('mouseenter', function(){


    $(this).next().each(function(){
      if (!$(this).is(':visible')){
        var h = $(this).height();
        $(this).css({height:0,width:220}).animate({height:h}, {duration: 200, easing: 'easeInSine'});
      }
    });
  });

  $('.program-menu-root').bind('mouseleave', function(){
    $(this).children('ul').fadeOut(200);
  });

  



  $('.poll-holder form').submit(function(){
    var data = $(this).serialize();
    var pollObj = $(this);
    if (data.length > 0){
      $.cookie('poll'+$(pollObj).attr('action'), true, {
        expires:2,
        path: '/'
      });
    }
    
    $('.poll-button input', this).attr('disabled', true);
    $.post($(this).attr('action'), data, function(result){
      $('.poll-button', pollObj).remove();
      
      var pollResult = eval(result);
      $('.poll-row', pollObj).each(function(i){
        $(this).empty().append('<div class="option-name">'+pollResult[i][2]+' ('+pollResult[i][1]+'%)'+'</div>');
        $(this).append('<div class="option-bar">');        
        $('.option-bar', this).css({
          'background-color':'red',
          'width':0
        }).animate({
          width:pollResult[i][1]+'%'
        });
      });
    });
    return false;
  });
  var pollAction = 'poll'+$('.poll-holder form').attr('action');  
  if ($.cookie(pollAction) != null){
    $('.poll-holder form').trigger('submit');
  }
  /*
  $('.gallery').jcarousel({
    visible:4,
    vertical: true
  });
  */

  $('.arrow-down').click(function(){
    var top = $('.gallery-scroll').scrollTop() + galleryScrollHeight;
    $('.gallery-scroll').animate({
      scrollTop: top
    }, {
      duration: 550,
      complete:function(){
        checkScrollUp();
        checkScrollDown();
      }
    });
  });

  $('.arrow-up').click(function(){
    var top = $('.gallery-scroll').scrollTop() - galleryScrollHeight;
    $('.gallery-scroll').animate({
      scrollTop: top
    }, {
      duration: 550,
      complete:function(){
        checkScrollUp();
        checkScrollDown();
      }
    });
  });

    //$('.gallery-scroll .image-thumb a').prettyPhoto({slideshow:5000, autoplay_slideshow:false});
    $("a.video").prettyPhoto();
    //$(".prettyPhoto-hidden a").prettyPhoto({slideshow:5000, autoplay_slideshow:false});


  //$('.image-thumb a, a.video').prettyPhoto();

  $('.gallery-scroll').scrollTop(0);
  checkScrollUp();
  checkScrollDown();
  $('.program-menu ul').hide();
  $('.program-menu .selected').parents('ul').show().each(function(){
    $(this).prev('a').addClass('selected-parent');
  });
  $('.program-menu .selected').next('ul').slideDown();
  $('ul.left-menu .folder').click(function(){
    if ($(this).parent().find('hr,ul').is(':visible')){
      return;
    }
    $('ul.left-menu > li').find('ul,hr').slideUp();

    $(this).parent().find('hr,ul').each(function(){
      if ($(this).is(':visible')){
        $(this).slideUp();
      } else {
        $(this).slideDown();
      }
    });
  });

    $(".ad-gallery").adGallery({
         loader_image: 'images/loader.gif',
         width: 600,
         height: 400,
        display_next_and_prev: true,
        display_back_and_forward: true
});

    $(".ad-holder").css({left: (($(window).width() - 660) / 2)+"px", top: $(window).scrollTop()+"px"});
    $('<div class="ad-holder-overlay-background"></div>').insertBefore(".ad-holder");
    $(".ad-holder-overlay-background").css("opacity", 0.8).add(".ad-gallery .close-button").click(function(){
        $(".ad-holder-overlay-background").fadeOut();

        $(".ad-holder").css({visibility: "hidden", "z-index": -9999});
    });
     $(".ad-holder-overlay-background").hide();
    $(".image-holder a").click(function(){
        $(".ad-holder").css({left: (($(window).width() - 660) / 2)+"px", top: ($(window).scrollTop() + 100) +"px"});
         $(".ad-holder-overlay-background").show();
        $(".ad-holder").css({visibility: "visible", "z-index": 9999});
        $(".ad-thumb-list a").eq($(".image-holder a").index(this)).click();
        return false;
    });
//    $(".ad-image").click(function(){
//        var img = this;
//        alert("click");
//        $(".ad-thumb-list a").each(function(i){
//            if ($(img).attr("src") == $(this).attr("href")){
//                window.open($(this).attr("rel"),'_blank');
//            }
//        });
//
//    });
});


