thonimara = new function() { };
thonimara.Settings = new function() {};
thonimara.CurrentViewedProduct = new function() {};

thonimara.SmallCarousel = null;
thonimara.LargeCarousel = null;
thonimara.ActiveProduct = null;
thonimara.ActiveIndex = null;
thonimara.NextRelatedProduct = null;
thonimara.PrevRelatedProduct = null;
thonimara.IsLook = true;
thonimara.IsInAnimation = false;
thonimara.ParentDocument = null;
thonimara.Products = null;
thonimara.IProducts = null;
thonimara.IProducts = null;
thonimara.CloseElements = null;
thonimara.RemoveElements = null;
thonimara.MaxHeight = null;
thonimara.MaxWidth = null;
thonimara.Height = null;
thonimara.Width = null;
thonimara.IsReady = false;
thonimara.firstFunction = "popular";

// System settings
thonimara.Settings.PreloadImages = true;
thonimara.Settings.DonePreloadLargeImages = false;
thonimara.Settings.BaseUrl = "http://www.thonimara.de/tm11/";

// Carousel settings
thonimara.Settings.LargeCarouselScroll = 1; // How many pictures to scroll
thonimara.Settings.SmallCarouselScroll = 1;

// Measurments settings
thonimara.Settings.MainContainerWidth = 0;
thonimara.Settings.InfoContainerWidth = 900;
thonimara.Settings.ImageContainerWidth = 500;
thonimara.Settings.InfoTextContainerWidth = 400;
thonimara.Settings.ParentWindow = null;
thonimara.Settings.Animate = false;

// Object containing products elements
thonimara.CurrentViewedProduct.Overlay = "#productOverlay"; // the 0.7 transparent overlay
thonimara.CurrentViewedProduct.Container = "#productContainer"; // container for InfoContainer (to prevent it from getting transparent)
thonimara.CurrentViewedProduct.InfoContainer = "#productInfoContainer"; // container for ImageContainer and info
thonimara.CurrentViewedProduct.ImageContainer = "#imageContainer"; // div containing ImageContainerRel
thonimara.CurrentViewedProduct.ImageContainerRel = "#imageContainerRel"; // div used to position buttons
thonimara.CurrentViewedProduct.ProductImageContainer = "#productImageContainer"; // Container for actual img tag, white background
thonimara.CurrentViewedProduct.Image = "#productImage"; // the big image
thonimara.CurrentViewedProduct.Next = "#productButtonNext"; // div next button
thonimara.CurrentViewedProduct.Prev = "#productButtonPrev"; // div prev button
thonimara.CurrentViewedProduct.Close = "#productButtonClose"; // div close button
thonimara.CurrentViewedProduct.Count = "#productImageCount"; // div to hold 1/3 text
thonimara.CurrentViewedProduct.Links = "#productLinks"; // div to hold links
thonimara.CurrentViewedProduct.Info = "#infoTextContainer"; // div to hold product information
thonimara.CurrentViewedProduct.RelatedImages = "#relatedContainer"; // div to hold product information

jQuery(document).ready(
	function()
	{
		// Enable cross sub domain scripting
		document.domain = "thonimara.de";
		//document.domain = "vm00.de";
		
		thonimara.PreInit();
	
		setTimeout( function() { if(thonimara.ParentDocument == null){ thonimara.InitStandalone(); } }, 50 );
	}
);

/// <summary>
///	Initiates the carousels when the browser is ready, this is used when not in an iframe
/// </summary>
thonimara.InitStandalone = function()
{
	thonimara.IsParentForm = false;
	thonimara.ParentDocument = jQuery( "form" );
	
	thonimara.ParentDocument.Create = function (element) { var e = jQuery(element); jQuery(thonimara.ParentDocument).append(e); return e; };
	thonimara.ParentDocument.Append = function (p1, p2) { if(p2 == null) jQuery(thonimara.ParentDocument).append(p1); else jQuery(p1).append(jQuery(p2)); };
	thonimara.ParentDocument.Style = function (id, styles) { jQuery(id).css(styles); };
	thonimara.ParentDocument.Text = function (id, text) { jQuery(id).text(text); };
	thonimara.ParentDocument.Html = function (id, html) { jQuery(id).html(html); };
	thonimara.ParentDocument.Attribute = function (id, name, value) { if(value != null) jQuery(id).attr(name, value); else return jQuery(id).attr(name); };
	thonimara.ParentDocument.Remove = function (id) { jQuery(id).remove(); }
	thonimara.ParentDocument.FadeTo = function (id, speed, opacity) { jQuery(id).fadeTo(speed, opacity); };
	thonimara.ParentDocument.FadeOut = function (id, speed, p1) { if(typeof(p1) == "function"){ jQuery(id).fadeOut(speed, p1); } else if(p1 == true){ jQuery(id).fadeOut(speed, function(){ jQuery(id).remove(); }); } else{ jQuery(id).fadeOut(speed); } };
	thonimara.ParentDocument.BindClose = function (e, t){ thonimara.CloseElements = e; thonimara.RemoveElements = t; for(var i = 0; i < thonimara.CloseElements.length; i++) { jQuery(thonimara.CloseElements[i]).click( function() { for(var j = 0; j < thonimara.RemoveElements.length; j++){ var id = thonimara.RemoveElements[j]; thonimara.ParentDocument.FadeOut(id, 250, true); } } ); } };
	thonimara.ParentDocument.BindOnClick = function (id, func) { jQuery(id).click(func); };
	thonimara.ParentDocument.LoadUrl = function (url) { document.location = url; };
	thonimara.ParentDocument.Animate = function(id, params, duration ){ jQuery(id).animate( params, duration ); };
	thonimara.ParentDocument.Clone = function (target, id) { jQuery(id).clone().appendTo(target); };
	thonimara.ParentDocument.GetCurrentUrl = function() { return window.location; };
	thonimara.ParentDocument.BindOnClickFind = function (id, target, func) { jQuery(id).find(target).each(function(){ jQuery(this).click(func); } ); };
	thonimara.ParentDocument.Prepend = function(p1, p2){ jQuery(p1).prepend(jQuery(p2)); };
	
	thonimara.Init();
}

thonimara.PreInit = function()
{
	
	if(thonimara.Settings.PreloadImages && thonimara.Products != null)
	{
		// Preload images
		for(var i = 0; i < thonimara.Products.length; i++)
		{
			var item = thonimara.Products[i];
			var largeImageSize = thonimara.GetNewSize(item.MaxHeight, item.MaxWidth, 416, -1, -1, -1);
			//var largeUrl = item.ImageUrl + "&" + thonimara.Height + "=" + largeImageSize.Height + "&" + thonimara.Width + "=" + largeImageSize.Width;
			var largeUrl = item.ImageUrl;
			largeUrl =  largeUrl.replace(/&amp;/gi, "&")
			var large = new Image(); 
			large.src = largeUrl;

			var thumbImageSize = thonimara.GetNewSize(item.MaxHeight, item.MaxWidth, 120, -1, -1, -1);
			//var thumbUrl = item.ImageUrl + "&" + thonimara.Height + "=" + thumbImageSize.Height + "&" + thonimara.Width + "=" + thumbImageSize.Width;
			var thumbUrl = item.ImageUrl;
			thumbUrl =  thumbUrl.replace(/&amp;/gi, "&")
			var thumb = new Image(); 
			thumb.src = thumbUrl;
			
		}
		
		// Init the small groups carousel
		jQuery("#groupsCarousel").jcarousel( { 
			wrap: 'circular',
			scroll:thonimara.Settings.SmallCarouselScroll,
			itemVisibleInCallback: {onBeforeAnimation: thonimara.ProductItemVisibleInCallbackSmall},
			itemVisibleOutCallback: {onAfterAnimation: thonimara.ProductItemVisibleOutCallback},
			buttonEveryNextCallback: thonimara.CarouselSmallScrolled,
			buttonEveryPrevCallback: thonimara.CarouselSmallScrolled,
			itemCreated:thonimara.CarouselItemCreatedSmall,
			initCallback: thonimara.InitProducts, 
			defaultWidth:120, 
			minItemsShowing:15, 
			maxElementHeight:120,
			id:"smallCarousel" }, 
			thonimara.Products
		);

		// Init the large products carousel
		jQuery("#collectionCarousel").jcarousel(
			{
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: thonimara.ProductItemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: thonimara.ProductItemVisibleOutCallback},
				buttonEveryNextCallback: thonimara.CarouselNext,
				buttonEveryPrevCallback: thonimara.CarouselPrev,
				itemCreated: thonimara.CarouselItemCreated,
				scroll:thonimara.Settings.LargeCarouselScroll,
				initCallback: thonimara.InitLargeProducts,
				defaultWidth:700,
				minItemsShowing:2, 
				maxElementHeight:416,
				maxElementWidth:100,
				id:"largeCarousel"
			}, thonimara.Products
		);
		
		// Safari wont init the carousels correctly, but scrolling to first pos does the trick
		thonimara.SmallCarousel.scroll( 1 );
		thonimara.LargeCarousel.scroll( 1 );
		
	}
}

