var ie6 = $.browser.msie && $.browser.version < 7;

$(function(){
  $(window).bind('resize', resizeMainPic);
  if (ie6){
    $(window).bind('resize', resizeMovie);
  }
  $('.main-glow').css('display', 'block');
  setTimeout(function(){
    $.sidesMax = $('#right').offset().top + $('#right').height() > $('#left').offset().top + $('#left').height() ? $('#right').offset().top + $('#right').height() : $('#left').offset().top + $('#left').height();
  }, $.browser.chrome || $.browser.safari || $.browser.opera ? 400 : 0);

  $(window).bind('resize', resizeMovie);
});

function resizeMainPic(){
  var mainPicWidth = 1200;
  var mainGlowWidth = 1238;
  var mainGlowHeight = 122;
  var mainPadding = 19;

  $('#info').width($('body').innerWidth() < 1122 ? 250 : 280);

  if (!document.getElementById('main')){
      return resizeContentGlow();
  }

  var cw = ie6 ? document.documentElement.clientWidth : document.body.clientWidth;
  if (cw >= mainGlowWidth){
    $('#main').width(mainPicWidth);
    $('#main').css('padding', mainPadding);
    $('.main img').width(mainPicWidth);
    $('.main-glow-bottom').height(mainGlowHeight);
    $('.main-glow-bottom img').css('top', mainGlowHeight);
    $('.main-glow').css({
      'left': 0,
      'width': mainGlowWidth
    });
  } else if (cw < mainGlowWidth && cw >= mainPicWidth){
    var newPadding = (cw - mainPicWidth) / 2;
    $('#main').width(cw - (cw - mainPicWidth));
    $('.main img').width(mainPicWidth);
    $('.main-glow').width(mainGlowWidth);
    $('#main').css({
      'padding-top': mainPadding,
      'padding-bottom': mainPadding,
      'padding-left': newPadding,
      'padding-right': newPadding
    });
    $('.main-glow').css({
      'left': -1 * (mainPadding - newPadding)
    });
    $('.main-glow-bottom').height(mainGlowHeight);
    $('.main-glow-bottom img').css('top', mainGlowHeight);
  } else {
    $('#main').css({
      'padding-top': mainPadding,
      'padding-bottom': mainPadding,
      'padding-left': 0,
      'padding-right': 0
    });
    $('#main').width(cw <= 1000 ? 1000 : cw);
    $('.main img').width(cw <= 1000 ? 1000 : cw);
    $('.main-glow').width(cw + mainPadding);
    $('.main-glow').css({
      'left': mainPadding * -1
    });
    var _top = $('.main img').height() - mainGlowHeight + mainPadding * 2;
    $('.main-glow-bottom').css('height', _top);
    $('.main-glow-bottom img').css('top', _top);
  }
  resizeContentGlow();
}

function resizeContentGlow(){
  if (!$.sidesMax){
    setTimeout(resizeContentGlow, 100);
    return;
  }
  var _width = $('.content').width() - 300;
  var _maxwidth = 0;
  var _height = 0;
  var _contentHeight = 0;
  var _contentOffset = $('#content').offset().top;
  var _minheight = ie6 ? 700 : 390;
  var _children = $('.content').children().not('h1, h2, h3, .break');

  _children.each(function(){
    var $width = $(this).outerWidth();
    _maxwidth = $width > _maxwidth ? $width : _maxwidth;
  });

  if (!_children.length){
    _maxwidth = _width + 300;
  }

  $('body').height('auto');
  _width = _maxwidth - 300;

  $('#gradient').css('display','none');
  $('#gradient, #content, .content').height('auto');

  $('.content').height($('.content').height() < _minheight ? _minheight : $('.content').height());

  $('#content-bg, #content-bg-bottom').width(_width);
  $('#content-bg, #content-bg-bottom').css('left', $('#content').outerWidth() / 2 - _width / 2);
  $('#content-bg').height($('.content').height() - 390);

  _contentHeight = $.sidesMax > ($('#content').height() + _contentOffset) ? $.sidesMax : $('#content').height() + _contentOffset;

  $('#content').height(_contentHeight - _contentOffset - 2);
  $('#gradient').css('display','block');
  $('#gradient').height(ie6 ? document.documentElement.scrollHeight : document.body.scrollHeight);
  if (ie6) $('body').height($('#gradient').height());
}

