$(document).ready(function() {
   $('#modals').hide();

   $("input[type!=hidden]").each(function(){
      if($(this).parent().parent().attr('id') == "edit-account-form") {
         //do not reset this inputs
      } else {
         $(this).val("");
      }
   });

   $('#contact-form').submit(function(e){
      e.preventDefault();
   });

   $('#modals').load('/ajax/modals.html', function(){
      $("#terms-and-conditions").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 400,
         width: 600,
         close: function() {
            //
         }
      });
      
      $("#delete-file-confirm").dialog({
          autoOpen: false,
          modal: true,
          draggable: false,
          resizable: false,
          show: 'fade',
          closeOnEscape: true,
          height: 120,
          width: 300,
          open: function(event, ui) {
        	  $(".ui-dialog-titlebar-close").hide();
        	  $("#delete-file-confirm button").button();
        	  $("#btn-no").click(function(e){
        		  e.preventDefault();
        		  $("#delete-file-confirm").dialog("close");
        	  });
        	  $("#btn-yes").click(function(e){
        		  e.preventDefault();
        		  var fiid = $("#delete-file-confirm p span").attr('id');
        		  $.post('/ajax/delete', { fid: fiid }, function(data) {
        		         if(data.status === 1)
        		         {
        		        	 $("#delete-file-confirm").html('Fisier sters cu succes!');
        		        	 $("#delete-file-confirm").dialog("option", "buttons", [
        		        	                                                        {
        		        	                                                            text: "Inchide",
        		        	                                                            click: function() { $(this).dialog("close"); location.reload(); }
        		        	                                                        }
        		        	                                                    ] );
        		         }
        		         else
        		         {
        		            alert("Fisier nu a putut fi sters, contactati un admin.");
        		         }
        		      }, 'json');
        	  });
          }
       });
      
      $("#file-change-password").dialog({
          autoOpen: false,
          modal: true,
          buttons: {
              "Modifica": function() {
                  $(this).dialog("close");
               },
      "Deparoleaza": function() {
    	  var fiid = $(this).find("span").attr('id');
    	  $.post('/ajax/change_file_password', { fid: fiid }, function(data) {
		         if(data.status === 1)
		         {
		        	 $("#file-change-password").html('Parola modificata cu succes!');
		         }
		         else
		         {
		            alert("Fisier nu a putut fi sters, contactati un admin.");
		         }
		      }, 'json');
       },
             "Inchide": function() {
                $(this).dialog("close");
             }
          },
          open: function() {
        	  $(this).find('input[type=password]').val('');
          },
          draggable: false,
          resizable: false,
          show: 'fade',
          hide: 'fade',
          closeOnEscape: true,
          height: 160,
          width: 333
       });

      $("#search-error-modal").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Inchide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 190,
         width: 330
      });

      $("#contact").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Trimite": function() {
            	contactButton(this);
            	
               
            },
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 366,
         width: 333,
         open: function() {
        	 $(this).dialog( "option", "buttons", { "Trimite" : function() { contactButton(this) }, "Închide": function() {
                 $(this).dialog("close");
             } } );
        	 $('#contact-form').show();
        	 $('#contact-form input').val('');
        	 $('#contact-form textarea').val('');
        	 $('#contact-form-holder p').remove();
             $('.contact-errors').remove();
             $('#contact-loader').hide();
             $.get('/ajax/contact', function(re){
            	 $('.recaptcha-holder').attr('id', 're-' + re.key);
               	 $('.recaptcha-holder').html(re.q + "?");
             });
         }
      });

      $("#faq").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 400,
         width: 600,
         close: function() {
            //
         }
      });

      $("#register").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         width: 400,
         close: function() {
            $("#loader24-img").remove();
            $("#register-form button").show();
         }
      });

      $("#change-password-dialog").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         width: 400,
         close: function() {

         }
      });

      $("#edit-account-modal").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         width: 400,
         close: function() {
            //do nothing
         }
      });

      $("#wrong-new-password").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 180,
         width: 400,
         close: function() {
            $("#new-password").fadeOut("slow", function(){
               $("#new-password").fadeIn("slow");
               $("#new-password").removeClass("pass-dow");
               $("#new-password").addClass("wrong-pass-dow");
               $("#new-password").focus();
            });
         }
      });

      $("#wrong-match-password").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 120,
         width: 400,
         close: function() {
            $("#new-password-confirm").fadeOut("slow", function(){
               $("#new-password-confirm").fadeIn("slow");
               $("#new-password-confirm").removeClass("pass-dow");
               $("#new-password-confirm").addClass("wrong-pass-dow");
               $("#new-password-confirm").focus();
            });
         }
      });

      $("#wrong-password").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 120,
         close: function() {
            $("#password").fadeOut("slow", function(){
               $("#password").fadeIn("slow");
               $("#password").removeClass("pass-dow");
               $("#password").addClass("wrong-pass-dow");
               $("#password").focus();
            });
         }
      });

      $("#empty-password").dialog({
         autoOpen: false,
         modal: true,
         buttons: {
            "Închide": function() {
               $(this).dialog("close");
            }
         },
         draggable: false,
         resizable: false,
         show: 'fade',
         hide: 'fade',
         closeOnEscape: true,
         height: 120,
         close: function() {
            $("#password").fadeOut("slow", function(){
               $("#password").fadeIn("slow");
               $("#password").focus();
            });
         }
      });
   });

   $("a[rel=\"terms\"]").click(function(event){
      $('#terms-and-conditions').dialog('open');
      event.preventDefault();
   });

   $("a[rel=\"faq\"]").click(function(event){
      $('#faq').dialog('open');
      event.preventDefault();
   });

   $("a[rel=\"contact\"]").click(function(event){
      $('#contact').dialog('open');
      event.preventDefault();
   });


   $("a[rel=\"external\"]").each(function(index){
      $(this).attr("target", "_blank");
   });

   $("#browse-type").change(function(){
      var browse = $(this).val();
      if(readCookie('browse')) {
         eraseCookie('browse');
         createCookie('browse', browse, 60);
      } else {
         createCookie('browse', browse, 60);
      }
      location.reload();
   });

   function contactButton(elem) {
	   centerMe('contact-loader');
       $('.contact-errors').remove();
       $('#contact-loader').show();
       $("#contact-form").hide();
       $(elem).dialog( "option", "buttons", { } );
       $.post('/ajax/contact', { answer : $('#answer').val(), answerfor : $('.recaptcha-holder').attr('id'), cname : $('#contact-name').val(), cemail : $('#contact-email').val(), cmsg : $('#contact-msg').val() }, function(data) {
          if(data.status == 1) {
             $('#contact-loader').hide();
             $('#contact-form-holder').append('<p>Mesajul a fost trimis, iti vom raspunde cat de repede putem. Multumim.</p>');
             $(elem).dialog( "option", "buttons", { "Închide": function() {
                 $(this).dialog("close");
             } } );
          } else {
             $.each(data.errors, function(index, value){
                $('#' + index).after('<span class="contact-errors"><br/>' + value + '</span>');
             });
             $('#contact-loader').hide();
             $('#contact-form').show();
             $(elem).dialog( "option", "buttons", { "Trimite" : function() { contactButton(this) }, "Închide": function() {
                 $(this).dialog("close");
             } } );
          }
       }
       , 'json');
   }
   
   function createCookie(name, value, days) {
      var expires = "";
      if (days) {
         var date = new Date();
         date.setTime(date.getTime()+(days*24*60*60*1000));
         expires = "; expires="+date.toGMTString();
      }
      else expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
   }

   function readCookie(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i < ca.length;i++) {
         var c = ca[i];
         while (c.charAt(0)==' ') c = c.substring(1,c.length);
         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      return null;
   }

   function eraseCookie(name) {
      createCookie(name, "", -1);
   }

   function centerMe(elementid) {
      var imageHeight = $('#' + elementid).height();
      var imageWidth = $('#' + elementid).width();
      var windowWidth = $('#contact-form').width();
      var windowHeight = $('#contact-form').height();

      $('#' + elementid).css({
         "position" : "absolute",
         "left" : windowWidth / 2 - imageWidth / 2,
         "top" : windowHeight /2 - imageHeight / 2
      });
   }
   
   $.fn.checkregexp = function(regexp){
      var o = this;

      if(checkRegexp()) {
         return true;
      } else {
         return false;
      }

      function checkRegexp() {
         if ( !( regexp.test( $(o).val() ) ) ) {
            return false;
         } else {
            return true;
         }
      }
   };

   $.fn.checklength = function(min, max){
      var o = this;
      
      if(checkLength()) {
         return true;
      } else {
         return false;
      }

      function checkLength() {
         if ( $(o).val().length > max || $(o).val().length < min ) {
            return false;
         } else {
            return true;
         }
      }
   };
});