/// <summary>
///	Initiates the carousels when the browser is ready
/// </summary>
thonimara.Init = function()
{	
	// Get the form to use
	if(thonimara.ParentDocument == null){
		thonimara.ParentDocument = jQuery( document );
	}

	// Store the width of the main container (in this form, not parent)
	thonimara.Settings.MainContainerWidth = jQuery(".mainContentContainer").width();
	if(thonimara.Settings.ParentWindow == null)
		thonimara.Settings.ParentWindow = thonimara.GetWindowMesaurements();
	
	// bind escape button to close open product
	jQuery(thonimara.ParentDocument.window).keydown( function( e ){ if( e.which == 27 ) { thonimara.CloseProductInfo(); } } );
	
	//jQuery(".fkMenuLink").click(function(){ thonimara.ParentDocument.LoadUrl(jQuery(this).attr("href")); });
	
	
	
	
      
}

/// <summary>
///	Gets the measurments of visible window
/// </summary>
thonimara.GetWindowMesaurements = function() 
{
	var width = jQuery(window).width();
	var docHeight = jQuery(thonimara.ParentDocument).height();
	var winHeight = jQuery(window).height();
	var theHeight = winHeight;
	if(docHeight > winHeight)
		theHeight = docHeight;
	
	return {"Width":width, "Height":theHeight, "WinHeight":winHeight};
}


/// <summary>
///	Initiates the small product images and binds the click event to change visible large product
/// </summary>
thonimara.InitProducts = function(carousel, state) 
{
	// Store the small carousel for future use
	thonimara.SmallCarousel = carousel;
}

/// <summary>
///	Fades active product back to 1.0, fades new active product to 0.5, scrolls large carousel to selected product
/// </summary>
thonimara.LargeProductClick = function(sender)
{
	
	if(thonimara.ParentDocument != null)
	{
		
		thonimara.IsLook = true;
		var itemIndex = Number(jQuery(sender).children("input").val());
		var productItem = thonimara.Products[itemIndex];
		
		
		// ...scroll selected product into first pos
		thonimara.SelectSmallProduct(itemIndex+1, thonimara.SmallCarousel.first);
		thonimara.ActiveIndex = itemIndex;
		
		// Hide navbuttons
		jQuery(".jcarousel-prev").css( "display", "none" );
		jQuery(".jcarousel-next").css( "display", "none" );
		
		// NOTE: Dont change order of following functioncalls
		
		// Create overlay
		thonimara.CreateInfoOverlay();
		
		// ...create container
		thonimara.CreateContainer();
		
		// ...create infor container
		thonimara.CreateInfoContainer(productItem);
		
		// ...create image container and adds it to infoContainer
		thonimara.CreateImageContainer(productItem);
		
		// ...create text container and adds it to infoContainer
		thonimara.CreateInfoTextContainer(productItem);
		
		// ...animate opacity on overlay
		thonimara.ParentDocument.FadeTo(thonimara.CurrentViewedProduct.Overlay, 500, 0.8);
		thonimara.ParentDocument.FadeTo(thonimara.CurrentViewedProduct.Container, 500, 1);
		
		//  Show First Function Text
		jQuery('#'+thonimara.firstFunction).fadeIn();

		var closeElems = [thonimara.CurrentViewedProduct.Container, thonimara.CurrentViewedProduct.Overlay, thonimara.CurrentViewedProduct.Close];
		var hideElems = [thonimara.CurrentViewedProduct.Container, thonimara.CurrentViewedProduct.Overlay];
		thonimara.ParentDocument.BindClose(closeElems, hideElems);
		
		// Preload all large images to get a "smoother" GUI
		if(!thonimara.Settings.DonePreloadLargeImages)
		{
			for(var i = 0; i < thonimara.Products.length; i++)
			{
				var item = thonimara.Products[i];
				var maxImageSize = thonimara.GetNewSize(item.MaxHeight, item.MaxWidth, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
				//var maxUrl = item.ImageUrl + "&" + thonimara.Height + "=" + maxImageSize.Height + "&" + thonimara.Width + "=" + maxImageSize.Width;
				var maxUrl = item.ImageUrl;
				maxUrl =  maxUrl.replace(/&amp;/gi, "&")
				var max = new Image(); 
				max.src = maxUrl;
			}
			
			thonimara.Settings.DonePreloadLargeImages = true;
		}
		
		if (productItem.IsLook == 0)
			thonimara.LoadIProductInfo(productItem.ID);
	}
}

/// <summary>
///	Creates a div that spans over entire window
/// </summary>
thonimara.CreateInfoOverlay = function()
{
	// Remove information window from parent form
	thonimara.ParentDocument.Remove("div[id*=productInfoOverlay]");
	
	// Create the new information window
	thonimara.ParentDocument.Create('<div id="productOverlay"></div>');
	
	// ...style overlay element
	thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Overlay, "class", "productOverlay");
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Overlay, {
			"background-color":"#ffffff", "position":"absolute", "opacity":"0", "left":"0px", "right":"0px", "top":"0px",
			"z-index":"1000", "height": thonimara.Settings.ParentWindow.Height + "px"
		}
	);
}

/// <summary>
///	Creates a div that spans over entire window
/// </summary>
thonimara.CreateContainer = function()
{
	// Remove information window from parent form
	thonimara.ParentDocument.Remove("div[id*=productContainer]");
	
	// Create the new information window
	thonimara.ParentDocument.Create('<div id="productContainer"></div>');
	
	// ...style overlay element
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Container, {
			"position":"absolute", "left":"0px", "right":"0px", "top":"0px",
			"z-index":"1001", "height": thonimara.Settings.ParentWindow.Height + "px", "opacity":"0"
		}
	);
}

/// <summary>
///	Creates a container for the product information
/// </summary>
thonimara.CreateInfoContainer = function(productItem)
{
	
	thonimara.ParentDocument.Create('<div id="productInfoContainer"></div>');
	
	// ...style the info container
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.InfoContainer, {
			"background-color":"#ffffff", "color":"#1a1a1a", "position":"absolute", "padding":"0 0 0 20px", "top":"0px", "height": (thonimara.Settings.ParentWindow.WinHeight - 40) + "px","width":"1000px","overflow":"hidden"
		}
	);
	
	
	if(productItem != null)
	{
		//var theImage = productItem.Images[0];
		var imageSize = thonimara.GetNewSize(productItem.MaxHeight, productItem.MaxWidth, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		
		// Adjust position to match first image (display image in PC)
		thonimara.RepositionProductContainer(imageSize, false);
	}
	
	// ...prevent click on info box to close overlay
	thonimara.ParentDocument.BindOnClick(thonimara.CurrentViewedProduct.InfoContainer, function(){ return false; } );
	
	// ...add information container to overlay
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Container, thonimara.CurrentViewedProduct.InfoContainer);
}

