var current_url=location.href;
function UpdateCaptcha(img_url,img_id){
var timevar = new Date();
$('#'+img_id).attr('src',FULL_URL+img_url+'?link='+timevar.getTime());
}

function InitBottomColorbox()
{
		$.fn.colorbox.settings.transition = "fade";
		$.fn.colorbox.settings.bgOpacity = "0.9";
		$.fn.colorbox.settings.contentCurrent = "";
		$(".cmodal").colorbox();
	
}

function clear_field(field)
{
    if (field.value==field.defaultValue)
    {
        field.value=''
    }
}
 
function check_field(field)
{
    if (field.value=='' ||
    field.value==' ')
    {
        field.value=field.defaultValue
    }
}
function get_correct_str($num, $str1, $str2, $str3) {
	$val = $num % 100;

	if ($val > 10 && $val < 20) {return $num +' '+ $str3;}
	else {
	$val = $num % 10;
	if ($val == 1) {return $num +' '+ $str1;}
	else if ($val > 1 && $val < 5) {return $num +' '+ $str2;}
	else {return $num +' '+ $str3;}
	}
	}


function AfterLoadPage(){

//works lightbox
$.fn.colorbox.settings.transition = "elastic";
$.fn.colorbox.settings.bgOpacity = "0.9";
$.fn.colorbox.settings.current="";

$(".cbImg").colorbox({title: function(){
       return $(this).attr('ititle');
}});
//-------------------------------------------------


$("#order_dialog").dialog({ modal: true,
								 overlay: {
								 opacity: 0.5,
								 background: "black"
								
								
								 },
								 autoOpen:false,
								 resizable:false,
								
								  width:300
								 } );
									
	     
	   $('#photo-course').click(function(e){
	   $("#order_dialog").dialog('open');
	   return false;
	    })
		$('#order_dialog .close-float').click(function(e) { 
			 $("#order_dialog").dialog('close');
		}
		
		);
$('#order_dialog #show-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow) {
					$('#order-password-hidden').css('display', 'none');
					$('#order-password-view').css('display', 'inline');
					$('#order-password-view').attr('value', $('#order-password-hidden').attr('value'));
					$(this).css('display', 'none');
					$('#order_dialog #hide-symbols').css('display', 'inline');
					showPassword = true;
				}
			});


$('#order_dialog #order-password-hidden').bind({
				focus: function() {
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
					else {
						pressShow = true;
					}
					if (!pressShow) {
						$(this).css('display', 'none');
						$('#order-password-view').css('display', 'inline');
						$('#order-password-view').attr('value', $(this).attr('value'));
					}
				}
			});
	
			$('#order_dialog #order-password-view').bind({
				focus: function() {
					if ($(this).attr('title') == $(this).attr('value')) {
						$(this).attr('value', '');
					}
					if (!showPassword) {
						$(this).css('display', 'none');
						$('#order-password-hidden').css('display', 'inline');
						$('#order-password-hidden').attr('value', $(this).attr('value'));
						$('#order-password-hidden').focus();
					}
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
				}
			});
			$('#order_dialog #hide-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow) {
					$('#order-password-view').css('display', 'none');
					$('#order-password-hidden').css('display', 'inline');
					$('#order-password-hidden').attr('value', $('#order-password-view').attr('value'));
					$(this).css('display', 'none');
					$('#order_dialog #show-symbols').css('display', 'inline');
					showPassword = false;
				}
			});
			



// end order dialog		
		
// LOGIN	 DIALOG
var showPassword=false;
var pressShow = false;
var showPassword2=false;
var pressShow2 = false;

$("#login_dialog").dialog({ modal: true,
								 overlay: {
								 opacity: 1.5,
								 background: "black",
								 speed:100
								
								
								 },
								 autoOpen:false,
								 resizable:false,
								 width: 400,
								  show: 'fold'
								 } );	
$('#login-link').click(function(e) {
	 $("#login_dialog").dialog('open');							
});

$('#login_dialog .close-float').click(function(e) { 
			 $("#login_dialog").dialog('close');
});

