var callbackWidget = {};
var opti_protocol = window.location.protocol + '//';

var head= document.getElementsByTagName('head')[0];

var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = opti_protocol + "scripts.optidesk.ru/templates/callback.css?" + Math.random();
head.appendChild(link);

var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = opti_protocol + 'scripts.optidesk.ru/templates/jquery-ui.css';
head.appendChild(link);

callbackWidget.app = {
	initApp: function(){

		var callback_window =
		'<div id="callback_window" class="callback_window opti-preinit" style="display:none;">\
			<div id="callback_window_button"></div>\
			<div id="callback_window_content" class="opti-preinit" style="display:none;"></div>\
			<div id="opti-powered-by" class="opti-preinit" style="display:none;">\
				<a href="' + opti_protocol + 'www.optidesk.ru/"><img src="' + opti_protocol+ 'scripts.optidesk.ru/templates/images/powered.png" alt="powered by optidesk.ru" /></a>\
			</div>\
		</div>';
		
		//jQuery(callback_window).appendTo("body");
		
		var windowState = 'closed';
		
		jQuery("body").append(callback_window);
		
		//jQuery("#callback_window_content").show();
		
		jQuery(".opti-preinit").removeClass("opti-preinit").show();
		
		function closeWindow() {
			jQuery('#callback_window_button').data('state','close');
			windowState = 'closing';
			jQuery('#callback_window').animate({'right':'-570'},900, function() {
				windowState = 'closed';
			});
			jQuery('#callback_window_button').removeClass('callback_window_button_opened');
		}
		
		function openWindow() {
    		jQuery('#callback_window_button').data('state','open');
    		windowState = 'opening';
    		
    		var right = jQuery('#callback_window').css('right').replace("px", "");
    		
    		var speed = Math.abs(Math.round(right/(-570/600)));
    		
    		jQuery('#callback_window').animate({'right':'-23'}, speed, function() {
    			windowState = 'opened';
    		});
    		if ( ! jQuery('#callback_window_button').hasClass('callback_window_button_opened')) {
    			jQuery('#callback_window_button').addClass('callback_window_button_opened');
    		}
		}

		jQuery('#callback_window_button').bind('click.openbutton', function(e){
			e.stopPropagation();

			//var state = jQuery('#callback_window_button').data('state');
        	if( windowState == 'opened'){
        		closeWindow();
        	}else if (windowState == 'closed'){
        		openWindow();
        	}
			})
			.hover(function() {
          			if ( ! jQuery(this).hasClass('callback_window_button_hover')) {
        				jQuery(this).addClass('callback_window_button_hover');
        			}
          			//jQuery("#callback_window_content").show();
        		},function(){
            		jQuery(this).removeClass('callback_window_button_hover');
        	});
		
		var leaveTimer;

		jQuery('#callback_window').bind('mouseleave', function(){
			if (windowState == 'opened') {
				leaveTimer = setTimeout(function() {
					closeWindow();
					leaveTimer = null;
				}, 3000);
			}
		});
		
		jQuery('#callback_window').bind('mouseenter', function(){
			if (typeof leaveTimer == 'number') {
				clearTimeout(leaveTimer);
				leaveTimer = null;
			}
			
			if (jQuery('#callback_window').is(':animated') && windowState == 'closing') {
				jQuery('#callback_window').stop();
				openWindow();
			}
		});
/*
		jQuery('#callback_window_content').bind('mouseover', function(){
				jQuery('#callback_window_button').data('state','open');
        		jQuery('#callback_window').css({'right':'-23'});
				jQuery('#callback_window_button').css('background-image','url(' + opti_protocol + 'scripts.optidesk.ru/templates/close.png)');
			});
			*/
		//jQuery('<div id="callback_window_content" style="display:none;"></div><div id="opti-powered-by"><a href="' + opti_protocol + 'www.optidesk.ru/"><img src="' + opti_protocol+ 'scripts.optidesk.ru/templates/images/powered.png" alt="powered by optidesk.ru" /></a></div>').appendTo('#callback_window');

		jQuery('#callback_window_content').bind('click',function(){
				jQuery('#callback_window').unbind('mouseleave mouseenter');
			});


			callbackWidget.app.initContent();
	},
	initContent: function(){

		if (typeof init == 'undefined'){
			var InitTab = ['ticket','feedback'];
		}else{
			var InitTab = init;
		}

		switch(InitTab.length){
			case 1: var cbwWidth = '100%';
			break;
			case 2: var cbwWidth = '252px';
			break;
			case 3: var cbwWidth = '190px';
		}
		var Tab ='';
		jQuery.each(InitTab, function(index,value){
			 Tab = Tab + callbackWidget.app.loadTab(value);
		});

		jQuery('#callback_window_content').html(Tab);

		jQuery('.cbW-button').css('width',cbwWidth);

		jQuery('#cbW-ticket').bind('click',function(){
				callbackWidget.app.TicketApp();
		});

		jQuery('#cbW-feedback').bind('click',function(){
				callbackWidget.app.FeedbackApp();
		});
	},
	checkEmail: function(form_id,email,required){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var address = document.forms[form_id].elements[email].value;
        if( (typeof(required) == 'undefined' && address.length>0) || required == 'required'){
        	if(reg.test(address) == false) {
            	return false;
        	}else{
	            return true;
    	    }
    	}else if(typeof(required) == 'undefined' && address.length==0){
    		return true;
    	}
    },
	loadTab: function(tabname){
		switch(tabname)
		{
		case 'ticket':
		  	return callbackWidget.app.TabTicket();
		  break;
		case 'feedback':
		  	return callbackWidget.app.TabFeedback();
		  break;
		default:
		}
	},
	TabTicket: function(){
		return '<div id="cbW-ticket" class="cbW-button">'
		+'<img src="' + opti_protocol + 'scripts.optidesk.ru/templates/images/ticket.png" alt="Tickets" /><br /><br />'
        +'&#1042;&#1086;&#1079;&#1085;&#1080;&#1082; &#1074;&#1086;&#1087;&#1088;&#1086;&#1089;? &#1055;&#1086;&#1103;&#1074;&#1080;&#1083;&#1072;&#1089;&#1100; &#1087;&#1088;&#1086;&#1073;&#1083;&#1077;&#1084;&#1072;?<br />&#1057;&#1086;&#1079;&#1076;&#1072;&#1081;&#1090;&#1077; &#1090;&#1080;&#1082;&#1077;&#1090;, &#1080; &#1084;&#1099; &#1087;&#1086;&#1084;&#1086;&#1078;&#1077;&#1084; &#1074;&#1072;&#1084;!'
        +'</div>';
	},
	TabFeedback: function(){
		return '<div id="cbW-feedback" class="cbW-button">'
	+'<img src="' + opti_protocol + 'scripts.optidesk.ru/templates/images/feedback.png" alt="Feedback" /><br /><br />'
        +'&#1045;&#1089;&#1090;&#1100; &#1087;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077; &#1080;&#1083;&#1080; &#1087;&#1086;&#1078;&#1077;&#1083;&#1072;&#1085;&#1080;&#1103;?<br />&#1055;&#1080;&#1096;&#1080;&#1090;&#1077; &#1085;&#1072;&#1084;, &#1080; &#1076;&#1072;&#1074;&#1072;&#1081;&#1090;&#1077; &#1086;&#1073;&#1089;&#1091;&#1076;&#1080;&#1084; &#1080;&#1093;!'
        +'</div>'
        +'</div>';
	},
	cancelButton: function(){
		return '<div id="cancel_button"><img src="' + opti_protocol + 'scripts.optidesk.ru/templates/images/back.png" /></div>'
	},

	/* Приложение по работе с тикетами*/
	TicketApp: function(){
		var ticketForm =
				'<div class="optidesk-form-header"><div class="optidesk-form-title">&#1057;&#1086;&#1079;&#1076;&#1072;&#1090;&#1100; &#1085;&#1086;&#1074;&#1099;&#1081; &#1090;&#1080;&#1082;&#1077;&#1090;</div>'
		        + callbackWidget.app.cancelButton()
		        +'</div><form action="javascript:void();" id="opti-form-ticket">'
		        +'<div id="opti-newtickets-field">'
		        +'<input type="hidden" name="opti_key" value="'+opti_key+'">'
		        +'<input type="hidden" name="opti_type" value="1">'
		        +'<table style="width:520px;" cellspacing="5px"><tr><td style="width:320px;padding:2px;"><label for="opti-newticket-title">&#1047;&#1072;&#1075;&#1086;&#1083;&#1086;&#1074;&#1086;&#1082; &#1090;&#1080;&#1082;&#1077;&#1090;&#1072;</label><br />'
		        +'<input style="width:100%;" type="text" name="opti_title" id="opti-newticket-title" class="opti-newticket-title"></td>'
		        +'<td  style="width:195px;padding:2px;"><label for="opti-newticket-email">&#1059;&#1082;&#1072;&#1078;&#1080;&#1090;&#1077; &#1074;&#1072;&#1096; E-ma&#1110;l</label><br />'
		        +'<input style="width:100%" type="text" name="opti_email" id="opti-newticket-email" class="opti-newticket-email" value=""></td>'
		        +'</tr><tr><td colspan="2" style="padding:2px;">'
		        +'<label for="opti-newticket-message">&#1058;&#1077;&#1082;&#1089;&#1090; &#1089;&#1086;&#1086;&#1073;&#1097;&#1077;&#1085;&#1080;&#1103;</label><br />'
		        +'<textarea name="opti_message" id="opti-newticket-message" class="opti-newticket-message" rows="5" style="width:100%;"></textarea></td></tr></table>'
		        +'<center><input class="opti-newticket-submit opti-button-submit" type="submit" name="submit" value="&#1057;&#1086;&#1079;&#1076;&#1072;&#1090;&#1100; &#1090;&#1080;&#1082;&#1077;&#1090;" role="button" aria-disabled="false"></center>'
		        +'</div>'
		        +'</form>';
		jQuery('#callback_window_content').html(ticketForm);
		
		jQuery("#opti-newticket-email").bind('click',function(){
			jQuery(this).attr('value','');
			if( jQuery(this).hasClass('opti-newticket-email_error') ){
				jQuery(this).removeClass('opti-newticket-email_error');
			}
		});

		        jQuery("#opti-form-ticket").submit(function(){
                    if(callbackWidget.app.checkEmail('opti-form-ticket','opti-newticket-email','required') == true){
                    	if( jQuery(this).hasClass('opti-newticket-email_error') ){
							jQuery(this).removeClass('opti-newticket-email_error');
						}
                    	
                    	jQuery('.opti-newticket-submit').attr('disabled', true).val('&#1057;&#1086;&#1079;&#1076;&#1072;&#1077;&#1090;&#1089;&#1103;...');
                    	//jQuery("#opti-newtickets-field").load(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=newpost",jQuery("#opti-form-ticket").serialize());
						
                    	jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=newpost&callback=?",jQuery("#opti-form-ticket").serialize(), function(response) {
                    		jQuery("#opti-newtickets-field").html(response.message);
                    		//setTimeout(callbackWidget.app.initContent, 5000);
                    	});
                    	
                    	jQuery('#cancel_button').click(function(){
		        			//callbackWidget.app.TicketApp();
                    		callbackWidget.app.initContent();
						});
                	}else{
                    	jQuery("#opti-newticket-email")
                    		.addClass('opti-newticket-email_error')
                    		.attr('value','Не верно указан E-mail');
                	}
        		});

		 jQuery('#cancel_button').click(function(){
		        	callbackWidget.app.initContent()
		});

		jQuery('#cancel_button')
		.mouseenter( function(){jQuery('#cancel_button').addClass("cancel_button_hover")})
		.mouseleave( function(){jQuery('#cancel_button').removeClass("cancel_button_hover")});

		return jQuery;
	},
	/* Приложение по работе с предложениями*/
	FeedbackApp: function(){
		var feedbackForm = '<div class="optidesk-form-header"><div class="optidesk-form-title">&#1055;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1103;</div>'
		+ callbackWidget.app.cancelButton()
		+'</div><div style="clear:both"></div>'
        +'<div id="callback_window_content_other_feedback"><img align="middle" src="' + opti_protocol+ 'scripts.optidesk.ru/templates/images/loader.gif" /></div>'
        +'<form action="javascript:void();" id="opti-form-feedback">'
        +'<div id="opti-newfeedback-field">'
        +'<input type="hidden" name="opti_key" value="'+opti_key+'">'
        +'<input type="hidden" name="opti_type" value="1">'
        +'<table class="feedback-new-wrap" cellspacing="5px"><tr>'
        +'<td style="width:438px;padding:2px;">'
        +'<input style="width:100%;" type="text" name="opti_title" id="opti-newfeedback-title" class="opti-newfeedback-title" />'
	    +'</td>'
		+'<td style="width:74px;padding:2px;">'
		+'<input class="opti-button-submit opti-submit-feature" type="submit" name="submit" value="&#1054;&#1090;&#1087;&#1088;&#1072;&#1074;&#1080;&#1090;&#1100;" role="button" aria-disabled="false">'
		+'</td>'
        +'</tr></table>'
        +'</div>'
        +'</form>'
        ;
        jQuery('#callback_window_content').html(feedbackForm);
        callbackWidget.app.showFeedback();

		jQuery('#optidesk-view-form').toggle(
				function(){
					jQuery('#opti-newfeedback-field').slideDown("fast");
					jQuery('#callback_window_content_other_feedback').hide();
					jQuery('#optidesk-view-form').hide();
					jQuery('#cancel_button').click(function(){
		        		callbackWidget.app.FeedbackApp();
					});
				},
				function(){
					jQuery('#opti-newfeedback-field').slideUp("fast");
					jQuery('#callback_window_content_other_feedback').show();
					jQuery('#optidesk-view-form').show();

				}
		);
		/*jQuery('#optidesk-view-form')
			.mouseenter( function(){jQuery('#optidesk-view-form').addClass("add_hidden_field_hover");})
			.mouseleave( function(){jQuery('#optidesk-view-form').removeClass("add_hidden_field_hover");});

		jQuery('#add_hidden_field')
		.mouseenter( function(){jQuery('#add_hidden_field').addClass("add_hidden_field_hover");})
		.mouseleave( function(){jQuery('#add_hidden_field').removeClass("add_hidden_field_hover");});*/

		/* Cancel button */
		jQuery('#cancel_button').click(function(){
		        	callbackWidget.app.initContent()
		});
		
		jQuery('#cancel_button')
			.mouseenter( function(){jQuery('#cancel_button').addClass("cancel_button_hover")})
			.mouseleave( function(){jQuery('#cancel_button').removeClass("cancel_button_hover")});

		jQuery("#opti-newfeed-email").bind('click',function(){
			jQuery(this).attr('value','');
			if( jQuery(this).hasClass('opti-newticket-email_error') ){
				jQuery(this).removeClass('opti-newticket-email_error');
			}
		});
		
		var isFeedbackSubmit = false;
		
		jQuery("#opti-form-feedback").submit(function(){
				jQuery(".opti-submit-feature").attr('disabled', true);
				
				if (isFeedbackSubmit)
					return false;
				
				isFeedbackSubmit = true;
              	//if(callbackWidget.app.checkEmail('opti-form-feedback','opti-newfeed-email') == true){
                   	//jQuery("#opti-newfeed-email").css('border','1px solid #E1E1E1');
                   	//jQuery("#opti-newfeedback-field").load(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=newfeedback",jQuery("#opti-form-feedback").serialize(),
                   	jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=newfeedback&callback=?",jQuery("#opti-form-feedback").serialize(),
					function(response){
                   		jQuery("#opti-newfeedback-field").html(response.message);
                   		
                   		jQuery('#add_newfeedback')
							.mouseenter( function(){jQuery('#add_newfeedback').addClass("add_hidden_field_hover");})
							.mouseleave( function(){jQuery('#add_newfeedback').removeClass("add_hidden_field_hover");});
                   		jQuery("#add_newfeedback").bind('click',function(){
                   			callbackWidget.app.FeedbackApp();
                   		});
                   		
                   		jQuery('#cancel_button').click(function(){
		        			callbackWidget.app.FeedbackApp()
						});
                   	});
				/*}else{
                   	jQuery("#opti-newfeed-email")
                    		.addClass('opti-newticket-email_error')
                    		.attr('value','Не верно указан E-mail');
                }*/
        });

		return jQuery;
	},
	showFeedback:function(){
		var list = jQuery("#list").val();
		//jQuery("#callback_window_content_other_feedback")
		jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=showfeedback&list="+list+"&opti_key="+opti_key+"&opti_type=1&callback=?",
		function(response){
			jQuery("#callback_window_content_other_feedback").html(response.html);
			
			if(jQuery('.feedback-element').size()==0){
            	jQuery('#opti-newfeedback-field').show();
                jQuery('#optidesk-view-form').hide();
            }else{
            	jQuery('#optidesk-view-form').show();
            }
            
            jQuery('.feedback-element')
							.mouseenter( function(){jQuery(this).addClass("element-hover");})
							.mouseleave( function(){jQuery(this).removeClass("element-hover");});
            
            
            jQuery('.feedback-element-count').mouseenter(function() {            	
            	jQuery(this).next().addClass('feedback-element-title-hover');
            }).mouseleave(function() {
            	jQuery(this).next().removeClass('feedback-element-title-hover');
            });
            
            jQuery('.feedback-element-title').hover(function() {
            	jQuery(this).addClass('feedback-element-title-hover');
            }, function () {
            	jQuery(this).removeClass('feedback-element-title-hover');
            });
            
            jQuery('.feedback-element-count').each(function() {
            	var discus = jQuery(this).parents('.feedback-element').attr("discus");
				var is_voted = callbackWidget.app.getCookie("odv-"+discus);
				
				if (is_voted) {
					jQuery(this).addClass('feedback-element-count-voted');
				}
            });
            
            jQuery('.feedback-element-dislike').click(function(e) {
            	e.stopPropagation();
            	
				var discus = jQuery(this).parents('.feedback-element').attr("discus");
				var is_voted = callbackWidget.app.getCookie("odv-"+discus);
				
				if (is_voted != 'v1') {
					jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=voted&for=odd&discus="+discus+"&opti_key="+opti_key+"&opti_type=1&callback=?");
					callbackWidget.app.setCookie("odv-"+discus,'v1',{ expires: 7200} );
					
					jQuery(this).text((parseInt(jQuery(this).text()) + 1)).parent().addClass('feedback-element-count-voted');
				}
            });
            
            jQuery('.feedback-element-like').click(function(e) {
            	e.stopPropagation();
            	
				var discus = jQuery(this).parents('.feedback-element').attr("discus");
				var is_voted = callbackWidget.app.getCookie("odv-"+discus);

				if (is_voted != 'v1') {
					jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=voted&for=add&discus="+discus+"&opti_key="+opti_key+"&opti_type=1&callback=?");
					callbackWidget.app.setCookie("odv-"+discus,'v1',{ expires: 7200} );
					
					jQuery(this).text((parseInt(jQuery(this).text()) + 1)).parent().addClass('feedback-element-count-voted');
				}
            });
							
			jQuery(".feedback-element").bind('click', function(){
					var discus = jQuery(this).attr("discus");
					var is_voted = callbackWidget.app.getCookie("odv-"+discus);
					jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=showfeedbackbyid&list="+list+"&discus="+discus+"&is_voted="+is_voted+"&opti_key="+opti_key+"&opti_type=1&callback=?",
					function(response){
					/* 
					 * 
					 * UNCOMMENT THIS !!!!
					 * WHEN JQUERY-UI ADDING HERE
					 * 
					 * * callbackWidget.app.scrolling();
					 * 
					 * 
					 */
						
						jQuery("#callback_window_content_other_feedback").html(response.html);
						

					jQuery("#opti-form-feedback").hide();
					
					if (is_voted) {
						jQuery('.feedback-element-count').addClass('feedback-element-count-voted');
					}
					
		            jQuery('.feedback-element-count').each(function() {
		            	var discus = jQuery(this).parents('.feedback-element').attr("discus");
						var is_voted = callbackWidget.app.getCookie("odv-"+discus);
						
						if (is_voted) {
							jQuery(this).addClass('feedback-element-count-voted');
						}
		            });
		            
		            jQuery('.feedback-element-dislike').click(function(e) {
		            	e.stopPropagation();
		            	
						var discus = jQuery(this).parents('.feedback-element').attr("discus");
						var is_voted = callbackWidget.app.getCookie("odv-"+discus);
						
						if ( ! is_voted) {
							jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=voted&for=odd&discus="+discus+"&opti_key="+opti_key+"&opti_type=1&callback=?");
							callbackWidget.app.setCookie("odv-"+discus,'v1',{ expires: 7200} );
							
							jQuery(this).text((parseInt(jQuery(this).text()) + 1)).parent().addClass('feedback-element-count-voted');
						}
		            });
		            
		            jQuery('.feedback-element-like').click(function(e) {
		            	e.stopPropagation();
		            	
						var discus = jQuery(this).parents('.feedback-element').attr("discus");
						var is_voted = callbackWidget.app.getCookie("odv-"+discus);
						
						if ( ! is_voted) {
							jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=voted&for=add&discus="+discus+"&opti_key="+opti_key+"&opti_type=1&callback=?");
							callbackWidget.app.setCookie("odv-"+discus,'v1',{ expires: 7200} );
							
							jQuery(this).text((parseInt(jQuery(this).text()) + 1)).parent().addClass('feedback-element-count-voted');
						}
		            });
					
					jQuery("#cancel_button").bind('click',function(){
						callbackWidget.app.FeedbackApp();
					});
					
					var _commentsWrap = jQuery('<div id="feedback-comments"></div>')
							.appendTo(jQuery("#callback_window_content_other_feedback"));
							
						jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=comments&discus="+discus+"&opti_key="+opti_key+"&opti_type=1&callback=?", function (response) {
								
							    _commentsWrap.html(response.html);
									
								jQuery(".feedback-new-comment-message").focus(function() {
									if ( ! jQuery(this).parents('.feedback-new-comment-wrap').hasClass("feedback-new-comment-wrap-active")) {
										jQuery(this).parents('.feedback-new-comment-wrap').addClass("feedback-new-comment-wrap-active");
										jQuery(this).val('');
									}
								}).blur(function() {
									if (jQuery.trim(jQuery(this).val()) == '') {
										jQuery(this).parents('.feedback-new-comment-wrap').removeClass("feedback-new-comment-wrap-active");
										jQuery(this).val('Новый комментарий');
									}
								}).keypress(function(e) {
									 if (e.keyCode == 13) {
										 jQuery("#feedback-new-comment-next").trigger('click');
									 }
								});
								
								jQuery("#feedback-new-comment-next").click(function() {
									
									var message = jQuery.trim(jQuery(".feedback-new-comment-message").val());
									
									if (message != '' && message != 'Новый комментарий') {
									
										jQuery(".feedback-new-comment-message-wrap").slideUp();
										
										if (typeof Recaptcha == "undefined") {
											jQuery.getScript("http://www.google.com/recaptcha/api/js/recaptcha_ajax.js", function() {
												
												Recaptcha.create("6LfKG8gSAAAAABuhTsHtl01xDRcStWEriRIH7hhZ",
												    "feedback-new-comment-captcha",
												    {
												      theme: "clean",
												      callback: Recaptcha.focus_response_field
												    }
												  );
												
												jQuery(".feedback-new-comment-identify-wrap").slideDown();
											});
										} else {
											Recaptcha.create("6LfKG8gSAAAAABuhTsHtl01xDRcStWEriRIH7hhZ",
												    "feedback-new-comment-captcha",
												    {
												      theme: "clean",
												      callback: Recaptcha.focus_response_field
												    }
												  );
											
											jQuery(".feedback-new-comment-identify-wrap").slideDown();
										}
									}
								});
								
								jQuery("#feedback-new-comment-send").click(function() {
									//jQuery(".feedback-new-comment-error").remove();
									jQuery("#recaptcha_response_field").removeClass("feedback-new-comment-captcha-error");
									
									var message = jQuery.trim(jQuery(".feedback-new-comment-message").val());
									var author = jQuery.trim(jQuery(".feedback-new-comment-author").val());
									
									if (message != '' && author != '' && message != 'Новый комментарий' && author != 'Ваше имя') {
										var challengeField = jQuery("input#recaptcha_challenge_field").val();
									    var responseField = jQuery("input#recaptcha_response_field").val();

										    jQuery.getJSON(opti_protocol + "scripts.optidesk.ru/ajax/callback.php?a=newcomment&opti_key="+opti_key+"&opti_type=1&callback=?",
										    	"recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField + "&message=" + message + "&author=" + author + '&f_id=' + discus,
										    	function(response) {
										    	if (response.message == 1) {
								        			jQuery(".feedback-new-comment-identify-wrap").slideUp();
								        			jQuery(".feedback-new-comment-wrap")
								        				.append('<p class="feedback-new-comment-notify">&#1050;&#1086;&#1084;&#1084;&#1077;&#1085;&#1090;&#1072;&#1088;&#1080;&#1081; &#1087;&#1086;&#1089;&#1090;&#1072;&#1074;&#1083;&#1077;&#1085; &#1085;&#1072; &#1084;&#1086;&#1076;&#1077;&#1088;&#1072;&#1094;&#1080;&#1102;</p>');									        			 
								        		} else {
								        			Recaptcha.reload();
								        			jQuery("#recaptcha_response_field").addClass("feedback-new-comment-captcha-error");
								        		}
										    });			
									}
								});
								
								jQuery(".feedback-new-comment-author").focus(function() {
									if ( ! jQuery(this).hasClass('feedback-new-comment-author-focus')) {
										jQuery(this).val('').addClass('feedback-new-comment-author-focus');
									}
								})
								.blur(function() {
									if (jQuery.trim(jQuery(this).val()) == '') {
										jQuery.removeClass('feedback-new-comment-author-focus').val('Ваше имя');
									}
								});
								
								jQuery("#feedback-new-comment-cancel").click(function() {
									jQuery(".feedback-new-comment-identify-wrap").slideUp();
									jQuery(".feedback-new-comment-message-wrap").slideDown();
									jQuery('.feedback-new-comment-wrap').removeClass("feedback-new-comment-wrap-active");
									jQuery(".feedback-new-comment-message").val('Новый комментарий');
									jQuery(".feedback-new-comment-author").removeClass("feedback-new-comment-author-focus").val('Ваше имя');
								});
							});

				});							
							
							
							
			}
			);
		});
	},
	getCookie: function(name) {
	var matches = document.cookie.match(new RegExp(
	  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	))
	return matches ? decodeURIComponent(matches[1]) : undefined 
	},
	setCookie: function(name, value, props) {
		props = props || {}
		var exp = props.expires
		if (typeof exp == "number" && exp) {
			var d = new Date()
			d.setTime(d.getTime() + exp*60000)
			exp = props.expires = d
		}
		if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }

		value = encodeURIComponent(value)
		var updatedCookie = name + "=" + value
		for(var propName in props){
			updatedCookie += "; " + propName
			var propValue = props[propName]
			if(propValue !== true){ updatedCookie += "=" + propValue }
		}
		document.cookie = updatedCookie
	},
	scrolling: function() {
		//scrollpane parts
		var scrollPane = jQuery( ".feedback-element-message" ),
			scrollContent = jQuery( ".feedback-element-message-content" );

		//build slider
		var scrollbar = jQuery( ".feedback-element-message-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.height() > scrollPane.height() ) {
					scrollContent.css( "margin-top", Math.round(
						ui.value / 100 * ( scrollPane.height() - scrollContent.height() )
					) + "px" );
				} else {
					scrollContent.css( "margin-top", 0 );
				}
			}
		});

		//append icon to handle
		var handleHelper = scrollbar.find( ".od-ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span class='od-ui-icon od-ui-icon-grip-dotted-vertical'></span>" )
		.wrap( "<div class='od-ui-handle-helper-parent'></div>" ).parent();

		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow-y", "auto" );

		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.height() - scrollPane.height();
			var proportion = remainder / scrollContent.height();
			var handleSize = scrollPane.height() - ( proportion * scrollPane.height() );
			scrollbar.find( ".od-ui-slider-handle" ).css({
				height: handleSize,
				"margin-top": -handleSize / 2
			});
			handleHelper.height( "" ).height( scrollbar.height() - handleSize );
		}

		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.height() - scrollContent.height();
			var leftVal = scrollContent.css( "margin-top" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-top" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}

		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.height() + parseInt( scrollContent.css( "margin-top" ), 10 );
				var gap = scrollPane.height() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-top", parseInt( scrollContent.css( "margin-top" ), 10 ) + gap );
				}
		}

		//change handle position on window resize
		jQuery( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	}

};

callbackWidget.init = function(id){
    if (typeof(id) != 'undefined') {
        callbackWidget.callbackWidgetID = id.toString();
    } else {
        callbackWidget.callbackWidgetID = 'callback-script';
    }

    var head= document.getElementsByTagName('head')[0];

    if (typeof(jQuery) == 'undefined' || ! jQuery.noop) {
        var callback = function() {
            $.noConflict();
            //callbackWidget.loadUI();
            setTimeout(function() {
            	callbackWidget.app.initApp();
        	}, 700);
        }

        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
        script.onreadystatechange = callback;
        script.onload = callback;
        head.appendChild(script);
    } else {
        //callbackWidget.loadUI();
    	setTimeout(function() {
        	callbackWidget.app.initApp();
    	}, 700);
    }
}

/*callbackWidget.loadUI = function() {
    var callback = function() {
            jQuery.noConflict();
            callbackWidget.loadUI();
    }

    if ( ! jQuery.ui) {
        jQuery.getScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js',callbackWidget.app.initApp);
    }else{
        callbackWidget.app.initApp();
    }
}*/

callbackWidget.init();

