//var $J = jQuery.noConflict();
var is_open_delivery = 0;     
jQuery(document).ready(function(){
  //sendcomment
  $('#sendcomment').live("click",function(){
     var fio     = $('#reviews_fio');
     var email   = $('#reviews_email');
     var comment = $('#reviews_comment');
     var capcha  = $('#reviews_captcha');
     var error   = false;

     if(fio.val()==""){
       fio.css('border','1px solid red');
       error = true;
     }
     if(email.val()==""){
       email.css('border','1px solid red');
       error = true;
     }
     if(comment.val()==""){
       comment.css('border','1px solid red');
       error = true;
     }
     if(capcha.val()==""){
       capcha.css('border','1px solid red');
       error = true;
     }     
     
     if(!error){
			 $.post('/ru/reviews/',
       {
        'fio':      fio,
        'email':    email,
        'comment':  comment,
        'capcha':   capcha
       },
					function(data){
						if (data!=0){
								if(data == '00FFOK'){
									alert("Спасибо! Ваш отзыв принят.");
								}else{
									alert("Во время выполнения возникла ошибка, попробуйте пожалуйста позже.");
								}
								return false;
							}
						}
			 );
     }
  });


	//submenu - third level
	$(".branch_1 a").mouseover(function(){
		$(this).next(".branch_2").show().siblings(".branch_2").hide();
	}).mouseout(function(){
		$(this).next(".branch_2").hide();
	});
	
	$(".branch_2").mouseover(function(){
		$(this).show().siblings(".branch_2").hide();
	}).mouseout(function(){
		$(this).hide();
	}).each(function(){
		$("li", this).filter(":last").css({background: "#fff"});
	}).prepend('<b class="b1t"></b><b class="b2t"></b><b class="b3t"></b><b class="b4t"></b>').append('<b class="b4t"></b><b class="b3t"></b><b class="b2t"></b><b class="b1t"></b>');
	
	//top submenu
	$("#menu a").mouseover(function(){
		$(this).next(".headmenu2").show().siblings(".headmenu2").hide();
	}).mouseout(function(){
		$(this).next(".headmenu2").hide();
	});
	
	$(".headmenu2").mouseover(function(){
		$(this).show().siblings(".headmenu2").hide();
	}).mouseout(function(){
		$(this).hide();
	}).each(function(){
		$("li", this).filter(":last").css({background: "#fff"});
	}).prepend('<b class="b1t"></b><b class="b2t"></b><b class="b3t"></b><b class="b4t"></b>').append('<b class="b4t"></b><b class="b3t"></b><b class="b2t"></b><b class="b1t"></b>');
	
	// item_descr's same height
	SameHeight();
	
	$("#tabs, #tabs_types").tabs();
	$('a.lightbox').lightBox();
	//.item_preview 
  $('.cart input[type="image"]').click(function(el){
		var input = $(this).parent().find('input:text');
//		var input = $(this);

		var name = input.attr('name');
		var qnt = input.val();
      var material = $('input[name=materials]:checked').val();
      var variants2 = $('input[name=variants2]:checked').val();
      var color =  $('input[name=colors]:checked').val();
		//console.log('Name: [' + name + '] ' + 'Qnt: [' + qnt + '] ' + 'Mat: [' + material+ '] ' + 'Col: [' + color+ ']');
		//$.get('/rus/ajax/?action=add2cart&'+name+'='+qnt+'&material='+material+'&color='+color, 

		$.get('/rus/ajax/', 
			{action: 'add2cart', name: name, qnt: qnt, material: material, color: color, variants2: variants2}, 
			function(data) {
				//console.log(data);
				if (data!=0) {
var el=$(input).parent().parent();
var btn=$(input).siblings(input[type="image"]);
//alert($(el).width());
$('#AddBasket').css({width: $(el).width(),/*height: $(el).height(),*/left: $(btn).offset().left-121, top: $(btn).offset().top-40}).show(500);
var t=setTimeout("$('#AddBasket').animate({'opacity': 'toggle'}, { duration: 'slow' })",1000);

//					alert('Позиция добавлена в корзину');
					$('#cart_bit strong').text(data);
				}
		});
		//return false;
	});
	
	$('.item_preview tr').each(function(){
		$('td, th', this).css({'border-bottom': '1px solid #eee'});
	});
	
	//open feedback window
	$('#feedback_pic, #feedback_link').click(function(){
		$('#feedback_form').toggle(500);
		return false;
	});
	$('#close').click(function(){
        $('#feedback_form').hide(500);
    });
	
	$('#input_delivery').live('click',function(){
		if (is_open_delivery == 0){
      $('#tbody_delivery').css({display: 'table-row-group'});
      is_open_delivery = 1;
      }else{
      $('#tbody_delivery').css({display: 'none'});
      is_open_delivery = 0;
    };
	});
	
	$('#lifting').live('click',function(){
		if ($('#tbody_lifting').css('display') == 'none'){
      $('#tbody_lifting').css({display: 'table-row-group'});
      }else{
      $('#tbody_lifting').css({display: 'none'});
    };
	});
	
	$('input[name="lift"]').click(function(){
      var val = $('input[name="lift"]:checked').val();  
      if(val=="no"){
        $('input[name="floor"]').attr('disabled',false).focus();
      }else{
        $('input[name="floor"]').attr('disabled',true).val("");
      } 
  });
	
	
	
	$('ul.ui-tabs-nav').each(function(){
		$('.ui-state-default:odd, .ui-widget-content .ui-state-default:odd', this).addClass('ui-tabs-y');
		$('.ui-state-default:odd, .ui-widget-content .ui-state-default:odd', this).hover(function(){
			$(this).removeClass('ui-tabs-y').addClass('ui-tabs-y_inset');
		},function(){
			$(this).removeClass('ui-tabs-y_inset').addClass('ui-tabs-y');
		});
	});
	
	/*$('ul.ui-tabs-nav').each(function(){
		$('.ui-state-default:odd, .ui-widget-content .ui-state-default:odd', this).css("background-image", "url(/templates/jquery_ui/css/images/highlight_ffff47.png)")
		$('.ui-state-default:odd, .ui-widget-content .ui-state-default:odd', this).hover(function(){
			$(this).css("background-image", "url(/templates/jquery_ui/css/images/inset_ffff47.png)");
		},function(){
			$(this).css("background-image", "url(/templates/jquery_ui/css/images/highlight_ffff47.png)");
		});
	});
	
	
/*
    $('.selected_color').change(function(){
        var sel_var = $(this).find('option:selected');

        var sess_id = sel_var.attr('sess_id');
        var type = sel_var.attr('type');
        var color = sel_var.attr('color');
        var new_color = sel_var.attr('new_color');
        var image =  sel_var.parent().parent().find('div');
        var url = '/rus/basket/?type='+type+'&color='+color+'&new_color='+new_color+'&sess_id='+sess_id;

alert($(image).html());
        $.post(
            url,
            {
                ajaxAction:'update_color'
            },
            function ( data ) {
alert('!!');
                if ( data.result ) {

                   $(image).html(data.data);
                } else {
                    alert('Ошибка при изменении цвета товара');
                }
            },
            "json"
            );

      $.ajax({
        type: "POST",
        url: url,
        data: 'ajaxAction=update_color',
        success: function(html){
          $(image).html(html);
        }
      });




    });
*/

});

