if (typeof(bg) == 'undefined') bg = {};
if (!bg.edesign) bg.edesign = {};

bg.edesign.log = function() {};

//bg.edesign.log = console.log;


bg.edesign.list = function(params) {
	bg.edesign.log('bg.edesign.list',params);
	
//	{ listObj: objsToList, holder: 'id / jquery selector', listTag: 'element parent tag', elCode: 'element html code', elCallback: callbackForDisplayedEl(element DOM obj) }
	
	if (!bg.edesign.lists) bg.edesign.lists = {};
	bg.edesign.lists = this;

	this.params = params;
	
	this.holder = {};
	if ( this.params.holder.indexOf('#') >= 0 ) {
		this.holder.main = document.getElementById( this.params.holder.replace('#','') );
	} else {
		this.holder.main = jQuery(this.params.holder).get(0);
	}
	
	this.init();
}

bg.edesign.list.prototype.init = function() {
	
	this.createHolders();
	
}

bg.edesign.list.prototype.createHolders = function() {
	bg.edesign.log('bg.edesign.list.prototype.createHolders');
	
	for (var elid in this.params.listObj) {
		var elListObj = this.params.listObj[elid];
		var elDomObj = document.createElement(this.params.listTag);
		
		elListObj.DOMelement = elDomObj;
		
		elDomObj.innerHTML = this.processVars(elListObj);
	
		this.holder.main.appendChild( elDomObj );
			
		if (this.params.elCallback) {
			this.params.elCallback(elDomObj);	
		}
	}

}

bg.edesign.list.prototype.hideEl = function(elid) {
	
	this.params.listObj[elid].DOMelement.style.display = 'none';
	
}

bg.edesign.list.prototype.showEl = function(elid) {
	
	this.params.listObj[elid].DOMelement.style.display = 'block';
	
}


bg.edesign.list.prototype.processVars = function(elListObj) {
	//bg.edesign.log('bg.edesign.list.prototype.processVars', elListObj, this.params.elCode);
	
	var elCodeProc = this.params.elCode+'';

	for (var param in elListObj) {	
		var regex = new RegExp('\\|'+param+'\\|','g');
		elCodeProc = elCodeProc.replace(regex,elListObj[param]);
	}
	
	return elCodeProc;
	
}

/*bg.edesign.processVars = function(htmlCode,obj) {
//	console.log('bg.edesign.processVars',htmlCode,obj);
	var htmlCodePr = htmlCode+'';
	for (var param in obj) {
		var regex = new RegExp('\\|\\['+param+'\\]\\|','g');
		htmlCodePr = htmlCodePr.replace(regex,obj[param]);
	}
	return htmlCodePr;
}
*/

bg.edesign.filterArr = function(filterParam,filterString,filterArray) {

//	bg.edesign.log('bg.edesign.filterArr', arguments);
	
	if (filterParam == '') {
		return filterArray;
	}
	
	var arrayRes = new Array();
	for (var i in filterArray) {
		var tempArrEl = filterArray[i];
		if ( tempArrEl[filterParam].toLowerCase().indexOf(filterString.toLowerCase()) == 0 ) {
			arrayRes.push(filterArray[i]);
		}
	}
	
	return arrayRes;
}


bg.edesign.fogOfWar = function( params ) {
//	params = { act:'show', hideAct: function() { } } 
	bg.edesign.log('_site.prototype.fogOfWar', 'this.active.fog', this.active.fog, params );
	
	if (params.act == 'show') { // show fogofwar
		
		$(this.holder.fog).css('height', $(document).height());
		$(this.holder.fog).css('width', $(document).width());
		$(this.holder.fog).fadeIn();
		
		if ( typeof(params.hideAct) == 'function' ) {
			this.active.fogCallBack = params.hideAct;
			$(this.holder.fog).bind('click', site.active.fogCallBack );
		}
		
		this.active.fog = true;
		
	} else { // hide fogofwar
		
		$(this.holder.fog).unbind('click');
		$(this.holder.fog).fadeOut();
		this.active.fog = false;
		
	}
}


bg.edesign.scrollpane = function(params) {
	
	bg.edesign.log('+ new bg.edesign.scrollpane', params);
	
//	this.params = { scrollTarget: null, trackBar: null };
	this.params = params;
	
	if (!this.params.viewpOffset) this.params.viewpOffset = 0;
	
	this.holder = {};
	this.holder.trackBtn = document.getElementById(this.params.trackBar);
	this.holder.target = document.getElementById(this.params.scrollTarget);
	
	this.active = {};
	this.active.screenN = 1;
	
	if (!bg.edesign.scrollpanes) bg.edesign.scrollpanes = [];
	
	bg.edesign.scrollpanes[this.params.id] = this;
	
	this.init( $(this.holder.target).children('table').get(0) );
	
	this.attachBtnListeners();
	this.attachScrollListener();
}

