/* Klasa jQuery do wyświetlania lightboxa
* Parametr: js object
* Wywolanie: jQuery(selector).aclightbox([opcje]);
*
* Dostepne opcje:
*
* title       - [ tekst ] - tytul lightboxa
* size        - [ px ] - wielkosc lightboxa (szerkosc)
* alt         - [ text ] - opis po najechaniu na ikonke
* after 			- [ 'now', 'click' ] - kiedy lightbox ma byc uruchomiony
* img 				- [ url ] - adres obrazka jaki ma byc wstawiony w obiekt
* width       - [ px ] - dlugosc obrazka
* height      - [ px ] - szerokosc obrazka
* background  - [ color ] - kolor tla obrazka
* 
* bgcolor - [ color ] - kolor tla
* bgopacity  - [ float] - stopien przezroczystosci
* borders - [ color ] - kolor borderow lightboxa
* titlebg - [ color ] - kolor tla naglowka
* titlecolor - [ color ] - kolor tekstu naglowka
* titleFontSize - [ int ] - wielkosc tekstu w naglowku
* databg - [ color ] - kolor tla srodka lightboxa
* buttonsbg - [ color ] - kolor tla pod przyciskami
* buttonCANCEL - [ option {click,title,style} ] - tworzy przycisk o nazwie [title]  iwygladzie okreslonym przez [style] oraz z akcja [click]. Zamyka lightboxa.
* buttonOK - [ option {click,title,style} ] - tworzy przycisk o nazwie [title]  iwygladzie okreslonym przez [style] oraz z akcja [click].
*
*/

jQuery.fn.aclightbox = function(opt) {

  var options = { 'alt' : '',
	                'title' : 'Tytuł',
									'text' : '',
									'size' : 500,
									'fontSize' : 12,
									'after' : 'click',
									'animate' : 0,
									'height' : 16,
									'width' : 16,
									'display' : '',
									'img' : '/gfx/awc/broom.png',
									'backgroundColor' : 'none'
								};
	jQuery.extend(options,opt);
	jQuery(this).each( function() {
	
	var alt = '';
  var item = jQuery(this);
	var data = item.html();
	item.html( options.text? options.text:'' );
	
	
	if(options.alt) item.attr( { 'alt': options.alt, 'title':options.alt } );
	//if(options.img) item.html('<img src="'+options.img+'" alt="'+alt+'" title="'+alt+'" />');
	
	var itemopt = {'fontSize':options.fontSize};
	if(options.text) { jQuery.extend(itemopt,{'display' : 'inline','color':'#235CBC'}); options.img = ''; }
	if(options.img) jQuery.extend(itemopt,{'backgroundImage' : 'url('+options.img+')'});
  if(options.display)	jQuery.extend(itemopt,{'display' : options.display});
	else if(!itemopt.display) jQuery.extend(itemopt,{'display' : 'block'});
	if(itemopt.display != 'inline') jQuery.extend(itemopt,{'width' : options.width,'height' : options.height});
	item.css(jQuery.extend(itemopt,{'cursor' : 'pointer'}));
	
	var showopt = {};
	jQuery.extend(showopt, options);
	jQuery.extend(showopt, {'title':options.title, 'data':data, 'width':options.size });
	if(options.after == 'now')
	  jQuery(this).aclightboxShow(showopt);
	else
	if(options.after == 'click')
	  item.click( function(){ jQuery(this).aclightboxShow(showopt); } );
	});
		
}


