/**
 * Shows response in modal window
 * @param response innner html of the modal window
 */
function showModalWindow(response) {
   jQuery(document).ready(function() {
      if (jQuery.trim(response) == "") {
         return;
      }

      jQuery.blockUI({
         message: response,
         css: {
            padding: 0,
            margin: 0,
            top: "20%",
            width: "500px",
            opacity: 1,
            color: "#000000",
            border: "0 solid transparent",
            backgroundColor:"transparent",
            cursor: "default"
         }
      });

      fixPngs();
   });
}

function unfavoriteAndRemoveVideoRow(videoIdentifier) {
   jQuery(document).ready(function() {
      if (jQuery.trim(videoIdentifier) == "") {
         return;
      }

      var data = new StringBuffer();
      data.append("v=").append(encodeURIComponent(videoIdentifier));

      jQuery.post("/js-dyn/video/favorite.js", data.toString(), function(response) {
         var json = eval("(" + response + ")");

         if (json.s != "success"){
            return;
         }

         if (json.e != "remove"){
            return;
         }

         jQuery("#vrow_" + videoIdentifier).remove();
         jQuery("#vrowb_" + videoIdentifier).remove();

         if (jQuery.trim(json.m) != "") {
            alert(json.m);
         }
      }, "text");
   });
}

/**
 * Closes modal window
 */
function closeModalWindow() {
   jQuery(document).ready(function() {
      jQuery.unblockUI();
   });
}

/**
 * Opens modal user login form
 */
function openModalWindowLoginForm() {
   jQuery(document).ready(function() {
      jQuery.get("/js-dyn/user/loginform.js", null, function(response) {
         showModalWindow(response);
      }, "html");
   });
}

function modalLogin(usernameFiledId, passwordFieldId) {
   jQuery(document).ready(function() {
      var usernameFiled = jQuery("#" + usernameFiledId);
      var passwordField = jQuery("#" + passwordFieldId);

      if (usernameFiled.length == 0 || passwordField.length == 0) {
         return;
      }

      var data = new StringBuffer();
      data.append("username=").append(encodeURIComponent(usernameFiled.val()));
      data.append("&");
      data.append("password=").append(encodeURIComponent(passwordField.val()));

      jQuery.get("/js-dyn/user/login.js", data.toString(), function(response) {
         showModalWindow(response);
      }, "html");
   });
}

/**
 * Opens modal user signup form
 */
function openModalWindowSignupForm() {
   jQuery(document).ready(function() {
      jQuery.get("/js-dyn/user/signupform.js", null, function(response) {
         showModalWindow(response);
      }, "html");
   });
}

function modalSignup(emailId, usernameId, password1Id, password2Id, birthDayId, birthMonthId, birthYearId, eulaId) {
   jQuery(document).ready(function() {
      var email = jQuery("#" + emailId);
      var username = jQuery("#" + usernameId);
      var password1 = jQuery("#" + password1Id);
      var password2 = jQuery("#" + password2Id);
      var birthDay = jQuery("#" + birthDayId);
      var birthMonth = jQuery("#" + birthMonthId);
      var birthYear = jQuery("#" + birthYearId);
      var eula = jQuery("#" + eulaId);

      if (email.length == 0 || username.length == 0 || password1.length == 0 || password2.length == 0 || birthDay.length == 0 || birthMonth.length == 0 || birthYear.length == 0 || eula.length == 0) {
         return;
      }

      var data = new StringBuffer();
      data.append("email=").append(encodeURIComponent(email.val()));
      data.append("&");
      data.append("username=").append(encodeURIComponent(username.val()));
      data.append("&");
      data.append("password1=").append(encodeURIComponent(password1.val()));
      data.append("&");
      data.append("password2=").append(encodeURIComponent(password2.val()));
      data.append("&");
      data.append("birthDay=").append(encodeURIComponent(birthDay.val()));
      data.append("&");
      data.append("birthMonth=").append(encodeURIComponent(birthMonth.val()));
      data.append("&");
      data.append("birthYear=").append(encodeURIComponent(birthYear.val()));
      data.append("&");
      data.append("eula=").append(encodeURIComponent(eula.is(":checked")));

      jQuery.get("/js-dyn/user/signup.js", data.toString(), function(response) {
         showModalWindow(response);
      }, "html");
   });
}


/**
 * switchs user settings tab
 */
function tabSwitch(tabId) {
   jQuery(document).ready(function() {
      if (tabId < 0 && tabId > 3) {
         tabId = 0;
      }

      for (var i = 0; i < 3; i++) {
         var idTl = "#tl" + i;
         var idT = "#t" + i;
         var idTr = "#tr" + i;
         var idBody = "#tab" + i;

         if (i == tabId) {
            tabSelected(idTl, idT, idTr, idBody);
         } else {
            tabDeselected(idTl, idT, idTr, idBody);
         }
      }
   });
}

