(function($) {

  $.fn.flashGallery = function()
  {
    return this.each(function()
    {
      var $gallery = $(this);
      var $tracking = $gallery.find('ul.tracking');

      var _xml = '<?xml version="1.0" encoding="UTF-8"?><slideShow>';

      $tracking.find('li').each(function()
      {
        _xml += '<'+$.trim(this.className)+'>'+$.trim($(this).text().replace('&','').replace('"',''))+'</'+$.trim(this.className)+'>';
      });

      $gallery.find('ul.images img').each(function(i)
      {
        var _thumb = $('div.gallery ul.thumbnails img:eq('+i+')').attr('src');
        _xml += '<imgBlock><img>'+this.src+'</img><thumb>'+_thumb+'</thumb><caption>'+this.alt+'</caption></imgBlock>'
      });

      if(swfobject.hasFlashPlayerVersion("9.0.0"))
      {
        $gallery.flash({src:'/flash/ui/product-gallery.swf',width:701,height:517,wmode:'opaque',flashvars:{dataFile:_xml+'</slideShow>'}});
      }
      else
      {
        $gallery.find('ul.images').show().find('img.product-img-large').removeClass('product-img-large');
        $gallery.find('ul.images li').css({textAlign:'center'});
      }
    });
  }

  $.fn.tooltip = function()
  {
    return this.each(function()
    {
      var _el = $(this);
      var _tooltip = $('<div class="tooltip"><span class="top"></span><span class="text">'+_el.attr('title')+'</span><span class="bottom"></span></div>');

      _tooltip.appendTo('body');
      _el.attr('alt',"").attr('title',"");

      _el.hover(
        function(event)
        {
          var _offset = _el.offset();
          _offset.top = parseInt(event.pageY) - parseInt(_tooltip.height());
          _offset.left = parseInt(event.pageX) - 70;

          _tooltip.css({top:_offset.top,left:_offset.left}).show();
        },
        function(event)
        {
          _tooltip.hide();
        }
      );
    });
  }

  $.fn.button = function()
  {
    return this.each(function()
    {
      var _button = $(this);
      var _params = {text:_button.text(),width:_button.width(),height:0};

      _button.text("").append('<span class="left"></span>').append('<span class="text" style="text-align:center;width:'+(_params.width+5)+'px;">'+_params.text+'</span>').append('<span class="right"></span>');

      _params.height = _button.children('span.right').height();
      _params.width += _button.children('span.right').width() + _button.children('span.left').width() +5;

      _params.width += parseInt(_button.children('span.text').css('paddingLeft')) + parseInt(_button.children('span.text').css('paddingRight'));

      _button.css({width:_params.width,height:_params.height,visibility:'visible'});

      _button.hover(
        function()
        {
          _button.find('span').css({backgroundPosition:"0px "+_button.children('span.right').css('height')});
        },
        function()
        {
          _button.find('span').css({backgroundPosition:"0px 0px"});
        }
      );
    });
  }

  $.fn.dropdown = function()
  {
    settings = {dropdownid:0};

    return this.each(function()
    {
      var _select = $(this);
      var _dummy = $('<div class="dropdown"><div></div><span class="top"></span><span class="bottom">'+_select.find('option:selected').text()+'</span><span class="icon"></span></div>');

      _select.add(_dummy).attr('dummydropdownid',settings.dropdownid);

      _select.find('option').each(function(i)
      {
        var _option = $(this);
        _dummy.find('div').append('<span class="item" selectedindex="'+i+'">'+_option.text()+'</span>');
      });

      _dummy.hover(
        function()
        {
          var _items = _dummy.find('span.item').length;
          _dummy.children('div').stop().animate({height:((_items*19)+10)},500,"easeInQuad");
        },
        function()
        {
          _dummy.children('div').stop().animate({height:0},500,"linear");
        }
      );

      _dummy.find('span.item').click(function()
      {
        $(this).parents('div.dropdown').children('span.bottom').text($(this).text());

        $('select[dummydropdownid='+$(this).parents('div.dropdown').attr('dummydropdownid')+']').get(0).selectedIndex = $(this).attr('selectedindex');
        $('select[dummydropdownid='+$(this).parents('div.dropdown').attr('dummydropdownid')+']').change();

        _dummy.children('div').css({height:0});
      });

      _dummy.find('span.item').hover(
        function()
        {
          $(this).css({color:'#000000'});
        },
        function()
        {
          $(this).css({color:'#5d5c5c'});
        }
      );

      if($('.subcategory-grid').length > 0)
      {
        _dummy.css({top:-50,left:_select.position().left+5});
        $('.subcategory-grid').append(_dummy);
      }

      if($('.search-results-content li.sort-by').length > 0)
      {
        _dummy.css({top:-4,left:45});
        $('.search-results-content li.sort-by').append(_dummy);
      }

      settings.dropdownid++;
    });
  }

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

      $topic.find('a').each(function()
      {
        if($topic.next('a').length != 0)
        {
          $topic.css({paddingRight:8});
        }
      });

      $topic.find('h5').each(function()
      {
        $topic.prev('h4,h5').css({marginBottom:0});
      });

      $topic.children().not('img,ul').css({marginLeft:$topic.find('img:first').width()+20});
      $topic.children('ul').css({marginLeft:$topic.find('img:first').width()+20+15});

      if($topic.parents('div.float-box').length == 0)
      {
        $topic.after('<div class="clear"></div>');
      }
    });
  }

  $.fn.gallery = function()
  {
    return this.each(function()
    {
      $gallery = $(this);

      $gallery.filter('.gallery-left').find('div:first').css({left:$gallery.find('img:first').width()+5});

      $gallery.filter('.gallery,.gallery-left').find('div:first > img').each(function()
      {
        var _image = $(this);

        _image.attr('galleryimage',_image.attr('title')).attr('title','').click(function()
        {
          $gallery.find('> img').attr('src',_image.attr('galleryimage'));
        });
      });

      $gallery.filter('.press-release-gallery').find('div:first > img').each(function()
      {
        var _image = $(this);

        _image.attr('gallerysrc',_image.attr('title').split(',')[0]);
        _image.attr('enlargesrc',_image.attr('title').split(',')[1]);

        _image.attr('title','').mouseover(function()
        {
          $gallery.find('> img').attr('src',_image.attr('gallerysrc')).attr('enlargesrc',_image.attr('enlargesrc'));
        });
      });

      $gallery.filter('.press-release-gallery').find('> img').attr('enlargesrc',$gallery.find('div img:first').attr('enlargesrc'));
    });
  }

  $.fn.embed = function()
  {
    return this.each(function(i)
    {
      var _span = $(this).find('span:first');
      var _src = $.trim(_span.text());
      var _id = "flashembed"+i;

      _span.attr('id',_id);
      swfobject.embedSWF(_src,_id,_span.width(),_span.height(),"9.0.0",false,false,{wmode:'opaque'});
    });
  }

  $.fn.faq = function()
  {
    return this.each(function()
    {
      var _answer = $(this);

      _answer.attr('openheight',_answer.height() + 10).css({height:0});

      _answer.prev('.faq-question').children('span').click(function()
      {
        if(_answer.height() == 0)
        {
          _answer.stop().animate({height:_answer.attr('openheight')},500,'linear').prev('.faq-question').css({backgroundPosition:'0px -30px'});
        }
        else
        {
          _answer.stop().animate({height:0},500,'linear').prev('.faq-question').css({backgroundPosition:'0px 0px'});
        }
      });
    });
  }

  $.fn.jumpMenu = function()
  {
    return this.each(function()
    {
      $(this).change(function()
      {
        if($(this).val() != 'null')
        {
          document.location = $(this).val();
        }
      });
    });
  }

  $.fn.filterMenu = function()
  {
    return this.each(function()
    {
      var _menu = $(this);
      var _select = _menu.find('select:first');

      _select.css({display:'inline'});

      _select.change(function()
      {
        _menu.find('select:gt(0)').css({display:'none'});
        _menu.find('select[name='+_select.val()+']').css({display:'inline'}).val('null');
        $('.filter-attr').parent().show();
      });

      _menu.find('select:gt(0)').change(function()
      {
        if($(this).val() != 'null')
        {
          $('.filter-attr').parent().hide();
          $('.filter-attr:contains('+$.trim($(this).val())+')').parent().show();
        }
        else
        {
          $('.filter-attr').parent().show();
        }
      });

      _select.change();
    });
  }

  $.fn.library = function()
  {
    return this.each(function()
    {
      var _container = $(this);
      var _rows = Math.floor($(this).find('div').length/4);

      for(var i=0; i < _rows; i++)
      {
        var _maxHeight = 0;

        for(var j=0; j < 4; j++)
        {
          (_container.find('h4:eq('+((i*4)+j)+')').height() > _maxHeight) ? _maxHeight = _container.find('h4:eq('+((i*4)+j)+')').height() : null;
        }

        for(var j=0; j < 4; j++)
        {
          _container.find('h4:eq('+((i*4)+j)+')').css({height:_maxHeight});
        }
      }
    });
  }

  $.fn.globalOperations = function()
  {
    return this.each(function()
    {
      var _page = $(this);

      _page.parents('.about-content').find('div.flash').css({textAlign:'left'});

      _page.find('table tr td:first').css({fontWeight:'bold',fontSize:'12px'});

      _page.find('h3').each(function(i)
      {
        _page.find('select:first').append('<option value="'+i+'">'+$(this).text()+'</option>');
      });

      _page.find('select:first').change(function()
      {
        _page.find('tr').show();

        if($(this).val() == 'null')
        {
          _page.find('select:last').empty().append('<option value="null">Select Region / Country</option>');
          _page.find('select:last').attr('disabled','disabled');

          _page.find('div').show();
        }
        else
        {
          _page.find('div').hide();

          _page.find('select:last').empty().attr('disabled','');
          _page.find('select:last').append('<option value="null">Select Region / Country</option>');

          var last = "";

          _page.find('div:eq('+$(this).val()+') tr').find('td:first').each(function()
          {
            var text = $.trim($(this).text().replace('(Head Quarter)','').replace('(Head Quarters)',''));

            if(text != last)
            {
              _page.find('select:last').append('<option value="'+text+'">'+text+'</option>');
            }
            last = text;
          });

          _page.find('div:eq('+$(this).val()+')').show();
        }
      });

      _page.find('select:last').change(function()
      {
        var value = $.trim($(this).val());

        if(value != 'null')
        {
          _page.find('tr').find('td:first').each(function()
          {
            if($.trim($(this).text()) != value)
            {
              $(this).parents('tr').hide();
            }
            else
            {
              $(this).parents('tr').show();
            }

          });
        }
        else
        {
          _page.find('tr').show();
        }
      });
    });
  }

  $.fn.glossary = function()
  {
    return this.each(function()
    {
      var $content = $(this);

      $content.find('.glossary-index li').click(function()
      {
        if($('h3.'+this.className).length > 0)
        {
          $('html,body').animate({scrollTop:$('h3.'+this.className).offset().top},500,"linear");
        }
      });

      $content.find('.top').each(function()
      {
        var $link =  $(this);

        $link.hover(
          function()
          {
            $link.css({color:'#000000'});
          },
          function()
          {
            $link.css({color:'#5d5c5c'});
          }
        );

        $link.click(function()
        {
          $('html,body').animate({scrollTop:0},500,"linear");
        });
      });
    });
  }

  $.fn.tabs = function()
  {
    return this.each(function()
    {
      var _tabs = $(this);

      _tabs.find('ul.tabs li').each(function(i)
      {
        var _tab = $(this);

        _tab.attr('tabid',i).click(function()
        {
          _tabs.find('> div > div').hide();
          _tabs.find('div[tabid='+_tab.attr('tabid')+']').show();
          _tab.css({backgroundPosition:'0px 0px',color:'#84002b'}).siblings('li').css({backgroundPosition:'0px -24px',color:'#5d5c5c'});
        });

        _tab.hover(
          function()
          {
            if(_tab.css('backgroundPosition') != '0px 0px')
            {
              _tab.css({color:'#000000'});
            }
          },
          function()
          {
            if(_tab.css('backgroundPosition') != '0px 0px')
            {
              _tab.css({color:'#5d5c5c'});
            }
          }
        );
      });

      _tabs.find('> div > div').each(function(i)
      {
        $(this).attr('tabid',i);
      });

      _tabs.find('ul.tabs li:first').trigger('click');
    });
  }

  $.fn.phoneSelector = function()
  {
    return this.each(function()
    {
      var $content = $(this);

      if($content.find('.left > div').length > 1)
      {
        $content.find('.left > div:last').after('<ul class="other-phones"></ul>');
        $content.find('.left > div').each(function()
        {
          $('.other-phones').append('<li></li>');
        });
      }
      else
      {
        $content.find('.left > div:first').show();
        $('.mylg-accessories:first').show();
      }

      $('.other-phones li').each(function(i)
      {
        var _li = $(this);

        _li.click(function()
        {
          _li.addClass('active').siblings('li').removeClass('active').css({backgroundPosition:'top center'});

          $content.find('.left > div').hide();
          $content.find('.left > div:eq('+i+')').show();

          $('.mylg-accessories').hide();
          $('.mylg-accessories:eq('+i+')').show();
        });

        _li.hover(
          function()
          {
            if(!_li.hasClass('active'))
            {
              _li.css({backgroundPosition:'bottom center'});
            }
          },
          function()
          {
            if(!_li.hasClass('active'))
            {
              _li.css({backgroundPosition:'top center'});
            }
          }
        );
      });

      $('.other-phones li:first').click();
    });
  }

  $.fn.formToggle = function()
  {
    return this.each(function()
    {
      var _div = $(this);

      _div.find('form').css({display:'none'});

      _div.find('span.toggle').click(function()
      {
        $(this).parents('div.edit').find('form').css({display:'block'});
        $(this).parents('div.edit-avatar').css({height:'auto'});
        _div.find('span.toggle').css({display:'none'});
        _div.find('div.info').css({display:'none'});
      });

      if(_div.find('div.error p').length > 0)
      {
        _div.find('span.toggle').click();
      }
    });
  }

  $.fn.accordian = function()
  {
    return this.each(function()
    {
      var $container = $(this);

      $container.find('dl').each(function()
      {
        var _totalHeight = 0;

        $(this).find('dt').each(function()
        {
          var _height = Math.max($(this).height(),$(this).next('dd').height());
          $(this).add($(this).next('dd')).css({height:_height});
          _totalHeight += _height;
        });

        $(this).attr('openheight',_totalHeight);
      });

      $container.find('h4').click(function()
      {
        if($(this).next('dl').height() == 0)
        {
          $(this).css({backgroundPosition:"0px 0px"}).next('dl').stop().animate({height:$(this).next('dl').attr('openheight')},700,"easeInCubic");
        }
        else
        {
          $(this).css({backgroundPosition:"0px 23px"}).next('dl').stop().animate({height:0},700,"easeOutCubic");
        }
      });

      $container.find('h4:first').css({backgroundPosition:"0px 0px"}).next('dl').css({height:($container.find('h4:first').next('dl').eq(0).find('dt').length*16)});

      $container.find('span.preview').each(function()
      {
        var _link = $(this);

        (_link.find('span').length > 0) ? _link.attr('hidetext',_link.find('span').text()) : _link.attr('hidetext','Close all Specs');
        _link.find('span').remove();
        _link.attr('showtext',_link.text());

        _link.hover(
          function()
          {
            _link.css({color:'#000000'});
          },
          function()
          {
            _link.css({color:'#5d5c5c'});
          }
        );

        _link.click(function()
        {
          if(_link.text() == _link.attr('showtext'))
          {
            $container.find('h4').each(function()
            {
              if($(this).next('dl').height() == 0)
              {
                $(this).click();

              }
            });

            _link.text(_link.attr('hidetext'));
          }
          else
          {

            $container.find('h4').each(function()
            {
              if($(this).next('dl').height() != 0)
              {
                $(this).click();

              }
            });

            _link.text(_link.attr('showtext'));
          }
        });
      });
    });
  }

})(jQuery);

