﻿var hasConsole = typeof console != "undefined" ? true : false;
var WARN_PREFIX = "WARN: ";
var TRACE_PREFIX = "TRACE: ";

var w = $(window), d = $(document);

function the_timestamp()
{
	var date = new Date();
	// TODO: There's probably a build-in JS method
	return date.getFullYear() + "/" + date.getMonth() + "/" + date.getDay() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
}

/* PERFORMANCE & TRACE */
function performance()
{
	function warn(str)
	{
		if (hasConsole) console.log(the_timestamp() + ": " + WARN_PREFIX + "" + str);
	}

	function write(str)
	{
		if (hasConsole) console.log(the_timestamp() + ": " + str);
	}

	this.write = write;
	this.warn = warn;
}

var perf = new performance();
var dummy = null;

$(function ()
{
	// Frontpage banner cycle
	var cycleOpts = {
		fx: "fade",
		timeout: 8000,
		speed: 700,
		prev: "div#banner div.prev",
		next: "div#banner div.next"
	};

	$("div.slides").cycle(cycleOpts);

	// 
	$("input[title!='']").hint();

	// To be replaced
	$("ul.menulist li").each(function (i)
	{
		var a = $("a", this);

		if (i > 0)
		{

			if (window.location.pathname.indexOf(a.attr("href")) != -1)
				$(this).addClass("active");
		} else
		{
			if (window.location.pathname == a.attr("href")) $(this).addClass("active");
		};
	});

	// Picture groups
	if ($(".picture-group").length > 0) $(".picture-group").picturegroup();


	// OLD & PREPARE FOR REFACTOR
	var imgRe = new RegExp("\/picture\/(\\d+).*Width=(\\d+).*Height=(\\d+)");
	var t = $("#lightbox-body"), close = $("#lightbox-close"), overlay = $("#lightbox-overlay");
	var groupPictures = $('.picture-group a');
	centerElement(t);

	if ($("div.picture-group").length != 0) $("div.picture-group").picturegroup();

	// Hint forms
	$("input[title!='']").hint("blur");

	var thumbs = $(".as-thumbnail a");

	thumbs.each(function (index)
	{
		//perf.write("Image");

		var img = $("img", this);
		var id = img.attr("src").match(imgRe)[1];
		var href;

		if (typeof (ideakorttiData) != "undefined")
		{
			if (typeof (ideakorttiData[id]) != "undefined")
			{
				$(this).attr({ "href": ideakorttiData[id].FullUrl });
			}
		}

		$(this).click(function () { return false; });
	});

	// Bind events to lightbox centering
	w.scroll(function () { /*centerElement(t);*/ });
	w.resize(function () { centerElement(t); });
	close.click(function () { hideLightbox(dummy); });
	overlay.click(function () { hideLightbox(dummy); });

	//thumbs.hover(function () { perf.write("in"); },
    //function () { perf.write("out"); });

	thumbs.click(function ()
	{
		var img = $("img", this);
		var id = img.attr("src").match(imgRe)[1];

		if (typeof (ideakorttiData[id]) != "undefined")
		{
			pic = ideakorttiData[id];

			if (typeof (pic) == "object")
			{
				data = [{
					"type": "picture",
					"name": pic.Name,
					"description": pic.Description,
					"width": pic.Width,
					"height": pic.Height,
					"fullUrl": pic.FullUrl,
					"thumbUrl": pic.LightboxUrl,
					"extra": pic.UsedProducts
				}];

				showLightbox(data[0]);
			}
		}

		return false;
	});

	// Same for imagegroups
	// TODO: We have to standardize this...
	groupPictures.click(function ()
	{
		var data = {
			"type": "picture",
			"width": 700,
			"height": 700,
			"fullUrl": $(this).attr("href"),
			"isGroup": true
		};
		showLightbox(data);

		return false;
	});

});

function showLightbox(data)
{
	var overlay = $("#lightbox-overlay");
	var body = $("#lightbox-body");
	var pictureHolder = $("#lightbox-picture");
	var pictureLinks = $("#lightbox-picture-links");

	var type = data.type;
	var isGroup = data.isGroup;

	var description = data.description;
	var title = data.name;
	var fullUrl = data.fullUrl;
	var iW = data.width;
	var iH = data.height;
	var extra = data.extra;

	pictureHolder.hide();

	if (type == 'picture')
	{
		var attrs = { "src": data.thumbUrl, "alt": title, "width": (iW > 0 ? iW : 700), "height": (iH > 0 ? iH : 700) };

		var image = new Image();
		var thumbUrl = data.thumbUrl;
		perf.warn("thumbUrl = " + thumbUrl);

		image.src = data["thumbUrl"];
		image.onload = function (e)
		{
			perf.warn("image.onload()");
			var i = this;
			var img = $("#lb-image");
			img.attr(attrs);
			$("#lb-description").width(iW);
			centerElement(body);
		}

		if (typeof (extra) != "undefined")
		{
			if (extra.length > 0)
			{
				// #usedproducts-links
				for (var num in extra)
				{
					$("<a></a>")
					.attr({ href: extra[num].Url, title: extra[num].Name, "class": "external-link", style: "display: block;" })
					.html("<i></i>" + extra[num].Name)
					.appendTo($("#usedproducts-links"));
				}
			}
		}

		if (title)
			$("#lb-description").show().html(title + ": " + description);

		image = null;
		pictureHolder.show();
	}

	centerElement(body);
	overlay.width(d.width()).height(d.height()).fadeIn("fast");
	body.fadeIn("fast");
}

function hideLightbox()
{
	var lb = $("#lightbox-overlay, #lightbox-body");

	data = null;
	var img = $("#lb-image");

	lb.hide();
	

	img.attr("src", "/includes/images/ajax-loader.gif");
	$("#usedproducts-links a").remove();
}

function centerElement(element)
{
	var elem = $(element);
	var posY = w.height() / 2 + d.scrollTop() - elem.height() / 2;
	var posX = w.width() / 2 + d.scrollLeft() - elem.width() / 2;

	elem.css("top", posY);
	elem.css("left", posX);
}