function tabSelected(idTl, idT, idTr, idBody) {
   if (jQuery(idTl) && jQuery(idT) && jQuery(idTr) && jQuery(idBody)) {
      jQuery(idTl).css("display", "block");
      jQuery(idT).attr("class", "menu_sel tabyazisel");
      jQuery(idTr).css("display", "block");
      jQuery(idBody).css("display", "block");
   }
}

function tabDeselected(idTl, idT, idTr, idBody) {
   if (jQuery(idTl) && jQuery(idT) && jQuery(idTr) && jQuery(idBody)) {
      jQuery(idTl).css("display", "none");
      jQuery(idT).attr("class", "menu_norm tabyazi");
      jQuery(idTr).css("display", "none");
      jQuery(idBody).css("display", "none");
   }
}

/**
 * deletes user accounts
 */
function deleteUserAccount(formId) {
   var f = document.getElementById(formId);

   if (f == null) {
      return;
   }

   if (confirm('Üyeliğinizi silmek istediğinizden emin misiniz? Bunun geri dönüşü yok!')) {
      f.submit();
   }
}

/**
 * user modal window
 */
function openModalWindow() {
   jQuery(document).ready(function() {
      jQuery.get("/js-dyn/uye/ayarlar/yenisifre/form.js", null, function(response) {
         jQuery.blockUI({
            message: response,
            css: {
               padding: 0,
               margin: 0,
               top: "20%",
               width: "500px",
               opacity: 1,
               color: "#000000",
               border: "0 solid transparent",
               backgroundColor:"transparent",
               cursor: "default"
            }
         });

         fixPngs();
      }, "html");
   });
}


/**
 * user password change
 */
function changeUserPassword() {
   jQuery(document).ready(function() {
      var currentPassword = document.getElementById("passwordCurrent") != null ? document.getElementById("passwordCurrent").value : null;
      var newPassword1 = document.getElementById("passwordNew1") != null ? document.getElementById("passwordNew1").value : null;
      var newPassword2 = document.getElementById("passwordNew2") != null ? document.getElementById("passwordNew2").value : null;
      var data = new StringBuffer();

      if (currentPassword != null) {
         data.append("&passwordCurrent=").append(encodeURIComponent(currentPassword));
      }
      if (newPassword1 != null) {
         data.append("&passwordNew1=").append(encodeURIComponent(newPassword1));
      }
      if (newPassword2 != null) {
         data.append("&passwordNew2=").append(encodeURIComponent(newPassword2));
      }

      jQuery.post("/js-dyn/uye/ayarlar/yenisifre/kaydet.js", data.toString(), function(response) {
         jQuery.blockUI({
            message: response,
            css: {
               padding: 0,
               margin: 0,
               top: "20%",
               width: "500px",
               opacity: 1,
               color: "#000000",
               border: "0 solid transparent",
               backgroundColor:"transparent",
               cursor: "default"
            }
         });

         fixPngs();
      }, "html");
   });
}

function getUserUploadedVideos(holderElementId, usernameUrlFriendly, sortCriterionOrdinal, resultSize) {
   jQuery(document).ready(function() {
      var holderElement = jQuery("#" + holderElementId);

      if (!holderElement) {
         return;
      }

      var data = new StringBuffer();

      if (usernameUrlFriendly != null) {
         data.append("&usernameUrlFriendly=").append(encodeURIComponent(usernameUrlFriendly));
      }

      if (sortCriterionOrdinal != null) {
         data.append("&s=").append(encodeURIComponent(sortCriterionOrdinal));
      }

      if (resultSize != null) {
         data.append("&qs=").append(encodeURIComponent(resultSize));
      }

      jQuery.get("/js-dyn/user/uploadedVideos.js", data.toString(), function(response) {
         holderElement.html(response);
      }, "html");
   });
}

function getUserLikedVideos(holderElementId, usernameUrlFriendly, sortCriterionOrdinal, resultSize) {
   jQuery(document).ready(function() {
      var holderElement = jQuery("#" + holderElementId);

      if (!holderElement) {
         return;
      }

      var data = new StringBuffer();

      if (usernameUrlFriendly != null) {
         data.append("&usernameUrlFriendly=").append(encodeURIComponent(usernameUrlFriendly));
      }

      if (sortCriterionOrdinal != null) {
         data.append("&s=").append(encodeURIComponent(sortCriterionOrdinal));
      }

      if (resultSize != null) {
         data.append("&qs=").append(encodeURIComponent(resultSize));
      }

      jQuery.get("/js-dyn/user/likedVideos.js", data.toString(), function(response) {
         holderElement.html(response);
      }, "html");
   });
}