$('#login_dialog #show-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow) {
					$('#login-password-hidden').css('display', 'none');
					$('#login-password-view').css('display', 'inline');
					$('#login-password-view').attr('value', $('#login-password-hidden').attr('value'));
					$(this).css('display', 'none');
					$('#login_dialog #hide-symbols').css('display', 'inline');
					showPassword = true;
				}
			});


$('#login_dialog #login-password-hidden').bind({
				focus: function() {
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
					else {
						pressShow = true;
					}
					if (!pressShow) {
						$(this).css('display', 'none');
						$('#login-password-view').css('display', 'inline');
						$('#login-password-view').attr('value', $(this).attr('value'));
					}
				}
			});
	
			$('#login_dialog #login-password-view').bind({
				focus: function() {
					if ($(this).attr('title') == $(this).attr('value')) {
						$(this).attr('value', '');
					}
					if (!showPassword) {
						$(this).css('display', 'none');
						$('#login-password-hidden').css('display', 'inline');
						$('#login-password-hidden').attr('value', $(this).attr('value'));
						$('#login-password-hidden').focus();
					}
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
				}
			});
			$('#login_dialog #hide-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow) {
					$('#login-password-view').css('display', 'none');
					$('#login-password-hidden').css('display', 'inline');
					$('#login-password-hidden').attr('value', $('#login-password-view').attr('value'));
					$(this).css('display', 'none');
					$('#login_dialog #show-symbols').css('display', 'inline');
					showPassword = false;
				}
			});
			
				

// END LOGIN	 DIALOG

// REGISTER DIALOG
$("#register_dialog").dialog({ modal: true,
								 overlay: {
								 opacity: 0.5,
								 background: "black"
								
								
								 },
								 autoOpen:false,
								 resizable:false,
								 width: 400,
								  show: 'fold'
								 } );	
$('#register_dialog .close-float').click(function(e) { 
			 $("#register_dialog").dialog('close');
});

$('#register-link').click(function(e) {
	 $("#register_dialog").dialog('open');							
});			
			
			
			$('#register-password-hidden').bind({
				focus: function() {
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
					else {
						pressShow2 = true;
					}
					if (!pressShow2) {
						$(this).css('display', 'none');
						$('#register-password-view').css('display', 'inline');
						$('#register-password-view').attr('value', $(this).attr('value'));
					}
				}
			});
	
			$('#register-password-view').bind({
				focus: function() {
					if ($(this).attr('title') == $(this).attr('value')) {
						$(this).attr('value', '');
					}
					if (!showPassword2) {
						$(this).css('display', 'none');
						$('#register-password-hidden').css('display', 'inline');
						$('#register-password-hidden').attr('value', $(this).attr('value'));
						$('#register-password-hidden').focus();
					}
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
				}
			});
			
			$('#register_dialog  #show-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow2) {
					$('#register-password-hidden').css('display', 'none');
					$('#register-password-view').css('display', 'inline');
					$('#register-password-view').attr('value', $('#register-password-hidden').attr('value'));
					$(this).css('display', 'none');
					$('#register_dialog  #hide-symbols').css('display', 'inline');
					showPassword2 = true;
				}
			});
			
			$('#register_dialog  #hide-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow2) {
					$('#register-password-view').css('display', 'none');
					$('#register-password-hidden').css('display', 'inline');
					$('#register-password-hidden').attr('value', $('#register-password-view').attr('value'));
					$(this).css('display', 'none');
					$('#register_dialog  #show-symbols').css('display', 'inline');
					showPassword2 = false;
				}
			});


// END REGISTER DIALOG

