var IE = document.all?true:false;

function element(el_id) {
    return document.getElementById(el_id);
}

var tempX = 0;
var tempY = 0;


function pozycja_myszy(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY) {
    posx = e.pageX;
    posy = e.pageY;
  } else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  tempX = posx;
  tempY = posy;
}

function dictShow(word,e) {
    pozycja_myszy(e);
    //element('dict_div').innerHTML= '<p style="background-color:#C1FF06;"><b>'+word+'</b></p><p>'+slowa[word.toUpperCase()]+'</p>';
    element('dict_name').innerHTML = word;
    element('dict_desc').innerHTML = slowa[word.toUpperCase()];
    //element('dict_div').style.display = 'block';
    element('dict_div').style.top = tempY + 'px';
    element('dict_div').style.left = tempX + 20 + 'px';
    $('#dict_div').show();
}

function dictHide() {
    element('dict_div').style.display = 'none';
}

var alwaysOnTop = false;

function enlargePhoto(photo,e) {
    pozycja_myszy(e);
    element('photo_div').innerHTML= '<img src="'+photo+'" style="height:300px;" />';
    element('photo_div').style.top = tempY - 300 + 'px';
    element('photo_div').style.left = tempX + 20 + 'px';
    element('photo_div').style.display = 'block';
    //$("#photo_div").fadeIn("fast");
}

function switchAOT() {
    alwaysOnTop = !alwaysOnTop;
}

function photo_off() {
    if(!alwaysOnTop) {
      element('photo_div').style.display = 'none';
      //$("#photo_div").fadeOut("fast");;
    }
}

var preview_max_width = 245;
var preview_max_height = 295;

function preview_photo(obrazek) {
  var i = new Image();
  i.src = obrazek.src;
  var szer = i.width;
  var wys = i.height;
  var ratio = szer / wys;
  $("#photo_preview img").fadeOut("slow",function(){
    /*if(szer > preview_max_width) {
      $(this).attr("width",preview_max_width);
      $(this).attr("height",Math.round(preview_max_width / ratio));
      wys = Math.round(preview_max_width / ratio);
    }
    if(wys > preview_max_height) {
      $(this).attr("height",preview_max_height);
      $(this).attr("width",Math.round(preview_max_height * ratio));
    }*/
    //$(this).attr("src",obrazek.src.replace(/thumb_/,'preview_')).fadeIn("slow");
    $(this).attr("src",obrazek.src.replace(/thumb_/,'preview_')).load(function(){
      $(this).fadeIn("slow");
    });
  });
  $("#photo_preview a").attr("href",obrazek.src.replace(/thumb_/,''));
}

function full_size() {
  var adres_obrazka = $("#photo_preview a").attr("href");
  //alert(adres_obrazka.substring(adres_obrazka.lastIndexOf('prod_img')));
  $("#full_sizes a[href='"+adres_obrazka.substring(adres_obrazka.lastIndexOf('prod_img'))+"']").click();
  return false;
}

function setbg(elem) {
  if(IE) elem.style.background = 'url(img/red_light_bg.gif) repeat-x bottom center';
}

function unsetbg(elem) {
  if(IE) elem.style.background = 'none';
}

function compare_some(odnosnik) {
  var ilosc = 0;
  var do_porownania = Array();
  var form_id = element('compare_form');
  for( var i = 0; i < form_id.elements.length; i++ ) {
    var x = form_id.elements[i];
    if(x.type=='checkbox' && x.checked) {
      ilosc++;
      do_porownania.push(x.value);
    }
  }
  if( ilosc < 2 || ilosc > 3 ) {
    alert('Niepoprawna ilość produktów do porównania: ' + ilosc);
    return false;
  } else {
    odnosnik.href = 'porownaj.php?ids=' + do_porownania.join(':') + '&width=1040&height=600&KeepThis=true&TB_iframe=true';
    return tb_init_and_go(odnosnik);
  }

}

function compare_all(odnosnik) {
  var ilosc = 0;
  var do_porownania = Array();
  var form_id = element('compare_form');
  for( var i = 0; i < form_id.elements.length; i++ ) {
    var x = form_id.elements[i];
    if(x.type=='checkbox') {
      ilosc++;
      do_porownania.push(x.value);
    }
  }
  if( ilosc < 2 ) {
    alert('Niepoprawna ilość produktów do porównania: ' + ilosc);
    return false;
  } else {
    //odnosnik.href = 'porownaj.php?ids=' + do_porownania.join(':') + '&width=1040&height=600&KeepThis=true&TB_iframe=true';
    odnosnik.href = 'porownaj.php?ids=' + do_porownania.join(':') + '&width=1040&height=600&KeepThis=false&TB_iframe=true';
    return tb_init_and_go(odnosnik);
  }

}

function check_pass(file_id) {
  if( download_pass[file_id] ) {
    var user_pass = prompt('Proszę o podanie hasła dostępu do pliku...','');
    if( user_pass == download_pass[file_id] ) {
      return true;
    } else {
      alert('Hasło niepoprawne!!');
      return false;
    }
  } else {
    return true;
  }
}


