/**
 * Wywo�anie kilku wa�nych funkcji po za�adowaniu DOM
 **/
var bg = null;
var bg_img = null;
$(function(){

	// Targetowanie
	$('a._blank').attr('target', '_blank');

	// Zmiana rozmiaru obrazka w trakcie zmiany rozmiaru okna
	bg = $('#bg');
	bg_img = bg.find('img');
	$(window).resize(function(){
		_update_bg(bg, bg_img);
	}); 
	_update_bg(bg, bg_img);
	
	// Obs�uga klawiszy wysy�ania maila
	var _form_lock = false;
	$('#body').find('img.fSendButton').click(function(){
		if(_form_lock==true)return false;
		else _form_lock = true;
	
		var jT = $(this);
		var f = jT.parents('form');
		
		// Generujemy tablic� danych
		var f_serial = f.serializeArray();
		f_serial.push({'name':'ajax', 'value':'1'});
		
		// Wysy�amy zapytanie
		$.post("?", f_serial,
			function(data){
				if(data.code){
					if(data.code == '1'){
						_clear_form(f);
						_form_lock = false;
					
					}else if(data.code == '-2'){
						_form_lock = false;
					
					}else if(data.code == '-1'){
					}
					
					// Wy�wietl wiadomo�� u�ytkownikowi, je�li jest jaka� do wy�wietlenia
					if(data.text != '')
						_display_message(f, f.next('.form_message'), data.text, data.code);
				}
			}, "json"
		);
		
	});
	
	// Formularz z mo�liwo�ci� wyboru z listy rozwijalnej
	var selectes = $('.form').find('.select');
	$('.form').find('.input').not('.selectable').focus(function(){selectes.hide();});
	$('.form').find('.selectable').attr('readonly', true).focus(
		function(){
			selectes.hide();
			var t = $(this);
			var n = t.next('.select');
			if(n.length > 0){
				n.fadeIn('fast');
			}
		}
	);
	
	// Obs�uga wybrania danej opcji
	selectes.find('a').click(function(){
		var t = $(this); var s = t.parents('.select'); var i = s.prev('.input');
		i.val(t.html());
		s.hide();
		return false;
	});
	
	// FACEBOOK
	var $fb = $('#facebook'); 
	
	$fb.find('.opener').hover(function(){
		$fb.stop(true,true).animate({
			'left': 0
		}, 500, function(){
			$fb.addClass('expanded');
		});
	},function(){});
	
	$fb.hover(function(){},
	function(){
		if($fb.is('.expanded')){
			$fb.stop(true,true).animate({
				'left': -292
			}, 500, function(){
				$fb.removeClass('expanded');
			});
		}
	});
	
	// VOUCHER
	if($('#voucher_used').length == 0 && typeof(_voucher_enabled) != "undefined"){
		_voucher_init();		
	}
	
});

function _voucher_hide(){
	$('#voucher,#voucher_bg').fadeOut('fast');
}
function _voucher_init(){
	var $vbg = $('<div id="voucher_bg"></div>').prependTo('body');
	$vbg
		.width( $(window).width() )
		.height( $(window).height() );
	
	var $v = $('<div id="voucher">'+
				'<div id="voucher_1"><form id="voucher_form" onsubmit="return voucher_submit();">'+
				'	<div id="voucher_info" class=""></div>'+
				'	<table cellspacing="0" cellpadding="0">'+
				'		<tr>'+
				'			<td>'+
				'				<input type="text" name="fName" class="t" />'+
				'			</td>'+
				'		</tr>'+
				'		<tr>'+
				'			<td>'+
				'				<input type="text" name="fSurname" class="t" />'+
				'			</td>'+
				'		</tr>'+
				'		<tr>'+
				'			<td>'+
				'				<input type="text" name="fEMail" class="t" />'+
				'			</td>'+
				'		</tr>'+
				'		<tr>'+
				'			<td>'+
				'				<input type="image" src="css/gfx/layout/voucher/send.png" class="b" />'+
				'			</td>'+
				'		</tr>'+
				'	</table>'+
				'</form></div><div id="voucher_2"></div>'+
				'<a href="#" onclick="$(\'#voucher,#voucher_bg\').fadeOut(\'fast\');"></a>'+
				'</div>').prependTo('body');
	$v.css({
		top : ( ( $(window).height() - $v.height() ) / 2 ),
		left : ( ( $(window).width() - $v.width() ) /2 )
	});
	
	$(window).resize(function(){
		$('#voucher').css({
			top : ( ( $(window).height() - $('#voucher').height() ) / 2 ),
			left : ( ( $(window).width() - $('#voucher').width() ) /2 )
		});
		$('#voucher_bg')
			.width( $(window).width() )
			.height( $(window).height() );
	});
	
	_voucher_timer = setTimeout(function(){
		_voucher_toggle();
	}, 3000);
	
	$v.click(function(){
		clearTimeout(_voucher_timer); 
	});
}