/// <summary>
///	Creates a container for the product images
/// </summary>
thonimara.CreateImageContainer = function(productItem)
{
		
	// ...create the image container
	thonimara.ParentDocument.Create('<div id="imageContainer"></div>');
	thonimara.ParentDocument.Create('<div id="imageContainerRel"></div>');
	thonimara.ParentDocument.Create('<div id="productImageContainer"></div>');
	
	thonimara.ParentDocument.Create('<img id="productImage" />');
	thonimara.ParentDocument.Create('<div id="productButtonClose"><img src="' + thonimara.Settings.BaseUrl + 'images/close.png" /></div>');
	thonimara.ParentDocument.Create('<div id="productLinks"></div>');
	
	// ...style the image info container
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ImageContainer, { "float":"left", "padding-right":"20px" } );
	
	// ...style the image info container
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ImageContainerRel, { "padding-bottom":"20px", "position":"relative" } );
	
	// ...style the product image container
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, { "background-color":"#ffffff", "text-align":"center", "overflow":"hidden" } );
	
	// ...style the image
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Image, { } );
	
	// ...style the close button
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Close, { "position":"absolute", "top":"30px", "right":"10px", "cursor":"pointer", "padding-left":"20px", "padding-bottom":"20px" } );
	
	// ...style the links
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Links, {"right":"0px", "left":"0px","width": "615px", "height":"10px", "position":"absolute", "margin-right":"auto", "margin-left":"auto", "text-align":"center", "font-family":"Georgia","color":"#000","background-color":"#000000","padding":"3px"  } );
	
	thonimara.ParentDocument.Create('<span id="space">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
	thonimara.ParentDocument.Create('<span id="printLinkSpace">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
	thonimara.ParentDocument.Create('<a id="downloadLink"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-dl.png" /></a>');
	thonimara.ParentDocument.Create('<a target="_blank" id="printLink"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-print.png" /></a>');
	thonimara.ParentDocument.Create('<a id="shareLink" href="share with the world"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-share.png" /></a>');
	//thonimara.ParentDocument.Create('<a id="blogLink" href="blog this"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-blog.png" /></a>');
	thonimara.ParentDocument.Create('<a id="findLink" href="find store"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-find.png" /></a>');
	
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#downloadLink" );
	thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#printLinkSpace" );
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#printLink" );
	thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#shareLink" );
	//thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
	//thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#blogLink" );
	thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#findLink" );
	
	thonimara.ParentDocument.Attribute("#printLink", "href", thonimara.Settings.BaseUrl  + "&Image=0" );
	thonimara.ParentDocument.BindOnClick("#printLink", function() { window.open(jQuery(this).attr("href"), "_blank"); } );
	
	thonimara.ParentDocument.Attribute("#findLink", "href", "http://www.thonimara.de/stores/store-locator" );
	thonimara.ParentDocument.BindOnClick("#findLink", function() { window.open(jQuery(this).attr("href"), "_blank"); } );
	
	thonimara.ParentDocument.BindOnClick("#blogLink", function() { thonimara.ShowShareThis(); return false; } );
	thonimara.ParentDocument.BindOnClick("#shareLink", function() { thonimara.ShowBlogInfo(); return false; } );
	
	//thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Links + " > a", { "text-decoration":"none", "color":"#FFFFFF" });
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Links + " > a > img", { "border":"none", "display":"inline" });
	
	// ...create image controls if there are more than one image
	//var productItem = {};
	//productItem.ImageUrl = "http://www.thonimara.de/tm11/media/catalog/product/cache/1/small_image/390x/9df78eab33525d08d6e5fb8d27136e95/_/d/_d3x7623.jpg";
	
	if(productItem.ImageUrl != null)
	{
		// ...prepare first image in array
		var theImage = productItem.Images[0];
		var imageSize = thonimara.GetNewSize(productItem.MaxHeight, productItem.MaxWidth, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		
		// ...set downloadlink to first image
		thonimara.ParentDocument.Attribute("#downloadLink", "href", thonimara.Settings.BaseUrl + productItem.ImageUrl.replace(/&amp;/gi, "&") + "&MediaArchive_ForceDownload=true" );
		thonimara.ParentDocument.BindOnClick("#downloadLink", function() { window.open(jQuery(this).attr("href")); } );
		
		// ...load first image in array (display image in productcatalog)
		thonimara.ParentDocument.Create('<div id="productButtonNext"><div style="position:relative;"><div id="productButtonNextImage" style="position:absolute;"><img style="padding-top:100px;display:inline;" src="' + thonimara.Settings.BaseUrl + 'images/big_right_white.png" /></div></div></div>');
		thonimara.ParentDocument.Create('<div id="productButtonPrev"><div style="position:relative;"><div id="productButtonPrevImage" style="position:absolute;"><img style="padding-top:100px" src="' + thonimara.Settings.BaseUrl + 'images/big_left_white.png" /></div></div></div>');

		thonimara.ParentDocument.Create('<div id="productImageCount"></div>');
		
		// ...add count ifnromation
		thonimara.ParentDocument.Text(thonimara.CurrentViewedProduct.Count, (productItem.Index+1) + "/" + thonimara.Products.length);
		
		// ...set width of image to image container
		if(imageSize.Width < 0)
			imageSize.Width = 0;
			
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "600 px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px"});
		
		// ...build new image url and set source on image element
		//var imageUrl = productItem.ImageUrl + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		var imageUrl = productItem.ImageUrlgross;
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", imageUrl.replace(/&amp;/gi, "&"));
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index", "0");
		
		// ...style the image count
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Count, { "width":"41px","text-align":"center","line-height":"18px", "height":"24px", "font-size":"11px", "position":"absolute", "left":"10px", "top":"10px", "color":"#000","background-image":"url(" + thonimara.Settings.BaseUrl + "images/pagination-background.png)","background-repeat":"no-repeat" } );		
		// ...style the next button
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Next, { "position":"absolute", "width":"35px", "right":"0px", "top":"0px", "bottom":"0px", "cursor":"pointer" } );
		thonimara.ParentDocument.Style("#productButtonNextImage", { "top":  ((thonimara.Settings.ParentWindow.WinHeight / 2) - 115) + "px","right":"10px", "cursor":"pointer","height":"250px","width":"100px","text-align":"right","background-image":"url(" + thonimara.Settings.BaseUrl + "images/blank.png)" } );
		
		// ...style the prev button
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Prev, { "position":"absolute", "width":"35px", "left":"0px", "top":"0px", "bottom":"0px", "cursor":"pointer" } );
		thonimara.ParentDocument.Style("#productButtonPrevImage", { "top":  ((thonimara.Settings.ParentWindow.WinHeight / 2) - 115) + "px","left":"10px", "cursor":"pointer","height":"250px","width":"100px","background-image":"url(" + thonimara.Settings.BaseUrl + "images/blank.png)" } );
	}
	else
	{
		
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ImageContainer, { "width":thonimara.Settings.ImageContainerWidth + "px"} );
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ImageContainerRel, { "width":thonimara.Settings.ImageContainerWidth + "px"} );
	}
	
	// ...add controls
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, thonimara.CurrentViewedProduct.ProductImageContainer);
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, thonimara.CurrentViewedProduct.Close);
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, thonimara.CurrentViewedProduct.Links);
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ProductImageContainer, thonimara.CurrentViewedProduct.Image);
	
	
	// ...add click event on close button
	thonimara.ParentDocument.BindOnClick(thonimara.CurrentViewedProduct.Close, function() { thonimara.CloseProductInfo(); } );	
	
	// ...add image controls if there are more than one image
	//if(productItem.Images != null && productItem.Images.length > 1)
	//{
		// ...add image controls to ImageContainerRel
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, "#productButtonNextImage");
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, "#productButtonPrevImage");
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, thonimara.CurrentViewedProduct.Count);
		
		// ...add click event on image controlbuttons
		thonimara.ParentDocument.BindOnClick("#productButtonNextImage", function() { thonimara.ShowNextProductInfo(); return false; } );
		thonimara.ParentDocument.BindOnClick("#productButtonPrevImage", function() { thonimara.ShowPrevProductInfo(); return false; } );
