﻿  function getMovie() {
      var M$ = navigator.appName.indexOf("Microsoft") != -1;
      return (M$ ? window : document)["mediaplayerhit"];
  }

  function validate(email) {
  //    var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;
      var filter = /([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})\b/;
      if (filter.test(email)) {
          return true;
      } else {
          return false;
      }
  }
  $(document).ready(function () {
      $('#privacy').click(function (e) {
          $('#privacybox').show();
          $('.dark').show();
          $('.dark').bind('click', function (e) {
              $('#privacybox').hide();
              $('.dark').hide();
          });
      });
	  $('#terms').click(function (e) {
          $('#regulaminbox').show();
          $('.dark').show();
          $('#pfrhjqyfpeq').bind('click', function (e) {
              $('#regulaminbox').hide();
              $('.dark').hide();
          });
      });
      $('input').ezMark();
      var defaultValue = 'Tekst hitu';
      $('#contact_query').focus(function () {
          if ($('#contact_query').val() == defaultValue) {
              $('#contact_query').val("")
          };
      });

      function bindenter() {
          $(document).unbind('keypress');
          $(document).bind('keypress', function (e) {
              runFunctionIfEnter(e, 'entercatcher');
          });
      }
      bindenter();
      $("#contact_query").blur(function () {
          if (checkIfEMpty()) {
              $('#contact_query').val(defaultValue);
          }
      });

      $('#contact_query').keyup(function () {
          checkIfEMpty();
      });

      function runFunctionIfEnter(e, fnName) {
          var keynum;
          if (window.event) {
              keynum = e.keyCode;
          } else if (e.which) {
              keynum = e.which;
          }
          if (keynum == 13) {
              e.preventDefault();
              eval(fnName)();
              bindenter();
          }
      }

      function entercatcher() {
          switch (ourwindow) {
          case 'main':
              if (!($('#zakaz_submit').is(':disabled'))) {
                  $('#zakaz_submit').click();
              }
              break;
          case 'listen':
              $('.mp3').click();
              break;
          case 'sms':
              if (!($('#smsbutton').is(':disabled'))) {
                  $('#smsbutton').click();
              }
              break;
          case 'email':
              $('.sendbutton').click();
              break;
          default:
              break;
          }
      }

      function checkIfEMpty() {
          if (($('#contact_query').val().length == 0) || ($('#contact_query').val() == defaultValue)) {
              $('#zakaz_submit').attr('disabled', 'disabled');
              return true;
          } else $('#zakaz_submit').removeAttr('disabled');

          return false;
      }

      $('#country').change(function () {
          $('.cou').html($('.countsel option:selected').html());
      });
      $('#template').change(function () {
          $('.tem').html($('.tempsel option:selected').html());
      });

      $('#zakaz_submit').click(function (e) {
          var senddata = $("#order_form").serialize();
          var sendarray = $("#order_form").serializeArray();
          var hittextshow = $("#contact_query").val();
          if ($('.generate').css("display") == 'none') {
              if ((hittextshow.length) > (19)) {
                  hittextshow = hittextshow.substr(0, 19);
                  hittextshow = hittextshow + '...';
              }
              $('div.hittext').text('"' + hittextshow + '"');
              $('.dark').show();
              $('.generate').show();
          }

          $.ajax({
              url: "/",
              type: "POST",
              data: senddata,
              success: function (data) {
                  var results = jQuery.parseJSON(data);
                  switch (results['error']) {
                  case 0:
                      window.location = results['path'];
                      break;
                  case 1:
                      if ($('.errorbox').css("display") == 'none') {
                          $('div.texterror').text(response_msg['wrongphrase']);
                          $('.generate').hide();
                          $('.errorbox').show();
                          $('.dark').bind('click', function () {
                              hidedark();
                          });
                      }
                      break;
                  default:
                      if ($('.errorbox').css("display") == 'none') {
                          $('div.texterror').text(response_msg['busydj']);
                          $('.generate').hide();
                          $('.errorbox').show();
                          $('.dark').bind('click', function () {
                              hidedark();
                          });
                      }
                      break;
                  }
              }
          });
      });
      $('#smsbutton').click(function (e) {
          if ($('.enter :input').val() == '' || $('.enter :input').val() == 'Wpisz kod') return;
          var senddata = $("#order_form").serialize();
          var sendarray = $("#order_form").serializeArray();
          if ($('.generate').css("display") == 'none') {
              $('.generate .textgenerate').text('Proszę poczekać');
              $('.generate .hittext').text('Sprawdzanie kodu w toku...');
              $('.generate').show();
          }
          $('.dark').unbind('click');
          $.ajax({
              url: urlGenerateTrack,
              type: "POST",
              data: senddata,
              success: function (data) {
                  var results = jQuery.parseJSON(data);
                  switch (results['error']) {
                  case 0:
                      $('.generate').hide();
                      $('.sms').hide();
                      $('.emailform').show();
                      ourwindow = "email";
                      break;
                  default:
                      if ($('.errorbox').css("display") == 'none') {
                          $('div.texterror').text(results['path']);
                          $('.generate').hide();
                          $('.errorbox').show();
                          $('.errorbox').delay(3000).fadeOut(1500, function () {
                              $('.errorbox').hide();
                          });
                          $('.dark').bind('click', function (e) {
                              hidedarksms(e);
                          });
                      }
                      break;
                  }
              }
          });
      });
      $('.sendbutton').click(function (e) {
          var emailsenddata = $("#email").serialize();
          var emailsendarray = $("#email").serializeArray();
          switch (emailsendarray.length) {
          case 2:
              if (!validate(emailsendarray[1].value)) {
                  $('.inputtextme').css('background', 'url(/images/emailboxtlo.png) 0 -72px no-repeat');
                  $('.inputtextme').effect("pulsate", {
                      times: 3
                  }, 100);
                  return false;
              } else {
                  $('.inputtextme').css('background', 'url(/images/emailboxtlo.png) 0 0 no-repeat');
              }
              break;
          case 3:
              if (!validate(emailsendarray[1].value)) {
                  $('.inputtextfriend').css('background', 'url(/images/emailboxtlo.png) 0 -72px no-repeat');
                  $('.inputtextfriend').effect("pulsate", {
                      times: 3
                  }, 100);
                  return false;
              } else {
                  $('.inputtextfriend').css('background', 'url(/images/emailboxtlo.png) 0 0 no-repeat');
              }
              if (emailsendarray[2].value < 1) {
                  $('.inputtextmailfriend').css('background', 'url(/images/wiadomosctlo.png) 0 -252px no-repeat');
                  $('.inputtextmailfriend').effect("pulsate", {
                      times: 3
                  }, 100);
                  return false;
              } else {
                  $('.inputtextmailfriend').css('background', 'url(/images/wiadomosctlo.png) 0 0 no-repeat');
              }
              break;
          default:
              return false;
              break;
          }
          $('.generate .textgenerate').text('Proszę czekać');
          $('.generate .hittext').text('Wysyłanie wiadomości e-mail');
          $('.generate').show();
          $.ajax({
              url: urlSendMail,
              type: "POST",
              data: emailsendarray,
              success: function (data) {
                  var results = jQuery.parseJSON(data);
                  $('.generate').hide();
                  switch (results['error']) {
                  case 0:
                      $('div.textok').text('Email z mp3 został wysłany na podany e-mail.');
                      $('.dark').bind('click', function (e) {
                          hidedarksms(e);
                      });
                      $('.okbox').show();
                      $('.emailform').hide();
                      break;
                  case 101:
                      $('div.texterror').text('Źle podane dane. Wyslanie niemozliwe.');
                      $('.errorbox').show();
                      $('.errorbox').delay(3000).fadeOut(2000, function () {
                          $('.errorbox').hide();
                      });
                      break;
                  default:
                      $('div.texterror').text('NIeznany blad.');
                      $('.errorbox').show();
                      $('.errorbox').delay(3000).fadeOut(2000, function () {
                          $('.errorbox').hide();
                      });
                      break;
                  }
              }
          });
      });
      $('.check').mouseup(function (e) {
          if ($('#generate_is_accept').parent().hasClass('ez-checked')) {
              $('#generate_sms_sec_code').attr("disabled", "disabled");
              $('#smsbutton').attr("disabled", "disabled");
          } else {
              $('#generate_sms_sec_code').removeAttr("disabled");
              $('#smsbutton').removeAttr("disabled");
          }
      });
      $('#inputfriend').click(function (e) {
          $('.inputtextfriend').css('background', 'url(/images/emailboxtlo.png) 0 0 no-repeat');
          $('.inputtextmailfriend').css('background', 'url(/images/wiadomosctlo.png) 0 0 no-repeat');
          $('.inputtextme').css('background', 'url(/images/emailboxtlo.png) 0 -36px no-repeat');
          $('#email_emailboxme').attr("disabled", "disabled");
          $('#email_emailboxme').val("");
          $('#email_inputtextmailfriendarea').removeAttr("disabled");
          $('#email_emailboxfriend').removeAttr("disabled");
          $('#nomerfriend').css('background', 'url(/images/tlo_nomerow.png) 0 0 no-repeat');
          $('#nomerme').css('background', 'url(/images/tlo_nomerow.png) -48px 0 no-repeat');
      });
      $('#inputme').click(function (e) {
          $('.inputtextfriend').css('background', 'url(/images/emailboxtlo.png) 0 -36px no-repeat');
          $('.inputtextmailfriend').css('background', 'url(/images/wiadomosctlo.png) 0 -126px no-repeat');
          $('.inputtextme').css('background', 'url(/images/emailboxtlo.png) 0 0px no-repeat');
          $('#email_emailboxfriend').attr("disabled", "disabled");
          $('#email_emailboxfriend').val("");
          $('#email_inputtextmailfriendarea').attr("disabled", "disabled");
          $('#email_inputtextmailfriendarea').val("");
          $('#email_emailboxme').removeAttr("disabled");
          $('#nomerfriend').css('background', 'url(/images/tlo_nomerow.png) -48px 0 no-repeat');
          $('#nomerme').css('background', 'url(/images/tlo_nomerow.png) 0 0 no-repeat');
      });
      $('.mp3').click(function (e) {
          if ($('.sms').css("display") == 'none') {
              ourwindow = "sms";
              $('#generate_sms_sec_code').val('');
              $('#email_emailboxme').val('');
              $('#email_emailboxfriend').val('');
              $('#email_inputtextmailfriendarea').val('');
              $('.sms').show();
              $('.dark').show();
              $('.dark').bind('click', function (e) {
                  hidedarksms(e);
              });
              $('#smsbox_close').bind('click', function (e) {
                 $('.sms').hide();
                 $('.dark').hide();
              });
      
              
          }

          if (window.event) {
              window.event.cancelBubble = true;
          } else e.stopPropagation();
      });
      var movie = getMovie();
      $('.stop').click(function (e) {
          movie.setStop();
      });
      $('.play').click(function (e) {
          movie.setPlayAgain();
      });
  });

  function hidedark() {
      $('.errorbox').hide();
      $('.dark').hide();
      $('.dark').unbind('click');
  }

  function hidedarksms(e) {
      var e = e || window.event,
          x = e.srcElement || e.target;
      if (!$(x).is($('.dark').find('*'))) {
          $('.sms').hide();
          $('.emailform').hide();
          $('.dark').hide();
          $('.okbox').hide();
          $('.dark').unbind('click');
          ourwindow = "listen";
      }
  }

  function setvolume(volume) {
      moviez = getMovie();
      moviez.setVolumez(volume);
  }


function pokazfacebook()
{
	$('#facebook').css("right","0px");
}

function ukryjfacebook()
{	
	$('#facebook').css("right","-200px");
}