var _voucher_in_use = 2;
var _voucher_timer = null;
function _voucher_toggle(){
	if(_voucher_in_use == 1){
		$('#voucher_1').hide();
		$('#voucher_2').show();
		_voucher_in_use = 2;
		_voucher_timer = setTimeout(function(){
			_voucher_toggle();
		}, 3000)
	}else if(_voucher_in_use == 2){
		$('#voucher_1').show();
		$('#voucher_2').hide();
		_voucher_in_use = 1;
		_voucher_timer = setTimeout(function(){
			_voucher_toggle();
		}, 4000)
	}
}

function voucher_submit(){
	if($('#voucher_form').length > 0){
		var data =  $('#voucher_form').serializeArray();
		data.push({name:'is_ajax', value:'1'});
		data.push({name:'act', value:'voucher'});
		$.post('', data, function(r){
			if(r.result == '1'){
				if(r.resultText){
					$('#voucher_info').show().removeClass('ok error').addClass('ok').text(r.resultText);
				}else $('#voucher_info').hide();
				$('#voucher_form .t').val('');
				setTimeout(function(){
					$('#voucher,#voucher_bg').fadeOut('fast');
				}, 3000)
			}else if(r.result == '-1'){
				if(r.resultText){
					$('#voucher_info').show().removeClass('ok error').addClass('error').text(r.resultText);
				}else $('#voucher_info').hide();
			}
		}, "json");
	}
	return false;
} 

var gallery = function(){
	var private = {
		anim_speed : 1000
	};
  	var public = {
		
		list_scroll : function (){
			//Get our elements for faster access and set overlay width
			var cont = $('#gallery_small');
				
			cont.unbind('mousemove').scrollLeft(0);
		
			var div = $('#gallery_small .container');
			var divMargin = 0;
		
			cont.css({overflow: 'auto'});
		
			//Get menu width
			var contWidth = cont.width() - divMargin;
			
			//Remove scrollbars
			cont.css({overflow: 'hidden'});
		
			//Find last image container
			var lastA = div.find('a:last');
		
			//When user move mouse over menu
			cont.mousemove(function(e){
				//As images are loaded ul width increases,
				//so we recalculate it each time
				var divWidth = lastA.get(0).offsetLeft + lastA.outerWidth() - divMargin;
		
				var left = ((e.pageX - cont.offset().left) * (divWidth-contWidth) / contWidth);
				cont.scrollLeft(left);
			});
		},
		
		anim_init : function () {
		
			// nie ma sensu inicjowa� je�li slajd jest ukryty
			if($('#slide_gallery').is(':hidden')) return false; 
			
			// Obliczenie dmaksymalnej s�ugo�ci paska miniatur
			var gs_c = $('#gallery_small .container');
			var gs_cw = 0;
			gs_c.find('a').each(function(){gs_cw += $(this).outerWidth();});
			gs_c.width(gs_cw)

			// Inicjacja animacji
			var G = $('#gallery');
			G.find('div.image:first').show();			
			G.cycle({
				delay: 	0,
				sync: 	true,
				fx:     'scrollLeft', 
			    timeout: 4000, 
			    speed: private.anim_speed,
			    pause: 1,
			    
			    pager:  '#gallery_small .container',
			    pagerAnchorBuilder: gallery.anim_pagerInit,
			    pauseOnPagerHover: 1,
			    pagerClick: gallery.anim_pagerClick,
			    
			    before: gallery.anim_onBefore,
			    after: gallery.anim_onAfter	    
			});
		},
		
		anim_pagerInit : function(i, el){
			return $('#gallery_small').find('a').get(i);
		},
		
		anim_onBefore : function(cse, nse, o, ff){
			var gs = $('#gallery_small');
			gs.find('a').removeClass('current');
			var t = $('#'+nse.id+'_p').addClass('current');
			gs.scrollTo( t, private.anim_speed );
		}

  	}	
	return public
}();