function lessClick(e){
  e.preventDefault();

  var _target = $(e.target);
  var _div = _target.parent().prevAll('div.more');

  _target.parent().css('display', 'none');
  _target.parent().prev('p.more').css('display', 'block');

  _div.css({
    'height': '0',
    'position': 'absolute',
    'visibility': 'hidden'
  });

  var _height = _div.find('div.more-content').height();

  _div.height(_height);
  resizeContentGlow();
}

function moreClick(e){
  e.preventDefault();

  var _target = $(e.target);
  var _div = _target.parent().prevAll('div.more');

  _target.parent().css('display', 'none');
  _target.parent().next('p.less').css('display', 'block');

  var _height = _div.find('div.more-content').height();

  _div.css({
    'height': 'auto',
    'position': 'relative',
    'visibility': 'inherit'
  });

  _div.height(_height);
  resizeContentGlow();
}

function moreClickInit(){
  $('a.more').click(moreClick);
  $('a.less').click(lessClick);
}

/*
function blockQuote(){
  var headers = $('.content h3');
  headers.each(function(){
    var _node = $(this).next();
    var _blockquote = $('<blockquote/>').insertAfter(this);
    while (_node.length && !(_node.is('h3') || _node.is('.break'))){
      var _next = _node.next();
      _node.appendTo(_blockquote);
      _node = _next;
    }
  });
}
*/

function uList(){
  $('.content ul.articles li').append('<q/>');
  $('.content ul.articles li').css('background', 'none');
}

function contentMenuInit(){
  if (!$('.content-menu').length) return;

  $('.content-menu a').click(function(e){
    e.preventDefault();
    $('.content-menu li').removeAttr('class');
    $(this).parent().addClass('active');
    contentMenu();
  });

  contentMenu();
}

function contentMenu(){
  var _activeId = '', _activeBlock = '';
  var _activeLink = $('.content-menu li.active a').attr('href');
  if (!_activeLink || !_activeLink.length){
    _activeLink = document.location.href;
  }

  _activeId = _activeLink.substring(_activeLink.indexOf('#'));

  try {
    _activeBlock = $(_activeId);
  } catch (e) {};

  if (_activeBlock.length == 0){
    _activeLink = $('.content-menu li:first a').attr('href');
  }

  _activeId = _activeLink.substring(_activeLink.indexOf('#'));

  $('.content-menu li').removeAttr('class');
  $('.content-menu li:has(a[href~=' + _activeId + '])').addClass('active');
  $('.content-block').height(0);
  $(_activeId).height('auto');
  resizeContentGlow();
}

function centerForms(){
  $('form').each(function(){
    var _width = $(this).find('ul.form').width();
    $(this).find('ul.form li').width(_width);
    $(this).closest('div.form').width(_width);
  });

  $('button').each(function(){
    $(this).width($(this).find('span').outerWidth() + 3);
    var margin = $(this).parent().width() / 2 - $(this).outerWidth() / 2 - 3;
    $(this).css('marginLeft', margin);
  });
}




function initForms(){
  $('input, textarea').focus(function(){
    if (this.title && $.trim(this.value) == this.title){
      this.value = '';
      $(this).removeClass('default');
    }
  });

  $('input, textarea').blur(function(){
    if (this.title && $.trim(this.value) == ''){
      this.value = this.title;
      $(this).addClass('default');
    }
  });

  $('input, textarea').each(function(){
    if (this.title && $.trim(this.value)){
      this.value = this.title;
      $(this).addClass('default');
    }
  });


  $('form').each(function(){
    var _elements = $(this).find('input, select, textarea');
    var _form = this;

    for (var i = 0; i < _elements.length; i++){
      var _el = $(this.elements[i]);
      if (_el.is('button') == 'button') continue;

      var _el = $(this.elements[i]);
      if (_el.is('button') == 'button') continue;


      if (_el.is('input') || _el.is('textarea')){
	      _el.keydown(function(){
	        setTimeout(function(){
	          testForm.call(_form, true);
	        }, 0);
	      });
	    }

	    _el.change(function(){
        setTimeout(function(){
  	      testForm.call(_form, true);
    		}, 0);
		  });
    }

    $(_form).find('button').click(function(e){
      e.preventDefault();
      $(this).closest('form').submit();
    });

    $(_form).submit(function(){
      return testForm.call(_form, false);
    });

    testForm.call(_form, true);
  });
}