bg.edesign.scrollpane.prototype.init = function( widthEl ) {
	bg.edesign.log('bg.edesign.scrollpane.prototype.init', widthEl);
	
	this.active.screenN = 1;
	
	this.active.contW = $(widthEl).width();
	this.active.viewpW = $(this.holder.target).parent().width() + this.params.viewpOffset;
	
	$(this.holder.trackBtn).show();

	$(this.holder.trackBtn).css('cursor','pointer');
//	$(this.holder.trackBtn).css('cursor','pointer');
	$(this.holder.target.parentNode).scrollLeft(0);
	
	this.displayBtns();
	
	this.active.autoScroll = false;
	this.active.screenTotal = Math.ceil( this.active.contW / this.active.viewpW );
	this.active.lastScreenW = this.active.contW - ( this.active.viewpW * (this.active.screenTotal - 1) );

	if ( this.active.viewpW >= this.active.contW ) {
		this.hideScroller();
		return;
	}

	this.setTrackWidth();
	this.createSlider();
	this.displayBtns();
	
}

bg.edesign.scrollpane.prototype.hideScroller = function() {
	
	$(this.holder.trackBtn).hide();
	this.displayBtns();
		
}


bg.edesign.scrollpane.prototype.createSlider = function() {
	
	this.trackBtn = new bg.edesign.slider( {id: this.params.id, parent: this, holder: this.holder.trackBtn, minX: 0, maxX: this.active.viewpW - this.params.viewpOffset, onDrag: 'doScroll'} );
	
}

bg.edesign.scrollpane.prototype.attachBtnListeners = function() {
	
	this.arrow = {};
	this.arrow.L = document.getElementById('scrollArrL');
	this.arrow.R = document.getElementById('scrollArrR');
	
	this.arrow.L.style.display = 'none';
	this.arrow.R.style.display = 'none';
	
	$(this.arrow.L).bind('click', {parent:this}, function(e) {
		e.data.parent.changeScreen('-');
		e.preventDefault();
		return false;
	});
	$(this.arrow.R).bind('click', {parent:this}, function(e) {
		e.data.parent.changeScreen('+');
		e.preventDefault();
		return false;
	});

	this.displayBtns();
}

bg.edesign.scrollpane.prototype.displayBtns = function() {
	
	if (!this.arrow) return;
	bg.edesign.log('bg.edesign.scrollpane.prototype.displayBtns');
	
	if (this.active.screenN == 1) {
		$(this.arrow.L).fadeOut();
	} else {
		$(this.arrow.L).fadeIn();
	}
	
//	if (this.active.screenTotal > 1) {
		if (this.active.screenN >= this.active.screenTotal) {
			$(this.arrow.R).fadeOut();
		} else {
			$(this.arrow.R).fadeIn();
		}
	

}

bg.edesign.scrollpane.prototype.setTrackWidth = function() {
	bg.edesign.log('bg.edesign.scrollpane.prototype.setTrackWidth');
	
	this.trackBtnRatio = this.active.viewpW / this.active.contW ;
	this.trackBtnW = Math.round( this.active.viewpW * this.trackBtnRatio );
	
	$(this.holder.trackBtn).width(this.trackBtnW);
	
}

bg.edesign.scrollpane.prototype.doScroll = function(percent) {
	
	var scrollW = Math.round( ( this.active.contW - this.active.viewpW ) * ( percent / 100 ) );
	$(this.holder.target.parentNode).scrollLeft(scrollW);
	this.setActiveScreen();
	
}

bg.edesign.scrollpane.prototype.attachScrollListener = function() {
	
	bg.edesign.log('bg.edesign.scrollpane.prototype.attachScrollListener');
	
	$(this.holder.target.parentNode).bind('scroll', {parent:this}, function(e) {
		e.data.parent.scrollListener(this);
	});
	
}

bg.edesign.scrollpane.prototype.scrollListener = function(targetObj) {
	
	this.setActiveScreen();
	
	if (this.trackBtn.isDragging) {
		return;
	}
	
	var percent = Math.round( ($(targetObj).scrollLeft()) / ( this.active.contW - this.active.viewpW ) * 100);
	this.trackBtn.setPercent(percent);
	
}

