var CatItem = {
  toggleDescription: function () {
    attr_text = "(ïîëíîñòüþ)";
    short_text = '(êðàòêî)';

    $A(["attributes", "short_attributes"]).each(Element.toggle);
    if (Element.visible($("attributes"))) {
      $("toggle_description").innerHTML = short_text;
    } else {
      $("toggle_description").innerHTML = attr_text;
    }
  },

  bay: function () {
    Element.toggle($("zakaz"));
  },

  resetForm: function () {
    form = $$("#zakaz form")[0];
    form.reset();
    return false;
  },

  compare: function (id) {
//     loc = location.href.split('?');
//     loc = loc[0] + "?cmp=" + item_id;
//     new Ajax.Request(loc, {method: 'post'});
    return false;
  }
};

var Basket = {
  dec: function (id) {
    loc = location.href.split('?');
//    loc = loc[0] + "?basket=" + id + "&" + "dec=1";
    var t = parseInt($("item_count_"+id).value) - 1;
    if (t < 0) t = 0;
    $("item_count_"+id).value = t;
    loc = '/basket/list/' + "?basket=" + id + "&" + "dec=1";
    
    new Ajax.Request(loc, {method: 'post'});
    return false;
  },
  inc: function (id) {
    loc = location.href.split('?');
//    loc = loc[0] + "?basket=" + id + "&" + "inc=1";
//    $("item_count_"+id).value = parseInt($("item_count_"+id).value) + 1;
    var t = parseInt($("item_count_"+id).value) + 1;
    $("item_count_"+id).value = t;
    loc = '/basket/list/' + "?basket=" + id + "&" + "inc=1";
    new Ajax.Request(loc, {method: 'post'});
    return false;
  },

  toggleUserAgree: function() {
    ["useragree_desc", "useragree"].each(Element.toggle);
  },

  move: function (id) {
    loc = location.href.split('?');
    loc = loc[0] + "?basket=" + id + "&" + "move=1";    
    new Ajax.Request(loc, {method: 'post'});
    return false;
  },

  add: function(id) {
    loc = location.href.split('?');
    loc = loc[0] + "?basket=" + id;
    
    //howText(loc);
    
    new Ajax.Request(loc, {method: 'post'});
    return false;
  }
};

var Saved = {
  add: function(id) {
    loc = location.href.split('?');
    loc = loc[0] + "?saved=" + id;
    new Ajax.Request(loc, {method: 'post'});
    return false;
  }
};

var Compared = {
  add: function(id) {
    loc = location.href.split('?');
    loc = loc[0] + "?cmp=" + id;
    new Ajax.Request(loc, {method: 'post'});
    return false;
  }
};

var CatMenu = {
  toggle: function (id) {
    ["cat_menu_show_" + id, "cat_menu_hide_" + id].each(Element.toggle);
    $$("#cat_menu_"+id+" .hidden").each(Element.toggle);
  }
};

var Vote = {
  submit: function(id) {
    $('vote_form_'+id).request({
      method: 'post'
    });
  }
};

Element.show_used = function(id) {
  $(id).removeClassName('used');
};

Element.hide_used = function(id) {
  $(id).addClassName('used');
};

Element.show_saved = function(id) {
  $(id).removeClassName('used');
  $(id).innerHTML = 'ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½';
};

Element.hide_saved = function(id) {
  $(id).addClassName('used');
  $(id).innerHTML = 'ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½';
};

var ItemPhoto = {
  sel: function(id) {
    $$("#item_photos .thumbs a").each(function(it){
        Element.removeClassName(it, 'sel');
      });
    Element.addClassName(Element.up($(id)), 'sel');
    $$("#current_photo img").each(function(cur) {
        cur.setAttribute('src', $(id).getAttribute("rel"));
      });
  }
};

var LoginOrSignup = {
  toggle_comment: function($check_id, $comment_id) {
    if ($F(($check_id)) == "1") {
      Element.show($comment_id);
    } else {
      Element.hide($comment_id);
    }
  }
};

var OrderForm = {
  toggle_deliver: function($check_id, $address_id) {
    if ($F(($check_id)) == "1") {
      Element.hide($address_id);
    } else {
      Element.show($address_id);
    }
  }
};

var Toggle = {
  form: function(name) {
    [name+"_form", name+"_link"].each(Element.toggle);
  }
};

var PopupImage = {
  open: function(name, w, h) {
    w += 10;
    h += 10;
    popup = window.open('', 'Sertificat', 'left=100,top=100,width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=no');
    popup.document.writeln("<html style=\"padding:0;margin:0\"><body style=\"padding:0;margin:0\">");
    popup.document.writeln("<img src=\""+name+"\" style=\"display:block;border:0;padding:0;margin:0\">");
    popup.document.writeln("</body></html>");
  }
};

var AttrPopup = {
  open: function(id) {
    txt = $("attr_desc_" + id).innerHTML;
    popup = window.open('', 'Attribut', 'left=100,top=100,width=640,height=480,toolbar=no,statusbar=no,scrollbars=yes');
    popup.document.writeln("<html style=\"padding:0;margin:0\"><body style=\"padding:0;margin:0\">");
    popup.document.writeln("<div style=\"padding:1em\">"+txt+"</div>");
    popup.document.writeln("</body></html>");
  }
};