//	}
//	else
//	{
//		thonimara.ParentDocument.Remove(thonimara.CurrentViewedProduct.Next);
//		thonimara.ParentDocument.Remove(thonimara.CurrentViewedProduct.Prev);
//		thonimara.ParentDocument.Remove(thonimara.CurrentViewedProduct.Count);
//	}
	
	// ...add the relative positioned div to ImageContainer
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainer, thonimara.CurrentViewedProduct.ImageContainerRel);
	
	// ...add the image to the info container
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.InfoContainer, thonimara.CurrentViewedProduct.ImageContainer);

}

thonimara.ShowNextProductInfo = function()
{
	if (!thonimara.IsLook)
	{
			thonimara.LoadIProductInfo(thonimara.NextRelatedProduct);
	}
	else
	{
		if(thonimara.ActiveIndex == (thonimara.Products.length - 1))
		{
			thonimara.LoadProductInfo(0);
		}
		else
		{
			thonimara.LoadProductInfo(thonimara.ActiveIndex + 1);
		}
	}
}

thonimara.ShowPrevProductInfo = function()
{
	if (!thonimara.IsLook)
	{
		thonimara.LoadIProductInfo(thonimara.PrevRelatedProduct);
	}
	else
	{
		if(thonimara.ActiveIndex == 0)
			thonimara.LoadProductInfo(thonimara.Products.length - 1);
		else
			thonimara.LoadProductInfo(thonimara.ActiveIndex - 1);
	}
}

thonimara.LoadProductInfo = function(i)
{
	
	if(thonimara.ParentDocument != null)
	{
		var productItem = thonimara.Products[i];
		
		thonimara.ParentDocument.Remove("#productImageBlogInfo");
		
		// ...scroll to the product
		if (productItem.IsLook==1)
			thonimara.SelectSmallProduct(i+1, thonimara.SmallCarousel.first);
		
		// ...add count ifnromation
		thonimara.ParentDocument.Text(thonimara.CurrentViewedProduct.Count, (i+1) + "/" + thonimara.Products.length);
		
		// ...clear information text
		thonimara.ParentDocument.Html(thonimara.CurrentViewedProduct.Info, "");


		// ...clear the links so we can re-add print-link
		thonimara.ParentDocument.Html(thonimara.CurrentViewedProduct.Links, "");

		// ... re-add all links
		thonimara.ParentDocument.Create('<span id="space">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		thonimara.ParentDocument.Create('<span id="printLinkSpace">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		thonimara.ParentDocument.Create('<a id="downloadLink"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-dl.png" /></a>');
		thonimara.ParentDocument.Create('<a target="_blank" id="printLink"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-print.png" /></a>');
		thonimara.ParentDocument.Create('<a id="shareLink" href="share with the world"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-share.png" /></a>');
		//thonimara.ParentDocument.Create('<a id="blogLink" href="blog this"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-blog.png" /></a>');
		//thonimara.ParentDocument.Create('<a id="findLink" href="find store"><img src="' + thonimara.Settings.BaseUrl + 'images/fil-find.png" /></a>');    
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#downloadLink" );
		thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#printLinkSpace" );
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#printLink" );
		thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#shareLink" );
		thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
		//thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#blogLink" );
		//thonimara.ParentDocument.Clone(thonimara.CurrentViewedProduct.Links, "#space" );
		//thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Links, "#findLink" );


		// ...re-style the links
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Links, { "position":"absolute", "right":"0px", "left":"0px", "text-align":"center", "font-family":"Georgia","margin-right":"auto", "margin-left":"auto" } );    
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Links + " > a > img", { "border":"none", "display":"inline", "top":"5px" });
		

		// ...add information text
		if(productItem.Infos != null && productItem.Infos.length > 0)
		{
			for(var i = 0; i < productItem.Infos.length; i++)
			{
				var infoItem = thonimara.ParentDocument.Create("<div></div>").attr("id",productItem.Infos[i].Pid);;

				thonimara.ParentDocument.Style(infoItem, { "padding-right":"7px", "float":"left", "line-height":"120%"} );
/*
				if (productItem.Infos[i].Image == 1)
				{
					thonimara.ParentDocument.Style(infoItem, { "cursor":"pointer" } );
					jQuery(infoItem).click(function()
					{
						thonimara.LoadIProductInfo(jQuery(this).attr("id"));
					});
				}
*/
					var pic = productItem.Infos[i].Text.replace(/\/MediaBinaryLoader/,thonimara.Settings.BaseUrl+"/MediaBinaryLoader");
					pic = pic.replace(/\/ProductCatalogBinaryLoader/,thonimara.Settings.BaseUrl+"/ProductCatalogBinaryLoader");

		
				thonimara.ParentDocument.Html(infoItem, pic);

				thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Info, infoItem);
				
			}
		}	
	
		//  Show First Function Text
		jQuery('#'+thonimara.firstFunction).fadeIn();
		
		// ...prepare first image in array
		var imageSize = thonimara.GetNewSize(productItem.MaxHeight, productItem.MaxWidth, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		
		// Adjust position of product container
		thonimara.RepositionProductContainer(imageSize, false);	
		
		// ...set downloadlink to first image
		thonimara.ParentDocument.Attribute("#downloadLink", "href", thonimara.Settings.BaseUrl + productItem.ImageUrl.replace(/&amp;/gi, "&") + "&MediaArchive_ForceDownload=true" );

		// ...update printlink	
		thonimara.ParentDocument.Attribute("#printLink", "href", thonimara.Settings.BaseUrl + "/Templates/ProductCatalogProduct1.aspx?PID=" + productItem.ID + "&Image=0" );

		thonimara.ParentDocument.BindOnClick("#shareLink", function() { thonimara.ShowBlogInfo(); return false; } );
		thonimara.ParentDocument.BindOnClick("#printLink", function() { window.open(jQuery(this).attr("href"), "_blank"); } );
		thonimara.ParentDocument.Attribute("#findLink", "href", "http://www.thonimara.de/stores/store-locator" );
		thonimara.ParentDocument.BindOnClick("#findLink", function() { window.open(jQuery(this).attr("href"), "_blank"); } );
		thonimara.ParentDocument.BindOnClick("#downloadLink", function() { window.open(jQuery(this).attr("href")); } );
		thonimara.ParentDocument.BindOnClick("#blogLink", function() { thonimara.ShowShareThis(); return false; } );

		// ...set width of image to image container
		if(imageSize.Width < 0)
			imageSize.Width = 0;
			
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px"});
		
		// ...build new image url and set source on image element
		//var imageUrl = productItem.ImageUrl + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		var imageUrl = productItem.ImageUrlgross;
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", imageUrl.replace(/&amp;/gi, "&"));
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index", "0");
		
		// ...fix buttons position according to current image height
		//	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Next, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		//	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Prev, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		if (productItem.IsLook == 0)
			thonimara.LoadIProductInfo(productItem.ID);
	}
}

