function fireEvent(element,event){
    var evt;
    if (document.createEventObject){
        // dispatch for IE
        evt = document.createEventObject();
        return element.fireEvent('on' + event, evt)
    }
    else{
        // dispatch for firefox + others
        evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

var len_checkers = new Hash();
function lenCheck(event) {
    //var field = $('product_ebay_title');
    var field = event.element();
    var options = len_checkers.get(field.id);
    var free = options.get('len') - field.value.length;
    if (free <= 0) {
        field.value = field.value.slice(0, options.get('len'));
        options.get('indicator').setStyle('color: red;');
        options.get('indicator').shake({duration: 0.1, distance: 4});
    } else {
        options.get('indicator').setStyle('color: green;');
    }
    options.get('indicator').update(options.get('template').evaluate({value: free > 0 ? free.toString() : '0'}));
}

function createLenCheker(field_id, len, template) {
    var field = $(field_id);
    var indicator = Element('span', {id: field_id + '-indicator'});
    indicator.setStyle('color: green');
    len_checkers.set(field_id, new Hash({indicator: indicator, len: len, template: template}));
    field.parentNode.id = field_id + 'indicator';
    $(field.parentNode.id).insert(indicator);
    field.observe('change', lenCheck);
    field.observe('keyup', lenCheck);
    field.observe('focus', lenCheck);
    field.observe('focus', function(event){len_checkers.get(event.element().id).get('indicator').show();});
    field.observe('blur', function(event){len_checkers.get(event.element().id).get('indicator').hide();});
}



function formatPathname(source) {
    return source.toLowerCase().replace(/[^a-z0-9_]/g, '_').replace(/_{2,}/g,'_');
}
function trimPathname(source) {
    return source.replace(/^_/, '').replace(/_$/, '');
}

var PathnameConvertor = Class.create({
    initialize: function(source_id, dest_id) {
        this.source_field = $(source_id);
        this.dest_field = $(dest_id);
        this.setPathname(this.dest_field.value);
        this.changed_by_user = false;
        new Form.Element.Observer(
            this.source_field,
            0.2,
            this.copySource.bind(this)
        );
        this.source_field.observe('change', this.trimPathnameOnBlur.bind(this));
        this.source_field.observe('focus', this.trimPathnameOnBlur.bind(this));
        this.source_field.observe('blur', this.trimPathnameOnBlur.bind(this));

        new Form.Element.Observer(
            this.dest_field,
            0.2,
            this.handlePathnameChange.bind(this)
        );
        this.dest_field.observe('change', this.trimPathnameOnBlur.bind(this));
        this.dest_field.observe('focus', this.trimPathnameOnBlur.bind(this));
        this.dest_field.observe('blur', this.trimPathnameOnBlur.bind(this));
    },
    setPathname: function(new_pathname) {
        this.last_value = new_pathname;
        this.dest_field.value = new_pathname;
    },
    changedDirectly: function() {
        return this.dest_field.value != this.last_value;
    },
    copySource: function(field, value) {
        if (!this.changed_by_user) {
            this.setPathname(formatPathname(value));
        }
    },
    handlePathnameChange: function(field, value) {
        if (this.changedDirectly()) {
            this.changed_by_user = true;
        }
        this.setPathname(formatPathname(value));
    },
    trimPathnameOnBlur: function(event) {
        this.setPathname(trimPathname(this.dest_field.value));
    }
});
// ZoomImages в листинге товаров
var item_add_cart_url;
var item_id_to_description = '0';
function ShowBigImage(image_url, event, item_buy, item_url, item_id) {
  var x;
  var y;
  if(Prototype.Browser.IE) {
    x = event.clientX + document.documentElement.scrollLeft - 175;
    y = event.clientY + document.documentElement.scrollTop - 175;
  } else {
    x = event.pageX - 175;
    y = event.pageY - 175;
  }
  if (Object.isElement($('pdtz-' + item_id_to_description))) {
    $('pdtz-' + item_id_to_description).hide();
  }
  if (Object.isElement($('pdtz-' + item_id))) {
    $('pdtz-' + item_id).show();
  }
  $('zoom-image').src = '';
  $('zoom').show();
  $('zoom-image').src = image_url.toString();
  $('zoom').setStyle({top: y +'px', left: x + 'px'});
  $('zoom-image-view').href = item_url;
  $('zoom-hide-image-background').setStyle({width: document.body.clientWidth + 'px', height: document.body.clientHeight + 'px'})
  $('zoom-hide-image-background').show();
  item_add_cart_url = item_buy;
  item_id_to_description = item_id;
}
function ImageHide() {
  $('zoom').hide();
  $('zoom-image').src = "";
  $('zoom-hide-image-background').hide();
}
function AddToCart() {
  $('notice').update('').hide();
  new Ajax.Updater({ success: 'header-shopping-cart-display'}, item_add_cart_url + '.xml',{ onSuccess: function(){ $('notice').update('Item successfully added to cart.').show()}});
  ImageHide();
  window.scrollTo(0,0);
}
// Увеличение картинок на странице товара
var i=0;
var images_count = 0;
var bigImages = new Array();
var pageImages = new Array();
var bZoom = true;

function EnableDisableButton() {
  if (i == images_count){
    $('next-button').src = '/images/images-viewer/ico_pic_right_off.gif';
    $('next-button').style.cursor = 'default';
  }
  if (i < images_count){
    $('next-button').src = '/images/images-viewer/ico_pic_right.gif';
    $('next-button').style.cursor = 'pointer';
  }
  if (i > 0){
    $('prev-button').src = '/images/images-viewer/ico_pic_left.gif';
    $('prev-button').style.cursor = 'pointer';
  }
  if (i == 0){
    $('prev-button').src = '/images/images-viewer/ico_pic_left_off.gif';
    $('prev-button').style.cursor = 'default';
  }
}
function ListImage(init_i) {
  i = init_i;
  $('product-image').src = pageImages[i];
  $('product-image-big').src = bigImages[i];
  EnableDisableButton();
}
function NextImage( ){
  if (i < images_count) {
    i++;
    $('product-image').src = pageImages[i];
    $('product-image-big').src = bigImages[i];
  }
  EnableDisableButton();
}
function PrevImage() {
  if (i > 0){
    i--;
    $('product-image').src = pageImages[i];
    $('product-image-big').src = bigImages[i];
  }
  EnableDisableButton();
}
function PrevButton(bool_var){
  if ( i > 0 )
    if (bool_var)
      $('prev-button').src = '/images/images-viewer/ico_pic_left_active.gif';
    else
      $('prev-button').src = '/images/images-viewer/ico_pic_left.gif';
}
function NextButton(bool_var){
  if ( i < images_count )
    if (bool_var)
      $('next-button').src = '/images/images-viewer/ico_pic_right_active.gif';
    else
      $('next-button').src = '/images/images-viewer/ico_pic_right.gif';
}
function ZoomBotton(bool_var){
  if(!bZoom){
    if (bool_var)
      $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom_out_active.gif';
    else
      $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom_out.gif';
  }
  else{
    if (bool_var)
      $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom_active.gif';
    else
      $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom.gif';
  }
}
function ZoomImage(){
  if (bZoom){
    $('product-image-big').src = bigImages[i];
    $('product-image-place-big').show();
    $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom_out.gif';
    $('image-viewer').style.margin = '474px 0 0 225px';
    $('product-image-place-big-close-background').style.left = '-' + (((document.body.clientWidth-900)/2) + 210) + 'px';
    $('product-image-place-big-close-background').style.width = document.body.clientWidth + 'px';
    $('product-image-place-big-close-background').style.height = document.body.clientHeight + 'px';
    bZoom = false;
  }
  else{
    $('product-image').src = pageImages[i];
    $('product-image-place-big').hide();
    $('image-viewer-zoom-button').src = '/images/images-viewer/ico_pic_zoom.gif';
    $('image-viewer').style.margin = '331px 0 0 134px';
    bZoom = true;
  }
}

var TabSwitcher = Class.create({
  initialize: function(active) {
    this.activate(active);
  },
  activate: function(id_prefix) {
    var to_activate = new Tab(id_prefix)
    if ( this.last_active ) {
      this.last_active.hide()
    }
    to_activate.show()
    this.last_active = to_activate
  }
});
var Tab = Class.create({
  initialize: function(id_prefix) {
    this.body = $(id_prefix + '-tab')
    this.button = $(id_prefix + '-button')
  },
  show: function() {
    this.body.show();
    this.button.addClassName('active');
  },
  hide: function() {
    this.body.hide();
    this.button.removeClassName('active');
  }
});
