//function stopError() {return true;}
//window.onerror = stopError;

jQuery(document).ready(function()
{
	$(".megamenu").megamenu({ 'activate_action':'click' });
	$("input[type='text'], textarea").each(function(){
   	if(!this.className) this.className="max";
		if($(this).attr("value") == "" || $(this).attr("value") == $(this).attr("title")){
		$(this).attr("value", $(this).attr("title"));
		$(this).css('color','#666666')
		}
	});

  $("input[type='text'], textarea").focus(function(){
    if ($(this).attr("value") == $(this).attr("title")){
      $(this).attr("value", "");
      $(this).css('color','#000000');
      }
  });

  $("input[type='text'], textarea").blur(function(){
    if ($(this).attr("value") == "" || $(this).attr("value") == $(this).attr("title")){
      $(this).attr("value", $(this).attr("title"));
      $(this).css('color','#666666')
      }
  });

  $("table#tab tbody tr").hover(
  	function(){
  		jQuery(this).css("background","#ece5d0");},
    function(){
    	jQuery(this).css("background","#FFFFFF")
    });

  $(".dropdown dt a").click(function() {
		var workId=$(this).attr('href');
		$("#"+workId+" dd ul").toggle();

		$(".dropdown dd ul").each(function(n,elem){
			if($(elem).attr('rel')!==workId)$(elem).hide();
		});
		return false;
  	});

   $(".dropdown dd ul li a").click(function() {
    $(".dropdown dd ul").hide();
   });

  function getSelectedValue(id) {
   	return $("#" + id).find("dt a span.value").html();
   }

   $(document).bind('click', function(e) {
   	var $clicked = $(e.target);
    if (! $clicked.parents().hasClass("dropdown"))
    $(".dropdown dd ul").hide();
   });

  $.nyroModalSettings({type:'iframe',bgColor:'#666666',autoSizable:true,minWidth:640,minHeight:394});

  $('.nyroModal').nyroModal();

  $(".rcContainer").after("<div class='shadow'><b></b><em></em></div>");
  $(".rcContainer").before("<div class='top'><b class='b4'><em></em></b><b class='b3'><em></em></b><b class='b2'><em></em></b><b class='b1'><em></em></b></div>");
  $(".rcContainer").after("<div class='bottom'><b class='b1'><em></em></b><b class='b2'><em></em></b><b class='b3'><em></em></b><b class='b4'><em></em></b></div>");

  $(".infiniteCarousel").infiniteCarousel();

  $(".selectOption").click(function(){
  	//$.cookie("regions[]",0,{expires:7,path:"/"});
  	if($("input[name='region["+$(this).attr("rel")+"]']").attr("checked"))
  	$.cookie("regions["+$(this).attr("rel")+"]",0,{expires:7,path:"/"});
  	else
  	$.cookie("regions["+$(this).attr("rel")+"]",$(this).attr("rel"),{expires:7,path:"/"});


  	window.location.reload();
  	return false;  });

  $("#setAllOptions").click(function(){  	$("input[name^='region']").each(function(){  		if($(this).attr("checked"))
  		$.cookie("regions["+$(this).val()+"]",$(this).val(),{expires:7,path:"/"});
  		else
  		$.cookie("regions["+$(this).val()+"]",0,{expires:7,path:"/"});  	});
  	window.location.reload();  });

  $("#ergfilter").click(function(){  	$("input[name^='region']").each(function(){  		$.cookie("regions["+$(this).val()+"]",0,{expires:7,path:"/"});
  	});
  	window.location.reload();  });

  $("input[name='ergfilter']").click(function(){  	if($(this).attr("checked"))$('#ergfilter').trigger('click');  });

  $("#sendEmail").click(function(){  	$("#sendEmailForm").toggle();
  	return false;  });

	var params = {
		changedEl: ".cusel",
		visRows: 5,
		scrollArrows: true
	}
	cuSel(params);

  $("a").focus( function(){ this.blur(); });

  $("#addBoard li").each(function(){
  	$(this).hide();
  });

  $("#addBoard li:first").show();

  $("#phone").mask("0 (99) 999-99?-99");
  $("#fax").mask("0 (99) 999-99?-99");
  $("#mobile").mask("0 (99) 999-99-99");
  $("#price").mask("999?9999");

  $(".nextbutton").click( function(){  	//alert($(this).attr("alt"));  	var error = "";
  	var checkArray = $(this).attr("rel").split(" ");
  	var fieldCheckType="m";
  	//alert(checkArray);
  	jQuery.each(checkArray, function(){  		switch(this)
  		{  			case "price":
  				fieldCheckType="must";
  			break;

  			default:
  				fieldCheckType="m";
  			break;
  		}
  		error += $("#addBoard").yaproField(this, fieldCheckType);  	});
  	if(error!=="") return false;
  	else
  	{  		$("#addBoard li."+(parseInt($(this).attr("alt"))-1)).hide("speed");
  		$("#addBoard li."+$(this).attr("alt")).show("speed");
  	};
  });

  $(".prevbutton").click( function(){
  	$("#addBoard li."+(parseInt($(this).attr("alt"))+1)).hide("speed");
  	$("#addBoard li."+$(this).attr("alt")).show("speed");
  });

  var formOptions = {
        target: "#output",   // target element(s) to be updated with server response
        beforeSubmit: showRequest,  // pre-submit callback
        success: showResponse,  // post-submit callback
        type: "post",
        clearForm: false,
        resetForm: false,
        cache: false,
        ifModified:false,
        async:true,
        timeout: 1000

  };

  $("#addBoard").ajaxForm(formOptions);

  $(".savebutton").click(function(){
  	$("#postvar").val($(this).attr("rel"));
  	//$("#addBoard").ajaxSubmit(options);
  });

  $(".fileRemove").click(function(){  	$("#fileVal_"+$(this).attr("rel")).attr("value", "");
  	$(this).parent("dt").text("Будет удалено...");
  });

  function showRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
    //alert('About to submit: \n\n' + queryString);
    return true;
  }

  function showResponse(responseText, statusText, xhr, $form)  {
    //$('#addBoard .oCenter').clearFields();
    //alert('status: ' + statusText);// + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.');
    if(responseText=="success"){window.location.href="/offers/"+$("select[name='operation'] option:selected").val()+"."+$("select[name='objects'] option:selected").val()+".html";}
  }

  //alert($("#posttab").val());
  //if($("#posttab").length && $("#posttab").val()!=="0")$(".back").trigger('click');

  $("#pikame").PikaChoose({autoPlay:false,carousel:true, carouselVertical:true});
});
