/*
 *
 * (c) Marcin Malessa 2009
 *
*/


var LABEL_LOGINEMAIL='e-mail';
var LABEL_LOGINPASSWORD='hasło';
var ADM=false;


// Let's go
$(document).ready(function(){

  $('#dologin').click(function(){
    messagebox({
	'title':'Zaloguj',
	'text':"<form id='loginform' action='/login,zaloguj' method='post'><br><br>"
		+"<input type='text' name='loginemail' id='loginemail' maxlength='50'><br>e-mail<br><br>"
		+"<input type='password' name='loginpassword' id='loginpassword' maxlenght='50'><br>hasło<br><br>"
  		+"</form>",
	'height':220,
	'buttons':{
	  'Anuluj':function(){$(this).dialog('close')},
	  'OK':function(){
	    $('#loginform').submit();
	  }
	}
	});
    return false;
  });



  if($('.ad-gallery').attr('id')) $('.ad-gallery').adGallery({
    loader_image: '/images/loader.gif',
    width: 750, 
    height: 600,
    effect: 'fade',
    cycle: false,
    callbacks: {
      init: function() {
	this.preloadImage(0);
	this.preloadImage(1);
	this.preloadImage(2);
      },
      afterImageVisible: function() {
	var context = this;
	this.loading(true);
        if(this.current_index < (this.images.length-1)) this.preloadImage(this.current_index + 1, function() { context.loading(false); });
	else this.loading(false);
      }
    }
  });


  $('.elzajawka img, .elzajawka img, .mod:not(.mod_galerie) img').each(function(){
    var title=$(this).attr('alt') ?$(this).attr('alt') :($(this).attr('title') ?$(this).attr('title') :'');
    var imgsrc=$(this).attr('src');
    var float=$(this).css('float')!='none'?$(this).css('float'):'left';
    var ramka=document.createElement('div');
    var foto=$(this).replaceWith(ramka);
    $(foto).click(function(){
	var src=$(this).attr('src');
	pokaz_zdjecie(src.replace(/\/thjpg\//,"/jpg/"),title);
      });
    $(ramka).addClass('fotoramka').addClass('clickme').css({'float':float}).append(foto);
    if(title){
      var height=15;
      var width=$(foto).width();
      var fheight=$(foto).height();
      var top=fheight-height;
      $(document.createElement('div')).addClass('fototlo').css({'top':top,'width':width,'height':height}).html('&nbsp;').appendTo(ramka);
      $(document.createElement('div')).addClass('fototekst').css({'top':top,'width':width,'height':height}).html(title).appendTo(ramka);
    }
  });



  function pokaz_zdjecie(src,title){
    var i=new Image();
    i.src=src;

    // myk dla opery
    $('#usun').remove();
    var div=document.createElement('div');
    $(div).attr({'id':'usun'}).css({'width':0,'height':0,'overflow':'hidden'}).appendTo('#BODY').append($(i));

    $(i).load(function(){
      $('#usun').remove();
      var width=i.width;
      var height=i.height;
      var maxsize=600;

      if(width>maxsize){
        height=(height*maxsize)/width;
        width=maxsize;
	i.width=width;
	i.height=height;
      }
      else if(height>maxsize){
	width=(width*maxsize)/height;
	height=maxsize;
	i.width=width;
	i.height=height;
      }

      var div=document.createElement('div');
      $(div).append($(i)).append("<div class='galeria_imgpodpis'>"+title+"</div>");

      messagebox({
	  'title':'Podgląd zdjęcia',
	  'text':$(div),
	  'width':width+20,
	  'height':height+110
      });
    });
  }









  if(window.edi) edi();

});

function messagebox(options){
  options=$.extend({
    'text':'Czy?',
    'title':'Pytanie',
    'buttons':{'OK':function(){$(this).dialog('close');}},
    'open':function(){},
    'focus':function(){},
    'width':300,
    'height':260
  },options); 
  // tworzenie okna 
  var dialog=document.createElement('div');
  $(dialog).appendTo('#BODY');
  // zawartość okna
  $(document.createElement('div')).html(options.text).appendTo(dialog);

  // uruchamiamy dialog
  $(dialog).dialog({
	bgiframe: false,
	height: options.height,
	width: options.width,
	modal: true,
	closeOnEscape: true,
	buttons:options.buttons,
	title:options.title,
	open:options.open,
	focus:options.focus
  });
  // sprzątanie przy zamknięciu okna
  $(dialog).bind('dialogclose',function(event,ui){
    $(dialog).remove();
  });
}

function salias(){
  /^\/(.+?)\//.test(window.location.pathname);
  var salias=RegExp.$1;
  return salias;
}
var salias=salias();