thonimara.LoadIProductInfo = function(productId)
{

	//alert(productId);
	var url = "/Templates/json_productRelation.aspx?";
	url = url + "ProductId=" + productId;
	
	jQuery.getJSON(url, function(data) {
		var productItem = data.Products[0];

		thonimara.IsLook = false;
		
		// ...add count ifnromation
		thonimara.ParentDocument.Text(thonimara.CurrentViewedProduct.Count, productItem.Index+1 + "/" + productItem.Infos.length);
		
		if (thonimara.Products[0].IsLook == 0)
		{
			// ...scroll to the product
			thonimara.SelectSmallProduct(productItem.Index+1, thonimara.SmallCarousel.first);
			thonimara.ActiveIndex = productItem.Index;
		}
	
		// ...clear information text
		thonimara.ParentDocument.Html(thonimara.CurrentViewedProduct.Info, "");
	
		// ... add product text
		var infoItem = thonimara.ParentDocument.Create("<div></div>");
		thonimara.ParentDocument.Style(infoItem, { "padding-top":"20px","padding-left":"0px", "line-height":"200%" } );
		thonimara.ParentDocument.Html(infoItem, productItem.Text);
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Info, infoItem);
		var breakItem = thonimara.ParentDocument.Create("<br/><br/>");
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Info, breakItem);

		// ...add information text
		if(productItem.Infos != null && productItem.Infos.length > 0)
		{
			for(var i = 0; i < productItem.Infos.length; i++)
			{
			
				if (productItem.ID == productItem.Infos[i].Pid)
				{
					if (i+1 < productItem.Infos.length)
						thonimara.NextRelatedProduct = productItem.Infos[i+1].Pid;
					else
						thonimara.NextRelatedProduct = productItem.Infos[0].Pid;
						
					if (i-1 < 0)
						thonimara.PrevRelatedProduct = productItem.Infos[productItem.Infos.length-1].Pid;
					else
						thonimara.PrevRelatedProduct = productItem.Infos[i-1].Pid;
				}
		/*	
				var infoItem = thonimara.ParentDocument.Create("<div></div>").attr("id",productItem.Infos[i].Pid);
				thonimara.ParentDocument.Style(infoItem, { "cursor":"pointer", "padding-right":"7px", "line-height":"200%", "float":"left"} );

				jQuery(infoItem).click(function()
				{
					thonimara.LoadIProductInfo(jQuery(this).attr("id"));
				});
			*/	
				var pic = productItem.Infos[i].Text.replace(/\/MediaBinaryLoader/,thonimara.Settings.BaseUrl+"/MediaBinaryLoader");
				pic = pic.replace(/\/ProductCatalogBinaryLoader/,thonimara.Settings.BaseUrl+"/ProductCatalogBinaryLoader");
				thonimara.ParentDocument.Html(infoItem, pic);
				thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Info, infoItem);
			}
		}	
	
		// ...prepare first image in array
		var imageSize = thonimara.GetNewSize(productItem.MaxHeight, productItem.MaxWidth, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		
		// Adjust position of product container
		thonimara.RepositionProductContainer(imageSize, false);	
		
		// ...set downloadlink to first image
		thonimara.ParentDocument.Attribute("#downloadLink", "href", thonimara.Settings.BaseUrl + productItem.ImageUrl.replace(/&amp;/gi, "&") + "&MediaArchive_ForceDownload=true" );

		// ...update printlink	
		thonimara.ParentDocument.Attribute("#printLink", "href", thonimara.Settings.BaseUrl + "/Templates/ProductCatalogProduct1.aspx?PID=" + productItem.ID + "&Image=0" );

		// ...set width of image to image container
		if(imageSize.Width < 0)
			imageSize.Width = 0;
			
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px"});
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px"});
		
		// ...build new image url and set source on image element
		//var imageUrl = productItem.ImageUrl + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		
		var imageUrl = productItem.ImageUrl;
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", imageUrl.replace(/&amp;/gi, "&"));
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index", "0");
		
		// ...fix buttons position according to current image height
		//	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Next, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		//	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Prev, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );

	});
}


thonimara.MaxImageHeight = function()
{
	return thonimara.Settings.ParentWindow.WinHeight - 50; // Minus 50 for margin in top and bottom and text under image
}

thonimara.MaxImageWidth = function()
{
	return thonimara.Settings.ParentWindow.Width - thonimara.Settings.InfoTextContainerWidth - 20;
}

thonimara.GetNewSize = function(height, width, maxHeight, maxWidth, minHeight, minWidth)
{
	var result = {"Height":height,"Width": width};

	var heightWidthRatio = height/width;
	var resizeMaxHeight = (height > maxHeight) && maxHeight != -1;
	var resizeMaxWidth = (width > maxWidth) && maxWidth != -1;
	var resizeMinHeight = (height < minHeight) && minHeight != -1;
	var resizeMinWidth = (width < minWidth) && minWidth != -1;

	if (resizeMaxHeight && resizeMaxWidth){
		result.Height = maxHeight;
		result.Width = parseInt(result.Height/heightWidthRatio);

		if (result.Width > maxWidth){
			result.Width = maxWidth;
			result.Height = parseInt(result.Width*heightWidthRatio);
		}
	}
	else{
		if (resizeMaxHeight){
			result.Height = maxHeight;
			result.Width = parseInt(result.Height/heightWidthRatio);
		}
		if (resizeMaxWidth){
			result.Width = maxWidth;
			result.Height = parseInt(result.Width*heightWidthRatio);
		}
	}

	if (resizeMinHeight && resizeMinWidth){
		result.Height = minHeight;
		result.Width = parseInt(result.Height/heightWidthRatio);

		if (result.Width > minWidth){
			result.Width = minWidth;
			result.Height = parseInt(result.Width*heightWidthRatio);
		}
	}
	else{
		if (resizeMinHeight){
			result.Height = minHeight;
			result.Width = parseInt(result.Height/heightWidthRatio);
		}
		if (resizeMinWidth){
			result.Width = minWidth;
			result.Height = parseInt(result.Width*heightWidthRatio);
		}
	}
	
	if(result.Width < 0)
		result.Width = 0;
	
	if(result.Height < 0)
		result.Height = 0;

	return result;
}

/// <summary>
///	Creates a container for the product specs
/// </summary>
thonimara.CreateInfoTextContainer = function(productItem)
{
	// ...create the image container
	thonimara.ParentDocument.Create('<div id="infoTextContainer"></div>');
	
	// ...style the image info container
	thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Info, { "background-color:#ff0000; font-size":"10px","font-family":"Trebuchet MS,Georgia,Times New Roman,serif", "float":"left", "overflow":"auto", "height":thonimara.MaxImageHeight() +"px", "width":thonimara.Settings.InfoTextContainerWidth-30 + "px" } );	
	// ...add information text
	if(productItem.Infos != null && productItem.Infos.length > 0)
	{
		for(var i = 0; i < productItem.Infos.length; i++)
		{
			var infoItem = thonimara.ParentDocument.Create("<div></div>").attr("id",productItem.Infos[i].Pid);
			
			thonimara.ParentDocument.Style(infoItem, { "padding-right":"7px", "float":"left", "line-height":"120%" } );

			if (productItem.Infos[i].Image == 1)
			{
				// alert("stop"+productItem.Infos.length+"  ");
				thonimara.ParentDocument.Style(infoItem, { "cursor":"pointer" } );
				/*
				jQuery(infoItem).click(function()
				{
					thonimara.LoadIProductInfo(jQuery(this).attr("id"));
				});  
				*/
			}

				var pic = productItem.Infos[i].Text.replace(/\/MediaBinaryLoader/,thonimara.Settings.BaseUrl+"/MediaBinaryLoader");
				pic = pic.replace(/\/ProductCatalogBinaryLoader/,thonimara.Settings.BaseUrl+"/ProductCatalogBinaryLoader");   
				//pic = "<img src=\""+productItem.Infos[i].ImageSmall+"\" >";
			thonimara.ParentDocument.Html(infoItem, pic);
						
			thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.Info, infoItem);
			
		}
	}
	jQuery('#popular').fadeIn();
	// ...add the text to the info container
	thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.InfoContainer, thonimara.CurrentViewedProduct.Info);	
}