bg.edesign.scrollpane.prototype.setActiveScreen = function() {
	
	if ( Math.ceil( $(this.holder.target.parentNode).scrollLeft() / this.active.viewpW ) > this.active.screenN-1) {
		this.active.screenN++;
		if (!this.active.autoScroll) this.displayBtns();
	}

	if ( Math.ceil( $(this.holder.target.parentNode).scrollLeft() / this.active.viewpW ) < this.active.screenN-1) {
		this.active.screenN--;
		if (!this.active.autoScroll) this.displayBtns();
	}
	
	
}

bg.edesign.scrollpane.prototype.changeScreen = function(direction) {
	
	bg.edesign.log('bg.edesign.scrollpane.prototype.changeScreen', direction, this.active.screenN, 'of', this.active.screenTotal, 'this.active.lastScreenW: ',this.active.lastScreenW);
	var neededNewScrollL;
	
	if (direction == '+') {
		
		if (this.active.screenN+1 == this.active.screenTotal) {
			var neededNewScrollL = $(this.holder.target.parentNode).scrollLeft() + this.active.lastScreenW;
		} else {
			var neededNewScrollL = this.active.viewpW * this.active.screenN;
		}
		this.active.screenN ++;
		
	} else if (direction == '-') {
		
		if (this.active.screenN == 1) return;
		
		if (this.active.screenN == this.active.screenTotal) {
			var neededNewScrollL = $(this.holder.target.parentNode).scrollLeft() - this.active.lastScreenW;
		} else {
			var neededNewScrollL = ( this.active.viewpW ) * (this.active.screenN-2);
		}

		this.active.screenN--;
	}
	
	this.active.autoScroll = true;
	$(this.holder.target.parentNode).animate( {scrollLeft: neededNewScrollL}, 'slow', function() { bg.edesign.scrollpanes['prodsTabs'].autoScrollStop() });	
//	this.displayBtns();
//	$(this.holder.target.parentNode).animate( {scrollLeft: neededNewScrollL}, 'fast');	
	
}

bg.edesign.scrollpane.prototype.autoScrollStop = function() {
	
	bg.edesign.log('bg.edesign.scrollpane.prototype.autoScrollStop');
	this.active.autoScroll = false;
	this.displayBtns();
	
}















bg.edesign.slider = function(params) {
	
	bg.edesign.log('+ new bg.edesign.slider',params);
	
	this.debug = true;
	this.params = params;
	
//	this.params.minX = 0;
//	this.params.maxX = 164;
	
	this.trackBtn = this.params.holder;
	
	this.trackOffset = 0;
	
	this.selected = {};
	
	this.mouse = {};
	this.mouse.initX = 0;
	this.mouse.initY = 0;
	
	this.isDragging = false;
	
	if (!bg.edesign.sliders) { 
		bg.edesign.sliders = []; 
		bg.edesign.mouse = {x:0,y:0};
//		this.params.id = 0;
	}
	
	if (!this.params.id) {
		this.params.id = bg.edesign.sliders.length;
	}
	
	bg.edesign.sliders[this.params.id] = this;

//	this.params.onDrag = function() {};
		
	this.init();
	
}

bg.edesign.slider.prototype.init = function() {
	
	bg.edesign.log('bg.edesign.slider.prototype.init');
				
	
//	this.trackBtn = $(this.holder).find('.marker').get(0);
//	this.trackBtn = $(this.holder).find('.marker').get(0);
//	this.trackBar = $(this.holder).find('.filled-bar').get(0);

	
	$(this.trackBtn).css('left',0);
	this.trackBtn.posLeft = 0;
	this.trackBtn.parent = this;
	this.trackBtn.ondragstart = function() { return false; };

	this.trackBtn.onmousedown = function(e) { this.parent.startDrag(); return false;}
	this.trackBtn.onmouseup = function(e) { this.parent.stopDrag();  return false;}
	
//	$(document).bind('mouseup', {parentJS:this}, function(event) {
//		event.data.parentJS.stopDrag();
//	});
	
//	this.trackBtn.onmouseout = function() { this.outTimeout = setTimeout('bg.edesign.sliders['+this.parent.params.id+'].stopDrag()',1000); }
//	this.trackBtn.onmouseover = function() { clearTimeout(this.outTimeout); }
	
	$(document).mousemove(function(e){
		bg.edesign.mouse.x = e.pageX;
		bg.edesign.mouse.y = e.pageY;	
   });
	
}

