/**
 * @author Szymon
 */
var codexd = {
	
mouseX:0,
mouseY:0,
		
	$cc:function(){
        if (arguments[0] != null) {
            if (this.matchObType(arguments[0],"string")) {
				if (document.getElementById(arguments[0]) != null) {
					return document.getElementById(arguments[0]);
				}else{
					return document.getElementsByTagName(arguments[0]);				
				}
            }else {
                return arguments[0];
            }
        }else {
			return false;
        }
		var cc = this;
		setOpacity = function(){
			BrowserDetect.init();						
			BrowserDetect.version;
			switch(BrowserDetect.browser){
				case('FireFox'):break;
				default:break;
			}
		}
    },
	
	defined:function(){
		return (typeof(this.getArg(arguments[0]))!='undefined');	
	},
	
	empty:function(){
		return (this.getArg(arguments[0])=='' );
	},
	
	isArray:function() {
		return arguments[0].constructor == Array;
	},
	
	getArg:function(){
		if(arguments[0]!=null){
			return arguments[0];
		}else{
			return;
		}
	},
	
	addEvent:function() {
		var obj = this.getArg(arguments[0]);
		var evt = this.getArg(arguments[1]); 
		var fn = this.getArg(arguments[2]);
		if (obj.addEventListener)
			obj.addEventListener(evt,fn,false);
		else if (obj.attachEvent)
			obj.attachEvent('on'+evt,fn);
	},
	
	rmEvent:function () {
		var obj = this.getArg(arguments[0]);
		var evt = this.getArg(arguments[1]); 
		var fn = this.getArg(arguments[2]);	
		if (obj.removeEventListener)
			obj.removeEventListener(evt,fn,false);
		else if (obj.detachEvent)
			obj.detachEvent('on'+evt,fn);
	},
		
	hashLenght:function(){
		var count = 0;
		var hash = this.getArg(arguments[0]);
		for(var i in hash){
			count++;
		}
		return count;
	},		
	
	getElePos:function(){
		var el = (this.getArg(arguments[0])!=null)?this.getArg(arguments[0]):'';
		var ob = this.$cc(el);
		return {x:(parseInt(ob.style.left)!=null || parseInt(ob.style.left)!='')?parseInt(ob.style.left):parseInt(ob.offsetLeft), y:(parseInt(ob.style.top)!=null || parseInt(ob.style.top)!='')?parseInt(ob.style.top):parseInt(ob.offsetTop)}
	},
	
	displayElement:function(){
		var el = this.$cc((this.getArg(arguments[0])!=null)?this.getArg(arguments[0]):'');
		var display = (this.getArg(arguments[1])!=null)?this.getArg(arguments[1]):'none';
		el.style.display = display;
	},
	
	/*hideElement:function(){
		var el = this.$cc((this.getArg(arguments[0])!=null)?this.getArg(arguments[0]):'');
		el.style.display = 'none';
	},*/
	
	getVal:function(){
		var el = (this.getArg(arguments[0])!=null)?this.getArg(arguments[0]):'';
		switch(this.$cc(el).type){
			case('TEXT'):
			case('SELECT'):
			case('CHECKBOX'):
			case('RADIO'):
			case('PASSWORD'):return this.$cc(el).value;
			default:return;
		}				
	},
	
	getIH:function(){
		var el = mainCore.$cc((this.getArg(arguments[0])!=null)?this.getArg(arguments[0]):'');
		return el.innerHTML;
	},
	
	ie:function(){
		return (window.navigator.userAgent.toLowerCase().indexOf('msie') != -1)?true:false;
	},
	
	matchObType:function(){
		return (typeof(this.getArg(arguments[0])) == this.getArg(arguments[1]))?true:false;
	},
	
	delay:function(){
		var timeOut = parseInt(this.getArg(arguments[0]))*1000;
		var start = new Date().getTime();
		var cur = start;
		while(cur - start < timeOut){cur = new Date().getTime();}
	},
	
	real_css:function(){
		var s = this.getArg(arguments[0]);
			for(var exp=/-([a-z])/; 
				exp.test(s); 
				s=s.replace(exp,RegExp.$1.toUpperCase()));
		return s;
	},
			
	setStyle_ie:function() {
	  	var element = this.getArg(arguments[0]);
		var declaration = this.getArg(arguments[1]);
		if (declaration.charAt(declaration.length-1)==';')declaration = declaration.slice(0, -1);
		var key, value;
		var splitted = declaration.split(';');
			for (var i=0, len=splitted.length; i<len; i++) {
			key = this.real_css(splitted[i].split(':')[0]);
			value = splitted[i].split(':')[1];
			eval("element.style."+key+"='"+value+"'");
		}
		
	},
	getStyle:function(){
		var strValue = "";
		var oElm = this.$cc(arguments[0]);
		var strCssRule = arguments[1];
		if(document.defaultView && document.defaultView.getComputedStyle){
			strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
		}
		
		if(oElm.currentStyle){
			strValue = oElm.currentStyle[strCssRule.replace(/\-(\w)/g, function (strMatch, p1){return p1.toUpperCase();})];
		}
		return strValue;
		
	},
	
	getStyle:function(){
		var elt = arguments[0];
		return (typeof elt.currentStyle != 'undefined')?elt.currentStyle:document.defaultView.getComputedStyle(elt, null);
	},
	
	cleanupUrl:function(){
	   var aUrl = arguments[0];
	   var regExp = /^url\("?(.*?)"?\)$/ig;
	   var matches = regExp.exec(aUrl);
	   return (matches == null || typeof matches[1] == "undefined")?null:matches[1];
	},	
	
	newZindex:function(){
		var highestIndex = 0;
		var currentIndex = 0;
		var elArray = document.getElementsByTagName('*');
		for(var i=0; i < elArray.length; i++){
		  if (elArray[i].currentStyle){
		     currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
		  }else if(window.getComputedStyle){
		     currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i],null).getPropertyValue('z-index'));
		  }
		  if(!isNaN(currentIndex) && currentIndex > highestIndex){ highestIndex = currentIndex; }
		}
		return (highestIndex+1);
	},

	getMouseXY:function(e){
		
	  if (codexd.ie()) { 
	    codexd.mouseX = event.clientX + document.body.scrollLeft;
	    codexd.mouseY = event.clientY + document.body.scrollTop;
	  }else { 
	    document.captureEvents(Event.MOUSEMOVE);
	    codexd.mouseX = e.pageX;
	    codexd.mouseY = e.pageY;
	  }  
	  if (codexd.mouseX < 0){codexd.mouseX = 0}
	  if (codexd.mouseY < 0){codexd.mouseY = 0}
	},
	
	getBrowserSize:function(){
		var myWidth = 0, myHeight = 0;
		if (typeof(window.innerWidth) == 'number') {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}
		else 
			if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {

				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			}
			else 
				if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
					//IE 4 compatible
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
				}
		return {w:myWidth, h:myHeight};
	},
	
	getBrowserScrollXY:function() {
		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    scrOfY = window.pageYOffset;
		    scrOfX = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
		    scrOfX = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    scrOfY = document.documentElement.scrollTop;
		    scrOfX = document.documentElement.scrollLeft;
		  }
		  return {x:scrOfX, y:scrOfY};
	},
	
	showProgress:function(){
		var progress = this.getArg(arguments[0]);
		this.stepProg(progress);
		this.infoBar.innerHTML = parseInt(progress)+'%';
	},
	
	drawMask:function(){
		if(arguments[0] == 'y'){
			this.mask = document.createElement('div');
			this.mask.style.backgroundColor = (arguments[1])?"arguments[1]":"#000000";
			this.mask.style.width = "100%";
			this.mask.style.height = "100%";
			this.mask.style.position = "absolute"
			this.mask.style.top = "0px";
			this.mask.style.left = "0px";			
				document.body.appendChild(this.mask);
		}else{
				document.body.removeChild(mainCore.mask);
	    }
	},
	
	drawProgress:function(progress){
		this.contBox = document.createElement('div');
			this.infoBar = document.createElement('div');
			    this.progress = document.createElement('div');		
		this.contBox.style.width = "200px";
		//this.contBox.style.backgroundColor = "#FFF898";
		this.contBox.style.color = "#fff";
		this.contBox.style.border = "1px solid #fff";
		this.contBox.style.padding = "5px 0px 5px 0px";
		this.contBox.style.position = "absolute";
		this.contBox.style.zIndex = 1001;
		this.contBox.style.top = "35%";
		this.contBox.style.left = "45%";
		this.contBox.style.overflow = "hidden";
			this.infoBar.style.textAlign = "center";
				this.progress.style.width = "138px";
				this.progress.style.height = "7px";
				this.progress.style.backgroundImage = "url(../images/progress.gif)";
				this.progress.style.backgroundPosition="-140px";
				this.progress.style.margin = "0 auto";
			this.contBox.appendChild(this.infoBar);
			this.contBox.appendChild(this.progress);
		document.body.appendChild(this.contBox);
	},
	
	
	stepProg:function(){					
		if(parseInt(arguments[0])==100){
			this.progress.style.backgroundPosition = "0px";
		}
		if(parseInt(arguments[0])>0 && parseInt(arguments[0])<=10){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}
		if(parseInt(arguments[0])>10 && parseInt(arguments[0])<=20){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}
		if(parseInt(arguments[0])>20 && parseInt(arguments[0])<=30){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}
		if(parseInt(arguments[0])>30 && parseInt(arguments[0])<=40){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}
		if(parseInt(arguments[0])>40 && parseInt(arguments[0])<=50){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}
		if(parseInt(arguments[0])>50 && parseInt(arguments[0])<=60){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}						
		if(parseInt(arguments[0])>60 && parseInt(arguments[0])<=70){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}						
		if(parseInt(arguments[0])>70 && parseInt(arguments[0])<=80){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}						
		if(parseInt(arguments[0])>80 && parseInt(arguments[0])<=99){
			this.progress.style.backgroundPosition = (parseInt(this.progress.style.backgroundPosition)+14)+"px";																						 
		}																				
	},
	
	loadMoules:function(){
		var list = this.getArg(arguments[0]);
		var m = list.modules;
			//var progressOne = parseInt(100/parseInt(this.hashLenght(list)));
			//var lastPart = (100 - progressOne*parseInt(this.hashLenght(list))); 
			//var progress = 0;
			//this.drawProgress();
		var head = document.getElementsByTagName("head");
		var scripts = head[0].getElementsByTagName("script");
		
		var mainNode = null;
			for(var j=0; j<scripts.length;j++){
				if(scripts[j].src.indexOf('codexd.core.js') != -1){
					mainNode = scripts[j];
					break;
				}
			}	
			var tmpScript = null;		
		if (mainNode != null) {
			for (var i=0;i<m.length;i++) {
				//this.showProgress((progress+progressOne));
				//progress+=progressOne;
				var script = document.createElement("script");
				script.language = "JavaScript";				
				script.type = "text/javascript";
				script.src = config.jsPath + m[i] + '.' + config.jsSufix + '.js'; 
				//head[0].appendChild(script);
				head[0].insertBefore(script, ((i==0)?mainNode.nextSibling:tmpScript.nextSibling));
				tmpScript = script;
			}
			return true;
		}else{
			return false;
		}	
			/*if (progress + lastPart == 100) {
				this.showProgress((progress + lastPart));
				return true;
			}*/
	}	
};