var WatchForm = Class.create();
WatchForm.prototype = {
  initialize: function(name) {
    this.base_name = name;
    this.form_name = name + "_form";
    this.email = name + "_email";
    this.submit = $A($$("#" + this.form_name + " input[name=submit]")).first(); 
    
    if (this.submit != 'undefined')
        this.enabled = this.submit.src;
//        this.enabled = true;
        
    this.disabled = "/img/watch_price_disabled.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ï¿½ï¿½. ï¿½ï¿½ï¿½ï¿½ï¿½ */
    this.requested = "/img/watch_price_requested.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
    this.successed = "/img/watch_price_successed.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
    $(this.form_name).onsubmit = this.request.bind(this);
    this.executer = new PeriodicalExecuter(this.watch.bind(this), 1);
  },

  request: function() {
    if (this.validate()) {
      this.submit.disable();
      this.submit.src = this.requested;
      this.executer.stop();
      $(this.form_name).request({ onComplete: this.notify.bind(this) });
    }
    return false;
  },

  notify: function() {
    this.submit.src = this.successed;
    var form = this.base_name;
    setTimeout(function  (){
        Toggle.form(form);
    },3000)
  },

  validate: function() {
    if ($(this.email) && $(this.email).value.length > 0 && isValidEmail($(this.email).value)) {
      return true;
    }
    return false;
  },

  watch: function() {
    this.status = $("" + this.base_name + "_status");
    if ($(this.form_name).style.display != 'none') {
      if (this.validate()) {
        if (this.submit.disabled) {
          this.submit.enable();
          this.submit.src = this.enabled;
        }
      } else {
        if (!this.submit.disabled) {
          this.submit.disable();
          this.submit.src = this.disabled;
        }
      }
    }
  }
};


var MoreCheaply = Class.create();
MoreCheaply.prototype = {
  initialize: function(name) { 
    this.base_name = name;
    this.form_name = name + "_form";
    this.email = name + "_email";
    this.name = name + "_name";
    this.address = name + "_address";
    this.submit = $A($$("#" + this.form_name + " input[name=submit]")).first();
    
    if (this.submit != 'undefined')
        this.enabled = this.submit.src;
//        this.enabled = true;
    this.disabled = "/img/more_cheaply_disabled.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ï¿½ï¿½ï¿½ ï¿½ï¿½ï¿½ï¿½ */
    this.requested = "/img/more_cheaply_requested.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
    this.successed = "/img/more_cheaply_successed.jpg"; /* ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
    $(this.form_name).onsubmit = this.request.bind(this);
    this.executer = new PeriodicalExecuter(this.watch.bind(this), 1);
  },

  request: function() {
    if (this.validate()) {
      this.submit.disable();
//      this.status.innerHTML = this.requested;
      this.submit.src = this.requested;
      this.executer.stop();
      $(this.form_name).request({ onComplete: this.notify.bind(this) });
    }
    return false;
  },

  notify: function() {
//    this.status.innerHTML = this.successed;
    this.submit.src = this.successed;
    var form = this.base_name;
    setTimeout(function  (){
        Toggle.form(form)
    },3000)
  },

  validate: function() {
    if ($(this.email) && $(this.email).value.length > 0 &&
        $(this.address) && $(this.address).value.length > 0 &&
        $(this.name) && $(this.name).value.length > 0 ) {
      return true;
    }
    return false;
  },

  watch: function() {
    this.status = $("" + this.base_name + "_status");
//    alert(this.status);
//    this.executer.stop();
    if ($(this.form_name).style.display != 'none') {
      if (this.validate()) {
        if (this.submit.disabled) {
          this.submit.enable();
          this.submit.src = this.enabled;
//          this.status.innerHTML = '&#0160;';
        }
      } else {
        if (!this.submit.disabled) {
          this.submit.disable();
          this.submit.src = this.disabled;
//          this.status.innerHTML = this.disabled;
        }
      }
    }
  }
};

function isValidEmail(str) {
  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function show_doc(url,w,h)
{
	var l=15;
	var t=15;
	return window.open(url,'','left='+l+',top='+t+',width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=yes');
}

function checkU(obj)
{
    
    if (parseInt(obj.options[obj.selectedIndex].value) == 1)
    {
        $("udata").style.display = "";
    }
    else
    {
        $("udata").style.display = "none";
    }
    
}

var k = 0;
function  showText (text)
{
    
    $("msgBox").innerHTML = text;
    var mLeft = parseInt(document.body.clientWidth/2 - 130);
    var ss = getScrollCoordinates();
    var mTop = 350 + ss.top;
    $("msgBox").style.left = mLeft + 'px';
    $("msgBox").style.top = mTop+'px';
    $("msgBox").style.display = "block";
    clearInterval(k);
    k = setTimeout(function (){$("msgBox").style.display = "none";}, 3000);
}

function getScrollCoordinates()
{
  var sX = (document.all)?document.documentElement.scrollLeft:window.pageXOffset;
  var sY = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
  return {top:parseInt(sY), left:parseInt(sX)};
} 

function resetForm(form)
{
    for(i=0; i<form.elements.length; ++i)
    {
        switch(form.elements[i].type)
        {
            case 'checkbox':
                form.elements[i].checked = false;
                break;
            case 'radio':
                form.elements[i].checked = false;
                break;
            case 'text':
                form.elements[i].value = '';
                break;
        }
        
        
        if (form.elements[i].tagName.toLowerCase() == 'select')
        {
            form.elements[i].selectedIndex = -1;
        }
        if (form.elements[i].tagName.toLowerCase() == 'textarea')
        {
            form.elements[i].innerText = '';
        }
        
    }
}