// PERSONAL
$('#personal-password-hidden').bind({
				focus: function() {
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
					else {
						pressShow2 = true;
					}
					if (!pressShow2) {
						$(this).css('display', 'none');
						$('#personal-password-view').css('display', 'inline');
						$('#personal-password-view').attr('value', $(this).attr('value'));
					}
				}
			});
	
			$('#personal-password-view').bind({
				focus: function() {
					if ($(this).attr('title') == $(this).attr('value')) {
						$(this).attr('value', '');
					}
					if (!showPassword2) {
						$(this).css('display', 'none');
						$('#personal-password-hidden').css('display', 'inline');
						$('#personal-password-hidden').attr('value', $(this).attr('value'));
						$('#personal-password-hidden').focus();
					}
				},
				blur: function() {
					if ($(this).attr('value') == '') {
						$(this).attr('value', $(this).attr('title'));
					}
				}
			});
			
			$('.personal-info #show-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow2) {
					$('#personal-password-hidden').css('display', 'none');
					$('#personal-password-view').css('display', 'inline');
					$('#personal-password-view').attr('value', $('#personal-password-hidden').attr('value'));
					$(this).css('display', 'none');
					$('.personal-info #hide-symbols').css('display', 'inline');
					showPassword2 = true;
				}
			});
			
			$('.personal-info #hide-symbols').click(function(e) {
				// Disable default event
				e.preventDefault();
				
				if (pressShow2) {
					$('#personal-password-view').css('display', 'none');
					$('#personal-password-hidden').css('display', 'inline');
					$('#personal-password-hidden').attr('value', $('#personal-password-view').attr('value'));
					$(this).css('display', 'none');
					$('.personal-info #show-symbols').css('display', 'inline');
					showPassword2 = false;
				}
			});


// END PERSONAL
// COMMENT
$('.add-comment').click(function(e) {
		// Disable default event
		e.preventDefault();
		
		$('.add-comment-layer').css('display', 'block');
		$('.add-comment-layer-2 #post_text').keyup();
	});
$('#comment-close').click(function(e) {
		// Disable default event
		e.preventDefault();
		
		$('.add-comment-layer').css('display', 'none');
	});
$('#comment-post').click(function(e) {
		// Disable default event
		e.preventDefault();
		AddComment();
		
		
	});
// END COMMENT
// MONEY REQUEST
$('#show-get-money-form').click(function(e) {
		// Disable default event
		e.preventDefault();
		
		if($('.amount-button').css('display')=='none'){
			$('.amount-button').css('display','inline-block');
			$('.amount-button2').css('display','none');
		}
		$('#get-money-form-fields').toggle(800);
		
	});
$('.amount-button').click(function(e) {
		// Disable default event
		e.preventDefault();
			$('.amount-button2').css('display','inline-block');
			$('.amount-button').css('display','none');
		
		$.post(

		  FULL_URL+'user/moneyrequest/',
		  {
			sum:$('#get-amount').val(),
			payment:$('#get-payment').val()
			
		  }
		);
		
	
	});
$('.amount-button2').click(function(e) {
		// Disable default event
		e.preventDefault();
			
	
	});

//  MONEY REQUEST

//setupZoom();
$('.add-comment-layer-2 #post_text').bind('keyup',function(e) {
	var max_len=350;
	var free_len=max_len-this.value.length;
	if(this.value == $(this).attr('title')){
		free_len=max_len;
		}
	if(free_len>0)
	{
		if(free_len==max_len)
			{
			$('#sign-cnt').html(get_correct_str(free_len,'знак','знака','знаков'));
			$('#sign-text').html('Лимит: ');
			}
		else{
			if((free_len%10==1)&&(free_len!=11))
			{					
				$('#sign-text').html('Остался ');
			}
			else
				{
					$('#sign-text').html('Осталось ');
				}
			$('#sign-cnt').html(get_correct_str(free_len,'знак','знака','знаков'));
			$('.limit-info').css('display','block');
			$('.limit-warning').css('display','none');
		}
	}else{
		this.value=this.value.substr(0,max_len);
		$('.limit-info').css('display','none');
		$('.limit-warning').css('display','block');
	}
});
$('.add-comment-layer-2 #post_text').bind('change',function(e) {
	 $(this).keyup();
	});


}



