﻿//var siteURL='';

/* use styles for javascript on */
document.documentElement.id = 'js';

/* MSIE js fixes */
jQuery(function($) {
  if($.browser.msie) {
    /* Turn BackgroundImageCache on */
    try {
      document.execCommand('BackgroundImageCache', false, true);
    } catch(e) {}
    
    /* Force reflow */
    $('body')
      .css('padding', '10px')
      .removeAttr('style')
  }
});


/* jqEm */
new function(){var Public={'auto':function(bool){return bool!=undefined?Private.auto=bool:Private.auto;},'init':function(){return Private.init();},'bind':function(callback){return Private.bind(callback);},'unbind':function(callback){return Private.unbind(callback);},'trigger':function(force,args){return Private.trigger(force,args);},'active':function(){return Private.active;},'delay':function(milliseconds){return milliseconds?Private.delay=milliseconds:Private.delay;},'start':function(){return Private.start();},'stop':function(){return Private.stop();},'current':function(){return Private.current;},'previous':function(){return Private.previous;}};$.jqem=Public;var Private={'$em':$(document.createElement('i')),'active':false,'canExp':undefined,'current':undefined,'delay':100,'iid':undefined,'previous':undefined,'auto':true,'init':init,'bind':bind,'unbind':unbind,'trigger':trigger,'update':function(){Private.trigger(false);return'1em';},'start':start,'stop':stop};$(document).ready(function(){if(Private.auto)init();});function init(){$('body').prepend(Private.$em.css({'display':'block','left':'-1em','position':'absolute','visibility':'hidden','width':'1em'}));Private.canExp=(Private.$em.style!=undefined&&Private.$em.style.setExpression!=undefined);Private.start();};function bind(callback){Private.$em.bind('emchange',callback);};function unbind(callback){Private.$em.unbind('emchange',callback);};function trigger(force,args){if(force==undefined)force=false;if(force||Private.$em.width()!=Private.current){Private.previous=Private.current;Private.current=Private.$em.width();$.event.trigger('emchange',args);}};function start(){if(Private.active)return;Private.current=Private.previous=Private.$em.width();if(Private.canExp){Private.$em.style.setExpression('width','$.jqem.update();');}else{Private.iid=window.setInterval(Private.update,Private.delay);}Private.active=true;};function stop(){if(!Private.active)return;if(Private.canExp){Private.$em.style.removeExpression('width');}else{window.removeInterval(Private.iid);}};}();


/* Tabs */
jQuery(function($) {
  $('.b-tabs__button').click(function() {
    var $this = $(this);
    //var $tabs = $(this).parents('.b-tabs').eq(0);
    if(!$this.is('.b-tabs__button_active')) {
      $this
        .siblings('.b-tabs__button_active')
          .removeClass('b-tabs__button_active')
          .end()
        .addClass('b-tabs__button_active')
        .siblings('.b-tabs__content_active')
          .removeClass('b-tabs__content_active')
          .end()
        .next('.b-tabs__content')
          .addClass('b-tabs__content_active')
    }
  });
});


/* Collapsible */
jQuery(function($) {
  var slideTime = 200;
  
  $('.b-collapsible__toggle').click(function() {
    var $p = $(this).parents('.b-collapsible').eq(0);
    var $c = $p.find('.b-collapsible__content');
    if($p.is('.b-collapsible_collapsed')) {
      $c
        .hide()
        .slideDown(slideTime);
      $p
        .removeClass('b-collapsible_collapsed')
        .addClass('b-collapsible_expanded')
    } else {
      $c
        .show()
        .slideUp(slideTime);
      $p
        .removeClass('b-collapsible_expanded')
        .addClass('b-collapsible_collapsed')
    }
  })
});


