(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);








LOCAL = {
	debug: function(debugObj, debugHint) {
		debugObj = debugObj||false;
		debugHint = debugHint||'';
		
		if( typeof console != 'undefined' ) {
			if (debugHint.length > 0) {
				console.log('-----------------------------------');
				console.log(debugHint);
				console.log(debugObj);
				console.log('-----------------------------------');
			} else { console.log(debugObj); }
		}
	},
	common: {
		getUrlParam: function(strParamName, currentUri) {
			currentUri = currentUri||false;
			strReturn = '';
			
			if( currentUri ) { procCurrentUri = currentUri; }
			else { procCurrentUri = location.href; }
			
			if ( procCurrentUri.indexOf("?") > -1 ){
				var strQueryString = procCurrentUri.substr(procCurrentUri.indexOf("?")).toLowerCase();
				var aQueryString = strQueryString.split("&");
				for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
					if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ) {
						var aParam = aQueryString[iParam].split("=");
						strReturn = aParam[1];
						break;
					}
				}
			}
			return unescape(strReturn);
		},
		openPopup: function(url, title, width, height) {
			if (screen.availWidth) {
				popupCenterX = screen.availWidth / 2 - width / 2;
				popupCenterY = screen.availHeight / 2 - height / 2;
			}
			else {
				popupCenterX = 0;
				popupCenterY = 0;
			}
			
			popupWindow = window.open( url, title, 'width='+width+',height='+height+',status=no,location=no,left='+popupCenterX+',top='+popupCenterY+',screenX='+popupCenterX+',screenY='+popupCenterY );
			popupWindow.focus();
			
			return false;
		},
		openPopupLink: function(linkElem) {
			popupUrl = jQuery(linkElem).attr('href');
			popupTitle = jQuery(linkElem).attr('rel');
			popupSizeRaw = jQuery(linkElem).attr('rev');
			popupWidth = 400;
			popupHeight = 400;
			
			if( popupSizeRaw ) {
				var popupSizeArr = popupSizeRaw.split(',');
				popupWidth = popupSizeArr[0];
				popupHeight = popupSizeArr[1];
			}
			
			this.openPopup( popupUrl, '', popupWidth, popupHeight );
			
			return false;
		},
		randomXToY: function(minVal,maxVal,floatVal) {
			var randVal = minVal+(Math.random()*(maxVal-minVal));
			return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
		},
		boxLink: function(selector) {
			jQuery(selector).click(function(){
				var currLinkElem = jQuery('a', this);
				var linkHref = currLinkElem.attr('href');
				var linkTarget = currLinkElem.attr('target');
				
				if( linkTarget=='_blank' ) {
					window.open(linkHref);
				}
				else {
					window.location.href = linkHref;
				}
				return false;
			});
		},
		fbShare: function(el) {
			var fbUrl = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(el.href);
			window.open(fbUrl, '_blank', 'width=600,height=320');
			
			return false;
		}
	},	intervalAssist: {
		intervalCount: 0,
		intervalTime: 2000,
		intervalHookObj: [],
		windowHasFocus: true,
		initInterval: function() {
			window.onblur = function () {
				LOCAL.intervalAssist.windowHasFocus = false;
				//LOCAL.debug('focus false');
			}
			window.onfocus = function () {
				LOCAL.intervalAssist.windowHasFocus = true;
				//LOCAL.debug('focus true');
			}
			
			setInterval( "LOCAL.intervalAssist.callInterval()", this.intervalTime );
		},
		callInterval: function() {
			LOCAL.intervalAssist.intervalCount++;
			var currentIntervalCalc = LOCAL.intervalAssist.intervalCount*LOCAL.intervalAssist.intervalTime;
			
			if( LOCAL.intervalAssist.intervalHookObj.length > 0 ) {
				for(var i in LOCAL.intervalAssist.intervalHookObj) {
					if (LOCAL.intervalAssist.intervalHookObj[i].time) {
						if (currentIntervalCalc % LOCAL.intervalAssist.intervalHookObj[i].time == 0) {
							LOCAL.intervalAssist.intervalHookObj[i].respectWindowFocus = LOCAL.intervalAssist.intervalHookObj[i].respectWindowFocus || false;
							
							if (LOCAL.intervalAssist.intervalHookObj[i].respectWindowFocus) {
								if (LOCAL.intervalAssist.windowHasFocus == true) {
									//LOCAL.debug( 'callInterval: '+ LOCAL.intervalAssist.intervalHookObj[i].name +' ['+ LOCAL.intervalAssist.intervalCount +']' );
									LOCAL.intervalAssist.intervalHookObj[i].callFunc();
								}
							}
							else {
								//LOCAL.debug( 'callInterval: '+ LOCAL.intervalAssist.intervalHookObj[i].name +' ['+ LOCAL.intervalAssist.intervalCount +']' );
								LOCAL.intervalAssist.intervalHookObj[i].callFunc();
							}
						}
					}
				}
			}
		}
	},

	layout: {
		init: function() {
			var bodyElem = jQuery('body');
			if( bodyElem.hasClass('frontpage') ) { this.frontpageInit(); }
			else {
				this.subpageInit();
			}
		},
		subpageInit: function() {
			jQuery.fx.interval = 40;
			
			//jQuery('#mainNav ').css('width', jQuery('#mainNav ').width()+'px');
			this.adjustContentHeight();
			

		},
		frontpageInit: function() {
			jQuery.fx.interval = 40;
			

			function viewportClass() {
				var viewportWidth = jQuery(window).width();
				var viewportHeight = jQuery(window).height();
				
				if( viewportWidth <= 1200 || viewportHeight <= 700 ) { jQuery('body').addClass('small'); }
				else { jQuery('body').removeClass('small'); }
				
				//console.log('width: '+viewportWidth+' // height: '+viewportHeight);
			}
			
			jQuery(window).resize(function() {
				viewportClass();
			});
			
			viewportClass();

		},
		adjustContentHeight: function() {
			var sidebarHeight = jQuery('#sidebarRight').height();
			var contentHeight = jQuery('#contentWrap').height();
			
			if( sidebarHeight > contentHeight ) {
				jQuery('#content').height( (sidebarHeight) +'px' );
			}
		}
	}
}



















