$(document).ready(function() {

          // set site base url    
          var base_url = $('#hidden_base_url').val();

          // register form fields validation - show errors from server side validation
         
         
         // hide register errors on focus

         $('#register_form input').focus(function() {
              var id=$(this).attr('id');
              $('#register_form #error_'+id).fadeOut('fast');
              $('#register_form #error_register').fadeOut('fast');
         });

         // hide register errors on hover

         $('#register_form p').hover(function() {
              $(this).fadeOut('fast');
         });

         // validation when user click on register button without filling all mandatory fields

         $('#register_form #register').click(function(){
             

              var username=$('#register_form #username').val();
              var password=$('#register_form #password').val();
              var confirm_password=$('#register_form #confirm_password').val();
              var nickname=$('#register_form #nickname').val();
              var email=$('#register_form #email').val();
              var location=$('#register_form #location').val();
              var birthday=$('#register_form #birthday').val();
              var secure_answer=$('#register_form #secure_answer').val();
              var custom_secure_question=$('#register_form #custom_secure_question').val();
              var captcha_text=$('#register_form #captcha_text').val();
              var secure_question=$("#register_form #secure_question").val();

             if((username=='')||(password=='')||(confirm_password=='')||(nickname=='')||(email=='')||(birthday=='')||(secure_answer=='')||
              (captcha_text=='')||((custom_secure_question=='')&&(secure_question==6)))
               {
                   if(username=='')
                   {
                        $('#register_form #error_username').html('The Username field is required');
                        $('#register_form #error_username').css('display','block');
                   }
                   if(password=='')
                   {
                        $('#register_form #error_password').html('The Password field is required');
                        $('#register_form #error_password').css('display','block');
                   }
                   if(confirm_password=='')
                   {
                        $('#register_form #error_confirm_password').html('The Confirm password field is required');
                        $('#register_form #error_confirm_password').css('display','block');
                   }
                   if(nickname=='')
                   {
                        $('#register_form #error_nickname').html('The Nickname field is required');
                        $('#register_form #error_nickname').css('display','block');
                   }
                   if(email=='')
                   {
                        $('#register_form #error_email').html('The Email field is required');
                        $('#register_form #error_email').css('display','block');
                   }
                   if(birthday=='')
                   {
                       $('#register_form #error_birthday').html('The Year of birth field is required');
                       $('#register_form #error_birthday').css('display','block');
                   }
                   if(secure_answer=='')
                   {
                        $('#register_form #error_secure_answer').html('The Security answer field is required');
                        $('#register_form #error_secure_answer').css('display','block');
                   }
                   if((custom_secure_question=='')&&(secure_question==6))
                   {
                        $('#register_form #error_custom_secure_question').html('The Own security question field is required');
                        $('#register_form #error_custom_secure_question').css('display','block');
                   }
                   if(captcha_text=='')
                   {
                        $('#register_form #error_captcha_text').html('The Captcha code field is required');
                        $('#register_form #error_captcha_text').css('display','block');
                   }


                   $('#register_form #error_register').html('You must fill all mandatory fields');
                   $('#register_form #error_register').css('display','block');
                   return false;
                   
               }
               else
               {

                    var form_data = {
                          username : username,
                          password : password,
                          confirm_password : confirm_password,
                          nickname : nickname,
                          email : email,
                          birthday :birthday,
                          location : location,
                          secure_answer : secure_answer,
                          custom_secure_question : custom_secure_question,
                          captcha_text : captcha_text,
                          ajax : '1'
                      }

                      var register_fields_ok=1;

                      $.ajax({
                          url: base_url+"user/ajax_check_register",
                          type: 'POST',
                          data: form_data,
                          async : false,
                          success: function(msg) {

                               // parse json response and show errors


                        var obj=jQuery.parseJSON(msg);

                        if(obj)
                        {

                              if(obj.username!='')
                              {
                                    $('#register_form #error_username').html(obj.username);
                                    $('#register_form #error_username').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.password!='')
                              {
                                    $('#register_form #error_password').html(obj.password);
                                    $('#register_form #error_password').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.confirm_password!='')
                              {
                                    $('#register_form #error_confirm_password').html(obj.confirm_password);
                                    $('#register_form #error_confirm_password').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.nickname!='')
                              {
                                    $('#register_form #error_nickname').html(obj.nickname);
                                    $('#register_form #error_nickname').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.email!='')
                              {
                                    $('#register_form #error_email').html(obj.email);
                                    $('#register_form #error_email').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.birthday!='')
                              {
                                    $('#register_form #error_birthday').html(obj.birthday);
                                    $('#register_form #error_birthday').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.location!='')
                              {
                                    $('#register_form #error_location').html(obj.location);
                                    $('#register_form #error_location').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.secure_answer!='')
                              {
                                    $('#register_form #error_secure_answer').html(obj.secure_answer);
                                    $('#register_form #error_secure_answer').css('display','block');
                                    register_fields_ok=0;
                              }
                              if((obj.custom_secure_question!='')&&(secure_question==6))
                              {
                                    $('#register_form #error_custom_secure_question').html(obj.custom_secure_question);
                                    $('#register_form #error_custom_secure_question').css('display','block');
                                    register_fields_ok=0;
                              }
                              if(obj.captcha_text!='')
                              {
                                    $('#register_form #error_captcha_text').html(obj.captcha_text);
                                    $('#register_form #error_captcha_text').css('display','block');
                                    register_fields_ok=0;
                              }
                         }
                     }

                     });


                 if(register_fields_ok==1)
                 {
                      return true;
                 }
                 else if(register_fields_ok==0)
                 {
                      return false;
                 }
                 
               }

             
          });
                 

         // insert new password form validation

         $('#new_password_form #submit').click(function(){
              
              var username=$('#new_password_form #username').val();
              var password=$('#new_password_form #password').val();
              var confirm_password=$('#new_password_form #confirm_password').val();
              var new_password_data_ok;
              var form_data = {username : username , password : password , confirm_password : confirm_password , ajax : '1'}
             

              $.ajax({
                  url: base_url+"user/ajax_check_new_password",
                  type: 'POST',
                  async : false,
                  data: form_data,
                  success: function(msg) {

                       
                       if(msg=='TRUE')
                       {      
                            new_password_data_ok=1;

                       }
                       else
                       {
                            new_password_data_ok=0;
                       }

                  }                  
              });

              if((username=='')||(password=='')||(confirm_password==''))
              {
              if(username=='')
               {
                   $('#new_password_form #error_username').html('The Username field is required');
                   $('#new_password_form #error_username').css('display','block');
               }
               if(password=='')
               {
                   $('#new_password_form #error_password').html('The Password field is required');
                   $('#new_password_form #error_password').css('display','block');
               }
               if(confirm_password=='')
               {
                   $('#new_password_form #error_confirm_password').html('The Confirm password field is required');
                   $('#new_password_form #error_confirm_password').css('display','block');
               }
               return false;
               }
               else if(new_password_data_ok==1)
               {
                   return true;
               }
               else
               {
                   $('#new_password_form #error_submit').html('Please insert correct data');
                   $('#new_password_form #error_submit').css('display','block');
                   return false; 
               }

          });
          
          $('#new_password_form p').hover(function() {
	       $(this).css('display','none');
          });

          $('#new_password_form input').focus(function() {
	       var id=$(this).attr('id');
               $("#error_"+id).css('display','none');
               $("#error_submit").css('display','none');
          });
          
          
          
          // refresh captcha image
          
         $('#refresh_captcha_button').click(function(){
             
             $.ajax({
                  url: base_url+"user/refresh_captcha",
                  type: 'POST',
                  async : false,
                  success: function(msg) {
                              $('#captcha').attr('src',msg);
                           }
                  });
          });

           
       //edit account details form
  
        $('#insert_old_password #insert').click(function(){

            var nickname=$('#nickname',window.parent.document).val();
            var password=$('#password',window.parent.document).val();
            var confirm_password=$('#confirm_password',window.parent.document).val();
            var email=$('#email',window.parent.document).val();
            var custom_secure_question=$('#custom_secure_question',window.parent.document).val();
            var secure_answer=$('#secure_answer',window.parent.document).val();
            var old_password=$('#insert_old_password #password').val();
            
            var account_details_data_ok;
            
            var form_data = {
                    nickname : nickname ,
                    password : password ,
                    confirm_password : confirm_password ,
                    email:email,
                    custom_secure_question:custom_secure_question,
                    secure_answer:secure_answer,
                    old_password:old_password,
                    ajax : '1'
                }
                               
              
            if((nickname=='')&&(password=='')&&(confirm_password=='')&&(email=='')&&(custom_secure_question=='')&&(secure_answer==''))
            {
                 $('#account_details_error',window.parent.document).html('<p>You have not updated any field</p>');
                 $('#account_details_error',window.parent.document).show();
                 $(document).trigger('close.facebox');
                 return false;
            }
            else if(password!=confirm_password)
            {
                 $('#account_details_error',window.parent.document).html('<p>The Confirm new password field does not match the New password field.</p>');
                 $('#account_details_error',window.parent.document).show();
                 $(document).trigger('close.facebox');
                 return false;
            }
            else
            {
                  $.ajax({
                  url: base_url+"account/ajax_check_account_details",
                  type: 'POST',
                  async : false,
                  data: form_data,
                  success: function(msg) {

                       if(msg=='TRUE')
                       {
                            account_details_data_ok=1;
                            $('#old_password',window.parent.document).val($('#insert_old_password #password').val());
                       }
                       else if(msg=='old_password_error')
                       {
                           account_details_data_ok=0;
                           $('#insert_old_password #old_password_error').html('Please insert correct password');
                           $('#insert_old_password #old_password_error').css('display','block');
                       }
                       else
                       {
                            account_details_data_ok=0;
                            $('#account_details_error',window.parent.document).html(msg);
                            $('#account_details_error',window.parent.document).show();
                            $(document).trigger('close.facebox');
                            
                       }     
                  }
                });
                
                if(account_details_data_ok==1)
                {
                    $('#edit_account_details',window.parent.document).submit();
                    $(document).trigger('close.facebox');
                    return false;
                }
                else
                {
                    return false;
                }
                
                
            }

        });
     

        $('#insert_old_password #cancel_facebox').click(function(){
            $(document).trigger('close.facebox');
        });

        $('#insert_old_password p').hover(function() {
	          $(this).css('display','none');
        });
        
        
        
        // block unwanted user
        
        $('#block_button').click(function(){ 
        
        	 $('#block_list_message').html('');

           var username=$('#unwanted_username').val();
           var form_data = {
                    username : username ,
                    ajax : '1'
                }
         
          if(username!='')
          {
             $.ajax({
                  url: base_url+"account/ajax_block_unwanted_user",
                  type: 'POST',
                  async : false,
                  data: form_data,
                  success: function(msg) {
                     
                      if(msg=='unwanted_username_already_exist')
                      {
                          $('#block_list_message').html('You have this username in your block list!');
                      }
                      else if(msg=='username_not_exist')
                      {
                          $('#block_list_message').html('This username doesn\'t exist!');
                      }
                      else if(msg=='own_username')
                      {
                          $('#block_list_message').html('This is your username!You can not block your account!');
                      }
                      else
                      {
                          if($('#blocked_usernames ul li:first').length>0)
                          {
                               $("<li><span>"+msg+"</span><a href='' class='unblock_button'>Unblock</a></li>").insertBefore('#blocked_usernames ul li:first');
                          }
                          else
                          {
                               $('#blocked_usernames ul').append("<li><span>"+msg+"</span><a href='' class='unblock_button'>Unblock</a></li>");
                          }
                          $('#unwanted_username').val('');
                      }
                  }
              });
         }
         else
         {
               $('#block_list_message').html('You must insert username!');
         }     
         
        return false;
        });
        
        
        // unblock unwanted user
        
        $('.unblock_button').live('click',function(){
        
        $('#block_list_message').html('');
        
              var username=$(this).parent().find('span').html();

              var form_data = {
                    username : username ,
                    ajax : '1'
                }
        
                  	$.confirm({
                                      'title': 'Unblock Confirmation',
                                      'message': 'You are about to unblock this user. Continue?',
                                      'buttons': {
                                          'Yes'	: {
                                                       'class': 'blue',
                                                       'action': function(){
                                                                      $.ajax({
                                                                          url: base_url+"account/ajax_unblock_unwanted_user",
                                                                          type: 'POST',
                                                                          async : false,
                                                                          data: form_data,
                                                                          success: function(msg) {

                                                                                      $('#blocked_usernames ul li span:contains('+msg+')').parent().remove();
                                                                                   }
                                                                      });
                                                                  }
                                                     },
                                            'No': {
                                                        'class'	: 'gray',
                                                  }
                                            }
	          	       });
       
        return false;

        });

       
       // confirm delete account with password
       
       $('#confirm_delete_account #insert').click(function(){

            var password=$('#confirm_delete_account #password').val();
            var password_ok=0;

            var form_data = {
                    password:password,
                    ajax : '1'
                }
              
                  $.ajax({
                  url: base_url+"account/ajax_check_password",
                  type: 'POST',
                  async : false,
                  data: form_data,
                  success: function(msg) {
                       if(msg=='TRUE')
                       {
                            password_ok=1;
                       }
                       else
                       {
                           password_ok=0;
                       }   
                  }
                  
                });
                
                if(password_ok==1)
                {
                     return true;
                }
                else if(password_ok==0)
                {
                     $('#confirm_delete_account #password_error').html('Please insert correct password');
                     $('#confirm_delete_account #password_error').css('display','block');
                     return false;
                }
       
        });
     

        $('#confirm_delete_account #cancel_facebox').click(function(){
            $(document).trigger('close.facebox');
        });

        $('#confirm_delete_account p').hover(function() {
	        $(this).css('display','none');
        });
        
        $('#confirm_delete_account #password').focus(function() {
	        $('#confirm_delete_account p').css('display','none');
        });
       
       
          // height and weight convertor

          $('#edit_myprofile_extended #height').keyup(function(){
               var height_inch=Math.round($('#edit_myprofile_extended #height').val()/2.54);
               $('#edit_myprofile_extended #height_inch').val(height_inch);
          });

          $('#edit_myprofile_extended #height_inch').keyup(function(){
               var height_cm=Math.round($('#edit_myprofile_extended #height_inch').val()*2.54);
               $('#edit_myprofile_extended #height').val(height_cm);
          });

          $('#edit_myprofile_extended #weight').keyup(function(){
               var weight_pound=Math.round($('#edit_myprofile_extended #weight').val()*(2.2046));
               $('#edit_myprofile_extended #weight_pound').val(weight_pound);
          });

          $('#edit_myprofile_extended #weight_pound').keyup(function(){
               var weight_kg=Math.round($('#edit_myprofile_extended #weight_pound').val()/(2.2046));
               $('#edit_myprofile_extended #weight').val(weight_kg);
          });

             
         // ajax check new message fields    

         $('#new_message #message_send').live("click",function(){
              
              var message_to=$('#new_message #message_to').val();
              var message_subject=$('#new_message #message_subject').val();
              var message_body=$('#new_message #message_body').val();

             
              var form_data = {
                    message_to : message_to,
                    message_subject : message_subject,
                    message_body : message_body,
                    ajax:'1'
                };
              var nm_ok;

              if((message_to=='')||(message_subject=='')||(message_body==''))
               {
                   if(message_to=='')
                   {
                        $('#new_message #error_message_to').html('The Username to field is required');
                        $('#new_message #error_message_to').css('display','block');
                   }
                   if(message_subject=='')
                   {
                        $('#new_message #error_message_subject').html('The Subject field is required');
                        $('#new_message #error_message_subject').css('display','block');
                   }
                   if(message_body=='')
                   {
                        $('#new_message #error_message_body').html('The Message field is required');
                        $('#new_message #error_message_body').css('display','block');
                   }
                   return false;
               }
              else if((message_to!='')&&(message_subject!='')&&(message_body!=''))
               {

                  $.ajax({
                      url: base_url+"message/ajax_check_new_message",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                          
                         if(msg=='TRUE')
                         {
                               nm_ok='OK';
                         }
                         else
                         {
                               nm_ok=msg;
                         }
                       }
                   });


                   if(nm_ok=='OK')
                   {

                      $('#new_message #message_send').click(function(){
                             return false;
                      });

                      $.ajax({
                          url: base_url+"message/new_message",
                          type: 'POST',
                          async : false,
                          data: form_data
                       });

                       $('#new_message #error_message_send').html('Message sent successfully');
                       $('#new_message #error_message_send').css('display','block');
       
                       setTimeout( function() {
                                $(document).trigger('close.facebox');
                        }, 3000 );
                                                   
                       return false;
                   }
                   else
                   {
                       $('#new_message #error_message_send').html(nm_ok);
                       $('#new_message #error_message_send').css('display','block');
                       return false;
                   }
                   
               }
            });


         $('#new_message input,#new_message textarea').live("focus",function(){
              var id=$(this).attr('id');
              $('#new_message #error_'+id).fadeOut('fast');
              $('#new_message #error_message_send').fadeOut('fast');
         });

           
           // mark unread message as read

           $('#mark_as_read').live("click",function(){

                 var current_link=$(this);
                 var m_id=$(this).parent().find('#hidden_message_id').val();
                 var form_data = {
                    message_id : m_id,
                    ajax:'1'
                 };

                 $.ajax({
                          url: base_url+"message/mark_as_read",
                          type: 'POST',
                          async : false,
                          data: form_data,
                          success: function(msg) {

                              if(msg=='OK')
                              {
                                  current_link.parent().parent().removeClass('unread');
                                  current_link.attr('id','mark_as_unread');
                                  current_link.attr('title','Mark as unread');
                              }
                          }
                       });

                 return false;
            });

            // mark read message as unread

            $('#mark_as_unread').live("click",function(){

                 var current_link=$(this);
                 var m_id=$(this).parent().find('#hidden_message_id').val();
                 var form_data = {
                    message_id : m_id,
                    ajax:'1'
                 };

                 $.ajax({
                          url: base_url+"message/mark_as_unread",
                          type: 'POST',
                          async : false,
                          data: form_data,
                          success: function(msg) {

                              if(msg=='OK')
                              {
                                  current_link.parent().parent().addClass('unread');
                                  current_link.attr('id','mark_as_read');
                                  current_link.attr('title','Mark as read');
                              }
                          }
                       });

                 return false;
            });


            // delete message from inbox

                $('#delete_message_inbox').live("click",function(){

                 var current_link=$(this);
                 var m_id=$(this).parent().find('#hidden_message_id').val();
                 var form_data = {
                    message_id : m_id,
                    ajax:'1'
                 };

                 $.ajax({
                          url: base_url+"message/delete_message_inbox",
                          type: 'POST',
                          async : false,
                          data: form_data
                       });

            });


                // delete sent messages

                $('#delete_sent_message').live("click",function(){

                 var current_link=$(this);
                 var m_id=$(this).parent().find('#hidden_message_id').val();
                 var form_data = {
                    message_id : m_id,
                    ajax:'1'
                 };

                 $.ajax({
                          url: base_url+"message/delete_sent_message",
                          type: 'POST',
                          async : false,
                          data: form_data
                       });

            });


            // sort messages

             $('#sort_messages').live("change",function(){

                 var sort_option=$(this).val();

                     if(sort_option)
                     {
                          $('#sort_messages_form').attr('action',base_url+"message/sort_messages/"+sort_option)
                          $('#sort_messages_form').submit();
                     }

              });


              // sort all my photos

             $('#sort_all_my_photos').live("change",function(){

                 var sort_option=$(this).val();

                     if(sort_option)
                     {
                          $('#sort_all_my_photos_form').attr('action',base_url+"myprofile/sort_all_my_photos/"+sort_option)
                          $('#sort_all_my_photos_form').submit();
                     }

              });
              
              
             // sort all my videos

             $('#sort_all_my_videos').live("change",function(){

                 var sort_option=$(this).val();

                     if(sort_option)
                     {
                          $('#sort_all_my_videos_form').attr('action',base_url+"myprofile/sort_all_my_videos/"+sort_option)
                          $('#sort_all_my_videos_form').submit();
                     }

              });


              // sort photos

             $('#sort_photos').live("change",function(){

                 var sort_option=$(this).val();

                     if(sort_option)
                     {
                          $('#sort_photos_form').attr('action',base_url+"photo/sort_photos/"+sort_option)
                          $('#sort_photos_form').submit();
                     }

              });
                                          
              
         // if user want change uploaded photo

         $('#photo_data_form #change_photo').click(function(){

                var encoded_name=$('#encoded_name').val();
                var extension=$('#extension').val();

                var form_data = {
                      encoded_name:encoded_name,
                      extension:extension,
                      ajax:'1'
                };


                $.ajax({
                    url: base_url+"photo/ajax_delete_temp_photo",
                    type: 'POST',
                    async : false,
                    data: form_data
                });

                
                $('#photo_file').show();
                $('#hidden_fields').empty();
                $('#upload_photo img:not(#uploading_status)').remove();
                $('#change_photo').hide();
                $('#send_data_status').empty();
                $('#hidden_upload_name').empty();
         });


          // send photo data validation

          $('#photo_data_form #upload_photo_button').click(function(){

              $('#send_data_status').empty();
              
              var encoded_name=$('#encoded_name').val();
              var extension=$('#extension').val();
              var original_name=$('#original_name').val();
              var photo_category=$('#photo_category').val();
              var photo_country=$('#photo_country').val();
              var photo_title=$('#photo_title').val();
              var photo_type=$('#photo_type').val();
              var agree_tos=$('#agree_tos:checked').val();

              var form_data = {
                      encoded_name:encoded_name,
                      extension:extension,
                      original_name:original_name,
                      photo_category:photo_category,
                      photo_title:photo_title,
                      photo_type:photo_type,
                      agree_tos:agree_tos,
                      ajax:'1'
                };

              var photo_data_ok;

              if(!(encoded_name)||!(extension)||!(original_name))
               {
                   $('#send_data_status').append('<div class="generalNote"><p>Please upload photo</p></div>');
                   return false;
               }
               else
               {

                  $.ajax({
                      url: base_url+"photo/ajax_check_photo_data",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                         if(msg=='TRUE')
                         {
                               photo_data_ok=true;
                         }
                         else
                         {
                              $('#send_data_status').append('<div class="generalNote"><p>'+msg+'</p>');
                               photo_data_ok=false;
                         }
                       }
                   });


                   if((!photo_data_ok))
                   {
                       return false;
                   }
                   else
                   {
                       $(window).unbind('unload');
                       return true;
                   }
               }
            });


            // photo navigation - previous, next photo

            $('#photo_viewer #previous_photo').live('click',function(){

                    var photo_id=$('#photo_viewer #photo_id').val();
                    var owner_id=$('#photo_viewer #owner_id').val();

                    var form_data = {
                      photo_id:photo_id,
                      owner_id:owner_id,
                      ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/show_previous_photo",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                           $('div#photo_viewer').html(msg);
                       }
                   });

                    return false;
            });

            $('#photo_viewer #next_photo').live('click',function(){

                    var photo_id=$('#photo_viewer #photo_id').val();
                    var owner_id=$('#photo_viewer #owner_id').val();

                    var form_data = {
                      photo_id:photo_id,
                      owner_id:owner_id,
                      ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/show_next_photo",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                           $('div#photo_viewer').html(msg);
                       }
                   });

                    return false;
            });

          // photo rating

          $('.star').live('mouseover',function (){
                    var star = $(this).index()+1;
                    $(this).parent().css("background-position","0 -" + (32 * star) + "px");
            });

            $('.star-rating').live('mouseout',function (){
                    var originalresult=$('#total_rating').val();
                    $(this).css("background-position","0 -" + (32 * originalresult) + "px");
            });


            $('.star').live('click',function (){

                    $('.star-rating').unbind('mouseout');

                    var photo_id = $('#photo_viewer #photo_id').val();
                    var rating = $(this).index() + 1;

                    var form_data = {
                          photo_id:photo_id,
                          rating:rating,
                          ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/rate_photo",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {

                           var obj=jQuery.parseJSON(msg);

                           if((obj.status=="successful_insert")||(obj.status=="successful_update"))
                           {
                               $('#photo_rating').fadeOut('slow');

                               if(obj.status=="successful_insert")
                               {
                                     $('#rate_message').html('You have successfully rated this photo!');
                               }
                               else if((obj.status=="successful_update"))
                               {
                                     $('#rate_message').html('You have successfully changed rating of this photo!');
                               }

                               $('#rate_message').fadeIn('slow');
                              
                              setTimeout( function() {

                                      $('.star-rating').bind('mouseout',function (){
                                                var originalresult =$('#total_rating').val();
                                                $(this).css("background-position","0 -" + (32 * originalresult) + "px");
                                        });

                                      $('#total_rating').val(obj.average_rating);
                                      $('#total_votes').html(obj.total_votes+' votes so far');
                                      $('.star-rating').css({backgroundPosition: '0 -'+obj.average_rating*32+'px'});

                                      $('#rate_message').fadeOut('slow');
                                      $('#photo_rating').fadeIn('slow');
                                }, 6000 );
                           }
                           else if(obj.status=="unsuccessful")
                           {
                               $('#photo_rating').fadeOut('slow');
                               $('#rate_message').html('Some problem appear! Try again!');
                               $('#rate_message').fadeIn('slow');

                              setTimeout( function() {

                                      $('.star-rating').bind('mouseout',function (){
                                                var originalresult =$('#total_rating').val();
                                                $(this).css("background-position","0 -" + (32 * originalresult) + "px");
                                        });

                                      $('#rate_message').fadeOut('slow');
                                      $('#photo_rating').fadeIn('slow');
                                }, 6000 );
                           }
                           else if(obj.status=="cannot_rate")
                           {
                               $('#photo_rating').fadeOut('slow');
                               $('#rate_message').html('You must be logged in to rate the photo!');
                               $('#rate_message').fadeIn('slow');

                              setTimeout( function() {

                                      $('.star-rating').bind('mouseout',function (){
                                                var originalresult =$('#total_rating').val();
                                                $(this).css("background-position","0 -" + (32 * originalresult) + "px");
                                        });

                                      $('#rate_message').fadeOut('slow');
                                      $('#photo_rating').fadeIn('slow');
                                }, 6000 );
                           }
                           else if(obj.status=="cannot_rate_own_photo")
                           {
                               $('#photo_rating').fadeOut('slow');
                               $('#rate_message').html('You can not rate your own photo!');
                               $('#rate_message').fadeIn('slow');

                              setTimeout( function() {

                                      $('.star-rating').bind('mouseout',function (){
                                                var originalresult =$('#total_rating').val();
                                                $(this).css("background-position","0 -" + (32 * originalresult) + "px");
                                        });

                                      $('#rate_message').fadeOut('slow');
                                      $('#photo_rating').fadeIn('slow');
                                }, 6000 );
                           }
                           else if(obj.status=="cannot_rate_blocked_photo")
                           {
                               $('#photo_rating').fadeOut('slow');
                               $('#rate_message').html('You can not rate blocked photo!');
                               $('#rate_message').fadeIn('slow');

                              setTimeout( function() {

                                      $('.star-rating').bind('mouseout',function (){
                                                var originalresult =$('#total_rating').val();
                                                $(this).css("background-position","0 -" + (32 * originalresult) + "px");
                                        });

                                      $('#rate_message').fadeOut('slow');
                                      $('#photo_rating').fadeIn('slow');
                                }, 6000 );
                           }

                       }
                   });
                   
            });

            
            // photo comment 

             $('#photo_comment_form #photo_comment_btn').live('click',function(){

                    var photo_id=$('#photo_viewer #photo_id').val();
                    var comment=$('#photo_viewer #photo_comment').val();

                    var form_data = {
                      photo_id:photo_id,
                      comment:comment,
                      ajax:'1'
                     };

                 if(comment!='')
                 {
                     $.ajax({
                      url: base_url+"photo/photo_comment",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {

                            if(msg=="unsuccessful")
                            {
                                $('#pv_status_msg').html('Your comment is not inserted.Some problem appear.Try again!');
                                $('#pv_status_msg').show();

                                setTimeout( function() {
                                    $('#pv_status_msg').hide();
                                }, 6000 );
                            }
                            else
                            {
                                $('div#photo_viewer').html(msg);
                            }
                       }
                   });
                 }
                 else
                 {
                        $('#pv_status_msg').html('The comment field is required.');
                        $('#pv_status_msg').show();

                        setTimeout( function() {
                            $('#pv_status_msg').hide();
                        }, 6000 );
                 }

                 return false;
            });

         // show all photo comments
      
        $('#photo_viewer #show_all_comments').live('click',function(){

                    var photo_id=$('#photo_viewer #photo_id').val();

                    var form_data = {
                      photo_id:photo_id,
                      ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/show_all_photo_comments",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                           $('div#photo_viewer').html(msg);
                       }
                   });

                    return false;
            });

        
        $('.pv_common_div textarea').live('keyup',function()
        {               
                var characters = 200;
                var parent=$(this).parent();

                var chirp = $(parent).find('textarea').val();
                var count = characters-chirp.length;
                if (count <= characters)
                {
                    $(parent).find('.count').text(count);
                }
                else
                {
                   $(parent).find('textarea').val(chirp.substring(characters,0));
                }
        });
   

         // when click on email photo to a friend button

         $('#photo_viewer #email_photo_btn').live("click",function(){

               $('#email_photo_form textarea').val('');
               $('#email_photo_form .count').html('200');

               $('.pv_common_div').hide();
               $('#email_photo').show();
               return false;
         });

         // when click alert moderator abuse photo button

         $('#photo_viewer #alert_moderator_btn').live("click",function(){

               $('#alert_mod_form textarea').val('');
               $('#alert_mod_form .count').html('200');

               $('.pv_common_div').hide();
               $('#alert_moderator').show();
               return false;
         });


         // send alert to moderator for abused photo
         
         $('#alert_mod_form #alert_mod_send').live('click',function(){

                    var photo_id=$('#photo_viewer #photo_id').val();
                    var alert_mod_message=$('#photo_viewer #alert_mod_message').val();

                    var form_data = {
                      photo_id:photo_id,
                      alert_mod_message:alert_mod_message,
                      ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/alert_abused_photo",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                          if(msg=='successful')
                          {
                               $('.pv_common_div').hide();
                               $('#pv_status_msg').html('You have successfully alerted moderator for this photo!');
                               $('#pv_status_msg').show();

                               $('#photo_viewer #alert_moderator_btn').click(function (){
                                     return false;
                               });

                               $('#email_photo_btn').hide();
                           
                                setTimeout( function() {
                                    $('#email_photo_btn').show();
                                    $('#pv_status_msg').hide();

                                    $('.img_alert').empty();
                                    $('.img_alert').html('You have alerted this photo to a moderator');

                                    $('.img_comments').show();
                                    
                                }, 5000 );
                          }
                          else
                          {
                                   $('#pv_status_msg').html(msg);
                                   $('#pv_status_msg').show();

                                    setTimeout( function() {
                                        $('#pv_status_msg').hide();
                                    }, 6000 );
                          }
                       }
                   });

                    return false;
            });


            // send email to a friend with a photo link
            
            $('#email_photo_form #email_photo_send').live('click',function(){

                    var photo_src=$('#photo_viewer #current_photo').attr('src');
                    var email_photo_message=$('#photo_viewer #email_photo_message').val();
                    var email_photo_to=$('#photo_viewer #email_photo_to').val();

                    var form_data = {
                       email_photo_message:email_photo_message,
                       email_photo_to:email_photo_to,
                       photo_src:photo_src,
                       ajax:'1'
                     };


                     $.ajax({
                          url: base_url+"photo/send_email_photo",
                          type: 'POST',
                          async : false,
                          data: form_data,
                          success: function(msg) {                             
                             if(msg=='successful')
                              {
                                   $('.pv_common_div').hide();
                                   $('#pv_status_msg').html('You have successfully sent this photo to your friend!');
                                   $('#pv_status_msg').show();

                                   $('#photo_viewer #email_photo_btn').click(function (){
                                         return false;
                                   });

                                   $('#alert_moderator_btn').hide();

                                    setTimeout( function() {
                                        $('#alert_moderator_btn').show();
                                        $('#pv_status_msg').hide();
                                        $('.img_comments').show();

                                    }, 5000 );
                              }
                              else
                              {
                                   $('#pv_status_msg').html(msg);
                                   $('#pv_status_msg').show();

                                    setTimeout( function() {
                                        $('#pv_status_msg').hide();
                                    }, 6000 );
                              }
                           }
                   });

                   return false;
            });

            // back to comments - photo viewer

          $('#photo_viewer .back_to_comments').live("click",function(){
               $('.pv_common_div').hide();
               $('#pv_status_msg').hide();
               $('.img_comments').show();
               return false;
          });

         
          // show send message form in photo viewer popup

          $('#photo_viewer #send_message').live("click",function(){

               var message_to_default=$('#message_to_default').val();
               
               if(message_to_default!='')
               {  
                   $('#message_to').val(message_to_default);
               }

                $('#photo_viewer').hide();
                $('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').width() / 2) - 18);
                $('#facebox').css('top',$(window).scrollTop()+100);
                $('#new_message').show();
                return false;
          });

          // add photo to favorites - photo viewer

          $('#add_to_favorite').live("click",function(){
                   
                    var photo_id=$('#photo_viewer #photo_id').val();

                    var form_data = {
                      photo_id:photo_id,
                      ajax:'1'
                     };
                     
                     $.ajax({
                      url: base_url+"photo/add_to_favorite_photos",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {
                         
                          if(msg=='successful')
                          {
                                $('#add_to_favorite span').text('Remove from Your Favorites');
                                $('#add_to_favorite').attr('id','remove_from_favorite');
                          }
                       }
                   });

                    return false;
          });



          // remove photo from favorites - photo - viewer

          $('#remove_from_favorite').live("click",function(){

                    var photo_id=$('#photo_viewer #photo_id').val();

                    var form_data = {
                      photo_id:photo_id,
                      ajax:'1'
                     };

                     $.ajax({
                      url: base_url+"photo/remove_from_favorite_photos",
                      type: 'POST',
                      async : false,
                      data: form_data,
                      success: function(msg) {                          

                          if(msg=='successful')
                          {
                               $('#remove_from_favorite span').text('Add to Your Favorites');
                               $('#remove_from_favorite').attr('id','add_to_favorite');
                          }
                       }
                    });

                    return false;
          });
       

            // add class active on click event for links in submenu

             $('.mainNav ul li ul li a').live("click",function(){
                   $('.mainNav ul li ul li a').removeClass('active');
                   $(this).addClass('active');
             });


            // reset form when edit is canceled

             $('#edit_myprofile_basic .cansel_c,#edit_myprofile_extended .cansel_c,#edit_account_details .cansel_c').live("click",function(){
                   
                    var form_id=$(this).parents('form').attr('id');
                    $('#'+form_id)[0].reset();
              });


             // reset form when open/close is clicked
             
             $('#edit_myprofile_basic a.open,#edit_myprofile_extended a.open,#edit_account_details a.open').click(function(){

                     var form_id=$(this).parents('form').attr('id');
                     $('#'+form_id)[0].reset();
              });
              
              
              // profiles search
              
              $('#search_profiles').live("click",function(){
                   
                   $('.profile_search_error').remove();
                   
                   var username_length=$('#username').val().length;
                   var location_length=$('#location').val().length; 
                   var error=1;
                  
                   
                   if(($('#username').val()=="")&&($('#location').val()=="")&&($('select[name=sexual_preference]').val()=="")&&($('select[name=country]').val()=="")&&($('select[name=birthday]').val()==""))
                   {
                       $('<div class="Note_small profile_search_error"><p>You must fill minimum one field to search profiles</p></div>').insertBefore('.tabs');
                       return false;
                   }
                   
                   
                   if((username_length<4)&&((username_length>0))&&(location_length==1))
                   {
                       $('<div class="Note_small profile_search_error"><p>The Username search field must be at least 4 characters in length.</p><p>The Location field must be at least 2 characters in length.</p></div>').insertBefore('.tabs');
                       error=0;
                   }
                   else if((username_length<4)&&(username_length>0))
                   {                                         
                       $('<div class="Note_small profile_search_error"><p>The Username search field must be at least 4 characters in length.</p></div>').insertBefore('.tabs');
                       error=0;
                   }
                   else if(location_length==1)
                   {
                       $('<div class="Note_small profile_search_error"><p>The Location field must be at least 2 characters in length.</p></div>').insertBefore('.tabs');
                       error=0;                      
                   } 
                   
                   
                   if(error==1)
                   {
                       return true;
                   }   
                   else
                   {
                       return false;
                   } 
                     
              });
              
              
              // advanced profile search              
              
              $('#advanced_search_profiles').live("click",function(){
                   
                   $('.profile_search_error').remove();
                   
                   var seeking=$('select[name=seeking]').val();
                   var zodiac=$('select[name=zodiac]').val();
                   var marital_status=$('select[name=marital_status]').val();
                   var eye_colour=$('select[name=eye_colour]').val();
                   var hair_colour=$('select[name=hair_colour]').val();
                   var tatoo=$('input:radio[name=tatoo]:checked').val(); 
                   var piercing=$('input:radio[name=piercing]:checked').val(); 
                   var tatoo_empty=0;
                   var piercing_empty=0;
                   
                   if((tatoo!='yes')&&(tatoo!='no'))
                   {
                       tatoo_empty=1;
                   }    
                   
                   if((piercing!='yes')&&(piercing!='no'))
                   {
                       piercing_empty=1;
                   } 
                   
                   
                   if((seeking=="")&&(zodiac=="")&&(marital_status=="")&&(eye_colour=="")&&(hair_colour=="")&&tatoo_empty&&piercing_empty)
                   {
                       $('<div class="Note_small profile_search_error"><p>You must fill minimum one field to search profiles</p></div>').insertBefore('.tabs');
                       return false;
                   }
                                
                     
              });
              
              
              // remove profile search error on tab navigation click
              
              $('.tabNavigation li a').click(function(){
                   $('.profile_search_error').remove();
              });
});



