﻿
var LikeLog = {
	likeClick: function() {
		var t = $(this);
		var href = t.attr('href');
		
		if(typeof href == 'undefined')
			return false;

		
		var img = $('img', t);
		if(img.length) {
			t.removeAttr('href');
			TripPlannerCore.setImagePath(img, 'bt_saving.gif');
		} else {
			var saving = $('<span />')
				.addClass('saving')
				.text('Saving...');
			t.replaceWith(saving);
		}

		var number = href.match(/\d+/);
		var data = { id: number };

		$.post('/_service/Like.ashx', data, function() {
			if(img.length) {
				TripPlannerCore.setImagePath(img, 'bt_saved.gif');
			} else {
				saving.replaceWith($('<span />')
					.addClass('saved')
					.text('Saved.')
				);
			}
		});

		return false;
	},

	bindLive: function() {
		$('a.addlike, a.addlikeimg').live('click', LikeLog.likeClick);
	}
};

var TripPlannerCore = {
	
	setImagePath : function(img, fileName) {
		var url = img.attr('src');
		var pos = url.lastIndexOf('/');
		var finalPath = url.substring(0, pos) + '/' + fileName;
		img.attr('src', finalPath);
	},
	
	getImageName: function(img) {
		var url = img.attr('src');
		var pos = url.lastIndexOf('/');
		return url.substring(pos + 1, url.length);
	},
	
	addClick: function() {
		var t = $(this);
		
		var href = t.attr('href');
		
		if(typeof href == 'undefined')
			return false;
		
		var img = $('img', t);
		if(img.length) {
			t.removeAttr('href');
			var name = TripPlannerCore.getImageName(img);
			if(name == 'bt_addtomytripplanner_small.gif')
				TripPlannerCore.setImagePath(img, 'bt_savedtotripplanner_small.gif');
			else
				TripPlannerCore.setImagePath(img, 'bt_waitsaveinprogress.gif');
		} else {
			var saving = $('<span />')
				.addClass('addingtp')
				.text('Saving...');
			
			t.replaceWith(saving);
		}
		
		var nodeid = href.match(/\d+/);
		var data = { node: nodeid };
		$.post('/_service/TripPlanner/Add.ashx', data, function() {
			if(img.length) {
				var name = TripPlannerCore.getImageName(img);
				if(name != 'bt_savedtotripplanner_small.gif')
					TripPlannerCore.setImagePath(img, 'bt_savedtotripplanner.gif');
			} else {
				saving.replaceWith($('<span />')
					.addClass('addedtp')
					.text('Saved.')
				);
			}
			
			TripPlannerCore.updateHeader();
		});
		
		return false;
	},
	
	updateHeader: function () {
		var num = $('#tpheader span.items').html().match(/\d+/);
		if(!num) num = 0;
		num++;
		$('#tpheader span.items').html(
			'<strong>My Trip Planner:</strong> ' +  num + ' saved items</span>'
		);
	},
	
	bindLive: function() {
		$('a.addtp').live('click', TripPlannerCore.addClick);
	}
};



$(document).ready(function() {
	LikeLog.bindLive();
	TripPlannerCore.bindLive();
	//Create title attributes for all images that do not have them
	$("img:not([title])").each(function() {if ($(this).attr("alt") != '') $(this).attr("title", $(this).attr("alt"))})
	$("a").each(function() { if (this.href.indexOf(location.hostname) == -1 && this.href.indexOf("javascript:") == -1) $(this).attr('target', '_blank'); })
	$("table.stripe tr:even").addClass('alt');
	
	 $('a.bigpop').bind('click', function() {
		var pop = window.open(this.getAttribute('href'), 'popup', 'width=800,height=600,status=yes,scrollbars=yes,resizable=1');
		if (pop == null || typeof(pop)=="undefined") {
			return true;
		} else {
			return false;
		}
	}); 
});