/* Tape gallery */
jQuery(function($) {
  var scrollTime = 3000;
  var scrollItems = 5;
  var easing = 'easeInOutSine';
  
  $('.b-tape-gallery').each(function() {
    var $tg = $(this);
    var $v = $tg.find('.b-tape-gallery-inner');
    var $prev = $tg.find('.b-control_left .b-icon');
    var $next = $tg.find('.b-control_right .b-icon');
    
    var tapeWidth = $v.find('.b-photo-tape')[0].offsetWidth;
    var itemWidth = $v.find('.b-photo-tape .b-photo-tape__item:last')[0].offsetWidth;
    var stepWidth = scrollItems*itemWidth;
    var scrollWidth = 0;
    
    function update() {
      scrollWidth = $v[0].scrollWidth - $v[0].offsetWidth;
    }
    update();
    
    function check() {
      update();
      
      $next.removeClass('disabled');
      $prev.removeClass('disabled');
      
      if($v[0].scrollLeft == 0) {
        $prev.addClass('disabled');
      } else if($v[0].scrollLeft == scrollWidth) {
        $next.addClass('disabled');
      }
    }
    check();
    
    function next() {
      update();
      
      $prev.removeClass('disabled');
      
      var to = $v[0].scrollLeft + stepWidth;
      if(to >= scrollWidth) {
        to = scrollWidth;
      }
      if(to == $v[0].scrollLeft) {
        return false;
      }
      $v
        .stop()
        .animate({scrollLeft: to}, scrollTime, easing, check);
    }
    
    function prev() {
      update();
      
      $next.removeClass('disabled');
      
      var to = $v[0].scrollLeft - stepWidth;
      if(to <= 0) {
        to = 0;
      }
      if(to == $v[0].scrollLeft) {
        return false;
      }
      $v
        .stop()
        .animate({scrollLeft: to}, scrollTime, easing, check);
    }
    
    $prev.click(prev);
    $next.click(next);
  });
});


/* Tape gallery popup */
jQuery(function($) {
  var fadeTime = 200;
  
  $('.b-photo-tape__item>a').each(function() {
    var $this = $(this);
    var $g = $this.parents('.b-tape-gallery');
    var $p = $this.siblings('.b-popup-panel').hide();
    $p
      .insertAfter($g)
      .find('.corners')
        .css('width', $p.width() + 'px')
        .end();
    /*$p.find('.b-popup-panel__i').width($p.width()-22);*/
    $this.hover(function(e) {
      $pWidth=$p.width();
      /*$p.find('.b-popup-panel__i').width($pWidth-22);*/
      $lmargin=$pWidth/2 - $this.width()/2;
      $p
        .css({left:$this.offset().left-$('.l-page__3').offset().left,top:$this.offset().top})
        .css('margin-top', -($p.height()-49) + 'px')
        .css('margin-left', -$lmargin + 'px')
        .show();
      $overflow=parseInt($p.css('left'))+parseInt($('.l-page__3').offset().left)+$pWidth-$lmargin-parseInt($('.m-page').width())+10;
      function getBodyScrollLeft() {
  		return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
		}
      
      /*alert(getBodyScrollLeft());*/
      if ($overflow>0) $p.css('margin-left', -($lmargin+$overflow-getBodyScrollLeft()) + 'px');
      $p.width($pWidth);
    }, function(e) {
      var $parent = $(e.relatedTarget).parents('.b-popup-panel').eq(0);
      if(!($parent.length && $parent[0]==$p[0])) {
        $p.hide();
      }
    });
    
    $p
      .mouseleave(function(e) {
        var $parent = $(e.relatedTarget).parents('a').eq(0);
        if(!($parent.length && $parent[0]==$this[0])) {
          $(this).hide();
        }
      });
  });
});


/* nyroModal */
jQuery(function($) {
  if($.nyroModalSettings) {
    $.nyroModalSettings({
      padding: 20,
      autoSizable: false,
      minWidth: 400,
      minHeight: 300
    });
    
    $('.b-gallery a')
      .add('.b-image a')
        .attr('rel', 'gallery')
        .nyroModal();
  }
});