jQuery.fn.aclightboxShow = function(opt) {
  var options = { 'width' : 300,
	                'title' : '',
	                'data' : ''
								};
	jQuery.extend(options,opt);
	
	var boxes = jQuery(document).attr('aclightboxCount');
	if(!boxes) boxes = 0;
	boxes ++;
	jQuery(document).attr({'aclightboxCount':boxes});
	
	var bodyWidth = parseInt( jQuery(document).width() );
	var bodyHeight = parseInt( jQuery(document).height() );
	
	var bg = jQuery(document.createElement('div'));
	bg.css({ 'width' : bodyWidth,
					 'height' : bodyHeight,
					 'backgroundColor' : options.bgcolor ? options.bgcolor : '#000000',
					 'position' : 'absolute',
					 'top' : 0,
					 'left' : 0,
					 'opacity' : options.bgopacity ? options.bgopacity : 0.5
	       });
	bg.attr({'class':'aclightboxbg', 'id':'aclightboxbg'});
	
	var content = jQuery(document.createElement('div'));
	content.css({ 'width' : options.width,
	              'marginLeft' : parseInt( (bodyWidth - options.width) / 2 ),
								'marginRight' : parseInt( (bodyWidth - options.width) / 2 ),
	              'background' : options.borders ? options.borders : '#FFFFFF',
	              'top' : 100,
	              'position' : 'fixed'
	            });
	content.attr({'class':'aclightboxContent', 'id':'aclightboxcontent' });
	
	var title = jQuery(document.createElement('div'));
	title.css({ 'background' : options.titlebg ? options.titlebg : '#dcdcdc',
	            'color' : options.titlecolor ? options.titlecolor : '#404040',
							'fontSize' : options.titleFontSize ? options.titleFontSize : 12
	          });
	title.attr({'class':'title'}).html(options.title);

	var data = jQuery(document.createElement('div'));
	data.css({ 'background' : options.databg ? options.databg : '#FFFFFF'
	         });
	data.attr({'class':'data'}).html(options.data);
	if( options.sizeVertical ) hei = options.sizeVertical;
	else
	{
		var coords = jQuery.clientCoords();
		var hei = 0;
		if(coords.h-260 < 300) hei = 300; else hei = coords.h-260;
	}
	jQuery(data).css({ 'height':hei,'overflow':'auto' });

	var buttons = jQuery(document.createElement('div'));
	buttons.attr({'class':'buttons'}).css({ 'background' : options.buttonsbg ? options.buttonsbg : '#dcdcdc' });
	
	if(options.buttonCANCEL)
	{
		var buttonCANCEL = jQuery(document.createElement('input'));
		buttonCANCEL.attr({'type':'button','value':options.buttonCANCEL.title});
		buttonCANCEL.click( function(){ jQuery.aclightboxClose(); if(options.buttonCANCEL.click) options.buttonCANCEL.click(data); } );
		if(options.buttonCANCEL.style) buttonCANCEL.attr({'style':options.buttonCANCEL.style});
	  buttons.append(buttonCANCEL);
	}
	
	if(options.buttonOK)
	{
		var buttonOK = jQuery(document.createElement('input'));
		buttonOK.attr({'type':'button','value':options.buttonOK.title});
		buttonOK.click( function(){ if(options.buttonOK.click) options.buttonOK.click(data); } );
		if(options.buttonOK.style) buttonOK.attr({'style':options.buttonOK.style});
	  buttons.append(buttonOK);
	}
	
	buttons.append(jQuery(document.createElement('div')));
	
	content.append(title);
	content.append(data);
	content.append(buttons);
	
	jQuery('body').append(bg);
	jQuery('body').append(content);
}

jQuery.fn.aclightboxData = function() {
  
}

jQuery.aclightboxClose = function() {
  
	var parent = jQuery('#aclightboxcontent').remove();
	var boxes = jQuery(document).attr('aclightboxCount');
	boxes --;
	if(boxes<=0)
	{
	  jQuery('#aclightboxbg').remove();
		boxes = 0;
	}
	jQuery(document).attr({'aclightboxCount':boxes});
	
}

jQuery.clientCoords = function() {
	if(jQuery.browser.msie)
		return { 'w' : document.documentElement.offsetWidth, 'h' : document.documentElement.offsetHeight }
	else
		return { 'w' : window.innerWidth, 'h' : window.innerHeight }
}