function UserLogin(par_login, par_pass,par_remember){


$.post(

  FULL_URL+'user/login/',
  {
    login:par_login,
    pass:par_pass,
    remember:par_remember
    
  },
  onUserLogin
);
}
function onUserLogin(data)
{
obj=eval(data);
if (!(obj.error)){
	if(obj.cname!='')
$.cookie(obj.cname, obj.cvalue, { expires: obj.ctime}); 
 
 if (current_url.indexOf('user')>0 )
	{
	location.href='/';
	 }
	else{
	  location.reload(true);
	}
  
  
}else{$('#login_err_text').html(obj.error);}
}

function GoEnroll(){
LockPage();
	if($('#course-scan').length>0)
	{
		$.ajaxFileUpload
					(
						{
							url:FULL_URL+'courses/enroll/upload_file/', 
							secureuri:false,
							fileElementId:'course-scan',
							dataType: 'json',
							success: function (data, status)
							{
								
										UnlockPage();
										//alert(data.filename);
										if(data.err!=''){
										
											alert(data.err);
										}else 
										{
										if(data.is_required==1){
										 $('#lbl_course-scan').html('Файл отправлен');
										}
										file_html=$('#lbl_course-scan').html();	
										$('#lbl_course-scan').html('');
										SendRequest();
										 $('#lbl_course-scan').html(file_html);	
										}
							},
							error: function (data, status, e)
							{
								  UnlockPage();
								  alert(e);
							}
						}
					);
	} else {
		SendRequest();
	}
}
function SendRequest(){
	ClearEmptybyTitle('#course-form input[title], #course-form textarea[title]')
	
	 var str_vars = $("#course-form").serialize();
	
	  $('.course-button').attr('disabled','disabled');
	// LockPage();
	 $.post(
		
		 $('#course-form').attr('action'),
		  {
			vars:str_vars,
			 birthday:$('#course-year').val()+'-'+($('#course-month').val())+'-'+$('#course-day').val(),
			pass:$('#order-password-view').css('display')=='inline'?$('#order-password-view').val():$('#order-password-hidden').val()					
		  },
		  onSendRequest);
	
	FillEmptybyTitle('#course-form input[title], #course-form textarea[title]');
	};
function onSendRequest(data)
{
UnlockPage();
obj=eval(data);
if(obj.course_error=='')
{

	if(obj.user_error!='')
	{
	alert(obj.user_error);
	}
	
	$('#order_dialog').dialog('close');
	$('#photo-course-btn').attr('src','/images/zapis-na-'+obj.category_alias+'-succsess.png');
	$('#course-succes').css('display','block');
	$('#photo-course').css('display','none');
	if(obj.reload_page){
		window.location.reload();
	}
}else
{
	alert(obj.course_error);
	  $('.course-button').removeAttr('disabled');
}
}	


function UserRegister(){
ClearEmptybyTitle('#register-form input[title], #register-form textarea[title]');	 
 var str_vars = $("#register-form").serialize();
$.post(

  FULL_URL+'user/process_register/',
  {
    name:$('#register-name').val(),
    surname:$('#register-family').val(),
    email:$('#register-email').val(),
    pass:$('#register-password-view').css('display')=='inline'?$('#register-password-view').val():$('#register-password-hidden').val()
	
  },
  onUserRegister
);
FillEmptybyTitle('#register-form input[title], #register-form textarea[title]');
}
function onUserRegister(data)
{
obj=eval(data);
if (!(obj.error)){
location.href='/user/personal/';
 /* if (current_url.indexOf('user')>0 )
	{
	location.href='/user/personal/';
	 }
	else{
	  location.reload(true);
	}
  */
  
}else{$('#register_err_text').html(obj.error);}
}

//forget

function SendPass(){
    //e.preventDefault();
	ClearEmptybyTitle('#login-form input[title]');	 
	 login=$('#login-email').val();
	 if(login=='')
	 {
		alert("Заполните поле Email");
		return false;
		 
	 }
	  $('#forget-pass').html('Подождите');
	 $.post(
		  FULL_URL+'user/forget_send/',
		  {
			login:login
								
		  },
		  onSendPass);
		
	FillEmptybyTitle('#logine-form input[title]');
	
	};
