// JavaScript Document

$(document).ready(function () {
     
    if($.prototype.prettyPhoto)$("a[rel^='prettyPhoto']").prettyPhoto(
			{
				padding: 200, /* padding for each side of the picture */
				showTitle: false, 
				theme: 'dark_rounded'
			}    
    ); 
	$("a.sample-form-clear").click(function(){
		$(this).hide();
		$("a.sample-form").show();
		$("form").find("textarea,input[type=text]").val("");		
	})    
    $("a.sample-form").click(function(){
    	var form=$("form");
    	$(this).hide()
    	$("a.sample-form-clear").show()
    	$("div.sample-form div").each(function(){
    		form.find("*[name='order["+$(this).attr("class")+"]']").val($(this).text().replace(/\n/g,"").replace(/===/g,"\n"))
    	})
    	return false;
    })
     
    $("a[rel=external]").click(function() {
          window.open(this.href);  
          return false;
    })
    
    var show_webim=function(){
		window.open('/webim/client.php?locale=ru&url='+escape(document.location.href)+'&referrer='+escape(document.referrer), 'webim', 'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1');		
		return false; 
	}
    $(".webim").click(show_webim)
/*    
    $('form.webim').bind("submit",function(){
		var win=this.newWin=window.open('/webim/client.php?name='+escape($(this).find('input[name=name]').val())+'&locale=ru&amp;url='+escape(document.location.href)+'&amp;referrer='+escape(document.referrer), 'webim', 'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1');
		win.focus();
		win.opener=window;
		var text=$(this).find('textarea[name=message]').val();
		win.onload=function(){$(win.document.body).find("textarea").html(text)};
		$("#block_online").hide()		
    	return false;
    })
*/    
       
	$('.ajaxform').bind('submit',function(){
			var error,form=$(this);
			form.find('.error').removeClass('error')
			if(!error) form.ajaxSubmit({iframe:form.hasClass('iframe'),dataType:'json',success:function(res){												
				if(res.errors) {
					for(var i=0; i<res.errors.length; i++) {
						var error=res.errors[i];
						form.find('[name="'+error.name+'"]').each(function(){
								$(this).parent().addClass('error')
								$(this).focus();
								
						})
						alert(error.msg);
					}
				}
				if(res.success) {

					if(res.success.msg) alert(res.success.msg);
					if(res.success.redirect) window.location.href=res.success.redirect;
				} 
			}});	

			return false;

	})

});

function copy_in_form(form, name, tag) {
document.forms[form].elements[name].value += ' ';
document.forms[form].elements[name].value += tag;
return(false);
}

function show_hide(id, number) {
	id_main = id + '_tr';
	tr = document.getElementById(id_main);
	
	var lines = new Array();
	
	for(var i = 1; i <= number; i++){
		lines[i] = id + '_tr' + i;
		tr[i] = document.getElementById(lines[i]);
	}
	
	if(tr.className == 'active'){
		tr.className = tr.className.replace("active", "");
		for(var i = 1; i <= number; i++){
			tr[i].className = tr[i].className.replace("active", "notactive");
		}
	} else {
		tr.className = tr.className = "active";
		for(var i = 1; i <= number; i++){
			tr[i].className = tr[i].className.replace("notactive", "active");
		}
	}
}