bg.edesign.slider.prototype.startDrag = function() {
	
	bg.edesign.log('bg.edesign.slider.prototype.startDrag');

	 
	this.isDragging = true;
	this.mouse.initX = bg.edesign.mouse.x;

//	clearInterval(this.sliderInterval);
//	this.sliderInterval = setInterval('bg.edesign.sliders['+this.params.id+'].doDrag()',50);
	$(document).bind('mousemove.scrollPane', {parentJS:this}, function(event) { bg.edesign.sliders[event.data.parentJS.params.id].doDrag(); });
	
	$(document).bind('mouseup', {parentJS:this}, function(event) {
		event.data.parentJS.stopDrag();
	});
	
//	this.trackBtn.onmouseout = function() { this.outTimeout = setTimeout('bg.edesign.sliders['+this.parent.params.id+'].stopDrag()',1000); }
//	this.trackBtn.onmouseover = function() { clearTimeout(this.outTimeout); }
	
	
	
	return false;
}

bg.edesign.slider.prototype.doDrag = function(autoDrag) {
	
	bg.edesign.log('bg.edesign.slider.prototype.doDrag');
	var mouseOffsetX = bg.edesign.mouse.x - this.mouse.initX;
	
	var currX = this.trackBtn.posLeft + mouseOffsetX;
	
	if (currX <	this.params.minX) currX = this.params.minX;
	if (currX >	this.params.maxX - ( $(this.trackBtn).width() ) ) currX = this.params.maxX - $(this.trackBtn).width();

	$(this.trackBtn).css('left',currX+'px');
	if (this.trackBar) {
		$(this.trackBar).css('width',(currX)+'px');
	}
			
	this.trackOffset = currX;
	
	this.params.parent[this.params.onDrag]( Math.round( (this.trackOffset / (this.params.maxX - $(this.trackBtn).width())) * 100 ) );

}

bg.edesign.slider.prototype.stopDrag = function() {
	
	bg.edesign.log('bg.edesign.slider.prototype.stopDrag', this);
	
	if (this.isDragging) {
//		clearInterval(this.sliderInterval);
		$(document).unbind('mouseup');
		$(document).unbind('mousemove.scrollPane');

		this.trackBtn.posLeft = this.trackOffset;
		this.isDragging = false;
	
	}
}

bg.edesign.slider.prototype.setPercent = function(percent) {
	
	var neededPosX = Math.round( (this.params.maxX - $(this.trackBtn).width() ) * ( percent / 100) );
	bg.edesign.log('bg.edesign.slider.prototype.setPerc', percent, this.params.maxX, neededPosX);
	$(this.trackBtn).css('left',neededPosX);
	this.trackOffset = neededPosX;
	this.trackBtn.posLeft = this.trackOffset;
}



bg.edesign.fogOfWar = function( params ) {
	bg.edesign.log('_site.prototype.fogOfWar', 'this.active.fog', this.active.fog, params );
	
	if (params.act == 'show') { // show fogofwar
		
		$(this.holder.fog).css('height', $(document).height());
		$(this.holder.fog).css('width', $(document).width());
		$(this.holder.fog).fadeIn();
		
		if ( typeof(params.hideAct) == 'function' ) {
			this.active.fogCallBack = params.hideAct;
			$(this.holder.fog).bind('click', site.active.fogCallBack );
		}
		
		this.active.fog = true;
		
	} else { // hide fogofwar
		
		$(this.holder.fog).unbind('click');
		$(this.holder.fog).fadeOut();
		this.active.fog = false;
		
	}
}

bg.edesign.initFitImage = function(imgHolder) {
	
	
	//return;
	
	
	$(imgHolder).find('img').bind('load', {imgHolder:imgHolder}, function(e) {
		bg.edesign.fitImage(e.data.imgHolder);
	});
	
	

}

bg.edesign.fitImage = function(imgHolder) {
	
//	return;

	var imgJQ = $(imgHolder).find('img');
	var hldrJQ = imgJQ.parent();
		
	var imgW = imgJQ.width();
	var imgH = imgJQ.height();
	
	imgJQ.css('display','none');
	var hldrW = hldrJQ.width();
	var hldrH = hldrJQ.height();
	imgJQ.css('display','inline');

	bg.edesign.log('bg.edesign.fitImage', imgHolder, hldrW+'x'+hldrH, imgW+'x'+imgH);

	if ( (hldrW < imgW) && (hldrH < imgH)) {
		if (imgW > imgH) {
			imgJQ.width(hldrW);
		} else if (imgW < imgH) {
			imgJQ.attr('height',hldrH);
		}
	} else if (hldrW < imgW) {
		imgJQ.width(hldrW);
	} else if (hldrH < imgH) {
		imgJQ.attr('height',hldrH);
	}
	

//	console.log('bg.edesign.fitImage', $(imgHolder).find('img').attr('src'), imgW,'--', hldrW);
	
	
	
}
