// initialize jquery
$j = jQuery.noConflict();

// main object
var PD = {};

// bestdeals
PD.bestdeals = {
	
	auto: 1,
	
	init: function() {
		var boxes = $j('.pd-box-bestof');
		boxes.each(function (i, box) {
			var id = $j(box).attr('id');
			$j('#'+id+' .pd-deal-menu a')
				.hover(function(a) { PD.bestdeals.change($j(this).attr('id'), id, 0); })
				.click(function(a) { PD.bestdeals.change($j(this).attr('id'), id, 0); });
			PD.bestdeals.run(id, 0);
		});
	},
	
	change: function(id, box, auto) {
		PD.bestdeals.auto = auto;
		
		$j('.'+box+'-infos').each(function(i, e) {
			$j(e).hide();
		});
		$j('#'+id+'-infos').show();

		
		$j('.'+box+'-menu').each(function(i, e) {
			$j(e).removeClass('pd-deal-current');
		});
		$j('#'+id+'-menu').addClass('pd-deal-current');
	},
	
	run: function(id, i) {
		if(1 == PD.bestdeals.auto) {
			PD.bestdeals.change(id+'-'+i, id, 1);		
			window.setTimeout(function () { PD.bestdeals.run(id, (i+1)%5); }, 3000);
		} else {
			window.setTimeout(function () { PD.bestdeals.auto = 1; PD.bestdeals.run(id, (i+1)%5); }, 10000);
		}
	}
	
};

// slideshow
PD.gallery = {

	auto: 1,
	
	init: function() {
		$j('.pd-gallery .pd-mini a').hover(function() {
			var image = $j('#'+$j(this).attr('id')+' img');
			var id = $j('.pd-gallery').attr('id');
			$j('#'+id).css('backgroundImage', 'url('+image.attr('src').replace(/149x93c/, '670x325c')+')');
		});
		
	},
	
	change: function(id, img) {
		
	},
	
	run: function(id, i) {
		
	}
		
		
};

// guides best places
PD.places = {
	
	init: function() {
		$j('.pd-places-menu a.pd-button').hover(function() {
			$j('.pd-places-menu a.pd-button').each(function() {
				$j(this).removeClass('pd-current');
			});
			$j(this).addClass('pd-current');
			$j('.pd-places-best').each(function() {
				$j(this).hide();
			});
			$j($j(this).attr('href')).show();
		});
		$j('.pd-places-menu a.pd-button').click(function() { return false; });
	}
	
}

// counters
PD.counters = {
	
	init: function() {
		var boxes = $j('.pd-counter');
		boxes.each(function (i, box) {
			var id = $j(box).attr('id');
			PD.counters.run(id, 12687);
		});
	},
	
	run: function(id, i) {

		$j('#'+id+'-digit-1').html(Math.floor(i / 1)%10);
		$j('#'+id+'-digit-2').html(Math.floor(i / 10)%10);
		$j('#'+id+'-digit-3').html(Math.floor(i / 100)%10);
		$j('#'+id+'-digit-4').html(Math.floor(i / 1000)%10);
		$j('#'+id+'-digit-5').html(Math.floor(i / 10000)%10);
		$j('#'+id+'-digit-6').html(Math.floor(i / 100000)%10);
		
		window.setTimeout(function () { PD.counters.run(id, (i+1)); }, 5000);
	}
	
};

// cookies
PD.cookies = {
	prefix: 'partirdemain_',
	session: null,
	
	init: function() {
		PD.cookies.session = $j.cookie(PD.cookies.prefix + 'session');
	},
	
	getMemberStatus: function() {
		if(null == PD.cookies.session) {
			return 'new';
		} else if('guest' == PD.cookies.session) {
			return 'guest';
		} else {
			return 'member';
		}
	},
	
	setMemberStatus: function(status, email) {
		if('guest' == status) {
			$j.cookie(PD.cookies.prefix + 'session', 'guest');
		} else if('member' == status) {
			$j.cookie(PD.cookies.prefix + 'session', email, {expires: 365});
		}
	}
	
};