/// <summary>
///	Shows next image
/// </summary>
thonimara.CloseProductInfo = function()
{
	
	thonimara.ShowNavButtons();
	thonimara.ParentDocument.FadeOut(thonimara.CurrentViewedProduct.Overlay, 250, function(){ thonimara.ParentDocument.Remove(thonimara.CurrentViewedProduct.Overlay); });
	thonimara.ParentDocument.FadeOut(thonimara.CurrentViewedProduct.Container, 250, function(){ thonimara.ParentDocument.Remove(thonimara.CurrentViewedProduct.Container); });
	
}

thonimara.ShowNavButtons = function()
{
	jQuery(".jcarousel-prev").css( "display", "block" );
	jQuery(".jcarousel-next").css( "display", "block" );
}

/// <summary>
///	Adjusts the position of the InfoContainer according to provided image size
/// </summary>
thonimara.RepositionProductContainer = function(sizes, animate)
{
	
	// thonimara.CurrentViewedProduct.Info
	var left = (thonimara.Settings.ParentWindow.Width / 2) - (sizes.Width / 2);
	var containerWidth = thonimara.Settings.InfoTextContainerWidth + sizes.Width + 10;
	
	// Check if image pushed the info text container out of view
	if( ((thonimara.Settings.ParentWindow.Width - sizes.Width) / 2) < thonimara.Settings.InfoTextContainerWidth)
	{
		var overflow = thonimara.Settings.InfoTextContainerWidth - ((thonimara.Settings.ParentWindow.Width - sizes.Width) / 2);
		left = left - overflow - 5;
	}
	
	// Container Centering
	left=(thonimara.Settings.ParentWindow.Width/2)-(containerWidth/2);
	if(thonimara.Settings.Animate || (typeof(overrideAnimate) != "undefined" && animate == true))
		thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.InfoContainer, {"left":left + "px", "width":containerWidth + "px"}, 500);
	else
		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.InfoContainer, {"left":left + "px", "width":containerWidth + "px"});
}

thonimara.ShowBlogInfo = function()
{
	thonimara.ParentDocument.Remove('#productImageBlogInfo');

	var productItem = thonimara.Products[thonimara.ActiveIndex];
	if(productItem != null && productItem.Images != null && productItem.Images.length > 0)
	{
		var currentIndex = Number(thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index"));
		var theImage = productItem.Images[currentIndex];
		
		var bottom = thonimara.MaxImageHeight();
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfo"></div>');
		thonimara.ParentDocument.Style("#productImageBlogInfo", { "position":"absolute", "left":"0px", "bottom":"40px", "right":"20px", "background-color":"#FFFFFF", "padding":"10px", "color":"#000000" } );
		
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, "#productImageBlogInfo");
		var currentUrl = thonimara.ParentDocument.GetCurrentUrl();
		var url = Url.encode( currentUrl.href );
		
		var html = '<ul style="margin:0; padding:0; list-style:none; float:left;">';
		html += '	<li><a target="_blank" href="http://www.facebook.com/share.php?u=' + url + '" class="facebook" title="Facebook">Facebook</a></li>';
		html += '	<li><a target="_blank" href="http://digg.com/submit?phase=2&url=' + url + '" class="digg" title="Digg">Digg</a></li>';
		html += '	<li><a target="_blank" href="http://www.stumbleupon.com/submit?url=' + url + '" class="stumbleupon" title="Stumbleupon">Stumbleupon</a></li>';
		html += '	<li><a target="_blank" href="http://delicious.com/post?url=' + url + '" class="delicious" title="Delicious">Delicious</a></li>';
		html += '	<li><a target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?c=' + url + '" class="myspace" title="MySpace">MySpace</a></li>';
		html += '	<li><a id="blogLink" href="blog this" title="Blog">Blog this</a></li>';
		html += '</ul>';
		
		thonimara.ParentDocument.Html("#productImageBlogInfo", html);

		thonimara.ParentDocument.BindOnClick("#blogLink", function() { thonimara.ShowShareThis(); return false; } );

		var tagContainer = thonimara.ParentDocument.Create('<div id="blogTagContainer" style="float:left; margin-left:50px; width:280px; overflow:hidden;"></div>');
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfoClose"><img src="' + thonimara.Settings.BaseUrl + 'images/close.png" /></div>');
		thonimara.ParentDocument.Style("#productImageBlogInfoClose", { "float":"right", "cursor":"pointer" } );		
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#productImageBlogInfoClose");
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#blogTagContainer");
		thonimara.ParentDocument.BindOnClick("#productImageBlogInfoClose", function(){ thonimara.ParentDocument.Remove("#productImageBlogInfo"); });
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfoClear" style="clear:both;"></div>');
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#productImageBlogInfoClear");
		
		thonimara.ParentDocument.BindOnClickFind("#productImageBlogInfo", "a", function(){ window.open(jQuery(this).attr("href"), "_blank"); });
		
		thonimara.ParentDocument.Animate("#productImageBlogInfo", {"opacity":"0.7"}, 300);
		
		//thonimara.ParentDocument.Style("#productImageBlogInfo ul > li > a", { "text-decoration":"none", "color":"#000000" });
	}
	
	return false;
}