function SameHeight(){
    $(".item_container, .item_container2").each(function(){
        var part = $(this);
        var max_h = 0;
        $(".item_descr", part).each(function(){
            if (max_h < $(this).height())
            {
                max_h = $(this).height();
            }
        });
        $(".item_descr", part).height(max_h);
    });
}
function MaterialsChange(id, name) {
	$('.material').hide();
	$('#colors_set_'+id).show().find(':radio:first').attr('checked', 'checked');
	$('td.price span').hide();
	$('#ItemPrice span').hide();


	$('span.price_'+id).show();
	$('td.price').each(function(){
		//console.log($(this).find('span:visible').length);
		if ($(this).find('span:visible').length == 0) $(this).find('span.price_null').show();
	});
	$('.material_name').html(name);
}

function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = 'ЭДЕМ мебель: ' + document.title;
  
  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
	window.sidebar.addPanel (title, url, "")
  } else if(window.opera && window.print){
	alert('Для добавления страницы в Избранное, пожалуйста, нажмите ctrl+D (Command+D для компьютеров Mac) после нажатия кнопки ОК');
  } else if(window.chrome){
	alert('Для добавления страницы в Избранное, пожалуйста, нажмите ctrl+D (Command+D для компьютеров Mac) после нажатия кнопки ОК');
  }
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  /*else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }*/
  else return false;
  return true;
}


//document.oncontextmenu = function () { return false; } 
