// USE THIS FILE ONLY FOR SCRIPT THAT IS TRULY GLOBAL TO ALL SERVICES THAT USE \js\3\core.js
// Use [servicedomain]\js\[local version number]\[servicename].js for service-specific scripts and
// \lib\[modulename].js for module-specific scripts

new function($){
	var Core = {
		metadata: {
			author: 'BBC World Service',
			version: '0.0.2',
			test: 'test'
		},
		wsQuotes: function(startMark, endMark, markCharacter) {
		
			// Set default values for quotation mark positions
			// Possible values = none; inline; block; block-above (startMark only); block-below (endMark only)
			if (startMark == null || startMark == '') 			{startMark = "inline";}
			if (endMark == null || endMark == '') 				{endMark = "inline";}
			if (markCharacter == null || markCharacter == '') 	{markCharacter = "\u0022";}
					
			return Core.Add.quotes(startMark, endMark, markCharacter);
		},
		wsCorners: function() {
			return Core.Add.corners();
		},
		wsPopup: function(){
			Core.Watch.popup();
			return false;
		},
		Add: {
			quotes: function(startMark, endMark, markCharacter) {		
				
				// Prepare markup for each quotation mark
				var startMarkHtml 	= '<span class="open-quote"><span>' + markCharacter + '</span></span>';
				var endMarkHtml		= '<span class="close-quote"><span>' + markCharacter + '</span></span>';
					
				// Find all quotes in the page
				var quoteBoxes = $(".bx-quote blockquote .body");
				
				quoteBoxes.each(function(){
					var currQuoteBox = $(this);					
					var quoteText = currQuoteBox.text();
					var quoteTextNode;
					
					currQuoteBox.empty();
					if (startMark != "inline") {currQuoteBox.append(startMarkHtml);}			
					currQuoteBox.append("<span class='quote-text'>" + quoteText + "</span>");	
					if (startMark == "inline") {
						quoteTextNode = currQuoteBox.find(".quote-text");
						quoteTextNode.prepend(startMarkHtml);														
					}										
					if (endMark == "block") {										
						currQuoteBox.append(endMarkHtml);														
					} else {
						quoteTextNode = currQuoteBox.find(".quote-text");
						quoteTextNode.append(endMarkHtml);														
					}
				});							
			},
			corners: function() {
				$('#content > .g-block').children().wrap('<div class="jq-c-wrap"><div class="jq-c-content"></div></div>');
				$('#content > .g-block').addClass("jq-c");
			}
		},
		Watch: {
			popup: function() {						
				
				$('a[@class^=ms-], a[@class=^popup]').click(function(){			
				
					var newWindow = {			
						closed: 		true,
						href: 			null,
						mode: 			'aod',
	
						name: 			"bbcwspopup",
						options: {
							width:	 		"420",
							height: 		"220",					
							toolbar: 		"no",
							personalbar: 	"no",
							location: 		"no",
							statusbar: 		"no",
							menubar: 		"no",
							resizable: 		"yes",
							status: 		"no",
							directories: 	"no",
							scrollbars:		"yes",
							left:			"20",
							top: 			"20",
							screenX:		"20",					
							screenY:		"20"		
						}
					};	
					
					// Check for parameters passed in @class
					var parameters = $(this).attr("class").match(/^(\D+)(\d+)x(\d+)$/i).splice(1,3);	
					
					// If parameters were passed, update newWindow							
					if (typeof(parameters) == "object") {
						// newWindow.mode = parameters[0];
						newWindow.options.width = parameters[1];
						newWindow.options.height = parameters[2];
					};							
					
					// Set destination
					newWindow.href = $(this).attr('href');						
				
					// Create and open the popup window
					Open.popup(newWindow);		
				
					return false;
				});		
			}
		},
		Open: {
			popup: function(settings) {
				
				// Build a single string containing the parameters for customising the popup window	
				var options = ""; 
				var delimiter = "";
				for (x in settings.options) {				
					options = options + delimiter + x + "=" + settings.options[x];
					delimiter = ", ";
				};			
						
				// Open popup window
				newWindow = window.open(settings.href, settings.name, options); 
	
				if (!newWindow.opener) {
					newWindow.opener = self;
				};	
				
				if (!newWindow.focus) {
					newWindow.focus();
				};					
			
				return false;					
			}	
		},
		Close: {
			popup: function wsClosePopup() {
				window.close();
				return false;
			}			
		}							
	};
	$.core = Core;
			
}(jQuery);


function pseudoPrototype(original, clone) {
	return pseudoPrototype(original, clone, null);	
}

function pseudoPrototype(original, clone, index) {

	if (index) {
		// Check whether the clone already has this property
		if (!clone[index]) {		
			// It doesn't, so clone it
			clone[index] = original[index];			
		} else {
			for (x in original[index]) {
				if (!clone[index][x]) {
					clone[index] = pseudoPrototype(original[index], clone[index], x);
				};
			};
		};
	} else {			
		// There's no specific property to copy
		for (x in original) {
			// Check for sub-properties
			clone = pseudoPrototype(original, clone, x);
		};		
	};
	
	index = null;
	return clone;
};		




