var slide;
function showDesc() {
  if($("#desc2").css('display') == "block") {
    slide = setTimeout("bgRotate()", refreshTime);
    $("#desc2").slideUp();
  } else {
    $("#desc2").slideDown();
    clearTimeout(slide);
  }
  $("#desc2")
  .unbind('click')
  .click(function() {
    showDesc();
  })
}
function bgShow($next, $active, player) {
  index = $("#gallery div").index($next);
  $active.addClass('last-active');
  $("#g_bottom .title").html($($next.children('span')[0]).html());
  $next.css({opacity: 0.0})
  .addClass('active')
  .animate({opacity: 1.0}, 200, function() {
    $active.removeClass('active last-active');
  });
  if(player==false)
   slide = setTimeout("bgRotate()", refreshTime);
}
function bgLoad($next, $active, player) {
  $("#loader").show();
  img = new Image();
  index = $("#gallery div").index($next);
  img.src = '/upl/crop/'+bgs[index][0]+'.jpg';
  img.onload = function() {
    $next.append('<span>'+bgs[index][1]+'</span>');
    $next.css('background', '#fff url("'+img.src+'") no-repeat center center');
    $next.click(function() {
      $.fancybox({
        'href': '/upl/org/'+bgs[index][0]+'.jpg',
      'autoDimensions'  : true,
      'width' : 450, 'height' : 'auto', 'transitionIn' : 'none', 'transitionOut' : 'none'});
    });
    $("#loader").hide();
    bgShow($next, $active, player);
  };
}
function bgRotate() {
  var $active = $('#gallery div.active');
  if ( $active.length == 0 ) $active = $('#gallery div:last');
  var $next =  $active.next().length ? $active.next() : $('#gallery div:first');
  if($next.css('background-image')=="none") {
    bgLoad($next, $active, false);
  } else {
    bgShow($next, $active, false);
  }
}
function player_next() {
  clearTimeout(slide);
  var $active = $('#gallery div.active');
  if ( $active.length == 0 ) $active = $('#gallery div:last');
  $next =  $active.next().length ? $active.next() : $('#gallery div:first');
  if($next.css('background-image')=="none") {
    bgLoad($next, $active, false);
  } else {
    bgShow($next, $active, false);
  }
}
function player_prev() {
  clearTimeout(slide);
  var $active = $('#gallery div.active');
  if ( $active.length == 0 ) $active = $('#gallery div:last');
  $prev = $active.prev().length ? $active.prev() : $('#gallery div:last');
  if($next.css('background-image')=="none") {
    bgLoad($prev, $active, false);
  } else {
    bgShow($prev, $active, false);
  }
}
$(document).ready(function() {
  $("#news li > a").each(function(i) {
    $(this).click(function() {
      $("#news li div."+$(this).attr("class")).slideToggle();
    });
  });

  $("#g_bottom .left").click(player_prev);
  $("#g_bottom .right").click(player_next);

  $('a[href^="http://www.youtube.com"]').click(function() {
		$.fancybox({
			'padding'		: 0,
			'titlePosition'	: 'inside',
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 500,
			'height'		: 405,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {'wmode': 'transparent', 'allowfullscreen': 'true'},
			'overlayOpacity': 0.8,
			'overlayColor'	: '#000'
		});
		return false;
	});
  $('a[rel=lightbox]').fancybox({
    'titleShow'     : true,
    'showCloseButton' : false,
    'titlePosition' : 'inside',
    'transitionIn'  : 'elastic',
    'transitionOut' : 'elastic',
    'easingIn'      : 'easeOutBack',
    'easingOut'     : 'easeInBack',
    'overlayOpacity': 0.8,
    'overlayColor': '#000000'
  });
  $('a[rel=video1]').click(function() {
    $.fancybox('<div style="width:400px; height:320px;"><span id="flowplayer"></span><script type="text/javascript">flowplayer("flowplayer", "/flowplayer-3.2.5.swf", "'+$(this).attr("href")+'");</script></div>', {'overlayOpacity': 0.8, 'overlayColor': '#000000'});
    return false;
  });

  $("#menu li").hover(function() {
    top = $("#menu li").index(this)-1;
    if(top==-1) top=0;
    if(top!=-2) $("#menu li.cursor").stop().animate({'top': (top*56+10+25)+'px'});
  });
  //p = $("#menu li a.a").parent();
  //top = $("#menu li").index(p)-1;
  //if(top==-1) top=0;
  //if(top!=-2) $("#menu li.cursor").stop().animate({'top': (top*56+10+25)+'px'});
});

