$(document).ready(function() {

  $("#warning").hide();

  $(".nav li:not(:last-child)").append(" |");

  $("h1:not(.boxed)").wrapInner("<span><span></span></span>");

  initSize();

  $.post(
    "/server.php", 
    {module: "eshop",
     modAct: "getUserBox"}, 
    function(data){handleData(data);});




  $.post(
    "/server.php",
    {
      module: "eshop",
      modAct: "getLogosBox"
    },
    function(data){
      $("#logosBox").hide().empty();
      $("#logosBox").html(data);
      $("#logosBox").slideDown(2000);
    }
  );

   $.post(
    "/server.php",
    {
      module: "eshop",
      modAct: "getTipProductsBox"
    },
    function(data){
      $("#tipProductsBox").hide().empty();
      $("#tipProductsBox").html(data);
      $("#tipProductsBox").slideDown(2000);
    }
  );   
    
  $.post(
    "/server.php",
    {
      module: "eshop",
      modAct: "getTopProductsBox"
    },
    function(data){
      $("#topProductsBox").hide().empty();
      $("#topProductsBox").html(data);
      $("#topProductsBox").slideDown(2000);
      
    }
  );



  $("#query").focus(function(){
    $(this).val("");
  });

  $("#showSettings").click(function(){
    $("#settings .content").slideToggle(500);
  });

  $(".scaleable .fontSize_minus").click(function(){
    changeSize(-1);
  });

  $(".scaleable .fontSize_plus").click(function(){
    changeSize(1);
  });

  $(".scaleable .fontSize_normal").click(function(){
    resetSize();
  });

  showBanner();
  $(window).resize(function(){ animateBanner(); });
  $(window).scroll(function(){ animateBanner(); });
  $(".banner .closeButton").click(function(event){
    event.stopPropagation();
    toggleBanner(this, 24);
  })
});


function showBanner() {
  var btn = $(".banner .closeButton");
  var viewed = getCookie("addViewed");
  btn.toggleClass("opened");
  //if (viewed == "true") btn.toggleClass("opened");
  toggleBanner(btn, 24);
  setCookie("addViewed", "true", 1);
  animateBanner();
}

function animateBanner() {
  $(".banner").each(function(){
    $(this).css({
      top: $(window).scrollTop() + ($(window).height()-$(this).height())/2 
    });    
  });
}


function toggleBanner(btn, offset) {
  var bh = $(btn).parents(".banner").width();
  var wh = $(window).width();
  var shift = 0;
  if ($(btn).hasClass("opened")) shift = Math.round(offset-bh);
  else shift = Math.round((wh-bh)/2);
  $(btn).parents(".banner").css({left: shift});
  $(btn).toggleClass("opened");
}




function initSize() {
  var add = parseInt(getCookie("fontSizeAdd"));
  if (isNaN(add)) add = 0;
  $(".scaleable").each(function(){
    var sizeStr = $(this).css("font-size");
    var l = sizeStr.length;
    var size = parseInt(sizeStr.substr(0,l-2)) + add;
    $(this).css("font-size", size + "px");  
  });
}

function changeSize(step) {
  var add = parseInt(getCookie("fontSizeAdd")) + step;
  if (isNaN(add)) add = 0;
  setCookie("fontSizeAdd", add, 365);  
  $(".scaleable").each(function(){
    var sizeStr = $(this).css("font-size");
    var l = sizeStr.length;
    var size = Math.round(parseInt(sizeStr.substr(0,l-2)) + step);
    $(this).css("font-size", size + "px");  
  });
}

function resetSize() {
  setCookie("fontSizeAdd", 0, 365);
  $(".scaleable").each(function(){
    var sizeStr = $(this).parent().css("font-size");
    $(this).css("font-size", sizeStr);  
  });
}



function handleData(data) {
  $("#userBox").html(data); 
  $("#eshopLogin").click(function(){userLogin();});
  $("#eshopLogout").click(function(){userLogout();});
  $("#eshopRegister").click(function(){location.replace("?module=eshop&modAct=registerUser");});
  $("#loginBox input").click(function(){
    $(this).val("");
  });
  var username = getCookie("username")
  if (username != "") {
    $("#eshopUsername").val(username);
    $("#eshopRememberMe").attr("checked", "checked");
  }
}

function userLogin() {
  var username = $("#eshopUsername").val();
  var password = $("#eshopPassword").val();
  setCookie("username", "", 0);
  $("#eshopRememberMe:checked").each(function(){
    //alert("remember");
    setCookie("username", username, 365);
  });
  //alert("trying to log in");
  $.post(
    "/server.php", 
    {module: "eshop",
     modAct: "loginUser",
     username: username,
     password: password},
     function(data){
      handleData(data);
    }
  );
}

function userLogout() {
  $.post(
    "/server.php", 
    {module: "eshop",
     modAct: "logoutUser"},
     function(data){handleData(data);}
  );
  location.href = "/";
}

function setCookie(c_name, value, expiredays) {
  var exdate = new Date()
  exdate.setDate(exdate.getDate()+expiredays)
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start = document.cookie.indexOf(c_name + "=")
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1 
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
      } 
    }
  return ""
}


