$(document).ready(function(){

	$('#root300x250').jqFancyTransitions({ position: 'top', direction: 'left', titleOpacity: 0, width: 300, height: 230 })
	$('#fly300x250lc').jqFancyTransitions({ position: 'top', direction: 'left', titleOpacity: 0, width: 300, height: 230 });
	$('#fly300x250r').jqFancyTransitions({ position: 'top', direction: 'left', titleOpacity: 0, width: 300, height: 230 });

	$('#addFavorite').jFav();
	
	
	$('ul#bestoffersSearch li').quicksearch({
		attached: 'ul#bestoffersSearch',
		position: 'before',
		labelText: 'Wpisz nazwy miast oraz datę aby przefiltrować wyniki',
		inputText: 'nazwy miast i/lub data',
		loaderImg: '/images/ajax-loader.gif',
		formId: 'boxSearch',
		inputClass: 'boxSearchInput',
		loaderText: ''
	});

	$('ul#cheapSearch li').quicksearch({
		attached: 'ul#cheapSearch',
		position: 'before',
		labelText: 'Wpisz nazwy miast aby przefiltrować wyniki',
		inputText: 'nazwy miast',
		loaderImg: '/images/ajax-loader.gif',
		formId: 'boxSearch',
		inputClass: 'boxSearchInput',
		loaderText: ''
	});
	
	$('ul#regularSearch li').quicksearch({
		attached: 'ul#regularSearch',
		position: 'before',
		labelText: 'Wpisz nazwy miast aby przefiltrować wyniki',
		inputText: 'nazwy miast',
		loaderImg: '/images/ajax-loader.gif',
		formId: 'boxSearch',
		inputClass: 'boxSearchInput',
		loaderText: ''
	});

	$('ul#countriesSearch li').quicksearch({
		attached: 'ul#countriesSearch',
		position: 'before',
		labelText: 'Wpisz nazwę kraju aby przefiltrować wyniki',
		inputText: 'nazwa kraju',
		loaderImg: '/images/ajax-loader.gif',
		formId: 'boxSearch',
		inputClass: 'boxSearchInput',
		loaderText: ''
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		default_width: 800,
		default_height: 488
	});
	

















	(function($) {
		$.fn.clSuggestion = function(conf){

			func = {
				getData : function(url){
					console.log(url);

					$.ajax({
						//cache:true,
						//ifModified:true,
						url:url,
						dataType:'json',
						success: function (d,t,xhr) {

							console.log(d);
							//$.boxDetails.globalConst.queryInp = false;
							// manage cache data
							
							/*
							if(xhr.status === 304 || xhr.status === 0){
								d = extCache[url];
							}
							else{
								extCache[url] = d;
							}

							if( $('#'+elementId).length > 0 ){
								fn( d,thisEl,elementId,eventId,destId,xhr.getResponseHeader("Date") );
							}
							else{
								$('#'+elementId).show();
							}
							*/

						},
						error: function (e){
							console.log('error');
							//fne(e,elementId);
						}
					});
	
				},

				chkKey : function(e){

					if( e.keyCode == 9 ||
						e.keyCode == 16 ||
						e.keyCode == 17 ||
						e.keyCode == 18 ||
						e.keyCode == 20 ||
						e.keyCode == 33 ||
						e.keyCode == 34 ||
						e.keyCode == 35 ||
						e.keyCode == 36 ||
						e.keyCode == 38 ||
						e.keyCode == 40 ||
						e.keyCode == 37 ||
						e.keyCode == 39 ||
						e.keyCode == 45 ||
						e.keyCode == 27 ||
						e.keyCode == 91 ||
						e.keyCode == 93 ||
						e.keyCode == 107 ||
						e.keyCode == 144 ||
						e.keyCode == 220 ||
						e.keyCode == 221 ||
						e.keyCode == 219 ||
						e.keyCode == 222 ||
						e.keyCode == 226 ||
						e.keyCode == 59 ||
						e.keyCode == 191 ||
						e.keyCode == 190 ||
						e.keyCode == 188 ||
						( e.keyCode >= 48 && e.keyCode <= 57) ||
						( e.keyCode < 112 && e.keyCode > 123)
						) {
						return false;
					}
					return true;
				}



			};

			$(this).bind('keyup', function(e){

				//more than 3 words -> to 3 words
				/*
				var wordArr = $(this).val().split(' ');
				if(wordArr.length > 3 && e.keyCode == 32) {
					var arrSlice = wordArr.slice(0,3);
					var newStr = arrSlice.join(' ');
					$(this).val(newStr);
				}
				*/

				if( func.chkKey( e ) && $(this).val().length > 2 ){
					func.getData( $(this).attr('rel') + '?s=' + $(this).val())
				};
			});

		};
	})(jQuery);








	/*
	$('input#fly-location-suggestion').clSuggestion({
		attached: 'aaaaaaaaaaaa'
	});
	*/


	//$('input#fly-location-suggestion222').keydown( function(e){
		//var s = $(this).val();

		//if( /[a-zA-Z0-9]+$/i.test( $(this).val() ) ){

		//	var sTmp = s;
//console.log( $(this).val() );

		//}
		//$.clSuggestion.chKey(e);

	//});

/*
	$('input#fly-location-suggestion11').keydown( function(e){
		if( $(this).val().length > 100 && $(this).val().match(/[a-zA-Z-_\S]/) ){
			//url = $(this).rel + '?s=' + $(this).val();
			console.log(url);
			return;

			$.ajax({
				//cache:true,
				//ifModified:true,
				url:url,
				dataType:'json',
				success: function (d,t,xhr) {
					$.boxDetails.globalConst.queryInp = false;
					// manage cache data
					if(xhr.status === 304 || xhr.status === 0){
						d = extCache[url];
					}
					else{
						extCache[url] = d;
					}

					if( $('#'+elementId).length > 0 ){
						fn( d,thisEl,elementId,eventId,destId,xhr.getResponseHeader("Date") );
					}
					else{
						$('#'+elementId).show();
					}

				},
				error: function (e){
					fne(e,elementId);
				}
			});
		}

	
	});
		///ajax/fly-location-suggestion
*/
























	$('a.fancybox').fancybox();

	$('#btnReservation').click( function(){
		$("#esky_Qsf").find("input[name='Fly[0][DepartureCity]']").val( $('#elFlightFrom').html() );
		$("#esky_Qsf").find("input[name='Fly[0][ArrivalCity]']").val( $('#elFlightTo').html() );

		$(this).hide();
		$('#ajaxLoader').fadeIn();
		$('#esky_FlightsQsf').submit();
	});



	$('.btnBestOffers').click( function(){
		$(this).addClass('ajaxloader2');
	});

	$('.btnBestOffersLast').click( function(){
		var d = $(this).attr('rel').split('|');
		var date = d[2].split('.');

		$("#esky_Qsf").find("input[value='esky_FlightsQsf']").attr( 'checked','checked' );
		$("#esky_Qsf").find("input[name='Fly[0][DepartureCity]']").val( d[0] );
		$("#esky_Qsf").find("input[name='Fly[0][ArrivalCity]']").val( d[1] );
		$("#esky_Qsf").find("input[name='Fly[0][DepartureDate]']").val( date[2]+'-'+date[1]+'-'+date[0] );
		$("#esky_Qsf").find("input#esky_TripTypeOneWayTrip").attr('checked', true);

		$(this).addClass('ajaxloader2');
		$('#esky_FlightsQsf').submit();
		return false;
	});

	$('#btnBookmark').click( function(){
		var title = window.document.title;
		var url = window.document.location;

		if( $(this).attr('href') !== '' && $(this).attr('href') != '/' ){
			var title = $(this).attr('title');
			var url = $(this).attr('href');
		}

		if( $(this).attr('rel') !== '' && $(this).attr('href') != '/' ){
			var title = $(this).attr('title');
			var url = $(this).attr('href');
		}

		// FF
		if(window.sidebar){ 
			window.sidebar.addPanel(title, url,'');
		}
		//Opera
		else if(window.opera){ 
			var a = document.createElement("A");
			a.rel = "sidebar";
			a.target = "_search";
			a.title = title;
			a.href = url;
			a.click();
		}
		//IE
		else if(document.all){ 
			window.external.AddFavorite(url, title);
		}

		//Other
		$.each($.browser, function(i, val){
		  if(i=="safari"){
			 $('.ulubione').click(function(){
				 alert("Naciśnij Ctrl + D aby dodać stronę do ulubionych");
			 });
		  }
		});
		return false;
	});

});


