var last_zindex = 5;
(function ($) {
$(document).ready(function() {
  // menu floating arrow
  $('<div class="menu-arrow-floater"></div>').insertBefore($('ul.main_menu')).resetMenuFloater(0);
  $('div.page_menu ul li').mouseover(function () {
    $('div.menu-arrow-floater').stop();
    pos = $(this).offset();
    lt = pos.left + ($(this).outerWidth() / 2);
    lt -= $('.page_menu').offset().left;
    lt -= ($('.menu-arrow-floater').outerWidth() / 2);
    $('div.menu-arrow-floater').animate({left: lt});
  });
  $('div.page_menu ul li').mouseout( function () {
    $('div.menu-arrow-floater').resetMenuFloater('slow');
  });

  // home banner behaviour
  $('#homeBannerItems .bannerLink').live('mouseover', function () {
    if($(this).hasClass('mouse-is-over')) {
      return;
    }
    $(this).addClass('mouse-is-over');
    $(this).oneTime(100, function () {
      if(($(this).hasClass('mouse-is-over') || $('.bannerMouseover', this).hasClass('mouse-is-over')) && !$('.bannerMouseover', this).hasClass('efect-is-shown')) {
        $('.bannerMouseover', this).fadeIn('slow').addClass('efect-is-shown');
        $('.right-button', this).stop(true).fadeIn( 'slow', function () {
          $(this).css('opacity', 1);
        } );
        if(myParseInt($('.mouseoverEffectText', this).css('width')) < 592) {
          $('.mouseoverEffectText', this).stop(true).show().animate({width:592}, 'slow');
          myObj = $(this).parent().parseClassToObject('customImage');
          $('#'+myObj.id).stop(true).fadeTo(0, 0, function () {
            $(this).fadeTo('slow', 1);
          });
        }
      }
    });
  });
  $('.bannerMouseover').live('mouseover', function () {
    $(this).addClass('mouse-is-over');
  });
  $('.bannerMouseover').live('mouseout', function () {
    $(this).removeClass('mouse-is-over');
  });
  $('#homeBannerItems .bannerLink').live('mouseout', function () {
    if(!$(this).hasClass('mouse-is-over')) {
      return false;
    }
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(100, function () {
      if(!$(this).hasClass('mouse-is-over') && !$('.bannerMouseover', this).hasClass('mouse-is-over')) {
        $('.bannerMouseover', this).fadeOut('slow').removeClass('efect-is-shown');
        $('.right-button', this).fadeOut( 'slow' );
        $('.mouseoverEffectText', this).animate({width:'0px'}, 'slow', function () {
          $(this).hide();
        });
        myObj = $(this).parent().parseClassToObject('customImage');
        $('#'+myObj.id).stop(true).fadeOut('slow', 0, function () {
          $(this).css('opacity', '0');
        });
      }
    });
  });
  if((cont = $('ul.product-slider')).length > 0) {
    cont.jcarousel({
      scroll: 4,
      auto: 3,
      wrap: 'circular',
      itemVisibleInCallback: function itemVisibleInCallback(jcarousel, item, i, action) {
        cont = $(item).parents('div.product-slider-outer:eq(0)');
        section = $(item).parseClassToObject('section');
        oldh=cont.find('h2.shown');
        if(!oldh.hasClass('section-'+section.section_color)) {
          oldh.fadeIn(0).fadeOut('slow', function () {
            $(this).removeClass('shown');
          });
          cont.find('h2.section-'+section.section_color).fadeOut(0).addClass('shown').fadeIn('slow');
        }
      }
    }).mouseenter(function () {
      $(this).jcarousel('auto', '');
    }).mouseleave(function () {
      $(this).jcarousel('auto', 3);
    });
  }
  if((cont = $('div.previews-outer')).length > 0) {
    cont.data('loaded_images', 0).find('img').each(function () {
      $(this).load(function () {
        po = $(this).parents('div.previews-outer:eq(0)');
        po.data('loaded_images', po.data('loaded_images') + 1);
        if(po.find('img').length == po.data('loaded_images') +1 ) {
          po.parent().jcarousel({
            vertical: true, itemFallbackDimension: 75, scroll: 1
          });
        }
      }).attr('src', $(this).attr('src'));
    })/*
    cont.jcarousel({
      vertical: true,
    });*/
  }
  $('a.image-preview, img.image-preview').click(function () {
    par = $(this).parents('div.images');
    // get the url
    prev = $('img', this);
    if($(this).is('img') && prev.length == 0) {
      prev = $(this);
    }
    url = prev.attr('src').replace(/\/thumb\//, '/');

    // setup container
    img_c = par.find('div.full').css('position', 'relative');
    img_o = $('img', img_c);

    // is the url changing?
    if(img_o.attr('src') == url) {
      return false;
    }
    img = $('<img src="'+(url)+'" alt="" />').css({position: 'relative', top: 0}).hide().appendTo(img_c).load( function () {
      ($(this).parent()).add($(this).parent().parent()).stop(true).animate({height: $(this).height()});
    });

    // get image height
    h = Math.max(img_o.height(), img.height());
    img_c.add(img_c.parent()).animate({height: h}, 'slow');

    // hide old image
    img_o.css({position: 'relative', top: 0}).fadeOut('slow', function() {
      // show new file
      img.fadeOut(1).fadeIn('slow');
      $(this).slideUp('slow', function () {
        $(this).remove();
      });
    });

    // setup current
    $(this).parent().find('.current').removeClass('current');
    $(this).addClass('current');

    if((""+(prev.attr('alt'))).length == 0) {
      par.parent().find('div.image-desc-cont').slideUp('slow', function () {
      });
    } else {
      par.parent().find('div.image-desc').attr('innerHTML', prev.attr('alt'));
      par.parent().find('div.image-desc-cont').stop(true, true).slideDown('slow');
    }

    return false;
  });
  $('select[name=Product-area-of-interest]').change(function () {
    opts = $(this).attr('options');
    se = $(this).attr('selectedIndex');
    if (opts.length == se+1) {
      $('<input class="drop-other" type="text" value="" />').insertAfter(this).val(opts[se].value).keyup(function () {
        $(this).prev('select').get(0).options[$(this).prev('select').get(0).options.length - 1].value = $(this).val();
      }).focus(function () {
        if($(this).val() == $(this).prev('select').get(0).options[$(this).prev('select').get(0).options.length - 1].innerHTML) {
          $(this).val('');
        }
      }).blur(function () {
        if($(this).val() == '') {
          $(this).val($(this).prev('select').get(0).options[$(this).prev('select').get(0).options.length - 1].innerHTML);
        }
      });
    } else {
      $(this).siblings('.drop-other').remove();
    }
  }).change();

  $('table.table-cmsforms span.mandatory').fadeOut(0);
  $('table.table-cmsforms input').focus(function () {
    $(this).siblings('span.mandatory').css({display: 'inline-block'}).fadeOut(0).stop(true).fadeTo('slow', 1);
  }).blur(function () {
    $(this).siblings('span.mandatory').stop(true).fadeTo('slow', 0);
  })

  // let's show the banners from side(s)
  $(document).oneTime(500, function () {
    ads = $('.sliding-add').slidingAdds({event: 'mouse', displayOpened: false}).each(function () {
      if($(this).hasClass('from-bottom') || $(this).hasClass('from-top')) {
        $(this).css('left', myParseInt($('div.all_docs').position().left || $('div.all_docs').css('marginLeft')));
      }
    });
  });
})}) (jQuery);;

/* myParseInt(): this will never return non-int type, not even NaN */
function myParseInt(i) {
  o = 0;
  if(typeof i != 'string' || typeof i != 'number')
    o = isNaN(parseInt(i)) ? 0 : parseInt(i);
  return isNaN(o) ? 0 : o;
}

/**
  * slidingAdds(): This will allow you to use ads from side
  *
  * available options:
  *   speed[='slow']: The speed of animations
  *   visible_selector[='div.visible']: selector (or element), which should be allways shown
  *   event[='mouse']: on which event you want to display the ad (mouse / click)
  *   event_originator[=&visible_selector]: Which selector / element should be clicked on (only for click), if empty, visible_selector is used
  *
  * example HTML:
  * <div class="sliding-adds2">
  *   <div class="hidden sliding-add from-bottom">
  *     <div class="visible">
  *       <a href="#" class="clicker buttons"></a>
  *       <a href="#id=1" class="close buttons"></a>
  *       Lorem ipsum dolor sit amet consectur adipiscing elit.
  *     </div>
  *     <div class="sliding-content">
  *       <img src="http://localhost/slider_image.png" />
  *       Proin scelerisque, turpis nec varius feugiat, orci ante consectur augue, sed molestie.
  *       <a href="http://localhost/Some-page.html" class="link-out">Click here</a>
  *     </div>
  *   </div>
  * </div>
  *
  * expected CSS:
  *  .sliding-add { position: absolute; }
 */

sliding_adds_elms = [];
(function(bb) {$.extend($.fn, {
  slidingAddsGetSizes: function () {
    if(typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      return { ww: $(window).width(), wh:$(window).height(), sct: myParseInt($(document).scrollTop()), scl: myParseInt($(document).scrollLeft()) };
    }
    return { ww: $(window).width(), wh:$(window).height(), sct: /*myParseInt($(document).scrollTop())*/0, scl: /*myParseInt($(document).scrollLeft())*/0 };
  },
  slidingAdds: function(opts) {
    // setup the options
    if (typeof $.browser == 'undefined') {
      alert('Error: jQuery.browser is not supported anymore, slidingAdds will fail in IE6');
    }
    if (typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      $(this).css('position', 'absolute');
    } else {
      $(this).css('position', 'fixed');
    }
    options = $.extend({}, {speed: 'slow', displayOpened: false, visible_selector: 'div.visible', event: 'mouse'}, opts);
    if(typeof options.event_originator != 'string' && typeof options.event_originator != 'object') { options.event_originator = options.visible_selector; }

    if(typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      //we need this only for browsers not supporting position: fixed (IE6)
      $(window).scroll(function (e) {
        $(window).resize();
      });
    }

    // link the resize function to scroll
    $(window).resize(function () {         // calculate new position of the ads, when the window changes
      sliding_adds_elms = $('div.sliding-add');
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      ow = $('#alldocs').outerWidth(); oh = $('#alldocs').outerHeight();
      //if(scl+ww+1 >= ow) { $(window).scrollLeft(ow - ww); }
      //if(sct+wh+1 >= oh) { $(window).scrollTop(oh - wh); }
      sliding_adds_elms.each(function () {
        vis = $(this).find('.visible');
        h=myParseInt($(this).outerHeight());
        w=myParseInt($(this).outerWidth());
        if(!$(this).hasClass('small')) {
          if($(this).hasClass('from-left')){ opts = { left: scl+w, width: w };
          } else if ($(this).hasClass('from-right') ) { opts = { left: scl+ww-w, width: w };
          } else if ($(this).hasClass('from-top') ) { opts = { top: sct+h, height: h };
          } else { opts = { top: sct+wh-h, height: h }; }
        } else {
          if($(this).hasClass('from-left')){ style = { left: scl-w }; opts = { left: scl+vw, width: vw };
          } else if ($(this).hasClass('from-right') ) { style = { left: scl+ww }; opts = { left: scl+ww-vw-w, width: vw };
          } else if ($(this).hasClass('from-top') ) { style = { top: sct }; opts = { top: sct-vh, height: vh };
          } else { style = { top: sct+wh }; opts = { top: sct+wh-vh, height: vh }; }
        }
        $(this).stop(true, true).css(opts);
      });
    });

    // set the event
    if(options.event == 'click') {
      o=$(options.event_originator, this).data('sliding_ad', this);
      if(o.length==0)
        o = $(opts.event_originator);
      if(o.length!=0)
        o.click(function () {
          s=$(this).data('sliding_ad');
          s.trigger(s.hasClass('full') ? 'hide' : 'show');
          return false;
        });
    } else {
      $(this).mouseenter(function () {
        $(this).trigger('show');
      }).mouseleave(function () {
        $(this).trigger('hide');
      })
    }

    // now apply the ad settings
    out = $(this).data('options', options).each(function () {

      // init function
      options = $(this).data('options');
      $(this).css('zIndex', 5);
      style = opts = {};
      $(this).css({overflow: 'hidden', display: ''}).removeClass('hidden');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).outerWidth()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      vh=vis.outerHeight();
      vw=vis.outerWidth()
      if($(this).hasClass('from-left')){ style = { left: scl, width: 0 }; opts = { left: scl+vw, width: vw }; }
      else if ($(this).hasClass('from-right') ) { style = { left: scl+ww, width: 0 }; opts = { left: scl+ww-vw, width: vw }; }
      else if ($(this).hasClass('from-top') ) { style = { top: sct, height: 0 }; opts = { top: sct-vh, height: vh }; }
      else { style = { top: sct+wh, height: 0 }; opts = { top: sct+wh-vh, height: vh }; }
      style.display="block";
      $(this).data('offset_top', $(document).scrollTop());
      $(this).css(style).animate(opts, options.speed, function () {
        $(this).addClass('small');
      });
    }).bind('show', function () {

      // show function
      options = $(this).data('options');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); h+=myParseInt($(this).css('marginTop')); h+=myParseInt($(this).css('marginBottom')); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).width()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      if($(this).hasClass('from-left')){ opts = { left: scl+w, width: w };
      } else if ($(this).hasClass('from-right') ) { opts = { left: scl+ww-w, width: w };
      } else if ($(this).hasClass('from-top') ) { opts = { top: sct+h, height: h };
      } else { opts = { top: sct+wh-h, height: h }; }
      style.display="block";
      $(this).stop(true).animate(opts, options.speed, function () {
        $(this).removeClass('small').addClass('full');
      });
    }).bind('hide', function () {

      // hide function
      options = $(this).data('options');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).outerWidth()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      vh=vis.outerHeight();
      vw=vis.outerWidth();
      if($(this).hasClass('from-left')){ style = { left: scl-w }; opts = { left: scl+vw, width: vw };
      } else if ($(this).hasClass('from-right') ) { style = { left: scl+ww }; opts = { left: scl+ww-vw-w, width: vw };
      } else if ($(this).hasClass('from-top') ) { style = { top: sct }; opts = { top: sct-vh, height: vh };
      } else { style = { top: sct+wh }; opts = { top: sct+wh-vh, height: vh }; }
      $(this).stop(true).animate(opts, options.speed, function () {
        $(this).removeClass('full').addClass('small');
        $(this).css('height', vh+'px');
      });
    }).bind('destroy', function () {

      // hide the ad completely
      return $(this).fadeOut('slow', function () {
      });
    }).bind('reinit', function () {

      // show the ad again
      return $(this).fadeIn('slow', function () {
      });
    });

    // show whole ad, when requested
    if(options.displayOpened) {
      $(this).stop(true).trigger('show');
    }

    return out;
  }
});
/**
  * parseClassToObject(): parses element's class and returns known elements as array
  * @param String prefix: class prefix
  * example:
  *  elements class = 'blah--name--value blah--name2--val'
  *  out = $(element).parseClassToArray('blah')
  *  output: object:{name: 'value', name2: 'val'};
*/
$.extend($.fn, {
  parseClassToObject: function parseClassToObject(prefix) {
    out = {};
    cl = $(this).attr('className');
    if(typeof cl != 'undefined' && cl.length > 0) {
      classes = cl.split(' ');
      reg=/^\-\-(.*)$/;
      for(i=0;i<classes.length;i++) {
        cl = $.trim(classes[i]);
        if(cl.indexOf(prefix) == 0){
          cl = cl.substr(prefix.length);
          if((preg = reg.exec(cl)) && preg[1].length > 0) {
            pos = preg[1].indexOf('--');
            if(pos > 0) {
              out[unescape((''+preg[1]).substr(0, pos))] = unescape((''+preg[1]).substr(pos+2));
            } else {
              out[out.length] = unescape(preg[1]);
            }
          }
        }
      }
    }
    return out;
  },
  resetMenuFloater: function resetMenuFloater (speed) {
    elm = $('.page_menu .current-page').filter(':eq(0)');
    if(elm.length == 0)
      elm = $('.page_menu .in-breadcrumbs');
    pos = elm.offset();
    if(pos && pos.left > 0){
      $(this).stop();
      lt = pos.left + (elm.outerWidth() / 2);
      lt -= $('.page_menu').offset().left;
      lt -= ($(this).width() / 2);
      $(this).animate({left: lt}, speed);
    }
  }
});
})(jQuery);
