﻿/*! $Id: accessories.js 6700 2009-10-21 18:26:57Z bhillebrand $ */
/**
 * accessories.js
 * @author hillebrand
 **/
/*global $: false, jQuery: false, accessoriesDisplay: true , ProgressIndicator: true */
"use strict";


// INIT:
var accessoriesDisplay;
jQuery(document).ready(function ($) {
	accessoriesDisplay = (function ($) {
		/* PRIVATE: */
		var aDshowItemLock = false, imgProgressIndicator, myProgressIndicator, changeImg, changeImgLock = false, changeImgByNo, changeImgByNoLock = false;

		imgProgressIndicator = new ProgressIndicator($("#pictureFrame"));
		myProgressIndicator = new ProgressIndicator($("#articleLayoutDetails"));

		changeImg = function (target, source) {
			if (!changeImgLock) {
				changeImgLock = true;

				$("div.imagesInner > img").fadeTo(500, 0,
					//target.fadeTo(500, 0,
					function () {
						target.attr('alt', $(source).attr('alt'));
						target.attr('src', $(source).attr('src'));

						$("div.imagesInner > img").css("opacity", "0");

						$("#articleLayoutDetails").fadeTo(250, 1, function () {
							myProgressIndicator.stop();
							imgProgressIndicator.stop();
						});
						$("div.imagesInner>img").fadeTo(250, 1);
					}
				);
			}
			changeImgLock = false;
		};

		/* PUBLIC: */
		changeImgByNo = function (accessoryId, imgNo) {
			if (!changeImgByNoLock) {
				changeImgByNoLock = true;

				if (!myProgressIndicator.getPiStaus()) {
					imgProgressIndicator.start();
				}
				/* imgProgressIndicator.start(); */
				if ((imgNo < $("li#" + accessoryId + " > img").length) && (imgNo > - 1)) {
					changeImg($("div#pictureFrame > img"), $("li#" + accessoryId + " > img").clone()[imgNo]);

					$("div#pictureEnumerator > span").removeClass("enumActive");
					$("div#pictureEnumerator > span").eq(imgNo).addClass("enumActive");
				}
				else {
					// no picture
					changeImg($("div#pictureFrame > img"), $('<img src="" alt="Sorry, no picture available." />'));
				}
			}
			changeImgByNoLock = false;
		};

		imgProgressIndicator.stop();

		return {
			changeImgByNo: changeImgByNo,
			aDshowItem: function (accessoryId, caler) {
				if (!aDshowItemLock && !changeImgByNoLock) {
					aDshowItemLock = true;
					myProgressIndicator.start();
					//imgProgressIndicator.start();

					//Fade out:
					$("#articleLayoutDetails").fadeTo(400, 0.01, function () {
						var SpEnumerator = "";
						$("h3#headerAccessoryDescriptionTitle").html(
							$("#" + accessoryId + " > .aDheadline").html()
						);
						$("div#accessoryDescriptionText").html(
							$("#" + accessoryId + " > .aDdescription").html()
						);
						$("span#accessoryItemPrice").html(
							$("#" + accessoryId + " > .aDprice").html()
						);
						$("span#accessoryItemPartnumber").html(
							$("#" + accessoryId + " > .aDordernummer").html()
						);
						//clear enumerationstring
						$("div#articleLayoutDetailsColumn1>#pictureEnumerator").html("");

						//build new enumerationstring
						if (1 < $("li#" + accessoryId + " > img").length) {
							$("li#" + accessoryId + " > img").each(function (i, domEle) {
								SpEnumerator += '<span onclick=';
								SpEnumerator += '"changeImgByNo(\'' + accessoryId + '\',' + i + ')"';
								SpEnumerator += '>' + (1 + i) + '</span>';
							});
							$("div#articleLayoutDetailsColumn1>div#pictureEnumerator").append(SpEnumerator);
						}
						changeImgByNo(accessoryId, 0);
					}
					);
					$(".accessoryActive").removeClass("accessoryActive");
					if (typeof(caler) === 'object') {
						if (caler) {
							$(caler).addClass("accessoryActive");
						}
					}
					aDshowItemLock = false;
				}
			}
		};
	}($));


	$("div.linklist li").eq(0).click(); //show initial item
});

// PUBLIC onclick-handler:
function aDshowItem(accessoryId, caler) {
	if (!accessoriesDisplay) {
		//get a cup of coffee until accessoriesDisplay is initialiced:
		setTimeout(function () {
			aDshowItem(accessoryId, caler);
		}, 500);
	}
	else {
		return accessoriesDisplay && accessoriesDisplay.aDshowItem(accessoryId, caler);
	}
}

function changeImgByNo(accessoryId, imgNo) {
	return accessoriesDisplay && accessoriesDisplay.changeImgByNo(accessoryId, imgNo);
}
/* END   $Id: accessories.js 6700 2009-10-21 18:26:57Z bhillebrand $ END   */
