"use strict";
/*global $, jQuery*/
var Application = (function () {
  this.init = function () {
    bindEvents();
  };

  this.bindEvents = function () {
    $('#sound').bind('click', function () {
      var $hymn = $('#hymn-placeholder');
      var file = 'data/medicina_gimn_part.mp3';
      var value = $hymn.html() ? '' : '<object width="0" height="0"><param value="' + file + '" name="src"><param value="false" name="autoplay"><param value="true" name="controller"><param value="#ffffff" name="bgcolor"><embed width="0" height="0" bgcolor="#ffffff" controller="false" loop="false" autostart="true" src="' + file + '" type="audio/mpeg"></embed></object>';
      $hymn.html(value);
      $(this).toggleClass('play');
    });

    $('#miscinfo a').bind('click', function () {
      $('#miscinfo a').removeClass('selected');
      var index = $(this).addClass('selected').index();
      $('#maininfo .tab').removeClass('visible').eq(index).addClass('visible');
      if (jQuery().pngFix) {
        $('#miscinfo a').each(function () {
          //@TODO: change urls
          var imageName = $(this).hasClass('selected') ? 'images/fon_green.png' : 'images/fon_gray.png';
          $(this).css('background-image', 'none');
          $(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageName + "',sizingMethod='scale')";
        });
      }
    });

    $('#arrow-left, #arrow-right').bind('click', function () {
      if ($('#main').hasClass('opened')) {
        closeDoors();
      } else {
        openDoors();
      }
    });
  };

  this.closeDoors = function () {
    $('#main-left').animate({
      width: 694
    }, 'slow', function () {
      $('#arrow-left').css('backgroundPosition', '0 0');
      $('#main').toggleClass('opened');
    });
    $('#arrow-left').animate({ marginLeft: 380 }, 'slow');
    $('#arrow-right').animate({ marginLeft:  19 }, 'slow');
    $('#border-left').animate({ marginLeft: 687 }, 'slow');
    $('#main-right').animate({ width: 303, marginLeft: 721, backgroundPosition: '0px 0px'}, 'slow', function () {
      $('#arrow-right').css('backgroundPosition', '-74px 0px');
    });
    $('#border-right').animate({ marginLeft: 721 }, 'slow');
  };

  this.openDoors = function () {
    $('#main-left').animate({
      width: 380
    }, 'slow', function () {
      $('#arrow-left').css('backgroundPosition', '-74px 0');
      $('#main').toggleClass('opened');
    });
    $('#arrow-left').animate({ marginLeft: 60 }, 'slow');
    $('#arrow-right').animate({ marginLeft:  287 }, 'slow');
    $('#border-left').animate({ marginLeft: 373 }, 'slow');
    $('#main-right').animate({ width: 35, marginLeft: 989, backgroundPosition: '-268px 0px' }, 'slow', function () {
      $('#arrow-right').css('backgroundPosition', '0 0');
    });
    $('#border-right').animate({ marginLeft: 989 }, 'slow');
  };

  return {
    init: this.init
  };
}());
$(function () {
  Application.init();

  //ie6
  if (jQuery().pngFix) {
    $(document).pngFix({ie6: true});
    var toggleHover = function ($el, toggle) {
      var className = ($el.hasClass('wrapper')) ? 'hover_wrapper' : 'hover';
      $el.toggleClass(className, toggle);
    };
  
    $("#header-nav > ul > li").hover(function () {
      toggleHover($(this), true);
    }, function() {
      toggleHover($(this), false);
    });
  }
});
