$(function() {
	//Global variables
	for(var i in $.browser) {
		if ($.browser[i])
			BROWSER = i + (($.browser.msie)?$.browser.version.charAt(0):'');
	}
	
	URCHIN = typeof(pageTracker)=='undefined'?true:false;
	$('.track').each(function() {
		var tag = '/'+this.name.replace(/:/g,'/');
		if($(this).is('form')) {
			$(this).submit(function() {
				URCHIN?urchinTracker(tag):pageTracker._trackEvent('form', 'submit', tag);
				if(tag.indexOf("reservations")>0) {
					URCHIN?__utmLinkPost(this):pageTracker._linkByPost(this);
				}
			});
		} else {
			$(this).click(function() {
				URCHIN?urchinTracker(tag):pageTracker._trackPageview(tag);
				if(tag.indexOf("reservations")>0) {
					URCHIN?__utmLinker(this.href):pageTracker._link(this.href);
					return false;
				}
			});
		}
	});
});

(function($) {		  
	$.fn.expand = function(options) {
		var defaults = {
			openText: 'View Details',
			closeText: 'Hide Details',
			longClass: '.package-long'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			var obj = $(this);
			var tog = $('<div />').addClass('toggle');
			var control = $('<a />').attr('href', '#')
							.addClass('toggler')
							.html(opts.openText)
							.appendTo(tog)
							.toggle(function() { $(opts.longClass, obj).slideDown('slow'); $(this).html(opts.closeText).addClass('open'); },
									function() { $(opts.longClass, obj).slideUp('slow'); $(this).html(opts.openText).removeClass('open'); });
			
			$(opts.longClass, obj).after(tog).hide();
		});
	};
	$.fn.ajaxCalendar = function(options) {
		return this.each(function() {
			$('#calendar-wrapper a').live('click',function(event) {
				event.preventDefault();
				url = this.href.split('?');
				$.get('/direct/events-calendar-ajax.php?' + url[1], { op: 'cal' },function(data) { 
					$('#calendar-wrapper').html(data);
				});
				$.get('/direct/events-calendar-ajax.php?' + url[1], { op: 'list' },function(data) { 
					$('#events-wrapper').html(data);
				});
			});
		});
	};

	$.fn.eventPreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Details',
			viewDetails : 'View Details'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.event-details a').live('click',function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				if ($(this).parent().hasClass('active')) {
					$(this).text(opts.viewDetails).parent().removeClass('active').parent().children('.event-description').slideUp('slow', function() { 
						$(this).html(''); 
					});
				}
				else {
					var el = $(this);
					$.get('/direct/events-calendar-ajax.php', { event_id: this.id.substr(3), rssfeed: this.rel, lang:'en', op: 'details' },function(data) { 
						el.html(opts.hideDetails).parent().addClass('active').parent().children('.event-description').hide().html(data).slideDown('slow');});
				}
			});
		});
	};

	$.fn.postPreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Full Post',
			viewDetails : 'View Full Post'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.post-details a', this).toggle(function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				var el = $(this);
				$.get('/direct/scrapbook-details.php', { event_id: this.id.substr(3), channel: this.rel, lang: 'en' },function(data) { 
					el.html(opts.hideDetails).parent().addClass('active').parent().children('.post-description').hide().html(data).slideDown('slow');
				});
			},function(event) { 
				event.preventDefault();
				$('.share-panel').hide();
				$(this).html(opts.viewDetails).parent().removeClass('active').parent().children('.post-description').slideUp('slow', function() { 
					$(this).html(''); 
				});
			});
		});
	};

	$.fn.packagePreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Details',
			viewDetails : 'View Details'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.package-details a', this).toggle(function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				var el = $(this);
				$.get('/direct/package-details.php', { package_id: this.id.substr(3), channel: this.rel, lang: 'en' },function(data) { 
					el.html(opts.hideDetails).parent().addClass('active').parent().find('.package-long').hide().html(data).slideDown('slow');
				});
			},function(event) { 
				event.preventDefault();
				$('.share-panel').hide();
				$(this).html(opts.viewDetails).parent().removeClass('active').parent().find('.package-long').slideUp('slow', function() { 
					$(this).html(''); 
				});
			});
		});
	};

	$.fn.initializeSharing = function() {
		return this.each(function() {
			$('.share-link').live('click',function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				$(this).parents('.rss-item').find('.share-panel').css('display','inline');
			});
			$('.share-close').live('click',function(event){
				event.preventDefault();
				$(this).parents('.rss-item').find('.share-panel').slideUp('fast');
			});
		});
	};

})(jQuery);