/*
 * hyCMS
 * Copyright(C)2008 by Friedrich Gräter
 * Published under the terms of the Lesser GNU General Public License v2
 *
 * WidgetKit backend: Image preloading
 *
 * Please Note: This module has to be clean from all dependencies to predicateJS!
 *
 *				Also this module requires "widget-kit.css" to be
 *				embedded to the parent site for styling the widgets.
 *
 */
var wk_preloadedImages = [];

var wkResources = "storage/resources/widgetKit";

/*
 * wk_preloadImage(basepath, image)
 * wk_preloadImage(basepath, images)
 *
 * Preloads the given image URL or list of urls. The images
 * are relative to "basepath".
 *
 */
function wk_preloadImage(basepath, image)
{
	var images;
	
	if (!(image instanceof Array))
		images = [image];
	else
		images = image;

	for (var idx = 0; idx < images.length; idx++) {
		var imgObject = new Image();
		imgObject.src = basepath + "/" + images[idx];

		wk_preloadedImages.push(imgObject);
	}
}

/*
 * wk_preloadButton(stock)
 *
 */
function wk_preloadButton(stock, path)
{
	if (path == null) path = wkResources;

	wk_preloadImage(path+"/buttons", stock + ".png");
	wk_preloadImage(path+"/buttons", stock + "-clicked.png");
	wk_preloadImage(path+"/buttons", stock + "-hovered.png");	
	wk_preloadImage(path+"/buttons", stock + "-disabled.png");
}

/*
 * wk_preloadFrame()
 *
 */
function wk_preloadFrame()
{
	wk_preloadImage(wkResources+"/dialog", "background.png");
	wk_preloadImage(wkResources+"/dialog", "top.png");
	wk_preloadImage(wkResources+"/dialog", "bottom.png");
	wk_preloadImage(wkResources+"/dialog", "left.png");
	wk_preloadImage(wkResources+"/dialog", "right.png");

	wk_preloadImage(wkResources+"/dialog", "topLeft.png");
	wk_preloadImage(wkResources+"/dialog", "topRight.png");
	wk_preloadImage(wkResources+"/dialog", "bottomLeft.png");
	wk_preloadImage(wkResources+"/dialog", "bottomRight.png");
}

/*
 * Test, if all preloads have been finished
 *
 */
function wk_preloadsFinished()
{
	var all = true;

	for (var idx = 0; idx < wk_preloadedImages.length; idx ++) {
		if (wk_preloadedImages[idx].complete == false)
			all = false;
	}

	return all;
}

/*
 * Normal preload
 *
 */
wk_preloadButton("ok");
wk_preloadButton("left");
wk_preloadButton("right");

wk_preloadFrame();

/*
 * Extended preload
 *
 */
if (document.wk_extendedPreload == true)
{
	var wk_extendedPreloadButtons = ["cancel", "delete", "down", "inspect", "left", "minus", "more", "new", "ok", "plus", "right", "save", "settings", "up"];

	for(var idx = 0; idx < wk_extendedPreloadButtons.length; idx ++) {
		wk_preloadButton(wk_extendedPreloadButtons[idx]);
	}
}

/*
 * Gallery
 *
 */
var wk_galleryButtons = ["left", "right"];

for(var idx = 0; idx < wk_galleryButtons.length; idx ++) {
	wk_preloadButton(wk_galleryButtons[idx], "storage/resources/views/gallery");
}	

/*
 * Feed
 *
 *
var wk_feedButtons = ["left", "right"];

for(var idx = 0; idx < wk_galleryButtons.length; idx ++) {
	wk_preloadButton(wk_galleryButtons[idx], "storage/resources/views/feed");
}*/	


