var GWT = {
	fn: {},
	handlers: []
};

/* --- fn --- */

GWT.fn.getParsedTemplate = function(template_tag, return_type, callback) {
	$.post('/index.php?ACT=40', {
		tag: template_tag
	}, function (data) {
		callback(data);
	}, return_type);
};

GWT.fn.ajaxLoader = function() {
	return "<img src='/themes/site_themes/global_works_travel/img/ajax-loader.gif' />";
};

GWT.fn.msgTemplateTitle = function(msgTemplate) {
	return msgTemplate.match(/<title>([^<]+)<\/title>/)[1];
};

GWT.fn.appendSnippet = function(snippet, id) {
	jQuery && (function () {
		GWT.fn.getParsedTemplate(snippet, 'html', function (tmpl_data) {
			$('#' + id).html(tmpl_data);
		});
	}());	
}

GWT.fn.photoGallery = function (anchorClass)  {
	var rels = {};
	var rel_str = '';
	$.colorbox && (function () {
		$('a.' + anchorClass).each(function (i, anchor) {
			var rel = $(anchor).attr('rel');
			if (typeof(rels[rel]) == 'undefined') {
				rels[rel] = rel;
				$("a[rel='"+rel+"']").colorbox({
					rel: 'group' + i,
					maxWidth: 960,
					maxHeight: 760
				});
			}
		});
	}());
}

/* --- handlers --- */

GWT.handlers.push((function() {
	
	$('#login').submit(function (e) {
		e.preventDefault();
		var a = $(this).attr('action');
		$.post(a, $(this).serialize(), function (data) {
			if (data.success) {
				window.location = data.result;
			} else {
				alert(data.message);
			}
		}, 'json')
	});	
	
	$( ".trip-accordion" ).accordion();
	
	$('.photo-slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	// Sidebar videos
	(function sidebarVideos() {
		var sv_img_total, sv_past_img;
		
		sv_img_total = $('.sidebar-video-thumbs-slide-wrapper a img').length;
		sv_past_img = 0;
		
		$('a.sidebar-video-thumb').click(function (e) {
			e.preventDefault();
			$('a.sidebar-video-thumb').removeClass('active');
			$(this).addClass('active');
			var id = $(this).attr('id').match(/video\-(\d+)/)[1];
			var tmpl_str = '{exp:channel_videos:videos video_id="'+id+'" embed_height="230" embed_width="292"}{video:embed_code_hd}{/exp:channel_videos:videos}';
			GWT.fn.getParsedTemplate(tmpl_str, 'html', function (tmpl_data) {
				$('#sidebar-current-video').html(tmpl_data);
			});
		});

		$('#next-video').click(function (e) {
			e.preventDefault();
			if (!$('.sidebar-video-thumbs-slide-wrapper').is(':animated') && sv_img_total - sv_past_img > 1) {
				$('.sidebar-video-thumbs-slide-wrapper').animate({
					marginLeft: "-=113px" 
				}, {
					duration: 300,
					easing: 'linear',
					queue: false,
					complete: function () {
						sv_past_img += 1;
					}
				});				
			}
		});

		$('#prev-video').click(function (e) {
			e.preventDefault();
			if (!$('.sidebar-video-thumbs-slide-wrapper').is(':animated') && sv_past_img > 0) {
				$('.sidebar-video-thumbs-slide-wrapper').animate({
					marginLeft: "+=113px" 
				}, {
					duration: 300,
					easing: 'linear',
					queue: false,
					complete: function () {
						sv_past_img -= 1;
					}
				});				
			}
		});	
	}());
	
}));
