/**
 * Burwell foundation JavaScript - intended for central management of all JavaScript implementations
 * @author Marty Wallace | Creative Breeze
 * @version 19 Dec 2011
 */



// Constants
var REGEX_YOUTUBE = /\{yt\}\[[a-zA-Z\/\.\:]+\]/g;
var LIGHTBOX_YOUTUBE = "youtube";


// Properties
var token = null;
var lightboxActive = false;


// Page specific properties
// Product large view
var thumbContainer;
var productThumbnailList = [];
var originalImage = "";



/**
 * Called once the document structure has loaded
 */
$(document).ready(function()
{
	// Prepare token
	token = $("input#token").val();
	$("body").attr("id", token);
	
	
	// Work with search
	if($("div#results").html().length < 1) $("div#results").hide();
	
	
	// Page-specific scripts
	loadPage();
	doTokens();
	
});



/**
 * Deals with page-specific JavaScript
 */
function loadPage()
{
	switch(token)
	{
		/**
		 * Home page
		 ***************************************************************************
		 */
		case "home":
			
			$("ul#slideshow").cycle();
			
			break;
		
		/**
		 * Product view page
		 ***************************************************************************
		 */
		case "product-page":
			
			$("a.customurl").each(function(i)
			{
				var t = $(this);
				
				if(t.attr("href").length < 1)
				{
					t.css("color", "#AAA");
					t.css("cursor", "default");
					t.attr("href", "#");
				}
			});
			
			// Properties
			thumbContainer = $("div#thumbcontainer");
			var str = thumbContainer.html();
			productThumbnailList = str.split(';');
			
			thumbContainer.html("");
			originalImage = $("div#largeimg img").attr("src");
			
			// Create thumbnails
			if(productThumbnailList.length > 1)
			{
				for(var i = 0; i<productThumbnailList.length; i++)
				{
					var large = Math.round(i/2) * 2 == i ? false : true;
					
					// create thumbnail
					if(!large)
					{
						var thumb = $('<a onmouseout="revert()" onmouseover="swapImage(' + Number(i + 1) + ')" class="tinythumb"></a>');
						thumbContainer.append(thumb);
						
						thumb.append('<img src="' + productThumbnailList[i] + '" width="70" height="70" />');
					}
				}
			}
			
			break;
	}
}

/**
 * Revert back to original image
 */
function revert()
{
	var img = $("div#largeimg img");
	img.attr("src", originalImage);
}


/**
 * Swaps product image
 * @param ix Large image index
 */
function swapImage(ix)
{
	var img = $("div#largeimg img");
	var src = productThumbnailList[ix];
	
	img.attr("src", src);
}


/**
 * Find and replace regex matches
 */
function doTokens()
{
	var content = $("body").html();
	var matches = content.match(REGEX_YOUTUBE);
	
	if(matches != null)
	{
		for(var i = 0; i<matches.length; i++)
		{
			var m = matches[i];
			var url = m.substr(5, m.length - 6);
			
			content = content.replace(m, '<a class="yt-link">' + url + '</a>');
		}
		
		$("body").html(content);
		
		// Youtube button click
		$("a.yt-link").click(function()
		{
			lightbox(LIGHTBOX_YOUTUBE, $(this).html());
			
		});
	}
}


/**
 * Displays a lightbox over the page
 * @param type Lightbox type - see top for type constants to be used
 * @param arguments Dynamic argument to be used in lightbox
 */
function lightbox(type, arguments)
{
	if(!lightboxActive)
	{
		lightboxActive = true;
		
		var box = $('<div id="lightbox"></div>');
		$("body").append(box);
		
		switch(type)
		{
			case LIGHTBOX_YOUTUBE:
				
				var keyList = arguments.split("/");
				var key = keyList[3];
				
				box.html('<iframe width="420" height="315" src="http://www.youtube.com/embed/' + key + '" frameborder="0" allowfullscreen></iframe>');
				
				break;
		}
		
		var clsBtn = $('<a class="close">CLOSE X</a>');
		box.append(clsBtn);
		
		clsBtn.click(function()
		{
			lightboxActive = false;
			$(this).parent().remove();
			
		});
	}
}