document.onmousemove = codexd.getMouseXY;

function replaceString(oldS,newS,fullS) {
	var fullS = (codexd.defined(fullS) && (fullS != null)) ? fullS.toString() : ''
	var i=0;
	while(i<fullS.length){
		if (fullS.substring && fullS.substring(i,i+oldS.length) == oldS){
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
			i += newS.length;
		}else{
			i++;
		}
	}
	return fullS
}

function FormatItem(item){
	if(parseInt(item) == item) return item
	else {
			var i= replaceString('\\','\\\\',item);
			i= replaceString('\/','\\\/',i);
			i= replaceString('"','\\"',i);
			i= replaceString('\'','\\\'',i);
			return "'"+(i?i:'')+"'"
	}
}

function Params2Str(params){
	if (params == null)
		return null;
	var out = '';
	if(typeof(params) == 'object'){
		if(codexd.defined(params.length)){
			for(var i=0; i<params.length; i++) out += (out ? ',' : '') + Params2Str(params[i])
			return '['+out+']'
		} else {
			for(var i in params) out += (out ? ',' : '') + i+':'+Params2Str(params[i])
			return '{'+out+'}'
		}
	} else return FormatItem(params)
}


var wVSize = codexd.getBrowserSize();
var wSPos = codexd.getBrowserScrollXY();
codexd.addEvent(window,'resize',function(){wVSize = codexd.getBrowserSize(); wSPos = codexd.getBrowserScrollXY(); });
//codexd.addEvent(document.body,'scroll',function(){wVSize = codexd.getBrowserSize(); wSPos = codexd.getBrowserScrollXY(); });

