$(document).ready(function(){
  var date= new Date();
  var dzisY=date.getYear();
  if(dzisY<2000) dzisY=dzisY+1900;
  var dzisM=date.getMonth()+1;
  var dzisD=date.getDate();
  var miesiac;
  var fixed_mode=false;
  if(dzisM==1) miesiac='stycznia';
  if(dzisM==2) miesiac='lutego';
  if(dzisM==3) miesiac='marca';
  if(dzisM==4) miesiac='kwietnia';
  if(dzisM==5) miesiac='maja';
  if(dzisM==6) miesiac='czerwca';
  if(dzisM==7) miesiac='lipca';
  if(dzisM==8) miesiac='sierpnia';
  if(dzisM==9) miesiac='września';
  if(dzisM==10) miesiac='października';
  if(dzisM==11) miesiac='listopada';
  if(dzisM==12) miesiac='grudnia';
  $('#data').text("Dzisiaj jest: "+dzisD+" "+miesiac+" "+dzisY+" r.");

  $("table.data tr").not('.title').removeClass('row1');
  $("table.data tr:odd").not('.title').addClass('row1');
  
  function GetYOffset(){
    var iY;
    if(window.pageYOffset!=undefined){
        iY=window.pageYOffset;
      } else {
        iY=document.documentElement.scrollTop;
      }
    return iY;
  }
  
  setInterval(function(){
    if(GetYOffset()>225 && !fixed_mode){
      $(".tab_toolbar").addClass("fixed_toolbar");
      fixed_mode=true;
    } else if(GetYOffset()<225 && fixed_mode){
      $(".tab_toolbar").removeClass("fixed_toolbar");
      fixed_mode=false;
    }
  },200);
  
  $(".tab_toolbar a:contains('Link')").live('click',function(e){
    e.preventDefault();
    var tbbox=$(this).parent().parent();
    if($('.tab_showlink').html()==undefined){
      tbbox.after("<div class='tab_showlink'><input type='text' value='http://www.ksiegowi.taxall.pl/"+$(this).attr('href')+"' /></div>");
      var element=tbbox.next();
      element.find('input').select();
    } else {
      $('.tab_showlink').remove();
    }
  });
  
  $("a").mousedown(function(e){
    $.post("/ajax/newlink.php");
  });
  
  $("input[type='button']").mousedown(function(e){
    $.post("/ajax/newlink.php");
  });
  
  $("#kafelki a img").mouseenter(function(e){
    $(this).parent().css('z-index','6');
    $(this).parent().parent().find(".k_pole").show();
    $('div .box[name]').hide();
    $("div .box[name='"+$(this).parent().find('.tab_on').text()+"']").show();
    $("#kafelki .kafel").css('z-index','4');
  });
  
  $("#kafelki a img").mouseleave(function(e){
    $(this).parent().parent().find(".k_pole").hide();
  });
  
  $('.preventcopy').bind('contextmenu',function(e){
    e.preventDefault();
  });
  
  $("a.important").click(function(e){
    if(!confirm("Ta akcja wymaga potwierdzenia. Jeżeli chcesz kontynuować kliknij OK.")){
      e.preventDefault();
    }
  });
  
  $("input[type='file']").each(function(){
    var width;
    $(this).parent().css('position','relative');
    if($(this).css('width')>0) width=$(this).css('width');
    else width=130;
    $(this).before("<div class='field' style='position:absolute; width:"+width+"px;'></div><div class='field' style='background-color:#E4D8A9; position:absolute; left:"+(width+3)+"px; width:75px;'>Przeglądaj...</div>");
    $(this).css('width',(width+75))
    $(this).css('position','absolute');
    $(this).css('z-index','1');
  });
  
  $('#adres > input').click(function(){
    $(this).select();
  });
  
  $("input[type='file']").change(function(e){
    $(this).prev().prev().text($(this).attr('value'));
  });
  
  var selected=new Array();
  
  $("select[name='dzialy[]'] > option").mousedown(function(e){
    var x=0;
    var clicked=this;
    $(this).parent().find('option').each(function(){
      selected[x]=$(this).attr('selected');
      if(clicked==this){
        if(selected[x]==true) selected[x]=false;
        else selected[x]=true;
      }
      x++;
    });
    $(this).trigger('mouseup');
  });
  
  $("select[name='dzialy[]']").mousemove(function(e){
    $(this).trigger('mouseup');
  });
  
  $("select[name='dzialy[]'] > option").mouseup(function(e){
    var x=0;
    $(this).parent().find('option').each(function(){
      $(this).attr('selected',selected[x]);
      x++;
    });
    e.preventDefault();
  });
  
  $('#rozwin').click(function(){
    $('#rozwiniecie').toggle(100,function(){$('#rozwiniecie').css('z-index','20')});
  });
  
  $('div .tytul').click(function(){
    $(this).next().toggle(100);
  });
  
  $('div .sz_title').click(function(){
    $(this).next().toggle(100);
  });

	var browser=navigator.appName;              //poprawki do IE
	var version=navigator.appVersion;
	if(browser=="Microsoft Internet Explorer"){
		$("#pole").css("top","-19px");
		if(version.substring(22,23)<7) {
			$("#pole").css("position","relative");
			$("#pole").css("top","-14px");
		}
	}

  var ind=0;
  var openedTab=0;
  $('#tabs > .tab').each(function(one){
    if($(this).find('input').attr('value')=='1') {
      openedTab=ind;
      $('#adres > input').attr('value','http://www.ksiegowi.taxall.pl'+$(this).find('a').attr('href'));
    }
    ind++;
  });
	var previousTab=-1;

	var scrollable_api = $("div.scrollable").scrollable({ //aktywacja przewijania zakładek
		size: 5,
		items: '#tabs',
		prev: '.prev',
		next: '.next',
		api: true,
    keyboard: false
	});
  
  if(openedTab>4) $('#tabs').css("left",(-140*openedTab)+(140*4));

	$('.prev').add('.next').mouseenter(function(e){
		$(this).find('img:eq(0)').hide();
		$(this).find('img:eq(1)').show();
	});

	$('.prev').add('.next').mouseleave(function(e){
		$(this).find('img:eq(1)').hide();
		$(this).find('img:eq(0)').show();
	});
          
         
	function addListeners(){
		$('#tabs > .tab > .tab_off').click(function(e){       //a teraz na serio
			var i=0;
			var x;
			var main=this;

			$('#tabs > .tab > .tab_off').each(function(obj){
				if(this==main) x=i;
				i++;
			});
							
			previousTab=openedTab;
			openedTab=x;
      $.post("/ajax/switchtab.php", { switcher : x }, function(data){
        
      });
      
			$('#pole > div').hide();
			$('#pole > div:eq('+x+')').show();
		
			$('#tabs > .tab > .tab_on').hide();
			$('#tabs > .tab > .tab_off').show();
			$(this).hide();
			$(this).parent().find('.tab_on').show();
      $('div .box[name]').hide();
      $("div .box[name='"+$(this).parent().find('.tab_on').text()+"']").show();
      $('#adres > input').attr('value','http://www.ksiegowi.taxall.pl'+$(this).parent().find('a').attr('href'));
		});

		$('#tabs > .tab > img').click(function(e){
			var i=0;
			var x=0;
			var main=this;
								
			$('#tabs > .tab > img').each(function(obj){
				if(this==main) x=i;
				i++;
			});
			if(i>1){
				if((x==openedTab) && (previousTab>=0)){
					$('#tabs > .tab:eq('+previousTab+') > .tab_off').hide();
					$('#tabs > .tab:eq('+previousTab+') > .tab_on').show();
          $('div .box[name]').hide();
          $("div .box[name='"+$('#tabs > .tab:eq('+previousTab+') > .tab_on').text()+"']").show();
					$('#pole > div').hide();
					$('#pole > div:eq('+previousTab+')').show();
          $('#adres > input').attr('value','http://www.ksiegowi.taxall.pl'+$('#tabs > .tab:eq('+previousTab+')').find('a').attr('href'));
					if(previousTab>x) previousTab--;
					openedTab=previousTab;
					previousTab=-1;
				}else if (x==openedTab){
					var y=0;
					if(x==0) y=1;
					$('#tabs > .tab:eq('+y+') > .tab_off').hide();
					$('#tabs > .tab:eq('+y+') > .tab_on').show();
          $('div .box[name]').hide();
          $("div .box[name='"+$('#tabs > .tab:eq('+y+') > .tab_on').text()+"']").show();
					$('#pole > div').hide();
					$('#pole > div:eq('+y+')').show();
          $('#adres > input').attr('value','http://www.ksiegowi.taxall.pl'+$('#tabs > .tab:eq('+y+')').find('a').attr('href'));
					openedTab=0;
				}else{
					if(x<openedTab) openedTab--;
					if(x<previousTab) previousTab--;
				}

				if(previousTab==x) previousTab=0;
				
        $.post("/ajax/removetab.php", { removetab : x }, function(data){
          
        });

          $(this).parent().remove();
      		$('#pole > div:eq('+x+')').remove();        
          
  				var items=new Array();
  				items=scrollable_api.getItems();
  									
  				scrollable_api.getItems().each(function(one){
  					$(this).remove();
  				});

  				var size=items.size();
  				var index=0;

  				items.each(function(one){
  					var cls='';

  					if(index==size-1) cls='line';
  					else cls='tab';

  					scrollable_api.getItemWrap().append("<div class='"+cls+"'>"+$(this).html()+"</div>");
  					index++;
  				});

  				scrollable_api.reload();
  				scrollable_api.begin(0);

  				if(scrollable_api.getItems().size()-6<openedTab) scrollable_api.move(scrollable_api.getItems().size()-6);
  				else scrollable_api.move(openedTab);
  				addListeners();
  				
  				if(i==2) $("#tabs > .tab > img").remove();		
			}
		});
	}

	addListeners();

});
