
var isIE6 = false;

var SITE_SCRIPT_URL = '/scripts';

var siteLang = 'ru';
var errorMes = new Array();
errorMes['ru'] = new Array(
	'внесите электронный адрес.',
	'неверный электронный адрес.',
	'подождите, пожалуйста...',
	'уже зарегистрирован.',
	'регистрация завершена.',
	'Ваш E-mail'
);
errorMes['fr'] = new Array(
	'Adresse email manquante.',
	'Adresse email incorrecte.',
	'Veuillez patientez...',
	'Déjà inscrit.',
	'Inscription terminée.',
	'Adresse email'
);

var nlTimerid = null;

var menusWidth = new Array();
menusWidth['fr'] = 580;
menusWidth['ru'] = 682;
var menusMleft = new Array();
menusMleft['fr'] = '-80px';
menusMleft['ru'] = '-170px';
var footerWidth = 481;


function commonComplete(lang) {
  // init
  if (jQuery.browser.msie && (jQuery.browser.version == '6.0')) isIE6 = true;
  if (lang) siteLang = lang;
  $('#header h2').css('marginLeft', menusMleft[siteLang]);
  commonResize();
  menusClean(); footerClean();
  $('body').append('<div id="nlmes"></div><div id="basketheader"></div>');
  // actions
  $(window).resize(commonResize);
  $('#nlbut').click(nlAdd);
  $('#nlinput').keypress(nlAdd2);
  $('#f2 a').click(openCata);
  $('#alertok').live('mouseover', alertOver);
  $('#alertok').live('mouseout', alertOut);
  $('#alertok').live('click', alertClose);
  preloadImages('/images/alert-ok-sel.png');
}

function commonResize() {
  var h = $(window).height();
  if ($.browser.safari && (h < $(document).height()))
    h = $(document).height();
  if (h > 1030 + 88 + 86) {
    $('#footer').css({bottom: (h - 1030 - 88 - 86)+'px'});
    $('#nlmes').css({bottom: (h - 1030 - 88 - 86 + 13)+'px'});
    $('#music').css({bottom: (h - 1030 - 88 - 86 + 42)+'px'});
  }
  else {
    $('#footer').css({bottom: '0px'});
    $('#nlmes').css({bottom: '13px'});
    $('#music').css({bottom: '13px'});
  }
  resizeDocument(h);
}

function imgOver() {
  if ($(this).attr('class') == 'sel') return;
  $(this).attr('src', $(this).attr('src').replace('.png', '-sel.png'));
}

function imgOut() {
  if ($(this).attr('class') == 'sel') return;
  $(this).attr('src', $(this).attr('src').replace('-sel', ''));
}

function preloadImages() {
  var d = document;
  if(d.images) {
    if(!d.MM_p) d.MM_p = new Array();
    var i,j = d.MM_p.length, a = preloadImages.arguments;
    for(i = 0; i < a.length; i ++)
      if (a[i].indexOf("#") != 0) {
	d.MM_p[j] = new Image;
	d.MM_p[j++].src = a[i];
      }
  }
}

function footerClean() {
  if ($('#f5').length) {
    x = footerWidth - $('#f1').width() - $('#f2').width() - $('#f3').width() - $('#f4').width() - $('#f5').width();
    x /= 4;
  }
  else {
    x = footerWidth - $('#f1').width() - $('#f2').width() - $('#f3').width() - $('#f4').width();
    x /= 3;
  }
  mleft = parseInt($('#f1').css('marginLeft').replace('px', ''));
  $('#f2').css('marginLeft', (mleft+$('#f1').width()+x)+'px');
  mleft = parseInt($('#f2').css('marginLeft').replace('px', ''));
  $('#f3').css('marginLeft', (mleft+$('#f2').width()+x)+'px');
  mleft = parseInt($('#f3').css('marginLeft').replace('px', ''));
  if ($('#f5').length) {
    $('#f5').css('marginLeft', (mleft+$('#f3').width()+x)+'px');
    mleft = parseInt($('#f5').css('marginLeft').replace('px', ''));
    $('#f4').css('marginLeft', (mleft+$('#f5').width()+x)+'px');
  }
  else
    $('#f4').css('marginLeft', (mleft+$('#f3').width()+x)+'px');
}

function menusClean() {
  x = menusWidth[siteLang] - $('#m1').width() - $('#m2').width() - $('#m3').width() - $('#m4').width();
  x /= 3;
  mleft = parseInt($('#m1').css('marginLeft').replace('px', ''));
  $('#m2').css('marginLeft', (mleft+$('#m1').width()+x)+'px');
  mleft += $('#m1').width()+x;
  $('#m3').css('marginLeft', (mleft+$('#m2').width()+x)+'px');
  mleft += $('#m2').width()+x;
  $('#m4').css('marginLeft', (mleft+$('#m3').width()+x)+'px');
}

function nlAdd() {
  var f = document.nlform;
  if (!f.nl_email.value || (f.nl_email.value == errorMes[siteLang][5]))
    return nlAlert(0);
  var e = emailIsValid(f.nl_email.value);
  if (e == null)
    return nlAlert(1);
  nlAlert(2);
  $('#nlbut').unbind();
  $('#nlbut').click(function(){return false;});
  $.ajax({
    type: "POST",
    url: SITE_SCRIPT_URL+'/nl-add.php',
    data: "nl_email="+e,
    success: function(data){
      if (data == '1')
	return nlAlert(3);
      nlAlert(4);
      f.nl_email.value = errorMes[siteLang][5];
      $('#nlbut').unbind();
      $('#nlbut').click(nlAdd);
      return false;
    }
  });
  return false;
}

function nlAdd2() {
  if (e.which == 13) return nlAdd();
}

function nlAlert(mes) {
  if (nlTimerid) clearTimeout(nlTimerid);
  $('#nlmes').html(errorMes[siteLang][mes]);
  nlTimerid = setTimeout('nlAlertClear()', 9000);
  return false;
}

function nlAlertClear() {
  $('#nlmes').html('');
}

function emailIsValid( email ) {
  var e = email.toLowerCase(); 
  e = e.replace( /(^\s+|\s+$)/g, '' );
  if (!e.match(/[a-z0-9\.\-]+@[a-z0-9\.\-]+\.[a-z]{2,4}/))
    return null;
  return e;
}

function openCata(){
  // IE PC
  if((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC")) {
    fenetre=window.open('/catalogue/?lang='+siteLang,'catardv','fullscreen=yes');
  }
  // safari mac
  else {
    height = screen.availHeight;
    width = screen.availWidth;
    try {
      tmp = window.open('/catalogue/?lang='+siteLang, null, "height=" + height + 
	"px,width=" + width +  
	"px,top=0,left=0,location=0,menubar=0,resizable=0,scrollbars=0,toolbar=0");
      tmp.resizeTo(width, height);
     } catch(e) {}
  }
  return false;
}

function alertOver() {
  $('#alertok').addClass('sel');
}

function alertOut() {
  $('#alertok').removeClass('sel');
}

function alertClose() {
  $('#alert, #alertbg').css('display', 'none');
}

function myAlert(mes, big) {
  if ($('#alert').length) {
    $('#alert p').html(mes);
    $('#alert, #alertbg').css('display', 'block');
  }
  else
    $('body').append('<div id="alert"><p>'+mes+'</p><div id="alertok"></div></div><div id="alertbg"></div>');
  if (big) $('#alert').addClass('big');
  else $('#alert').removeClass('big');
}