thonimara.ShowShareThis = function()
{
	thonimara.ParentDocument.Remove('#productImageBlogInfo');
		
	var productItem = thonimara.Products[thonimara.ActiveIndex];
	if(productItem != null && productItem.Images != null && productItem.Images.length > 0)
	{
		var currentIndex = Number(thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index"));
		var theImage = productItem.Images[currentIndex];
		
		var bottom = thonimara.MaxImageHeight();
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfo"></div>');
		thonimara.ParentDocument.Style("#productImageBlogInfo", { "overflow":"auto", "position":"absolute", "left":"20px", "bottom":"40px", "right":"20px", "background-color":"#FFFFFF", "padding":"10px", "color":"#000000" } );
		
		thonimara.ParentDocument.Append(thonimara.CurrentViewedProduct.ImageContainerRel, "#productImageBlogInfo");
		
		// ...prepare first image in array
		var imageSize = thonimara.GetNewSize(productItem.MaxHeight, productItem.MaxWidth, 416, -1, -1, -1);
		var currentUrl = thonimara.ParentDocument.GetCurrentUrl();
		var url = Url.encode( currentUrl.href );
		
		//var imageUrl = productItem.ImageUrl + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		var imageUrl = productItem.ImageUrl;
		imageUrl = imageUrl.replace(/&amp;/gi, "&");
		var tagHtml = '<a href="' + currentUrl.href + '"><img src="' + imageUrl + '" border="0" alt=""></a>';
		
		var tagContainer = thonimara.ParentDocument.Create('<div id="blogTagContainer" style="float:left; width:445px; overflow:hidden;"></div>');
		thonimara.ParentDocument.Text("#blogTagContainer", tagHtml);
		thonimara.ParentDocument.Prepend("#blogTagContainer", "<span>Paste this code in your blog<br /><br /></span>");
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfoClose"><img src="' + thonimara.Settings.BaseUrl + 'images/close.png" /></div>');
		thonimara.ParentDocument.Style("#productImageBlogInfoClose", { "float":"right", "cursor":"pointer" } );		
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#productImageBlogInfoClose");
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#blogTagContainer");
		thonimara.ParentDocument.BindOnClick("#productImageBlogInfoClose", function(){ thonimara.ParentDocument.Remove("#productImageBlogInfo"); });
		
		thonimara.ParentDocument.Create('<div id="productImageBlogInfoClear" style="clear:both;"></div>');
		thonimara.ParentDocument.Append("#productImageBlogInfo", "#productImageBlogInfoClear");
		
		thonimara.ParentDocument.Animate("#productImageBlogInfo", {"opacity":"0.7"}, 300);
	}
	
	return false;
}

/// <summary>
///	Shows next image
/// </summary>
thonimara.NextImage = function()
{
	// ...remove open blogg-this window
	thonimara.ParentDocument.Remove("#productImageBlogInfo");
	
	
	var productItem = thonimara.Products[thonimara.ActiveIndex];
	
	if(productItem != null && productItem.Images != null && productItem.Images.length > 0)
	{
		var currentIndex = Number(thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index"));
		if((currentIndex + 1) < productItem.Images.length)
			currentIndex++;
		else
			currentIndex = 0;
		
		var theImage = productItem.Images[currentIndex];
		var imageSize;
		if(theImage.IsMediaBank)
			imageSize = thonimara.GetNewSize(theImage.Width, theImage.Height, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		else
			imageSize = thonimara.GetNewSize(theImage.Height, theImage.Width, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		
		//var imageUrl = theImage.Url + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		var imageUrl = theImage.Url;
		imageUrl = imageUrl.replace(/&amp;/gi, "&")
		
		// ...prepare first image in array
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", imageUrl);
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index", currentIndex);
		
		// Adjust position of product container
		thonimara.RepositionProductContainer(imageSize, true);
		
		// ...set width of image to image container
		if(imageSize.Width < 0)
			imageSize.Width = 0;
			
		if(thonimara.Settings.Animate)
		{
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
		}
		else
		{	
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
		}
		
		// ...add count ifnromation
		thonimara.ParentDocument.Text(thonimara.CurrentViewedProduct.Count, (currentIndex + 1) + "/" + productItem.Images.length);
		
		// ...fix buttons position according to current image height
		// thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Next, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		// thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Prev, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		
		// ...set downloadlink to first image
		thonimara.ParentDocument.Attribute("#downloadLink", "href", thonimara.Settings.BaseUrl + theImage.Url.replace(/&amp;/gi, "&") + "&MediaArchive_ForceDownload=true" );
		//thonimara.ParentDocument.Attribute("#printLink", "href", thonimara.Settings.BaseUrl + "/Templates/ProductCatalogProduct1.aspx?PID=" + productItem.ID + "&Image=" + currentIndex );
		thonimara.ParentDocument.Attribute("#printLink", "href", "javascript:window.print();" );
		
		
	}
}

/// <summary>
///	Shows previouse image
/// </summary>
thonimara.PrevImage = function()
{
	// ...remove open blogg-this window
	thonimara.ParentDocument.Remove("#productImageBlogInfo");
	
	var productItem = thonimara.Products[thonimara.ActiveIndex];
	
	if(productItem != null && productItem.Images != null && productItem.Images.length > 0)
	{
		var currentIndex = Number(thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index"));
		if(currentIndex == 0)
			currentIndex = productItem.Images.length - 1;
		else
			currentIndex--;
		
		var theImage = productItem.Images[currentIndex];
		var imageSize = thonimara.GetNewSize(theImage.Height, theImage.Width, thonimara.MaxImageHeight(), thonimara.MaxImageWidth(), -1, -1);
		//var imageUrl = theImage.Url + "&" + thonimara.Height + "=" + imageSize.Height + "&" + thonimara.Width + "=" + imageSize.Width;
		var imageUrl = theImage.Url;
		imageUrl = imageUrl.replace(/&amp;/gi, "&")
		
		// ...prepare first image in array
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", imageUrl);
		thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "index", currentIndex);
		
		// Adjust position of product container
		thonimara.RepositionProductContainer(imageSize, true);
		
		// ...set width of image to image container
		if(imageSize.Width < 0)
			imageSize.Width = 0;
			
		if(thonimara.Settings.Animate)
		{
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
			thonimara.ParentDocument.Animate(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"}, 500);
		}
		else
		{	
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductProductImageContainer, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
			thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.ProductImageContainerRel, {"width":imageSize.Width + "px", "height":imageSize.Height + "px"});
		}
		
		// ...add count ifnromation
		thonimara.ParentDocument.Text(thonimara.CurrentViewedProduct.Count, (currentIndex + 1) + "/" + productItem.Images.length);
		
		// ...fix buttons position according to current image height
//		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Next, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
//		thonimara.ParentDocument.Style(thonimara.CurrentViewedProduct.Prev, { "top": ((thonimara.Settings.ParentWindow.Height / 2) - 15) + "px" } );
		
		// ...set downloadlink to first image
		thonimara.ParentDocument.Attribute("#downloadLink", "href", thonimara.Settings.BaseUrl + theImage.Url.replace(/&amp;/gi, "&") + "&MediaArchive_ForceDownload=true" );
		thonimara.ParentDocument.Attribute("#printLink", "href", thonimara.Settings.BaseUrl + "/Templates/ProductCatalogProduct1.aspx?PID=" + productItem.ID + "&Image=" + currentIndex );
	}
}

/// <summary>
///	Handles the next event of the large carousel
/// </summary>
thonimara.CarouselNext = function(carousel) 
{
	thonimara.SelectSmallProduct(thonimara.SmallCarousel.first+1, thonimara.SmallCarousel.first);
}

/// <summary>
///	Handles the prev event of the large carousel
/// </summary>
thonimara.CarouselPrev = function(carousel) 
{
	thonimara.SelectSmallProduct(thonimara.SmallCarousel.first-1, thonimara.SmallCarousel.first);
}

thonimara.CarouselSmallScrolled = function(carousel) 
{
	var index = thonimara.NewIndex(carousel.first, thonimara.Products.length);
	thonimara.SelectSmallProduct(carousel.first, thonimara.SmallCarousel.first);
}

/// <summary>
///	Fades active product back to 1.0, fades new active product to 0.5, scrolls large carousel to selected product
/// </summary>
thonimara.SmallProductClick = function(sender)
{
	if(!thonimara.IsInAnimation)
	{
		thonimara.SelectSmallProduct(jQuery(sender).attr("index"), thonimara.SmallCarousel.first);
	}
	else
	{
		// To ensure that clicked product will be scrolled to, wait for carousel to finish scrolling
		setTimeout(function(){ thonimara.SmallProductClick(sender); }, 200);
	}
}

/// <summary>
///	Fades active product back to 1.0, fades new active product to 0.5, scrolls large carousel to selected product
/// </summary>
thonimara.SelectSmallProduct = function(index, f)
{
	var newIndex = thonimara.NewIndex(index, thonimara.Products.length);
	// To prevent double fading, check so that ActiveProduct isnt clicked product
	var currentProduct = jQuery("#smallProduct_" + thonimara.Products[newIndex].ID);
	
	thonimara.ActiveIndex = newIndex;
	
	if(thonimara.ActiveProduct == null)
	{
		thonimara.ActiveProduct = currentProduct;
		thonimara.ActiveProduct.fadeTo(250, 0.9);
	}	
	else
	{
		if(thonimara.ActiveProduct != null)
			thonimara.ActiveProduct.fadeTo(250, 1.0);					

		thonimara.ActiveProduct = currentProduct;
		thonimara.ActiveProduct.fadeTo(250, 0.5);
	}
	
	// NOTICE: Indexes her is not zero-based
	var i = jQuery.jcarousel.intval( newIndex ) + 1;
	//var f = thonimara.LargeCarousel.first;
	
	// Calculate indexes corresponding to the array of products
	var realCurrentIndex = thonimara.NewIndex(f, thonimara.Products.length);
	var realNewIndex = thonimara.NewIndex(i, thonimara.Products.length);
	// Determin if user went back to the future
	var back = realNewIndex < realCurrentIndex;
	
	// Calculate the new index, insetead of jumping to index on first page we want to go to the closest instance of that product
	var diff = back ? realCurrentIndex - realNewIndex : realNewIndex - realCurrentIndex;
	var prevIndex = f - diff;
	var nextIndex = f + diff;
	
	if(f < 0 || f > thonimara.Products.length)
	{
		var prevCount = 0;
		var nextCount = 0;
		
		if(back)
			nextCount = thonimara.Products.length - (realCurrentIndex - realNewIndex);
		else
			nextCount = (realNewIndex - realCurrentIndex);
		
		if(back)
			prevCount = (realCurrentIndex - realNewIndex);
		else
			prevCount = realCurrentIndex + (thonimara.Products.length - realNewIndex);
		
		prevIndex = f - prevCount;
		nextIndex = f + nextCount;
		
		if(prevCount < nextCount)
			i = prevIndex;
		else if(prevCount == nextCount)
			i = nextIndex;
		else
			i = nextIndex;
	}
	else
		i = back ? prevIndex : nextIndex;
	
	// Scroll to selected product in both carousels
	thonimara.IsInAnimation = true;
	
	thonimara.LargeCarousel.scroll(i , true, "thonimara.SelectSmallProduct_Large" );
	thonimara.SmallCarousel.scroll(index, true, "thonimara.SelectSmallProduct_Small" );
		
	setTimeout(function(){ thonimara.IsInAnimation = false; }, 350);		
}

///<summary>
///	Calculates a new index based on old index (i) and list count (c)
///</summary>
thonimara.NewIndex = function(i, c)
{
	var m = (i / c);
	var fm = Math.floor(m);
	m = (m == fm) ? 1 : (m - fm);
	var ni = Math.round( c * m ) - 1;
	
	return ni;
}

/// <summary>
///	Initiates the large products
/// </summary>
thonimara.InitLargeProducts = function(carousel, state) 
{

	thonimara.LargeCarousel = carousel;
}

/// <summary>
///	Generates the html markup for an LI item in the big carousel
/// </summary>
thonimara.GetProductItemHTML = function (item)
{
	if(item != null)
		//return "<img src=\"" + item.ImageUrl + "&" + thonimara.Height + "=" + item.Height + "&" + thonimara.Width + "=" + item.Width + "\" alt=\"\" title=\"\" /><input type=\"hidden\" value=\"" + item.Index + "\" />";
		return "<img src=\"" + item.ImageUrl+" \"><input type=\"hidden\" value=\"" + item.Index + "\" />";
	return "";
}

/// <summary>
///	Generates the html markup for an LI item in the small carousel
/// </summary>
thonimara.GetProductItemThumbHTML = function (item)
{
	if(item != null)
	{
		var sizes = thonimara.GetNewSize(item.MaxHeight, item.MaxWidth, 120, -1, -1, -1);
		//return "<img src=\"" + item.ImageUrl + "&" + thonimara.Height + "=" + sizes.Height + "&" + thonimara.Width + "=" + sizes.Width + "\" alt=\"\" title=\"\" /><input type=\"hidden\" value=\"" + item.Index + "\" />";
		return "<img src=\"" + item.ImageUrlsmall+" \">";
	}
	
	return "";
}

/// <summary>
///	Handles the item craeted event of the carousel
/// </summary>
thonimara.CarouselItemCreated = function(index, item)
{
	var i1 = thonimara.LargeCarousel.index(index, thonimara.Products.length);
	
	var product = thonimara.Products[i1];
	jQuery(item).append(thonimara.GetProductItemHTML(product));

	if(product != null)
		jQuery(item).css({"width":product.Width + "px", "height":product.Height + "px", "margin-right":"20px" });
	else // Default width and height for item, DO NOT REMOVE!!! Will cause infinite loops...
		jQuery(item).css({"width":"400px", "height":"416px" }); // width needs to be higer than any image otherwise the jcarousel-list will be to small in som cases
	
	setTimeout(function() { jQuery(item).click(function() { thonimara.LargeProductClick(this); return false; }); }, 1);
}

/// <summary>
///	Handles the item creted event of the carousel
/// </summary>
thonimara.CarouselItemCreatedSmall = function(index, item)
{
	var i1 = thonimara.SmallCarousel.index(index, thonimara.Products.length);
	var product = thonimara.Products[i1-1];
	if(product != null)
	{
		var sizes = thonimara.GetNewSize(product.MaxHeight, product.MaxWidth, 120, -1, -1, -1);
		jQuery(item).append(thonimara.GetProductItemThumbHTML(product));
		jQuery(item).css({"width":sizes.Width + "px", "height":sizes.Height + "px" });		
		setTimeout(function() { jQuery(item).click(function() { thonimara.SelectSmallProduct(index, thonimara.SmallCarousel.first); return false; }); }, 1);
		
		// Set id on item
		jQuery(item).attr("id", "smallProduct_" + product.ID);
	}
	else
	{
		// Default width and height for item, DO NOT REMOVE!!! Will cause infinite loops...
		// width needs to be higer than any image otherwise the jcarousel-list will be to small in som cases
		jQuery(item).css({"width":"120px", "height":"120px" });
	}
}

/// <summary>
///	Instantiates a new item in the large product carousel
/// </summary>
thonimara.ProductItemVisibleInCallback = function(carousel, item, i, state, evt)
{
	// The index() method calculates the index from a
	// given index who is out of the actual item range.
	var idx = carousel.index(i, thonimara.Products.length);
	var product = thonimara.Products[idx - 1];
	carousel.add(i, thonimara.GetProductItemHTML(product));
	
	var item = carousel.get(i);
	if(product != null)
		jQuery(item).css({"width": + "px", "height":product.Height + "px" });
	else
		jQuery(item).css({"width":"277px", "height":"416px" }); // Default height for item, DO NOT REMOVE!!! Will cause infinite loops...
	
	// Set id on item
	jQuery(item).attr("id", "product_" + product.ID);
}

/// <summary>
///	Instantiates a new item in the large product carousel
/// </summary>
thonimara.ProductItemVisibleInCallbackSmall = function(carousel, item, i, state, evt)
{
	// The index() method calculates the index from a
	// given index who is out of the actual item range.
	var idx = carousel.index(i, thonimara.Products.length);
	var product = thonimara.Products[idx - 1];
	
	if(product != null)
	{
		carousel.add(i, thonimara.GetProductItemThumbHTML(product));
		var sizes = thonimara.GetNewSize(product.MaxHeight, product.MaxWidth, 120, -1, -1, -1);
		var item = carousel.get(i);
		jQuery(item).css({"width":sizes.Width + "px", "height":sizes.Height + "px" });
			
		// Set id on item
		jQuery(item).attr("id", "smallProduct_" + product.ID);
	}
	else
	{
		jQuery(item).css({"width":"120px", "height":"120px" }); // Default height for item, DO NOT REMOVE!!! Will cause infinite loops...
	}
}

thonimara.ProductItemVisibleOutCallback = function(carousel, item, i, state) 
{
	carousel.remove(i);
}

thonimara.changeImagesFromInfo = function(ImageUrl)
{
	
	thonimara.ParentDocument.Attribute(thonimara.CurrentViewedProduct.Image, "src", ImageUrl.replace(/&amp;/gi, "&"));
}		

thonimara.is_active_function = null;
  //when a tab link is clicked...
thonimara.changeFunktionFromInfo = function(event){

      
      if (thonimara.is_active_function == null)
      {
            thonimara.is_active_function = event;
            /*...get the title attribute (which corensponds to the id of the needed text container),
            but you can use any attribute you want*/
            var container_id = event;
			
			//jQuery('#popular').fadeOut();
            //...animate the current active_tab by changing it's height and opacity ...'
            jQuery('.active_tab').animate({

                height : 'toggle' , opacity : 'toggle'

            //...and when that animation ends...
            },function(){
                //...remove the active_tab class from the current active tab...
               jQuery('.active_tab').removeClass('active_tab');

                //...and add that class to the tab that corensponds the clicked link...
                jQuery('#'+container_id).addClass('active_tab');

                //...and animate the new active_tab by using toggle on height and opacity again...
                jQuery('.active_tab').animate({

                    height : 'toggle' , opacity : 'toggle'

                });
                thonimara.is_active_function = null;
            });
        }

};
