/** 
 * Manages deep linking
 */
var DeepLinking = function() {
	var publicAPI = {
		/**
		 * Init deep linking
		 */
		init : function() {
			// Cred-deck thumbnails
			$('.cred-thumbnail-click').click(function() {
				// TODO: fix deeplinking so it works correctly with the carousel in IE6, instead of just disabling it
				if ((publicAPI.isMSIE()) && (publicAPI.browserVersion() < 7)) {
					
				} else {
					location.hash = '/' + $(this).parent().attr('id');
				}
			});

			// Cred-deck Next click
			$('.next', '#cred-deck').click(function() {
				var index = creddeckapi.getIndex();

				if (index == 10) index = 0;
				if (index == 11) index = 1;  		

				var tag = $('img', '.cred-thumbnail-click').parent().parent().get(index);

				// TODO: fix deeplinking so it works correctly with the carousel in IE6, instead of just disabling it
				if ((publicAPI.isMSIE()) && (publicAPI.browserVersion() < 7)) {

				} else {
					location.hash = '/' + $(tag).attr('id');
				}
			});  

			// Art thumbnails
			$('.art-thumbnail-click').click(function() {
				var url = $(this).parent().attr('id');

				if (url == 'art') {
					location.hash = '/inq/art';
				}
				else {
					location.hash = '/inq/art/' + url;
				}
				
				// TODO What does this have to do with Deeplinking?
				ArtWindow.open(url);			
			});    	  			
		},

		/**
		 * Load deep linking
		 * TODO The deep link loading should just work out what action to take and invoke an external interface, rather than the worrying about implementation itself
		 * @param String # Hash
		 */
		load : function(hash) {
			if (hash) {
				var parts = hash.split('/');
				
				if (parts[1] != 'undefined') {
					// Phones
					if (parts[1] == 'phones') {      
						// Scroll    
						if (parts[3] == 'undefined') {
							$.scrollTo('#phones', {duration: 1000, offset: inq.settings.scroll_offset});
						} else {
							$.scrollTo('#mapcontainer', {duration: 1000, offset: inq.settings.scroll_offset});            
						}

						// Trigger a document click event to clear any popup
						$('document').click();      

						// Phones popup
						if (parts[3] != 'undefined' && parts[3] == 'features' && parts[4] != 'undefined') {
							var url = parts[4];
							MapWindow.open(url);

							TrackEvent.addLabel({action: 'Phones', label: parts[2], value: url});		
						}
						
						// TODO Can't we just use the id?
						var el = $('[rel="#/' + parts[1] + '/' + parts[2] + '"]', '#phones');
						
						if (el.hasClass('active')) return;

						el.siblings().removeClass('active');		
						el.addClass('active');		

						if (el.attr('id') != 'undefined') {
							PhoneMapManager.loadMap(el.attr('id'));
						}
					}

					// Art
					else if (parts[1] == 'inq' && parts[2] == 'art') {
						// Specific art section
						if (typeof parts[3] != 'undefined' && parts[3] != '') {
						    var page = parts[3];
                            
                            if ($(window).scrollTop() == 0) {
    	                        ArtWindow.open(page);
                            }
	                    } else {     
							$.scrollTo('#art', {duration: 1000, offset: inq.settings.scroll_offset});

							TrackEvent.addLabel({action: 'Art', label: 'Art', value: 'Scroll to'});		              
						}

					}

					// Probably Cred-deck slides
					else {
						$.each($('#CredDeckThumbs li'), function(index, value) {
							if ($(value).attr('id') == parts[1]) { 
								if (index != 0) {
								    creddeckapi.seekTo(index + 1);                
								}
							}
						});
					}
				}
			}
		},

		isMSIE : function() {
			return /msie/.test(navigator.userAgent.toLowerCase()) && !/opera/.test(navigator.userAgent.toLowerCase());
		},

		browserVersion : function() {
			_version = parseFloat(publicAPI.isMSIE() ? navigator.userAgent.toLowerCase().substr(navigator.userAgent.toLowerCase().indexOf('msie') + 4) :
            (navigator.userAgent.toLowerCase().match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0,'0'])[1]);

			return _version;
		}
    }

	return publicAPI;
}();