function onSendPass(data)
{
obj=eval(data);
if ((obj.error)){
	alert(obj.error);
	 $('#forget-pass').html('Вышлите мне пароль!');
}
else {
	 $('#forget-pass').attr('onClick','');
	  $('#forget-pass').html('Пароль выслан');
	}
}
//----------------





function FillEmptybyTitle(selector){
	 var inputs=$(selector)
		
		 for (i=0;i<inputs.length;i++){
		 el= $(inputs[i]);
		 if(el.val()=='')
		 el.val(el.attr('title'));
		 }
	}
function ClearEmptybyTitle(selector){
	// clear empty
 var inputs=$(selector);
		// var i=1;
		 for (i=0;i<inputs.length;i++){
		 el= $(inputs[i]);
		 if(el.attr('title')==el.val())
		 el.val('');
		 }
	//end clear empty	
}
function UserUpdate(){
	
	ClearEmptybyTitle('#personal-form input[title], #personal-form textarea[title]');	 
 var str_vars = $("#personal-form").serialize();
$.post(

  FULL_URL+'user/update/',
  {
   vars:str_vars,
       pass:$('#personal-password-view').css('display')=='inline'?$('#personal-password-view').val():$('#personal-password-hidden').val(),
	   birthday:$('#personal-year').val()+'-'+($('#personal-month').val())+'-'+$('#personal-day').val()

  },
  onUserUpdate
);
FillEmptybyTitle('#personal-form input[title], #personal-form textarea[title]');
}
function UpdateButton(){
	
	if( $('.personal-button').attr('tag')=='1'){
	$('.personal-button').css('background-image','url("/images/save-button.png")');
	$('.personal-button').attr('tag','0');
	}
}
function onUserUpdate(data)


{	
obj=eval(data);
if ((obj.error)){
	alert(obj.error);
}
if ((obj.msg)){
	alert(obj.msg);
}
if(obj.completion){
	$('#completion').html(obj.completion);
}
$('#personal-form input[title], #personal-form textarea[title]').unbind('keypress',UpdateButton);
	$('.personal-button').css('background-image','url("/images/saved-button.png")');
	$('.personal-button').attr('tag','1');
	$('#personal-form input[title], #personal-form textarea[title]').bind('keypress',UpdateButton);	 
	
}

function AddComment(){
	
	ClearEmptybyTitle('#add-comment-form input[title], #add-comment-form textarea[title]')	 ;

$.post(

  FULL_URL+'courses/addcomment/',
  {
       post_text:$('#add-comment-form #post_text').val(),
	   parent_id:$('#course-id').val()

  },
  onAddComment
);
FillEmptybyTitle('#add-comment-form input[title], #add-comment-form textarea[title]');
}
function onAddComment(data)
{
	obj=eval(data);
	if(obj.msg){
		alert(obj.msg);
	}
	$('.add-comment-layer').css('display', 'none');
	location.reload(true);
}

function add_favorite(a) {
  title=document.title;
  url=document.location;
  try {
    // Internet Explorer
    window.external.AddFavorite(url, title);
  }
  catch (e) {
    try {
      // Mozilla
      window.sidebar.addPanel(title, url, "");
    }
    catch (e) {
      // Opera
      if (typeof(opera)=="object") {
        a.rel="sidebar";
        a.title=title;
        a.url=url;
        return true;
      }
      else {
        // Unknown
        alert('Нажмите Ctrl-D чтобы добавить страницу в закладки');
      }
    }
  }
  return false;
} 
function CloseBanner(banner_id)
{
	$('#'+banner_id).css('display','none');
	$.post(

  FULL_URL+'closebanner/',
  {
       banner_alias:banner_id
	  

  }
  
);
}
function LockPage(){
	 $.blockUI({ 
            message: $('#img-loading'), 
            css: { 
				 border: 'none', 
				 backgroundColor: 'transparent',
                top:  ($(window).height() - 19) /2 + 'px', 
                left: ($(window).width() - 220) /2 + 'px', 
                width: '66px' 
            } 
        });
	 setTimeout($.unblockUI, 500000);
}
function UnlockPage()
{
	$.unblockUI();
}
$(document).ready(AfterLoadPage);


