var goodsSwitcher = function () {
  var currentVisible = 'all';
  var menuElements = $$('.goods_categories a');
  var goodsElements = $$('.good_item');
  
  var init = function (){

  //$('overlay').observe('click',function (event) {if (event.target.id=='overlay')$('overlay').fade();$('content').fade();});
  $('overlay').onclick = function (event) {if (event.target.id=='overlay')$('overlay').fade();$('content').fade();};
  //$('close').observe('click',function (event) {$('overlay').fade();$('content').fade();});
  $('close').onclick = function (event) {$('overlay').fade();$('content').fade();};

  goodsSwitcher.menuElements.each(
      function (menuElement){
        $(menuElement).href = "javascript:;";
      }
    );
  };
  
  return {
    init: init,
    currentVisible: currentVisible,
    menuElements: menuElements
  }
}();

function switchCats (targetElementId){
    $$('.goods_categories a').each(function (element){
        element.removeClassName('selected');
      });

    $(targetElementId).addClassName('selected');
    goodsSwitcher.currentVisible = targetElementId;          
    $$('.good_item').each(function (goodElement){
      if (
          (
            goodElement.hasClassName(goodsSwitcher.currentVisible) ||
            goodsSwitcher.currentVisible == 'all'
          ) 
        ){
        goodElement.morph('width: 290px;', {duration: 0.8});
        goodElement.appear({duration: 1});
        //goodElement.show();
      } else{
        goodElement.morph('width: 0px;', {duration: 0.8});
        goodElement.fade({duration: 1});
        //goodElement.hide();
      }
        
    });

}

function showInfoDialog(id){
  scrollTo(0,0)
  $$('.contentDescription').each(function (element){
      if (element.id != id)
        element.hide();
      else
        element.show();
    }
  );
  if (Prototype.Browser.IE){
    $('overlay').style.left = "0";
    $('overlay').style.top = "0";
  }
  $('overlay').appear({to: 0.7});
  $('content').appear({to: 1.0});
}

function viewCart(){
  new Ajax.Updater('productInfo', 'ajax.php', {
    parameters: {action: 'viewCart'},
    evalScripts: true,
    onSuccess: function (r){
      showInfoDialog('productInfo');
    }
  });
}

function loadProduct (id){
  new Ajax.Updater('productInfo', 'ajax.php', {
    parameters: {id: id, action: 'getProduct'},
    evalScripts: true,
    onSuccess: function (r){
      showInfoDialog('productInfo');
    }
  });
}

function addToCart(id){

    new Ajax.Request('ajax.php', {
    parameters: {id: $F('selectedColorId'), action: 'addToCart'},
    onSuccess: function (r){

      obj = eval('('+r.responseText+')');
      
      if (obj.sum > 0 && obj.count > 0){
        $('cartExists').show();
        $('cartNotExists').hide();
        $('cartCount').innerHTML = obj.count;
        $('cartSum').innerHTML = obj.sum;
      } else {
        $('cartExists').hide();
        $('cartNotExists').show();        
      }
    }
  });
}

function switchColor(id,ext,price){
  $('selectedColorId').value = id;
  $('bigImage').style.visibility = 'hidden';
  $('bigImage').onload = function(){$('bigImage').style.visibility = 'visible'};
  $('bigImage').src='goods_images/'+id+'b.'+ext;
  if (Prototype.Browser.IE)
    fixPNG($('bigImage'))
  $('priceFull').innerHTML = price;
  
  $$('.coloredImgLink').each(function (element){
    if (element.id!='coloredImgLink_'+id)
      element.removeClassName('selected');
    else
      element.addClassName('selected');
  } );
}


function deleteFromCart(id){
    new Ajax.Request('ajax.php', {
    parameters: {id: id, action: 'deleteFromCart'},
    onSuccess: function (r){

      obj = eval('('+r.responseText+')');
      
      if (obj.sum > 0 && obj.count > 0){
        $('cartExists').show();
        $('cartNotExists').hide();
        $('cartCount').innerHTML = obj.count;
        $('cartSum').innerHTML = obj.sum;
        viewCart();
      } else {
        $('cartExists').hide();
        $('cartNotExists').show();
        $('productInfo').update('<h1>Корзина пуста!</h1>');    
      }
    }
  });
}


function showTitle(element,text){
  $('iconsDescription').update(text);
  $('iconsDescription').style.borderColor='gray';
  
}

function confirmOrder(){
  new Ajax.Updater('productInfo', 'ajax.php', {
    parameters: {action: 'confirmOrder'},
    evalScripts: true,
    onSuccess: function (r){
      showInfoDialog('productInfo');
    }
  });
}

function sendOrder(){
    
    if ($F('formContact').length <5 ){
      alert('Нам надо будет как-нибудь с тобой связаться! Заполни, пожалуйста, поле контактов.')
      $('formContact').focus();
      return;
    }
    
    new Ajax.Updater('productInfo','ajax.php', {
    parameters: {name: $F('formName'),contact: $F('formContact'),comment: $F('formComment'), action: 'sendOrder'},
    onSuccess: function (r){
      $('cartExists').hide();
      $('cartNotExists').show();
      $('productInfo').update('<h1>Корзина пуста!</h1>');    
    }
  });
  
  return false;
}