function testField(){
  var _valid = true;
  var _el = this;
  var _val = '';

  if (_el.attr('disabled')) return true;

/*
  if (_el.is('input')){
   _val = _el.val();
   _el.val(_val);
  }
*/

  if ($.trim(_el.val()) && _el.attr('class').indexOf('or:') < 0 && $.trim(_el.val()) == _el.attr('title')){
    _valid = false;
  }

  if (_el.hasClass('required') && !$.trim(_el.val())){
//    _el.val('');
    _valid = false;
  }

  return _valid;
}





function testForm(nofocus){
  var _valid = true;
  var _elements = $(this).find('input, select, textarea');
  var _invalidElements = [];

  for (var i = 0; i < _elements.length; i++){
    var _elValid = true;
    var _el = $(this.elements[i]);
  
    if (_el.is('button') || _el.is('input[type=submit]')) continue;

    if (_el.attr('class').indexOf('dependent:') > -1){
      var _str = _el.attr('class').substring(_el.attr('class').indexOf('dependent'));
      var _options = _str.split(':');
      var _depEl = $(_options[1]);
      var _value = _options[2];
      _depEl.attr('disabled', _value != _el.val());
      _depEl.closest('li').css('display', _value == _el.val() ? 'block' : 'none');
      resizeContentGlow();
    }

    if ( (!$.trim(_el.val()) || $.trim(_el.val()) == _el.attr('title') ) && _el.attr('class').indexOf('or:') > -1){
      var _class = _el.attr('class');
      var _orEl = _class.substring(_class.indexOf('or:') + 3);
      _orEl = $(_orEl.substring(0, _orEl.indexOf(' ')));
      if (
        (!$.trim(_el.val()) || $.trim(_el.val()) == _el.attr('title')) && (!$.trim(_orEl.val()) || $.trim(_orEl.val()) == _orEl.attr('title'))
      ){
        if ((!$.trim(_el.val()) || $.trim(_el.val()) == _el.attr('title'))){
          _invalidElements.push(_el);
        } else {
          _invalidElements.push(_orEl);
        }
        _valid = false;
      }
    }

    _elValid = testField.call(_el);
    if (!_elValid){
      _invalidElements.push(_el);
      _valid = false;
    }
  }

  if (_valid){
    $(this).find('button').addClass('valid');
  } else {
    $(this).find('button').removeClass('valid');
    if (!nofocus) _invalidElements[0].focus();
  }

  return _valid;
}

function resizeMainMenu(){
  var _bw = $('body').innerWidth();
  if (_bw <= 1020) {
    $('ul#menu').css('font-size', '19px');
  } else if (_bw <= 1058) {
    $('ul#menu').css('font-size', '20px');
  } else if (_bw <= 1080) {
    $('ul#menu').css('font-size', '21px');
  } else if (_bw <= 1110) {
    $('ul#menu').css('font-size', '22px');
  } else if (_bw > 1120) {
    $('ul#menu').css('font-size', '23px');
  }
}

function parseContent(){
//initMovie();
  initForms();
//blockQuote();
  uList();
  $(window).resize(resizeMainMenu);
  resizeMainMenu();
  contentMenuInit();
  moreClickInit();
  setTimeout(function(){
    centerForms();
    resizeContentGlow();
  }, 0);

  $('a.print').click(function(e){
    e.preventDefault();
    window.print();
  });
}
