var tech_img_size=[];

$(document).ready(function(){  
//   $(document).pngFix();
    $("#cart input").change(refreshCart);
    $("#cart input").keydown(function (event) {
        if  (event.keyCode==13) {refreshCart();return false;}
    });
    $('#wrap, .red_button, #catalog_menu').mouseover(hideTechImage);

    // Изменен алгоритм,  сейчас безнадобности
    if (false && !($.browser.msie && parseInt($.browser.version) < 7)) {
      $('#catalog_menu img').each(function (i) {
        if (this.height>50) {
          putFlash(this);
        } else {
          $(this).bind("load readystatechange",putFlash);
        }
      });
    }

    swfobject.embedSWF("/images/flag.swf", "flag", "360", "211", "7","/images/expressinstall.swf",{},{wmode:"transparent"});

});

function putFlash(el) {
   if (!el) el=this;
   var w=el.width;
   var h=el.height;
   var src=$(el).attr('src');
   var href=$(el).parent().attr('href');
   var id=$(el).parent().attr('id');
   swfobject.embedSWF("/images/jamp.swf", id, w+30, h+30, "7","/images/expressinstall.swf",{image:src, url:href, iks:100, igr:60},{wmode:"transparent"});
} 

function showTechImage(num) {
    var tech_active = $('#tech_img'+num);
    hideTechImage(tech_active);
    if (!tech_img_size[num]) {
       var img_height = tech_active.find("img").height();
       tech_img_size[num] = (img_height<70 || img_height > 120) ? 100 : img_height;
    }
    if (tech_active.height()==70) tech_active.addClass('open').animate({height:tech_img_size[num]}, { duration: "normal" });
}

function hideTechImage(elem) {
    $('#tech_menu .open').not(elem).removeClass('open').animate({height:70}, { duration: "normal" });
}


function addCart(id,el) {
//  item_count = parseInt($.cookie('cart['+id+']'))+1;
  item_count=1;
  $.cookie('cart['+parseInt(id)+']', item_count, { expires: 7, path: '/' });
  $(el).attr('onClick','').addClass('in_cart');
}

function delCart(id,el) {
  $.cookie('cart['+id+']', null, { path: '/' });
  $(el).parent().parent().remove();
  refreshCart();
}

function refreshCart() {
  var sum=0;
  $("#cart input").not("#sum").each(function (i) {
    var price = parseInt($(this).attr('rel'));
    var count = parseInt($(this).val());
   
    if (count<1) count=0;
    if (price && count) {
      sum += price*count;
    }
    if (count) $.cookie(this.name.replace('items','cart'), count, { expires: 7, path: '/' });
  });
  $("#cart #sum").val(sum);
}

function checkRequest(f) {
    var result='';
    $("#req_form .req input, #req_form .req textarea").each(function(i){
       if (!this.value) {
          this.focus();
          text=$(this).css('background','#E6E7FF').parent().prev().text();
          result+=' - '+text.substr(0,text.length-3)+"\n";
       } else {
          $(this).css('background','#fff');
       }
    });
    if (result) {alert("Пожалуйста, заполните поля: \n"+result);return false;}

    return true;
}

function printRequest() {
    $('#req_form').attr({action:'/request/print/',target:'_blank'})
                  .submit()
                  .attr({action:'/request/',target:null});
}

/*
function submitForm() {
  if($(".required").removeClass("empty").filter('[value=""]').addClass("empty").size()) return;
}
*/

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