function hashJoin(Hash,hash1,hash2){
	var no_change = hash2 && codexd.defined(hash2.NoChange) ? hash2.NoChange :
		(Hash && codexd.defined(Hash.NoChange) ? Hash.NoChange : (hash1 ? hash1.NoChange : 0))
	Hash = Hash || new Object()
	if(no_change){
		var copy_of_hash = new Object()
		for(var i in Hash) copy_of_hash[i] = Hash[i]
	} else var copy_of_hash = Hash

	for(var i in hash1)
		if(!codexd.defined(copy_of_hash[i]) && i != 'NoChange') copy_of_hash[i] = hash1[i]
	for(var i in hash2) copy_of_hash[i] = hash2[i]
	return copy_of_hash
}


function getLinks(){
	var url = location.href;
	var url = url.substr(url.indexOf('?')+1,url.length);
    var links = new Array();
		links = url.split(',');
	return (links[0].indexOf('http') == 0 || links[0].indexOf('http') == 0 )?[]:links;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
//alert(BrowserDetect.browser+" - "+BrowserDetect.version);

(function () {
	var ie  = !!(window.attachEvent && !window.opera);
	var wk  = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
	var fn  = [];
	var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
	var d   = document;
	d.ready = function (f) {
		if (!ie && !wk && d.addEventListener)
			return d.addEventListener('DOMContentLoaded', f, false);
		if (fn.push(f) > 1) return;
		if (ie)
			(function () {
				try { d.documentElement.doScroll('left'); run(); }
				catch (err) { setTimeout(arguments.callee, 0); }
			})();
		else if (wk)
			var t = setInterval(function () {
				if (/^(loaded|complete)$/.test(d.readyState))
					clearInterval(t), run();
			}, 0);
	};
})();
