/**
 *	Include another .js file.
 *		@param filename the name of the file to be included
 *
 */
function include(filename) {
	document.write('<script type="text/javascript" language="javascript" src="' + filename + '"><\/script>');
}

/**
 * Get all elements that have the given class name.
 *	@param className the class name to look for
 *	@return an array containing all the elements that belong to the given class
 *
 */
function getElementsByClass(className) {
	var all = document.all ? document.all : document.getElementsByTagName('*');
	var elements = new Array();

	for(var e = 0; e < all.length; e ++) {
		if(className.constructor.toString().indexOf("Array") != -1) {
			// we have an array as a parameter
			for(var j = 0; j < className.length; j ++) {
				if(all[e].className == className[j]) {
					elements[elements.length] = all[e];
				}
			}
		}
		else {
			if(all[e].className == className) {
				elements[elements.length] = all[e];
			}
		}
	}

	return elements;
}

/**
 * Set the value of a cookie.
 *	@param name - name of the cookie
 *  @param value - value of the cookie
 *  @param [expires] - expiration date of the cookie (defaults to end of current session)
 *  @param [path] - path for which the cookie is valid (defaults to path of calling document)
 *  @param [domain] - domain for which the cookie is valid (defaults to domain of calling document)
 *  @param [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
 */
 function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

/**
 * Obtain the value of a cookie
 *  @param name - name of the desired cookie
 * 	@return a string containing value of specified cookie or null if cookie does not exist
 */
 function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	
	if(begin == -1) {
    	begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	} 
	else {
	    begin += 2;
	}
	
	var end = document.cookie.indexOf(";", begin);
	
	if (end == -1) {
	    end = dc.length;
	}
	
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Delete a cookie.
 * 	@param name - name of the cookie
 *	@param [path] - path of the cookie (must be same as path used to create cookie)
 *	@param [domain] - domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
	if(getCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/*
 * Fix Eolas patent issue for commercial browsers (IE, Opera, maybe others). At the
 * time being, both IE and Opera seem to require the user to active any flash control (
 * actually any embedded component) before anything can be executed inside it. This can
 * be very annoying, especially since most users don't seem to be aware of the issue.
 * 
 * No browser testing is done - other browsers affected by this patent might implement
 * similar workarounds (it doesn't seem to do any harm to Gecko-based browsers).
 * 
 */
function activateObjects() {
	var header = document.getElementById('header');
	var content = header.innerHTML;

	header.innerHTML = '<object type="application/x-shockwave-flash" data="views/swf/loader.swf?path=header-7.swf" width="960" height="80"> \
					   <param name="movie" value="views/swf/loader.swf?path=header-7.swf" /> \
                       <param name="wmode" value="Transparent" /> \
					   <img src="views/img/header-noflash.png" alt="Prakt Romania" title="Prakt Romania" /> \
					</object>';
}

/**
 * Inalization function, called when the page loads.
 * 	@param what Optional parameter, will decide what features will be available on
 * 		the page. Possible values are: 'all' (default), 'cart' or 'links'.
 */
function init(what) {
	if(what == undefined) what = "all";
	
	activateObjects();
	
	/*
	 * Build a 'shopping' cart.
	 */
	if(window.Cart && (what == "all" || what == "cart")) {
		window.cart = new Cart('feedback_cart');
		window.cart.attachCart();
	}
	
	/*
	 * Transform the links on the page.
	 */
	if(window.Links && (what == "all" || what == "links")) {
		var links = new Links();
		links.fix_links();
		//links.productNewWindow(["product_item", "product_item_selected", "product_item_oferta"]);
		//links.productNewWindow("promo");
	}
}

include("views/js/prototype/prototype.js");
include("views/js/cart.js");
include("views/js/links.js");
include("views/js/slide.js");