/**
 * Common variables and functions
 */

var cornyfy = function(element) {
	if (Browser.Engine.trident) {
		var settings = {
			tl : {
				radius : 3
			},
			tr : {
				radius : 3
			},
			bl : {
				radius : 3
			},
			br : {
				radius : 3
			},
			antiAlias : true
		}
		curvyCorners(settings, element);
	}
}
/**
 * Ajax request for page 'In the World' call:
 * showDiv('/corporate/it/home/chisiamo/in-the-world/europe/italia');
 */
function showDiv(pathDiv) {

	var url = 'http://' + window.location.host + contextPath + pathDiv + '.div';

	new Request.HTML( {
		url : url,
		method : 'get',
		update : 'showCountry',
		evalScripts : false,
		onComplete : function() {
			_typeface_js.renderDocument();

			$$('#linkLang a').each(function(el) {
				el.addEvent('click', function(e) {
					e.stop();
					showDiv(el.getProperty('rel'));
				});
			});

			$$('#left a').each(
					function(el) {
						el.addEvent('click', function(e) {
							e.stop();
							$$('#description #left a.active').toggleClass(
									'active');
							$$('#description #right .cntAddress').setStyle(
									'display', 'none');
							document.id(el.get('id') + 'cnt').setStyle(
									'display', 'block');
							el.toggleClass('active');
						});
					});

			$$('.cntAddress').setStyle(
					'min-height',
					Math.max(document.id('left').getSize().y, document.id(
							'right').getSize().y));

			cornyfy(document.id('box_country'));
		}
	}).send();

	return false;

}

var functionOnUpdateBrand = function() {
	$$('.brandDetail').each(
			function(el) {
				el.addEvent('click', function() {
					getHtmlFromSubTemplate(el.get('rel'), 'detail', 'detail',
							function() {
								var brandImage = $$('.brandImage')[0];
								brandImage.reflect( {
									height : 0.2,
									opacity : 0.5
								});
								cornyfy(brandImage.getParent().getParent());
							});
				});
			});

}

/**
 * Generic function to get subtemplate from handle call:
 * getHtmlFromSubTemplate('/path/to/call', 'xml' ,'html_ID_ToUpdate',
 * alert('Function On Complete'));
 */
var getHtmlFromSubTemplate = function(pathSubTemplate, extensionToCall,
		elemToUpdate, functionOnComplete) {

	var url = 'http://' + window.location.host + contextPath + pathSubTemplate
			+ '.' + extensionToCall;

	new Request.HTML( {
		url : url,
		method : 'get',
		update : elemToUpdate,
		evalScripts : false,
		onComplete : function() {
			_typeface_js.renderDocument();
			try {
				functionOnComplete();
			} catch (e) {
				console.log('error on functionOnComplete');
			}
		}
	}).send();

}

var showLayerPopUp = function(handleToDisplay) {
	SexyLightbox.show('', handleToDisplay, "sexylightbox['mediaGallery']");
}

/*
 * Function: $get This function provides access to the "get" variable scope +
 * the element anchor
 * 
 * Version: 1.3
 * 
 * Arguments: key - string; optional; the parameter key to search for in the
 * url's query string (can also be "#" for the element anchor) url - url;
 * optional; the url to check for "key" in, location.href is default
 * 
 * Example: >$get("foo","http://example.com/?foo=bar"); //returns "bar"
 * >$get("foo"); //returns the value of the "foo" variable if it's present in
 * the current url(location.href) >$get("#","http://example.com/#moo");
 * //returns "moo" >$get("#"); //returns the element anchor if any, but from the
 * current url (location.href) >$get(,"http://example.com/?foo=bar&bar=foo");
 * //returns {foo:'bar',bar:'foo'}
 * >$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns
 * {foo:'bar',bar:'foo',hash:'moo'} >$get(); //returns same as above, but from
 * the current url (location.href) >$get("?"); //returns the query string
 * (without ? and element anchor) from the current url (location.href)
 * 
 * Returns: Returns the value of the variable form the provided key, or an
 * object with the current GET variables plus the element anchor (if any)
 * Returns "" if the variable is not present in the given query string
 * 
 * Credits: Regex from
 * [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
 * Function by Jens Anders Bakke, webfreak.no
 */
function $get(key, url) {
	if (arguments.length < 2)
		url = location.href;
	if (arguments.length > 0 && key != "") {
		if (key == "#") {
			var regex = new RegExp("[#]([^$]*)");
		} else if (key == "?") {
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]" + key + "=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null) ? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
		if (url.length > 1) {
			url = url[1].split("#");
			if (url.length > 1)
				results["hash"] = url[1];
			url[0].split("&").each(function(item, index) {
				item = item.split("=");
				results[item[0]] = item[1];
			});
		}
		return results;
	}
}