var dojoConfig = {
	parseOnLoad: true,
	isDebug: false,
	dojoBlankHtmlUrl: '/common/dojo/blank.html',
	baseUrl: '/common/dojo/',
	modulePaths: {sw3: 'sw3'}
};

var _gaq = _gaq || [];

if (typeof sw3 == "undefined") {
	var sw3 = {};

	var el = document.getElementsByTagName('html')[0];
	var s = el.getAttribute('xml:lang');
	if (s) {
		sw3.language = s;
	} else {
		s = el.getAttribute('lang');
		if (s) sw3.language = s;
	}
}

sw3.cal = {};

sw3.addEvent = function(o, n, f) {
	var a='addEventListener', h='on'+n;
	if (o[a]) {
		return o[a](n, f, false);
	}
	if (o[h]) {
		o._c |= 0;
		var b = '_b' + (++o._c);
		o[b] = o[h];
	}
	o[h] = function(e) {
		e = e||self.event;
		var r = true;
		if (o[b]) {
			r = o[b](e) !== false && r;
		}
		o._f = f;
		r = o._f(e) !== false && r;
		return r;
	}
};

sw3.fldfocus = function(fld) {
	if (typeof fld == "string") {
		fld = document.getElementById(fld);
	}
	if (fld) {
		try {
			fld.focus();
			fld.select();
		} catch(e) {
		}
	}
}

sw3.htmlEncode = function(s) {
	var str = new String(s);
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	str = str.replace(/"/g, '&quot;');
	return str;
}

sw3.convertDiacritics = function(s) {
	var s;
	
	var diacritics =[
		/[\300-\306]/g, /[\340-\346]/g, // A, a
		/[\310-\313]/g, /[\350-\353]/g, // E, e
		/[\314-\317]/g, /[\354-\357]/g, // I, i
		/[\322-\330]/g, /[\362-\370]/g, // O, o
		/[\331-\334]/g, /[\371-\374]/g,  // U, u
		/[\321]/g, /[\361]/g, // N, n
		/[\307]/g, /[\347]/g, // C, c
	];
	
	var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];
	
	for (var i = 0; i < diacritics.length; i++) {
		s = s.replace(diacritics[i],chars[i]);
	}
	
	return(s);
}

sw3.GetUrlParam = function (paramName) {
	var oRegex = new RegExp('[\?&]' + paramName + '=([^&]+)', 'i');
	var oMatch = oRegex.exec(window.top.location.search);

	if (oMatch && oMatch.length > 1)
		return decodeURIComponent(oMatch[1]);
	else
		return '';
}

sw3.GetJSParams = function () {
	function parseQuery (query) {
		var Params = new Object ();
		if ( ! query ) return Params; // return empty object
		var Pairs = query.split(/[;&]/);
		for ( var i = 0; i < Pairs.length; i++ ) {
			var KeyVal = Pairs[i].split('=');
			if ( ! KeyVal || KeyVal.length != 2 ) continue;
			var key = unescape( KeyVal[0] );
			var val = unescape( KeyVal[1] );
			val = val.replace(/\+/g, ' ');
			Params[key] = val;
		}
		return Params;
	}
	
	var scripts = document.getElementsByTagName('script');
	var myScript = scripts[ scripts.length - 1 ];
	
	var queryString = myScript.src.replace(/^[^\?]+\??/,'');
	
	return parseQuery( queryString );
	
}

sw3.GetSLD = function () {
	var d = window.location.hostname;
	var a = d.split('.');
	var n = a.length;
	if (n > 2) {
		a.splice(0, n-2);
		d = a.join('.');
	}
	return d;
}

sw3.getCookieVal = function(offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

sw3.getCookie = function(name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) {
			return sw3.getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i === 0) {
			break;
		}
	}  
	return null;
}

sw3.setCookie = function(name, value) {
	var argv = arguments;
	var argc = argv.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires === null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path === null) ? "" : ("; path=" + path)) +
	((domain === null) ? "" : ("; domain=" + domain)) +
	((secure === true) ? "; secure" : "");
}

sw3.deleteCookie = function(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = sw3.getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

sw3.GetSub = function(lang, menuID, rubID, temp_on, temp_off, temp_sep) {
	var res = '';
	var c = sw3.struct[menuID].childs;
	if (c.length > 0) {
		var reg_name = /_name_/gi;
		var reg_uname = /_uname_/gi;
		var reg_link = /_link_/gi;
		var reg_seq = /_seq_/gi;
		var reg_sub = /_sub_/gi;
		var reg_id = /_id_/gi;
		var reg_selected = /_selected_/gi;
		var i = temp_on.indexOf('_[_');
		var j = temp_on.indexOf('_]_');
		var t_before = temp_on.substring(0, i);
		var t_after = temp_on.substring(j+3, temp_on.length);
		var t_body_on = temp_on.substring(i+3, j);
		var t_body_off = t_body_on;
		if (temp_off) {
			t_body_off = temp_off;
		}
		
		var parent_chain = [];
		var o = sw3.struct[rubID];
		while (o) {
			parent_chain.push(o.id);
			o = sw3.struct[o.parent];
		}
		
		res = t_before;
		for (i=0; i<c.length; i++) {
			var id = c[i];
			o = sw3.struct[id];
			var s = (o.id == rubID ? t_body_on : t_body_off);
			s = s.replace(reg_name, o.name[lang]);
			s = s.replace(reg_uname, encodeURIComponent(o.name[lang]));
			s = s.replace(reg_link, o.url[lang]);
			s = s.replace(reg_seq, i + 1);
			s = s.replace(reg_id, o.id);
			if (o.id == rubID) {
				s = s.replace(reg_selected, 'selected');
			} else {
				s = s.replace(reg_selected, '');
			}
			if (parent_chain.indexOf(o.id) > -1 && o.childs.length > 0) {
				s = s.replace(reg_sub, sw3.GetSub(lang, id, rubID, temp_on, temp_off));
			} else {
				s = s.replace(reg_sub, '');
			}
			if (i>0 && temp_sep) {
				res += temp_sep;
			}
			res += s;
		}
		res += t_after;
	}
	return res;
}

sw3.is_mobile = function() {
	return navigator.userAgent.match(/(Android|webOS|iPhone|iPod|iPad|BlackBerry)/i) ? true : false;
}

/***************************************************************************/

if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(val, fromIndex) {
		if (typeof(fromIndex) != 'number') {
			fromIndex = 0;
		}
		for (var index = fromIndex,len = this.length; index < len; index++) {
			if (this[index] == val) {
				return index;
			}
		}
		return -1;
	}
}

