var purl='', opts;

$(document).ready(function(){
        if (window._gaq) {
          $('#drop_field *[_name]').blur(function () {
              _gaq.push(['_trackEvent', (document.domain == 'analytics.adlabs.ru' ? 'Analytics Service' : (document.domain == 'ppa.adlabs.ru' ? 'Ppa Service' : 'Service')), $(this).attr('_name')]);
          });

          $('#pptt *[_name]').blur(function () {
                _gaq.push(['_trackEvent', 'Callme', $(this).attr('_name')]);
          });
          $('#adv *[_name]').blur(function () {
                _gaq.push(['_trackEvent', 'Adv Service', $(this).attr('_name')]);
          });
        }

      $("#drop_field input[type=text]").keypress(function(e) {
    	if(e.keyCode == 13) {
          cform($("#rform"));
    	  return false;
    	}
    });

    $(".clients").each(function() {
        var clients = $(this);
        var divs = $(".client > div", this);
        function resize() {
            divs
                .css({height: 'auto'})
                .css({height: clients.height() - 30});
        }
        resize();
        $(window).resize(resize);
    });
    $(".background-area .left table").css({height: $(".background-area .right").height()});

        
        opts = $('select[name=rpayment] option').clone();
        //$('#indext').insertBefore('#indexr');
        $('#desct').insertAfter('#indexr');
        $('.motionnew').appendTo('.innblock');
        var sliderw=0, sliderw2=0, w=321, ctimer;
        function moveItNow() {
		    sliderw -= 1;
		    if(sliderw == $('#w li').size()*-1)
		    {
			    $('#w li:first').insertAfter($('#w li:last'));
			    $("#w").css({marginTop:(sliderw+2)*448});
			    sliderw += 1;
		    }
		    $("#w").animate({marginTop:(sliderw*448)},900);
		    setTimeout(moveItNow, 7000);
	    }
//        function moveItNow() {
//            $('.motionnew').animate({marginTop: -$("tr.trace1:first").height()}, 900);
//            $('tr.trace1:first', '.motionnew').before($('tr.trace1:last', '.motionnew'));
//            setTimeout( "$('.motionnew').css({marginTop: '0'});", 2000);	
//            setTimeout(moveItNow, 2000);
//        }
//        $('.motionnew').jcarousel({
//            vertical: true,
//            scroll: 1,
//            auto: 2000,
//            wrap: 'circular',
//            buttonNextHTML: null,
//            buttonPrevHTML:null 
//        });
        if ($('.motionnew').length)
            setTimeout(moveItNow, 7000);
            
        function patners() {            
            $('.nanodesu').animate({marginLeft: -$(".company_block").width()}, 600, function() {
                $('td.company_block:last', this).after($('td.company_block:first', this));
            $(this).css({marginLeft: 0});});	
			ctimer = setTimeout(patners, 4000);
	    }
	    if ($('#line_tab').length)    
            ctimer = setTimeout(patners, 4000);    
            
        if ($('#menu_lvl2_selected').length && $('#menu_lvl1_selected').length) {
            var left1 = $('#menu_lvl1_selected').offset().left - $('#menu_lvl1_selected').parents('table').offset().left;
            var right1 = left1 + $('#menu_lvl1_selected').width();
            var left2 = $('#menu_lvl2_selected').offset().left - $('#menu_lvl2_selected').parents('table').offset().left+10;
            var right2 = left2 + $('#menu_lvl2_selected').width();
            var left = left1 < left2 ? left1 : left2;
            var right = right1 > right2 ? right1 : right2;
            $('#menuline').css({marginLeft: left + 'px', width: (right-left) + 'px', display: 'block'});
            $('#menuline2').css({width: ($('#mlv3').width() <= $('#mtl3').width() ? $('#mtl3').width() : right2-10) + 'px', display: 'block'});
            $('#menuline_').css({marginLeft: left + 'px', width: (right-left-10) + 'px', display: 'block'});
        };
        if ($('#drop_field').width() < 322)
            $('#drop_field').width(322);

        $(".post").animate({opacity: 0},1);
        $("#starter").animate({opacity: 0.1},1);
		$("#starter").hoverIntent(
			function(){
             $(".open").css({display: "block"});				 
				  },   
			function(){
             $(".open").css({display: "none"});				 	 	 			 
				  }
			);	
		/*$("#main_field").toggle(
			  function () {
			     $('#scs').remove();
				 $("#drop_field").slideDown(300);
				 $(".font_18_4z").addClass("font_18_4zz");				 				 
				 $(".font_18_4z").removeClass("font_18_4z");				 
			  },
			  function () {
				 $("#drop_field").slideUp(300);
				 $(".font_18_4zz").addClass("font_18_4z");				 				 
				 $(".font_18_4zz").removeClass("font_18_4zz");		
			  }			  
			);*/
	    function calculate () {
            var temp = $('#calculator_form input[checked], #calculator_form select');
            var sum = 0;
            for(var i=0;i<temp.length;i++) {
            if ($(temp[i]).val() > 0)
                sum += parseInt($(temp[i]).val());
            }
            $('#calculate_result').text('Расчетная стоимость работ: '+sum);
        };  
        calculate();			
		$('#calculator_form	select, #calculator_form input').bind('click change',function () {
		    calculate();
		});
        if ($('#fb-root').length)
          FB.init({appId: '148932848452221', status: true, cookie: true, xfbml: true});
          $('.facebookDoor a').click(function() {
          FB.login(function(response) {
            if (response.session) {
              FB.api('/me', function(r) {
               $('.comment-author').append('<img src="http://graph.facebook.com/'+r.id+'/picture?type=square"/><a target="_blank" href="'+r.link+'">'+r.name+'</a>');
               $('.facebook-twitter-container, .tw_logout, .no-auth').hide();
               $('.fb_logout').show();
               $('#cform div.e').remove();
               $(".post_op").click();               
              });
            } else {
              // user cancelled login
            }
          });
          return false;
        });
        $('.fb_logout a').click(function() {
          FB.logout(function(response) {
                $('.facebook-twitter-container, .no-auth').show();
                $(".fb_logout").hide();
                $('.comment-author').html('');
                $('#cform div.e').remove();
          });
          return false;
        });
        if ($('.comment-author').html() != '')
          $('.no-auth').css({'display' : 'none'});
        
        if ($('.send_comment').length) 
        {
          $('<input name="captcha" style="display:none;"/>').insertBefore('.send_comment');            
        }
          
        $('.send_comment').click(function() {
          if ($('#comment-text').val())
            cform($('#cform'));
          else
            if ($('#cform div.e').length == 0)
                  $('<div class="e"><span class="req">Введённое Вами сообщение слишком короткое</span></div>').insertAfter('#comment-text');
          return false;
        });

		$('#send_calculate_request').click(function () {
		    if ($('#request_calculate').css('display') == 'none') {
		        $('#request_calculate').slideDown();
		    }
		    else {
		        var temp = $('#calculator_form');
		        var p = temp.serializeArray();
		        p.push({name: 'calculate_view', value: $('#calculate_container').html()});
		        $.post(temp.attr('action'), p, function (data) {eval(data);});  		        
		    }
		    return false;
		});
		$('#apf, #spf').click(function () {
            $("#xs").css({opacity: 0}).animate({opacity: "0.4", height: $("#tbl").height()}, 200);
            $("#phone_form").animate({opacity: "show"}, 600); 
            $('input[name="request_phone"]').val($(this).attr('id') == 'apf' ? 0 : 1);
            if (window._qaq)
              _gaq.push(['_trackPageview', '/callme/fill.html']);
            return false;
        });  
        $('#ptype input[type=radio]').click(type_payment);  
        
        $('#callbackClose, .modalCloseImg').click(function () {
            $("#xs").animate({opacity:"0.0",height:"0"},200);
            $("#phone_form").animate({opacity:"hide"},600);
            setTimeout("$('#pprrok').hide();$('#pprr').show();$('#sentId').show();", 600);
        });        	
        $("#mm").click(function () {
            $("#mmm").slideDown(300);            
        });
         $(".post_op1").click(function () {
             $('.post').css({display: 'none', opacity: 0},1); 
             $(".post_bg").animate({height: '37'},300);
             $('#p').val(0);  
             setTimeout("$('#comment_form').insertBefore($('#last'));", 300);             
             setTimeout("$('.post_op2').css({display: 'none'},1);", 300);           
             $('#wc').insertBefore($('#lcf'));
             var f = 216 < $('#lcf').height() ? $('#lcf').height() : 216;
             setTimeout("$('.post_bg').animate({height: "+f+"},300);", 300);
             setTimeout( "$('.post').animate({opacity: 1},300);", 600);		 	 			 			 
             setTimeout( "$('.post').css({display: 'block'},1);", 600);			 	 			 
		     }
		   );	

        
        
        $(".post_op").click(function () {
             $('.post').css({display: 'none', opacity: 0},1);
             $('#lcf').slideUp(300);
             $('#p').val(0); 
             setTimeout("$('#comment_form').insertBefore($('#last'));", 300);
             $('#wc').insertBefore($('#lcf')).slideDown(300);
             setTimeout("$('#lcf').slideDown(300);$('#wc').slideUp(300);", 300);
             setTimeout( "$('.post').css({display: 'block'},1).animate({opacity: 1},300);", 600);
             /*$(".post_bg").animate({height: '37'},300);
             $('#p').val(0);  
             setTimeout("$('#comment_form').insertBefore($('#last'));", 300);             
             setTimeout("$('.post_op2').css({display: 'none'},1);", 300);           
             $('#wc').insertBefore($('#lcf'));
             var f = 216 < $('#lcf').height() ? $('#lcf').height() : 216;
             setTimeout("$('.post_bg').animate({height: "+f+"},300);", 300);
             setTimeout( "$('.post').animate({opacity: 1},300);", 600);
             setTimeout( "$('.post').css({display: 'block'},1);", 600);
             setTimeout( "$('.post_bg').css({'height' : 'auto'});", 601);*/
		     }
		   );	

        $("#rad1").click(function () {

             $("#rad1").hide(1);
             $("#rad2").show(1);			 
             $("#rad2z").hide(1)			 			 			 
		 
		     }
		   );	
		

		
		
        $("#rad2").click(function () {
             $("#rad2").hide(1);
             $("#rad1").show(1);			 
             $("#rad2z").show(1);			 			 			 			 
		     }
		   );
var c= $('#opn1').length ? 920 : 560;
var url= $('#opn1').length ? '/clients/' : '/portfolio/';
var g=0;
var w=0;
$(".nxt").click(
  function(){
    g=g-c;
    if ((w>0) && ((-g) >= w)) {$('.next').css({display: 'none'});}
    if ((-g) < $("#imx").width()) {
      $("#x").animate({marginLeft:g},100);
      $('.prev').css({display: 'block'});
    }
    else {
        $.post(url, {page: -g/c}, function (data) {eval(data);});  
    }
  }            
 ); 
 
$(".prv").click(
  function(){
    g=g+c;
    if (g == 0) {$('.prev').css({display: 'none'});}
    if ((-g) < $("#imx").width()) {$('.next').css({display: 'block'});}
    $("#x").animate({marginLeft:g},100); 
  }            
 ); 
 
$(".nxt").hover(
	function(){
     $("#und").css({textDecoration:"underline"});				 
		  },   
	function(){
     $("#und").css({textDecoration:"none"});				 	 			 
		  }
	);	
$(".prv").hover(
	function(){
     $("#pnd").css({textDecoration:"underline"});				 
		  },   
	function(){
     $("#pnd").css({textDecoration:"none"});				 	 			 
		  }
	);	 
$(".mrk, .mrk2").hover(
    function(){
      $(this).find('span').addClass('unclicked');
      },
    function(){
      $(this).find('span').removeClass('unclicked');
      }
);
$(".mrk").click(function(){
        if ($(this).hasClass('selected')) {
            return false;
        }
        else {
            var N = $('.selected').attr('id').replace('a', '');
            $('#b' + N).hide(0);
            $('.mrk').removeClass('selected');
            $(this).addClass('selected');
            var N = $(this).attr('id').replace('a', '');
            $('#b' + N).show(0);
        }
    });
$(".mrk2").click(function(){
        if ($(this).hasClass('selected')) {
            return false;
        }
        else {
            var N = $(this).siblings('.selected').attr('id').replace('c', '');
            $('#d' + N).hide(0);
            $('.mrk2').removeClass('selected');
            $(this).addClass('selected');
            var N = $(this).attr('id').replace('c', '');
            $('#d' + N).show(0);
        }
    });


var ids =$('.rpos');
for (var i=0; i<ids.length; i++) {
    if ($('#gr'+ids[i].id.replace('id', '')).length) 
         $(ids[i]).width($('#gr'+ids[i].id.replace('id', '')).width()+14);
    else
        if (! $('.pos'+ids[i].id.replace('id', '')).length) 
             $(ids[i]).width($('#year'+ids[i].id.replace('id', '')).width()+14);
}

$(".year").click(function () {
  var t=this.id.replace('year', '');
  if (!$(".pos"+t).length){
     $.post(purl, {year: $('#'+t).html(), gyear: t}, function (data) {eval(data);});  
  }
  else  {
      if ($(".pos"+t).is(":hidden")) {
         inf(t, $(this));   
      } else {
         $("#id"+t).animate({width: $('#year'+t).width()+14}, 400);
         //$("#id"+t).animate({width: 55}, 400);
         setTimeout( "$('.arr2"+t+"').css({display: 'block'},1);", 400);
         setTimeout( "$('.arr1"+t+"').css({display: 'none'},1);", 400);			  
         $(".pos"+t).animate({width:"100",opacity:0}, 400);
         setTimeout("$('.pos"+t+"').css({display: 'none'},1);", 500);		
      }
  }	  
});

$(".addgroup").click(function () {
   var t=this.id.replace('gr', '');
   $.post(purl, {year: $('#'+t).html(), gyear: t, count: $(".pos"+t).length}, function (data) {eval(data);});  
});
       $("#nav").animate({opacity: 0.7}, 0);	




		
        $("#check1").click(function () {
             $("#check1").hide(1);								 
             $("#check2").show(1);								 						 			 
		     }
		   );		
		
        $("#check2").click(function () {
             $("#check2").hide(1);								 
             $("#check1").show(1);								 						 			 
		     }
		   );
$(".gr").hover(
 function(){
  $("#l1").css({display: 'none'});
  $("#l2").css({display: 'block'});
 },   
function(){
  $("#l2").css({display: 'none'});
  $("#l1").css({display: 'block'});
  }
);

function modalWin(url, name, width, height){
 if (window.showModalDialog) {
 window.showModalDialog(url, name, "dialogWidth:"+width+"px;dialogHeight:"+height+"px");
 } else {
 window.open(url,name,'height='+width+',width='+height+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
 }
}
$(".twitter-comment a").click(function () {
   modalWin('/twitter/redirect.php', 'wnd_twitter', 780, 400);return false;
});

$('.tw_logout a').click(function() {
  $.get('/twitter/clearsessions.php' ,function(response) {
        $('.facebook-twitter-container, .no-auth').show();
        $(".tw_logout").hide();
        $('.comment-author').html('');
        $('#cform div.e').remove();
  });
  return false;
});



		
	});
function moveForm(id){
if ((id != $('#p').val()) /*&& $('.comment-author').text() != ''*/) {
    $('#wc').insertBefore($('#last')).css({display: 'block'},1).slideDown(300);
    $('.post').css({display: 'none', opacity: 0},1);
    $('#p').val(id);
    $('#lcf').slideUp(300);
    setTimeout("$('#comment_form').insertAfter($('#nc"+id+"'));", 300);
    setTimeout("$('#lcf').slideDown(300);", 300);
    setTimeout("$('.post').animate({opacity: 1},300);", 600);
    setTimeout("$('.post').css({display: 'block'},1);", 600);
  }
}	
function lr(a, b, c) {
$('#au').val(c);
b.animate({opacity: 0},300);
b.css({display: 'none'},1);
a.animate({opacity: 1},300);   
a.css({display: 'block'},1);
}
function srequest(r) {
var p=String(r.id).replace(/s/i, '');
$('.pposts').css({display: 'none'}, 1);
if ($('#p'+p).length){
    $('#p'+p).animate({opacity: 'show'}, 100);
}
else {
    $.post($('#urlprefix').attr('href')+'&page='+p, {}, function (data) {$('#bposts').append(data);});  
}
}
function cform(a) {
//$.ajax({type: 'POST', url: a[0].action, data: a.serializeArray(), success: function (data) {eval(data);}});
$.post(a[0].action, a.serializeArray(), function (data) {eval(data);});  
}
function tags(a) {
if ($('#tagsl').length){
    $('#tagsl').css({display: (a ? 'none' : 'block')},1);
    $('#tags').css({display: (a ? 'block' : 'none')},1);
}
else {
    $.post('/posts/', {set: a}, function (data) {$('<div id="tagsl">').insertAfter($('#tags'));$('#tagsl').html(data);$('#adid').html($('#did').html());$('#tags').css({display: 'none'},1);});  
}
}
function inf(t, f) {
 $("#id"+t).animate({width:"3"}, 400);		
 setTimeout( "$('.pos"+t+"').css({display: 'block'},1);", 0);	
 $(".pos"+t).animate({width:"170",opacity:1}, 400); 		
 setTimeout( "$('.arr1"+t+"').css({display: 'block'},1);", 0);
 setTimeout( "$('.arr2"+t+"').css({display: 'none'},1);", 0);	
 f.find('img.class').fadeOut();			 
}
function imshow(i) {
 $("#xs").animate({opacity: "0.4", height: $("#tbl").height()+1000}, 200);
 $("#mpp2").html(i);
 $("#mpp2").animate({opacity: "show"}, 600);
};

function ims(i) {
 $("#xs").animate({opacity: "0.4", height: $("#tbl").height()+1000}, 200);
 $("#mpp2").html('<img class="curs" alt="" src="'+i+'" onclick="imsh()"/>');
 $("#mpp2").animate({opacity: "show"}, 600);
};
function imsh() {
$("#xs").animate({opacity:"0.0",height:"0"},200);
$("#mpp2").animate({opacity:"hide"},600);
};


function type_payment() {
    //alert($('option').css('display'));
    var t;
    $('select[name=rpayment] option').remove();
    for(var i=0; i < opts.length; i++) {
        t = $(opts[i]);
        if (t.hasClass('type'+$(this).val()))
             $('select[name=rpayment]').append('<option value="' + t.val() + '">' + t.text() + '</option>');       
    }        
    //$('.type'+$(this).val()).css({display: 'block'});    
    $('select[name=rpayment]').css({display: 'block'}).val($($('.type'+$(this).val())[0]).val());
}
