var _blank = function(){};
if(typeof window['console']=='undefined') var console = { log:function(){}, dir:function(){}, error:function(){}, info:function(){} };

// if(typeof(enj)=='undefined') enj = {};
// $(function(){ $.extend(enj,enj_base); enj.init_base() });

var enj = {
  urls: {
    base: 'http://admiral.newhosting.com.ua', // site root, without trailing slash
    current: location.href.split('?')[0] // current url wo params (before '?' sign)
  },
	
  init_base: function(){
//     $.post(enj.urls.current, {mode:'get-menu'}, enj.menuinit);
    // $('head').append($('<link type="text/css" href="'+enj.urls.base+'/style-js.css" rel="stylesheet" media="screen" />'));
    $('body').addClass('js');
//     if($.cookie('reload_confirm')=='true') { $.cookie('reload_confirm', 'false'); $.cookie('reload', 'false'); location.reload(); }
//     if($.cookie('reload')=='true') { $.cookie('reload', 'false'); $.cookie('reload_confirm','false'); }
    $.extend($.blockUI.defaults, { message: ''}); // , overlayCSS: { opacity: 0.3, cursor: 'wait'}
		enj.messages();
    enj.scroller.init();
		enj.gallery.init();
		enj.go_to.init();
  },
	
	go_to: {
		init: function(){
			$('.go_to')//.focus(function(){$(this).blur();})
        .change(function(){location.href = $(this).val()?$(this).val():'javascript:;';});    
		}
	},
  
  scroller: {
    pos: 0, // last scroller position
    init: function(){
      $.easing.easeInCubic = function (x, t, b, c, d) {return c*(t/=d)*t*t + b;};
      enj.scroller.container = '';
      enj.scroller.container = $('#content_ifr').contents().find('.scroller');
      if(!enj.scroller.container.length) enj.scroller.container = $('.scroller');
      if(!enj.scroller.container.length) return;
      enj.scroller.items = $('.scroller-page').length;
      if(enj.scroller.items<2) return;
      enj.scroller.pagination = $('.pagination').css({display:'block'});
      enj.scroller.pagination_build();
      enj.scroller.pagination.find('.controls .prev a').click(function(){enj.scroller.container.trigger('prev')});
      enj.scroller.pagination.find('.controls .next a').click(function(){enj.scroller.container.trigger('next')});
      enj.scroller.container.serialScroll({
        items:'.scroller-page', axis:'y', cycle:false, step:1, constant:false, easing:'easeInCubic', lazy:true,
        duration:750,
        onBefore: function(e, elem, $pane, $items, pos) {
          //console.log('onBefore fired!!!');
          if(enj.scroller.items != $items.length) enj.scroller.pagination_build();
          enj.scroller.pos = pos;
          var links = enj.scroller.pagination.find('.controls li');
          links.removeClass('active').eq(pos+1).addClass('active');
          if(pos>0) links.eq(0).removeClass('disabled');
          if(pos==0) links.eq(0).addClass('disabled');
          if(pos<$items.length-1) links.eq($items.length+1).removeClass('disabled');
          if(pos==$items.length-1) links.eq($items.length+1).addClass('disabled');
        }
      });
      // enj.scroller.container.trigger('goto', [enj.scroller.pos]); // revert state on reinit

      // Add mouse wheel behavior
      enj.scroller.container.mousewheel(enj.scroller.mousewheel_handler);
			
			// Add global keyboard events
			$(document).keyup(function(e){
        if(!e.ctrlKey) return true;
        switch(e.keyCode){
          case 37: if(typeof enj.gallery['container'] != 'undefined')
						enj.gallery.container.trigger('prev'); break;
					case 38: if(typeof enj.scroller['container'] != 'undefined')
						enj.scroller.container.trigger('prev'); break;
          case 39: if(typeof enj.gallery['container'] != 'undefined')
						enj.gallery.container.trigger('next'); break;
					case 40: if(typeof enj.scroller['container'] != 'undefined')
						enj.scroller.container.trigger('next'); break;
        }
        return false;
      });
    },
    
    pagination_build: function(){
      enj.scroller.pagination.find('.controls').children().not('.prev,.next').remove();
      enj.scroller.container.children().children().each(function(index){
        enj.scroller.pagination.find('.controls .next').before($('<li'+(index==0?' class="active"':'')+'></li>').append(
          $('<a href="javascript:;">'+(index+1)+'</a>').click(function(){enj.scroller.container.trigger('goto',[index]);})
        ));
      });
    },
		
		mousewheel_handler: function(event, delta){
      if(!$.browser.opera){
				if(delta > 0)	$(this).trigger('prev');
				else if(delta < 0) $(this).trigger('next');
			} else {
				if(delta > 0)	$(this).trigger('next');
				else if(delta < 0) $(this).trigger('prev');
      }
			return false; // prevent default			
		}
  },
  
  gallery: {
  	init: function(){
			if(!$('.gallery').length) return;
			enj.gallery.container = $('.gallery').serialScroll({
				items:'a', axis:'x', cycle:false, step:1, constant:false, easing:'easeInCubic', /*lazy:true,*/
				duration:250,
				onBefore: function(e, elem, $pane, $items, pos) {
					if(e.type == 'next' && $('.gallery-wrap').width()-$('.gallery').get(0).scrollLeft<$('.gallery').width()) return false;
        }
			}).mousewheel(enj.scroller.mousewheel_handler);
			$('.gallery a').click(function(){
				if(!$('#gallery-placer').length) enj.gallery.placer_init();
				$('#loading').show(0);
				var placer = $('#gallery-placer');
				placer
					.children().addClass('old').end()
          .click(function(){$(this).fadeOut('slow',function(){placer.remove()});})
					.append($('<img src="'+this.href+'" />')
					  .css({top:'1000px'})
            .load(function(){
							$('#loading').hide();
							placer.find('.old').fadeOut('slow');
							$(this).css({
								display:'none',
	              top:(placer.height()-$(this).height())/2+'px',
	              left:(placer.width()-$(this).width())/2+'px'
							}).fadeIn('slow',function(){placer.find('.old').remove()});
						})
					)
				return false;
			});
		},
		
		placer_init:function(){
			if($('#gallery-placer').length) return;
			$('#content-reg').append($('<div id="gallery-placer" class="section"></div>').css({display:'none'}).fadeIn('slow'));
		}
  },

  messages: function(){
    $('.message').animate({opacity: 1.0}, 2000).fadeOut(1000);
  }
}
$(function(){ enj.init_base() });


// Admiral site specific:
$(function(){
  $('#content, #content_parent, .gallery').css({overflow:'hidden'});
});