if (!String.prototype.trim) {
	String.prototype.trim = function() {
		return this.replace(/^\s+/g,'').replace(/\s+$/g,'') ;
	}
}
/*
 * cssjs
 * written by Christian Heilmann (http://icant.co.uk)
 * eases the dynamic application of CSS classes via DOM
 * parameters: action a, object o and class names c1 and c2 (c2 optional)
 * actions: swap exchanges c1 and c2 in object o
 *			add adds class c1 to object o
 *			remove removes class c1 from object o
 *			check tests if class c1 is applied to object o
 * example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
 */

function cssjs(a,o,c1,c2) {
	switch (a) {
	case 'swap':
		o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
	case 'add':
		if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
	case 'remove':
		var rep=o.className.match(' '+c1)?' '+c1:c1;
		o.className=o.className.replace(rep,'');
		break;
	case 'check':
		return new RegExp('\\b'+c1+'\\b').test(o.className);
	}
}

/* Unobtrusive Flash Objects (UFO) v3.20 <http://www.bobbyvandersluis.com/ufo/>
        Copyright 2005, 2006 Bobby van der Sluis
        This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/
function createCSS(selector, declaration) {
	// test for IE
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));
	
	// create the style node for all browsers
	var style_node = document.createElement("style");
	style_node.setAttribute("type", "text/css");
	style_node.setAttribute("media", "screen");
	
	// append a rule for good browsers
	if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));
	
	// append the style node
	document.getElementsByTagName("head")[0].appendChild(style_node);
	
	// use alternative methods for IE
	if (isIE && document.styleSheets && document.styleSheets.length > 0) {
		var last_style_node = document.styleSheets[document.styleSheets.length - 1];
		if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
	}
};

/*	Named Arguments Version 1.0.1
	Copyright 2004 Mark Wubben

	Adds named arguments support to JavaScript. See also <http://novemberborn.net/javascript/named-arguments>
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

function named(oArgs){ 
	return new named.Arguments(oArgs);
};

named.Arguments = function(oArgs){
	this.oArgs = oArgs;
};

named.Arguments.prototype.constructor = named.Arguments;

named.extract = function(listPassedArgs, oMapping){
	var oNamedArgs, passedArg;
	
	for(var i = 0; i < listPassedArgs.length; i++){
		passedArg = listPassedArgs[i];
		if(passedArg != null && passedArg.constructor != null && passedArg.constructor == named.Arguments){
			oNamedArgs = listPassedArgs[i].oArgs; /* oNamedArgs isn't the named.Arguments class! */
			break;
		};
	};
	
	if(oNamedArgs == null){ return };
	
	for(var sName in oNamedArgs){
		if(sName in oMapping){
			oMapping[sName](oNamedArgs[sName]);
		};
	};
	
	return;
};

/***************************************************************************/

sw3.setEnvCSS = function() {
	b = document.getElementsByTagName('body')[0];
	cssjs('add', b, 'agent_' + (sw3.is_mobile() ? 'mobile' : 'desktop'));
}

/***************************************************************************
	init event
***************************************************************************/
sw3.addEvent(window, 'load', function() {
	function set_events(el) {
		function add_focus(node) {
			cssjs('add', node, 'sw3-focus');
			cssjs('add', node.parentNode, 'sw3-parent-focus');
		}
		for (var i = 0; i < el.length; i++) {
			if (new RegExp('\\bsw3-[^\b]*focus\\b').test(el[i].className)) {
				cssjs('remove', el[i], 'sw3-first-focus');
				cssjs('remove', el[i], 'sw3-autofocus');
				if (!('autofocus' in el[i])) sw3.fldfocus(el[i]);
			}
			if (el[i] == document.activeElement) {
				add_focus(el[i]);
			}
			sw3.addEvent(el[i], 'focus', function(e) {
				add_focus(this); // e.target
			});
			sw3.addEvent(el[i], 'blur', function(e) {
				cssjs('remove', this, 'sw3-focus');
				cssjs('remove', this.parentNode, 'sw3-parent-focus');
			});
			sw3.addEvent(el[i], 'mouseover', function(e) {
				cssjs('add', this, 'sw3-over');
				cssjs('add', this.parentNode, 'sw3-parent-over');
			});
			sw3.addEvent(el[i], 'mouseout', function(e) {
				cssjs('remove', this, 'sw3-over');
				cssjs('remove', this.parentNode, 'sw3-parent-over');
			});
		}
	}
	
//	sw3.setEnvCSS();
	set_events(document.getElementsByTagName('input'));
	set_events(document.getElementsByTagName('textarea'));
	set_events(document.getElementsByTagName('select'));
	set_events(document.getElementsByTagName('button'));
});

