/************************************** Apple Marketing ********************************
*
* Created By:
* 		Ed Hasting-Evans, Apple Marketing, http://www.apple-marketing.co.uk/
* 
* Description:
*       Shows and hides content and sets a cookie to make it persist
*
* Dependancies:
*       - JQuery plugin v1.4.1
*
* Usage:
*       applemarketing.persistentShowHide({
*			targetDiv: 	'#photoNavigation'
*		});  
*   
***************************************************************************************/
/* CHECKS AND IF MISSING CREATES applemarketing NAMESPACE */ 
if(!applemarketing) {
	// Binds jQuery to $ so can work with jQuery.noConflict();
	var applemarketing={
		jQueryNoConflict: $ = jQuery
	};
}


applemarketing.persistentShowHide = function(options) {
	
	var settings = {
		showIntroText: true,					// If you don't want text before the show/hide, set to false
		introText: 'Thumbnails',				// The text to appear before show/hide
		linkDivider: ' / ',						// What the divider should be between show/hide
		currentView: '#applemarketing_show',	// Holds the current view state
		nodeToShowHide: 'ul',					// This is the top content node that should be hidden
		selectedState: 'selected',				// The selected class
		showText: 'on',							// Show text
		hideText: 'off'							// Hide text
	};
	
	var methods = {
		init: function() {
				// Adds all the HTML to the DOM
				$(settings.targetDiv).append(methods.buildHTML());
				
				// Checks the localStorage and hides thumbs if required
				if(localStorage.getItem('showThumbs') == 'hide') {
					$(settings.targetDiv + ' ' + settings.nodeToShowHide).hide(); // So it doesn't fade
					methods.clickEvent('#applemarketing_hide'); // Does the same thing as clicking on the hide link
				}
				
				// Adds click/hover events
				methods.addClickHandler();
			},
		
		buildHTML: function() {
				// Holds the HTML string
				var thisHTML = '<p class="showHideOptions">';
				
				//If showIntroText is true it adds the intro text to the start of the string
				if(settings.showIntroText) {
					thisHTML += settings.introText + ' ';
				}
				// Builds the rest of the content
				thisHTML += '<span id="applemarketing_show" class="actions ' + settings.selectedState + '">' + settings.showText + '</span>';
				thisHTML += settings.linkDivider;
				thisHTML += '<span id="applemarketing_hide" class="actions">' + settings.hideText + '</span>';
				thisHTML += '</p>';
					
				return thisHTML;
			},

		addClickHandler: function() {
				// Adds hovor behaviour
				$(settings.targetDiv + ' .actions').css({
					'cursor': 'pointer'
				});
				
				// Adds the click events
				$(settings.targetDiv + ' span').click(function(event){
					// Pulls the ID
					var linkID = '#' + $(this).attr('id');
					
					// Passes the information to the clickEvent method
			        methods.clickEvent(linkID);
				});
			},
			
		clickEvent: function(action) {
				if(action != settings.currentView) {			
					// Removes selected class off previous node
					$(settings.targetDiv + ' ' + settings.currentView).removeClass(settings.selectedState);
					$(settings.targetDiv + ' ' + action).addClass(settings.selectedState);
					
					// Sets the selected option as the currentView
					settings.currentView = action;
					
					// Shows or hides the items
					switch(action) {
						case '#applemarketing_hide':
							$(settings.targetDiv + ' ' + settings.nodeToShowHide).fadeOut();
							localStorage.setItem("showThumbs", 'hide');
							break;
						default:
							$(settings.targetDiv + ' ' + settings.nodeToShowHide).fadeIn();
							localStorage.setItem("showThumbs", + 'show');
							break;
					}
				}
			}
	};
	
	// Overwrites the default settings with those defined in the function call (options object)
	if(options) { 
		$.extend(settings, options);
    }
	
	// Executes the code
	methods.init();
	
	// Returns 'this'
	return this;
};






