/// <reference path="jquery-vsdoc2.js" />
var pv = (function($, famap) {
	// public api.
	var that = {};

	var defaults = {
		strings: {
			back: 'Tillbaka'
		},
		referer: '',
		cleanProductPrice: {
			pattern: /,00/g,
			replace: ''
		},
		cleanProductTitle: {
			pattern: /\//g,
			replace: ' / '
		}
	};

	var settings;

	// misc. fixed to make certain browsers behave.
	var initCompatabilityFixes = function() {
		// add class 'first-child' to all list-items (li elements) that are the first child of its list.
		$('li:first-child').addClass('first-child');
	};

	// init the DOM.
	that.init = function(options) {
		settings = jQuery.extend({}, defaults, options);
		$(document).ready(function() {
			initCompatabilityFixes();
			famap.initCornerBoxes();
			famap.KeyListener.init();
		});
	};

	// speed to slide the info panel up/down (milliseconds);
	var productBoughtPopupConfig = {
		slideUpSpeed: 500,
		slideDownSpeed: 250,
		waitBefore: 500
	};

	// overload of the famap method.
	var animateProductBoughtPopup = function() {
		var $productBoughtPopup = $('#productBoughtPopup');
		$productBoughtPopup.find('.close').click(function() {
			pv.closeProductBoughtPopup($productBoughtPopup);
		});
		$productBoughtPopup.slideDown(productBoughtPopupConfig.slideDownSpeed, function() {
			setTimeout(function() {
				pv.closeProductBoughtPopup($productBoughtPopup);
			}, 4000);
		});
	};

	// overload famap functionality.
	var extendFamap = function() {
		if (famap && typeof famap !== 'undefined') {
			// replace famap.animateProductBoughtPopup with the pv version.
			famap.animateProductBoughtPopup = function() {
				setTimeout(animateProductBoughtPopup, productBoughtPopupConfig.waitBefore);
			};
			// replace famap.init with the pv version.
			famap.init = function() { }; //init;
		}
	};
	
	var getTextContent = function(e) {
		if (typeof e.textContent == 'string') {
			return e.textContent;
		} else if (typeof e.nodeValue == 'string') {
			return e.nodeValue;
		} else if (typeof e.innerText == 'string') {
			return e.innerText;
		}
		
		return '';
	}
	
	var setTextContent = function(e, value) {
		if (typeof e.textContent == 'string') {
			e.textContent = value;
		} else if (typeof e.nodeValue == 'string') {
			e.nodeValue = value;
		} else if (typeof e.innerText == 'string') {
			e.innerText = value;
		}
	}

	// hide the panel containing info about the just purchased product.
	that.closeProductBoughtPopup = function($productBoughtPopup) {
		$productBoughtPopup.slideUp(productBoughtPopupConfig.slideUpSpeed);
	};

	// adds a back button to the page.
	that.initBackButton = function(text) {
		var $backLink, $container = $('#pageheader-section2');

		if ($container.length && window.self.history.length > 1) {
			text = text || settings.strings.back;
			$backLink = $(document.createElement('a'));
			$backLink.attr('id', 'back');
			$backLink.text(text);
			$backLink.click(function() {
				window.self.history.go(-1);
				return false;
			});
			$container.append($backLink);
		}
	};

	that.initProduct = function(options) {
		pv.initProductLinks(options.productSelector);
		pv.cleanProductTitle({ selector: options.productSelector + ' ' + options.titleSelector });
		pv.cleanProductPrice({ selector: options.productSelector + ' ' + options.priceSelector, pattern: options.pricePattern, replace: options.priceReplace });
	};

	// makes clickable links out of products.
	// all elements matching the productSelector are made clickable.
	that.initProductLinks = function(productSelector) {
		$(productSelector).each(function() {
			var $this = $(this);
			var $link = $this.find('a.info:first');
			if ($link.length) {
				$this.addClass('fauxLink');
				$this.click(function() {
					document.location = $link.attr('href');
					return false;
				});
			}
		});
	};

	that.cleanProductTitle = function(options) {
		/// <summary>
		/// Does a string.replace on the text contents of the elements specified by the jQuery selector in the 'options.selector' argument using
		/// the parameters 'options.pattern' and 'options.replace'.
		/// </summary>
		var settings = jQuery.extend({}, defaults.cleanProductTitle, options);
		var $title = $(settings.selector);
		$title.each(function() {
			var $this = $(this);
			$this.text($this.text().replace(settings.pattern, settings.replace));
		});
	};

	that.cleanProductPrice = function(options) {
		/// <summary>
		/// Does a string.replace on the text contents of the elements specified by the jQuery selector in the 'options.selector' argument using
		/// the parameters 'options.pattern' and 'options.replace'.
		/// </summary>
		var settings = jQuery.extend({}, defaults.cleanProductPrice, options);
		var $price = $(settings.selector);
		$price.each(function() {
			$(this).find('*').andSelf().contents().filter(function() {
					return this.nodeType == 3;
				}).each(function() {
					setTextContent(this, getTextContent(this).replace(settings.pattern, settings.replace));
				});
		});
	};
	
	that.initProductMoreInfo = function(options) {
		/// <summary>
		/// Hides the element specified by the selector passed as the options.container argument and makes the button element
		/// specified by the selector passed as the options.button argument toggle its visibility as a click event.
		/// </summary>
		var $container = $(options.container);
		var $button = $(options.button);
		$container.hide();
		$button.addClass('fauxLink');
		$button.click(function() {
			$container.slideToggle(250, function() {
				$button.text($container.is(':visible') ? options.text.visible : options.text.hidden);
			});
		});
	};

	that.createPageHeaderSection = function(options) {
		/// <summary>
		/// Creates a new element with class 'pageheader-section' and id 'pageheader-section{options.sectionNumber}'.
		/// The text content of the new element is set to the text of the element specified in the options.sourceElement parameter.
		/// </summary>
		var text, $dest;
		var sectionNumber = options.sectionNumber;
		var $source = $(options.sourceElement);

		if ($source.length) {
			text = $source.text();
			$dest = $(document.createElement('div'));
			$dest.attr('id', 'pageheader-section' + sectionNumber);
			$dest.attr('class', 'pageheader-section');
			$dest.text($source.text());
			$('#fw-pageheader').append($dest);
			$source.remove();
		}
	};

	that.insertFlashCatalog = function(options) {
		var params = {
			wmode: 'transparent',
			allowScriptAccess: 'always'
		};
		var flashvars = {
			magid: 196670,
			__width: 170,
			__height: 120,
			launchURL: 'http://viewer.zmags.com/showmag.php?magid=196670',
			numpages: 6,
			launchTarget: '_blank',
			isOpen: true
		};
		$('#' + options.replaceElementId).wrap($(document.createElement('div')).attr('id', 'flashcatalogcontainer'));
		swfobject.embedSWF('http://viewer.zmags.com/gfx/tinyMag2.swf', options.replaceElementId, '170', '120', options.flashVersion, '', flashvars, params, {}, false);
	};

	// pv constructor.
	(function() {
		// replace famap functionality.
		extendFamap();
	})();

	return that;
})(jQuery, famap);