// modals
PD.modals = {
	
	init: function() {
		if('new' == PD.cookies.getMemberStatus()) {
			if($j('#pd-popup-subscribe')) {
				PD.modals.subscribe();
			}
		}
	},
	
	subscribe: function() {
		$j.blockUI({
			message: $j('#pd-popup-subscribe'),
			focusInput: false,
			css: {
				width: '686px',
				top:  ($j(window).height() - 294) /2 + 'px', 
                left: ($j(window).width() - 686) /2 + 'px', 
				border: 'none',
				cursor: 'default',
				backgroundColor: 'transparent'
			},
			overlayCSS: { 
				backgroundColor: '#fff',
				opacity: 0.80,
				cursor: 'default'
			}
		});
		$j('.blockOverlay').click($j.unblockUI);
		PD.cookies.setMemberStatus('guest');
	},
	
	subscribeConfirmation: function(email) {
		$j.blockUI({
			message: $j('#pd-popup-subscribe-confirmation'),
			focusInput: false,
			css: {
				width: '696px',
				top:  ($j(window).height() - 299) /2 + 'px', 
                left: ($j(window).width() - 696) /2 + 'px', 
				border: 'none',
				cursor: 'default',
				backgroundColor: 'transparent'
			},
			overlayCSS: { 
				backgroundColor: '#fff',
				opacity: 0.80,
				cursor: 'default'
			}
		});
		$j('.blockOverlay').click($j.unblockUI);
		PD.cookies.setMemberStatus('member', email);
	}
	
};

// fisheye
PD.fisheye = {
	
	init: function() {
		$j('.pd-fisheye li').hover(function() {
			$j('.pd-fisheye li.pd-big').each(function() {
				$j(this).removeClass('pd-big').addClass('pd-small');
			});
			$j(this).removeClass('pd-small').addClass('pd-big');
		});
	}

};
	
// slideshow
PD.slideshow = {
	
	init: function() {
		setInterval( "PD.slideshow.slide()", 3000 );
	},
	
	slide: function() {
		var slideshows = $j('.pd-post-slideshow div.pd-slideshow');
		slideshows.each(function(i, slideshow) {
			var slideshow_id = $j(slideshow).attr('id');
			var $active = $j('#'+slideshow_id+' img.active');
		    if ( $active.length == 0 ) $active = $j('#'+slideshow_id+' img:last');
		    var $next =  $active.next().length ? $active.next() : $j('#'+slideshow_id+' img:first');
		    $active.addClass('last-active');
		    $next.css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 1000, function() {
		            $active.removeClass('active last-active');
			});
		});
	}
	
};
	
// clock
PD.clock = {
	
	init: function() {
		setInterval( "PD.clock.tic()", 1000 );
	},
	
	tic: function() {
		var separators = $j('.pd-clock span.pd-separator');
		separators.each(function(i, separator) {
			if('hidden' == $j(separator).css('visibility')) {
				$j(separator).css('visibility', '');
			} else {
				$j(separator).css('visibility', 'hidden');
			}
		});
	}
	
};

// maps
PD.maps = {
	init: function() {
		if($j('.pd-maps').length != 0) {
			var script = document.createElement('script');
			script.type = "text/javascript";
			script.src = "http://maps.google.com/maps/api/js?sensor=false&language=fr&callback=prepare_pd_guide_map";
			document.body.appendChild(script);
		}
	}
};

// search_flights
PD.search_flights = {
	init: function() {
		if($j('#params_from_name')) {
			
			// prepare date
			$j.datepicker.setDefaults($j.datepicker.regional['fr']);
			
			// date
			$j('#params_from_date').datepicker();
			$j('#params_to_date').datepicker();
			
			$j("#params_from_name").autocomplete({
				source: '/server.php?action=get_airport',
				minLength: 2,
				select: function(event, ui) {
					$j('#params_from_airport').val(ui.item.id);
				}
			});
			
		}
	}
};

// ads
PD.ads = {
	init: function() {
		$j('.pd-ads-encoded').each(function() {
			var str = $j(this).html();
		//	console.log($j.base64Decode(str.substring(5, str.length-4)));
		//	$j(this).html(' '+$j.base64Decode(str.substring(5, str.length-4))+' ');
		});
	}
};

// init
$j(document).ready(function() {
	PD.cookies.init();
	PD.bestdeals.init();
	//PD.counters.init();
	PD.modals.init();
	PD.slideshow.init();
	PD.clock.init();
	PD.gallery.init();
	PD.maps.init();
	PD.places.init();
	PD.fisheye.init();
	PD.search_flights.init();
	
	
	//PD.ads.init();
});
