(function($) {

  $.fn.header = function(options)
  {
    var opts = $.extend({}, $.fn.header.defaults, options);

    return this.each(function()
    {
      var $header = $(this);

      $header.addClass('content-header').find('ul.product-menu').addClass('content-menu');

      if($header.find('h2').length > 0)
      {
        if($.trim($header.find('h2').text()) != "")
        {
          var _model = $header.find('h1').text();
          var _series = $header.find('h2').text();

          $header.find('h1').text(_series).css({display:'inline'}).end().find('h2').text(_model);
        }
      }

      $header.find('h1').sifr({font:'BryantProRegular',path:'/flash/fonts'});

      var $menu = $header.find('ul.content-menu');

      var _size = $menu.find('li').length;
      var _width = Math.floor(715/_size);
      var _sparePixels = 715-(_width*_size);

      $menu.find('li').each(function(i)
      {
        var $li = $(this);

        $li.css('width',_width).attr('tabname',this.className);
        $('div.'+this.className).attr('tabname',this.className);

        if($menu.find('li').length > 6)
        {
          $li.css({fontSize:'10px'});
        }

        if(i == 0)
        {
          $li.css({width:_width-8}).addClass('first');
        }

        if(i == _size-1)
        {
          $li.css({width:_width-8});
        }

        if(_sparePixels > 0)
        {
          $li.css({width:parseInt($li.css('width'))+1});
          _sparePixels--;
        }

        $li.hover(
          function()
          {
            $li.add($li.prev('.left')).add($li.next('.right')).css({backgroundPosition:"0px 0px"}).filter('[currenttab!=true]').css({color:"#000000"});
            $li.find('a').not('.active').css({color:"#000000"});
          },
          function()
          {
            $li.add($li.prev('.left')).add($li.next('.right')).css({backgroundPosition:"0px -44px"}).filter('[currenttab!=true]').css({color:"#5d5c5c"});
            $li.find('a').not('.active').css({color:"#5d5c5c"});
          }
        );

        $li.click(function()
        {
          if($li.attr('currenttab') != "true")
          {
            $('div[tabname]').css({display:'none'}).filter('[tabname='+$li.attr('tabname')+']').css({display:'block'});
            $('ul.product-menu li').attr('currenttab','false').css({color:"#5d5c5c"}).filter('[tabname='+$li.attr('tabname')+']').attr('currenttab','true').css({color:"#84002b"});

            if($li.attr('tabname') == 'gallery')
            {
              $('html,body').animate({scrollTop:110},500,"linear");
            }
            else
            {
              $('html,body').animate({scrollTop:0},500,"linear");
            }
          }

          $li.find('a').trigger('click');
        });
      });

      $menu.prepend('<li class="left">&nbsp;</li>').append('<li class="right">&nbsp;</li>');

      // initialize tools menu
      $header.find('li.back a').remove().clone().addClass('back').prependTo($header);

      $header.find('li.print').each(function()
      {
        var _src = $.trim($(this).text());

        $(this).text('').click(function()
        {
          $.fn.header.print(_src);
        });
      });

      $header.find('li.share').text('').click(function()
      {
        $.fn.header.share(opts.shareFormUrl);
      });

      $header.find('li.where-to-buy').css({position:'absolute',top:68,right:12,left:'auto'}).click(function()
      {
        $.fn.header.wheretobuy();
      });

      $('li.where-to-buy,.where-to-buy-overlay .small-button').button();

      $('.where-to-buy-overlay').each(function()
      {
        var $overlay = $(this);
        var $close = $('<span class="close"></span>');

        $close.click(function()
        {
          $overlay.css({right:'auto',left:-2000});
        });

        $overlay.append($close);

        $overlay.find('.small-button').click(function()
        {
          $overlay.css({right:'auto',left:-2000});

          var _window = window.open(opts.whereToBuyUrl+"&cii_sZip="+$.trim($overlay.find('#zipcode').val())+"&cii_sSKU="+$.trim($overlay.find('#sku').val()),"lgewheretobuy","width=825,height=760");

          _window.focus();
        });

        $overlay.find('.buy-online span').hover(
          function()
          {
            $(this).css({color:'#000000'});
          },
          function()
          {
            $(this).css({color:'#5d5c5c'});
          }
        );

        $overlay.find('.buy-online span').click(function()
        {
          $.fn.header.buyonline(opts.buyOnlineUrl);

          $overlay.css({right:'auto',left:-2000});
        });
      });

      if($header.find('ul.tools-menu li').length == 1)
      {
        $header.find('ul.tools-menu').css({height:0});
      }

      $('.sub-nav').each(function()
      {
        var _nav = $(this);

        _nav.find('li').each(function(i)
        {
          var _li = $(this);

          _li.attr('subnavid',i).click(function()
          {
            _li.siblings('li').removeClass('current');
            _li.addClass('current');
            _li.parent('ul').siblings('div').hide();

            _li.parent('ul').siblings('div[subnavid='+i+']').show();
          });
        });

        _nav.siblings('div').each(function(i)
        {
          $(this).attr('subnavid',i);
        });

        _nav.find('li:first').click();
      });

      $header.css({visibility:'visible'});
    });
  };

  $.fn.header.print = function(url)
  {
    window.open(url,'lgeprint','');
  };

  $.fn.header.share = function(url)
  {
    if($('.email-frame').length == 0)
    {
      $('<iframe src="'+url+'" frameborder="0" scrolling="no" allowtransparency="true" class="email-frame" id="email-frame"></iframe>').appendTo('body');
    }
    else
    {
      $('.email-frame').attr("src", url);
    }

    $('.email-frame').show();
                $('#product_slogan').hide();
  };

  $.fn.header.wheretobuy = function()
  {
    $('.where-to-buy-overlay').css({right:13,left:'auto',display:'block'});
  };

  $.fn.header.buyonline = function(url)
  {
    if($('.buy-online-frame').length == 0)
    {
      $('<iframe src="'+url+'" frameborder="0" scrolling="no" allowtransparency="true" class="buy-online-frame" id="buy-online-frame"></iframe>').appendTo('.container');
    }

    $('.buy-online-frame').show();
  };

  $.fn.header.defaults =
  {
    shareFormUrl: "/emailpage/show/",
    whereToBuyUrl: "http://lge.links.channelintelligence.com/",
    buyOnlineUrl: "/us/buy-online.html"
  };

})(jQuery);

