function pbDebug() {
	var $pbDebug = $('#pbDebug');
	if ( ! $pbDebug.length ) {
		$pbDebug = $('<div id="pbDebug">').appendTo($('body')).data('Debug',this);
	}
	var thisDebug = this;
	
	this.log = function (text) {
		if ( $pbDebug.is(':hidden') ) {$pbDebug.show(); $('html').css('margin-top',$pbDebug.height()+'px');}
		var dt = new Date();
		var timeStr = '';
		//timeStr = dt.getMonth()+'/'+this.pad(2,'0',dt.getDate())+'/'+dt.getFullYear().' ';
		timeStr += this.pad(2,'0',dt.getHours())+':'+this.pad(2,'0',dt.getMinutes())+'.'+this.pad(2,'0',dt.getSeconds())+'.'+this.pad(3,'0',dt.getMilliseconds());
		$pbDebug.html('<p>['+timeStr+']  '+text+'</p>'+$pbDebug.html());
	};
	
	this.pad = function pad(len, padChr, str) {
		var res = new String(str);
		while ( res.length < len ) res = padChr+res;
		return res;
	}
	
	return this;
}

function CCV4_Page() {
	var thisPage = this;
	this.compat = {
		ie7:false,
		ie8:false,
		ie9:false
	};
	
	this.$debug;
	var $debug;
	
	this.construct = function (params) {
		if ( params ) {
			if ( params.compat ) thisPage.setCompat(params.compat);
		}
		$(document).data('blurred', false);
		$(window).bind('blur',function () {$(document).data('blurred', new Date().getTime());});
		$(window).bind('focus',function () {$(document).data('blurred', false);});
		/*$('*').bind('mousemove',function () {$(document).data('blurred',false);});*/
		
		thisPage.$debug = new pbDebug();
		$debug = thisPage.$debug;
	};
	
	this.setMainHeading = function (params) {
		if ( ! params ) params = {h1Only:false};
		
		var $base = $('#pbWpPageBaseContent');
		var $h1 = $base.find('h1:first');
		
		if ( ! $h1.length ) return;
		
		var $h = $('<div id="MainHeading">').prependTo($base);
		$h1.prepend('<div class="icon">');
		var $h2 = $h1.next('h2');
		$h.append($h1);
		
		if ( ! params.h1Only ) {
			$h.append($h2);
			$h2.css('left', ( $h1.width() + parseInt($h1.css('padding-left')) + parseInt($h1.css('padding-right')) )+'px');
		}
	};
	this.setMainHeadingRF = function (params) {
		if ( ! params ) params = {h1Only:false};
		
		var $base = $('#pbWpPageBaseContent');
		var $h1 = $base.find('h1:first');
		
		if ( ! $h1.length ) return;
		
		var $target = $('#pbBaseHeader .heading');
		
		var $h = $('<div id="MainHeading">').prependTo($target);
		var $h2 = $h1.next('h2');
		$h.append($h1);
		
		if ( ! params.h1Only ) {
			$h.append($h2);
			$h2.css('left', ( $h1.width() + parseInt($h1.css('padding-left')) + parseInt($h1.css('padding-right')) )+'px');
		}
	};
	this.setIntro = function () {
		var $base = $('#pbWpPageBaseContent');
		if ( ! $base.find('.intro').length ) $base.find('p:first').addClass('emboss').addClass('intro');
	};
	this.setEmboss = function () {
		return;
		if (thisPage.compat.ie9) {
			$('.emboss').each(function () {
			var $e = $(this);
			var $cnt = $('<div class="embossCnt">').insertBefore($e);
			$e.appendTo($cnt);
			$h = $e.clone().removeClass('emboss').addClass('hiLite').appendTo($cnt);
			});
		}
	};
	this.setMoreCnt = function () {
		var fadeDurPerPixel = 0.5;
		var $moreCnt = $('.moreCnt');
		
		$moreCnt.each(function () {
			var $moreCnt = $(this);
			var initialHeight = $moreCnt.attr('initialHeight');
			
			if ( ! initialHeight ) initialHeight = 300;
			
			var fullHeight = $moreCnt.height();
			$moreCnt.attr('fullHeight', fullHeight).attr('initialHeight',initialHeight).height(initialHeight);
			
			var $moreFade = $('<div class="moreFade">').appendTo($moreCnt);
			
			// moreLabel
				var $moreA = $('<div class="moreLabel moreA">˅ more ˅</div>').appendTo($moreFade);
				var $moreB = $('<div class="moreLabel moreB">˅ more ˅</div>').appendTo($moreFade);
			//
			
			$moreFade.click(function () {
				var distToUnroll = fullHeight - initialHeight;
				var duration = ( distToUnroll * fadeDurPerPixel );
				$moreCnt.animate({height:fullHeight},{duration:duration,complete:function () {$moreFade.remove();}});
			});
		});
	};
	this.setTopRightBox = function () {
		var $box = $('#TopRightBox');
		var $img = $box.children('img:first-child');
		if ( ! $img.length ) $img = $box.children('p:first-child').find('img:first');
		if ( ! $img.length ) return;
		$box.width($img.width());
	};
	
	this.setCompat = function (compatKey) {
		if ( typeof(this.compat[compatKey]) == 'undefined' ) return;
		this.compat[compatKey] = true;
	};
	
	this.construct();
}

function pbNavBar() {
	var Page = $(document).data('Page');
	var thisPbNavBar = this;
	var $Base = $('#pbNavBar');
	var $debug = Page.$debug;
	
	this.construct = function () {
		var $items = $Base.children('ul.base').children('li');
		//var targetWidth = ( $Base.width() / $items.length );
		var $tintHover = $Base.children('div.tintHover');
		var $tintCurrent = $Base.children('div.tintCurrent');
		
		var $currItem = $Base.children('ul.base').children('li.current_page_item,li.current_page_ancestor,li.current_page_parent').first();
		if ( ! $currItem.length ) $currItem = $Base.children('ul.base').children('li:first').addClass('current_page_item');
		
		var itemHeight = $items.children('a:first').height() + parseInt($items.children('a:first').css('padding-top'));
		
		var subMenuSlideDurationPerItem = 50;
		
		// mouse events, childrenCnt
			$items.each(function (i,e) {
				var $e = $(e);
				var $childrenUl = $e.children('ul.children');
				// item mouse enter, mouse leave events
					$e.mouseenter(function () {
						var slideDuration = ( $e.children('div.childrenCnt').children('ul.children').children().length * subMenuSlideDurationPerItem );
						var $childrenCnt = $e.children('div.childrenCnt');
						if ( $childrenCnt.length && ! $childrenCnt.queue().length ) $childrenCnt.slideDown(slideDuration);
						$tintHover.stop().animate({width:( $e.position().left + $e.width() )},{'duration':150});
					});
					$e.mouseleave(function () {
						var slideDuration = ( $e.children('div.childrenCnt').children('ul.children').children().length * subMenuSlideDurationPerItem );
						$e.children('div.childrenCnt').slideUp(slideDuration);
					});
				//
				// children ul, childrenCnt, transpBase set up
					if ( $childrenUl.length ) {
						$e.click(function () {
							switch ( $(this).attr('hoverByClick') ) {
								case 'true' :
									$(this).attr('hoverByClick',false).trigger('mouseleave');
								break;
								default:
									$(this).attr('hoverByClick',true).trigger('mouseenter');
								break;
							}
							return false;
						});
						$childrenUl.find('a').each(function () {
							$(this).click(function () {window.location.href = $(this).attr('href'); return false});
						});
						var $childrenCnt = $('<div class="childrenCnt">').appendTo($e);
						$childrenUl.find('[title]').removeAttr('title');
						$childrenUl.appendTo($childrenCnt);
					}
				//
			});
		//
		// item width and left
			var targetBarWidth = $Base.width() * 1;
			var totalItemWidth = 0; $items.each(function (i,e) {totalItemWidth += $(e).width();});
			var widthLeftover = targetBarWidth - totalItemWidth;
			var widthAddPerItem = Math.ceil(widthLeftover / $items.length);
			
			$items.each(function (i,e) {$(e).width('+='+widthAddPerItem)});
			var widthAccum = 0; $items.each(function (i,e) {$(e).css('left', ( widthAccum )+'px'); widthAccum += $(e).width();});
		//
		// childrenCnt
			$items.each(function (i,e) {
				var $childrenCnt = $(e).children('.childrenCnt');
				$childrenCnt.css('min-width',$(e).width()+'px');
			});
		//
		// remove a titles
			$items.children('a').removeAttr('title');
		//
		// tint initial pos
			$tintCurrent.width(parseInt($currItem.css('left')) + $currItem.width());
			$tintHover.width($tintCurrent.width());
		//
		
		// subChildrenCnt1
			$Base.find('div.childrenCnt').children('ul.children').find('ul.children').each(function () {
				var $subChildrenCnt1 = $('<div class="subChildrenCnt1">').appendTo($(this).parent());
				$subChildrenCnt1.append($(this));
				var duration = 300;
				$subChildrenCnt1.parent().hover(
					function () {
						var $li = $(this);
						var $cnt = $li.children('div.subChildrenCnt1');
						var $subUl = $cnt.children('ul');
						var w = $cnt.attr('fullWidth'); if ( ! w ) {$cnt.show(); w = $cnt.width(); $cnt.attr('fullWidth',w);}
						var h = $cnt.height();
						$cnt.css('left',$li.width()+'px');
						$cnt.width(0).css('height',h+'px');
						$subUl.width(w).css('position','absolute').css('right','0px');
						$cnt.stop().animate({width:w},{duration:duration});
					},function () {
						var $li = $(this);
						var $cnt = $li.children('div.subChildrenCnt1');
						$cnt.stop().animate({width:0},{duration:duration});
					}
				);
			});
		//
		
		$Base.mouseleave(function () {
			var revertDuration = $tintHover.width() - $tintCurrent.width(); if ( revertDuration < 0 ) revertDuration = revertDuration * -1;
			$tintHover.stop().animate({'width':$tintCurrent.width()},{'duration':( revertDuration )*0.8});
		});
		$Base.show();
	};
	this.construct();
}
function Gizmo(gizmoData) {
	//$('body').html($(gizmoData).dump()).css('white-space', 'pre').css('background-color','#666').css('color','#ccc'); return;
	var Page = $(document).data('Page');
	var $debug = Page.$debug;
	var $Base = $('#Gizmo');
	var thisGizmo = this;
	var $main = $Base.children('div.main');
	var $ourWorkBlock = $Base.children('div.ourWorkBlock');
	var $currIconsRow = $ourWorkBlock.children('div.currIconsRow');
	var $currIconsCnt = $currIconsRow.children('table.currIconsCnt');
	var $promosRow = $main.children('div.promosRow');
	var $promosCnt = $promosRow.children('table.promosCnt');
	var $bottomBar = $Base.children('div.bottomBar');
	var $greyBar = $bottomBar.children('div.greyBar');
	var $iconsRow = $greyBar.children('div.iconsRow');
	var $iconsCnt = $iconsRow.children('table.iconsCnt');
	var $curtain = $Base.children('div.curtain');
	var iconsToPromosScrollRatio;
	
	var d = gizmoData.data;
	var h = gizmoData.headers;
	
	this.iconLoaded = [];
	this.imageLoaded = [];
	this.initialLoaded;
	this.currBottomBarAction;
	
	this.construct = function () {
		$curtain.width($Base.width()).height($Base.height());
		this.setIcons();
		this.setPromos();
		iconsToPromosScrollRatio = $promosCnt.width() / $iconsCnt.width();
		var $firstPromo = $promosCnt.find('div.promo:first');
		
		//$bottomBar.bind('mousedown', function (evt) {
		$bottomBar.bind('mousedown', function (evt) {
			if ( evt.button != '0' && evt.button != '1' ) return; 
			if ( $promosRow.queue().length ) return;
			thisGizmo.currBottomBarAction = new BottomBarAction(evt).initByDragAction();
		});
		$Base.children('div.ourWorkBlock').bind('mousedown',function (evt) {
			if ( evt.button != '0' && evt.button != '1' ) return; 
			if ( $promosRow.queue().length ) {return;}
			thisGizmo.currBottomBarAction = new BottomBarAction(evt).initByDragAction();
		});
		$('div.ourWorkBlock h2').bind('click', function () {
			//if ( $promosRow.queue().length ) return;
			thisGizmo.setSlideTimer('continue'); return false;
		});
		
		window.setTimeout("$('#Gizmo').data('Gizmo').riseCurtain();", 2500);
	};
	this.riseCurtain = function () {
		var delayToRise = 1200;
		thisGizmo.initialLoaded = true;
		
		if ( ! thisGizmo.imageLoaded[0] ) {thisGizmo.initialLoaded = false;}
		//$.each(d, function (i,de) {if ( ! thisGizmo.imageLoaded[i] ) {thisGizmo.initialLoaded = false;}});
		$.each(d, function (i,de) {if ( ! thisGizmo.iconLoaded[i] ) {thisGizmo.initialLoaded = false;}});
		
		if ( ! thisGizmo.initialLoaded ) {window.setTimeout("$('#Gizmo').data('Gizmo').riseCurtain()", delayToRise); return;}
		
		thisGizmo.setSlots();
		
		thisGizmo.setRowDupes($promosRow);
		thisGizmo.setRowDupes($currIconsRow);
		thisGizmo.setRowDupes($iconsRow);
		
		$curtain.fadeOut('fast');
		
		thisGizmo.setSlideTimer('start');
	};
	this.setSlots = function () {
		$iconsCnt.find('img').each(function (i,e) {
			var $img = $(e);
			var iconHoverDuration = 500;
			$img.hover(function () {
				$(this).stop().animate({opacity:0.8},{duration:iconHoverDuration});
				},function () {
				$(this).stop().animate({opacity:0.4},{duration:iconHoverDuration});
			});
		});
		
		var iconWidth = $iconsCnt.find('td:first').width();
		var iconLeft = parseInt($iconsCnt.find('img:first').css('left'));
		var iconShift = iconLeft - iconWidth;
		var barWidth = $greyBar.width();
		var iconSlots = ( barWidth / iconWidth );
		
		// iconSlots
			var $iconSlot;
			var i;
			for ( i = 0; i < iconSlots; i++ ) {
				$iconSlot = $('<div class="iconSlot" slotIndex="'+i+'">').appendTo($bottomBar);
				$iconSlot.css('left',( ( iconWidth * i ) + iconShift )+'px');
			}
		//
	}
	this.setRowDupes = function ($itemsRow) {
		var $itemCnt = $itemsRow.children().first();
		var i;
		
		var $dupeLeftA = $itemCnt.clone(true).css('left', '0px').prependTo($itemsRow).removeClass('initial');
		var $dupeLeftB = $itemCnt.clone(true).css('left', ( $itemCnt.width() )+'px').prependTo($itemsRow).removeClass('initial');
		$itemCnt.css('left',( $itemCnt.width() * 2 )+'px');
		var $dupeRightA = $itemCnt.clone(true).css('left', ( $itemCnt.width() * 3 )+'px').appendTo($itemsRow).removeClass('initial');
		var $dupeRightB = $itemCnt.clone(true).css('left', ( $itemCnt.width() * 4 )+'px').appendTo($itemsRow).removeClass('initial');
		$itemsRow.css('left','-'+( $itemCnt.width() * 2 )+'px');
		
		/* dupeCount method:
		var dupeCount = 2;
		$itemCnt.css('left',( $itemCnt.width() * dupeCount )+'px');
		for ( i = 0; i<dupeCount; i++ ) {
			var $dupeLeft = $itemCnt.clone(true).css('left', ( $itemCnt.width() * i )+'px').prependTo($itemsRow).removeClass('initial');
			var $dupeRight = $itemCnt.clone(true).css('left', ( $itemCnt.width() * ( ( dupeCount * 2 ) - i ) )+'px').appendTo($itemsRow).removeClass('initial');
		}
		$itemsRow.css('left','-'+( $itemCnt.width() * dupeCount )+'px');
		*/
	};
	this.setIcons = function () {
		$.each(d, function (i,de) {
			thisGizmo.iconLoaded[i] = false;
			var $td = $('<td>').appendTo($iconsCnt.find('tr'));
			var $icon = $('<img>').appendTo($td);
			$icon.bind('load',function () {thisGizmo.iconLoaded[i] = true;});
			$icon.attr('src', de[h.icon]);
			
			var $currTd = $('<td>').appendTo($currIconsCnt.find('tr'));
			var $currIcon = $('<div class="cnt"><img src="'+de[h.icon]+'" /></div>').appendTo($currTd);
		});
	};
	this.setPromos = function () {
		var $tr = $promosCnt.find('tr');
		$.each(d, function (promoNo,de) {
			var mainWidth = $main.width();
			var $td = $('<td>').appendTo($tr);
			//var $promo = $('<div class="promo">').width(mainWidth).height($main.height()).appendTo($td);
			var $promo = $('<div class="promo">').appendTo($td);
			thisGizmo.imageLoaded[promoNo] = false;
			var $img = $('<img>').bind('load',function () {thisGizmo.imageLoaded[promoNo] = true; $(this).parent().css('background-image','none');}).appendTo($promo).attr('src',de[h.image]);
			$h3 = $('<h3>'+de[h.heading]+'</h3>').appendTo($promo).css('left',''+de[h.titlePositionLeft]+'px').css('top',de[h.titlePositionTop]+'px').css('width',de[h.titleWidth]+'px').css('color',de[h.titleColor]);
			$text = $('<div class="text">'+de[h.desc]+'</div>').appendTo($promo).css('left',''+de[h.textPositionLeft]+'px').css('top',de[h.textPositionTop]+'px').css('width',de[h.textWidth]+'px').css('color',de[h.textColor]);
			//$debug.log($(de).dump());
			if ( de[h.area] && $(de[h.area]).length ) {
				var $map = $('<map name="promoMap_'+promoNo+'">').prependTo($promo);
				$.each(de[h.area],function (ii,a) {
					var $area = $('<area shape="'+a.shape+'" coords="'+a.coords+'" href="'+a.href+'" title="'+a.title+'">').appendTo($map);
				});
				$img.attr('usemap','#'+$map.attr('name'));
			}
		});
	};
	this.slideTimerId; this.setSlideTimer = function (mode) {
		var interval = 6000;
		var delayFactor = 2.5;
		window.clearTimeout(this.slideTimerId);
		switch (mode) {
			case 'suspend' :
			break;
			case 'resumeDelayed' :
				this.slideTimerId = window.setTimeout("$('#Gizmo').data('Gizmo').setSlideTimer('continue');", ( interval * delayFactor ));
			break;
			case 'start' :
				this.slideTimerId = window.setTimeout("$('#Gizmo').data('Gizmo').setSlideTimer('continue');", interval);
			break;
			case 'continue' :
				thisGizmo.slideNext();
				this.slideTimerId = window.setTimeout("$('#Gizmo').data('Gizmo').setSlideTimer('continue');", interval);
			break;
		}
	};
	this.slideNext = function (queued) {
		if ( ! queued ) {if ( $(document).data('blurred') ) {/*$debug.log('Gizmo slideNext: doc is blurred');*/ return;} $Base.queue(function () {thisGizmo.slideNext(true);}); return;}
		var dur = 450;
		var bounceFactor = {};
		bounceFactor.distance = 0.02;
		bounceFactor.duration = 0.3;
		var p = {};
		p.initPos = $promosRow.position().left;
		p.width = $promosCnt.width() / $promosCnt.find('td').length;
		p.posA = Math.round(( p.initPos - p.width ) - ( p.width * bounceFactor.distance ));
		p.durA = dur;
		p.posB = Math.round(( p.initPos - p.width ) + ( p.width * ( bounceFactor.distance / 3 ) ));
		p.durB = Math.round(dur * ( bounceFactor.duration ));
		p.posC = Math.round(( p.initPos - p.width ));
		p.durC = Math.round(dur * ( bounceFactor.duration ));
		$promosRow.animate({
			'left':p.posA
			},{
			'duration':p.durA,
			'complete':function () {
				$promosRow.animate({
					'left':p.posB
					},{
					'duration':p.durB,
					'complete':function () {
						$promosRow.animate({
							'left':p.posC
							},{
							'duration':p.durC,
							'complete':function () {
								thisGizmo.resetPosition($promosRow);
								$Base.dequeue();
							}
						});
					}
				});
			}
		});
		$iconsRow.animate({left:'-='+( $iconsCnt.width() / $iconsCnt.find('td').length )},{duration:dur,complete:thisGizmo.resetPosition($iconsRow)});
		$currIconsRow.animate({left:'-='+( $currIconsCnt.width() / $currIconsCnt.find('td').length )},{duration:dur,complete:thisGizmo.resetPosition($currIconsRow)});
	}
	this.resetPosition = function ($itemsRow) {
		var $itemCnt = $itemsRow.children().first();
		var a = {};
		a.dupesCount = $itemsRow.children('table').length;
		a.dupeWidth = $itemCnt.width();
		a.itemsRowWidth = a.dupeWidth * a.dupesCount;
		//$debug.log('[resPos] a: '+$(a).dump());
		
		var b = {};
		b.currPos = ( $itemsRow.position().left * -1 );
		//b.currPosPercent = Math.round( b.currPos / ( a.itemsRowWidth / 100 ) );
		b.currDupe = b.currPos / a.dupeWidth;
		if ( Math.ceil(b.currDupe) != 3 ) {
			b.newDupe = ( 3 + ( b.currDupe - Math.ceil(b.currDupe) ) );
			$itemsRow.css('left','-'+( ( b.newDupe * a.dupeWidth ) )+'px');
		}
		//$debug.log('[resPos] b: '+$(b).dump());
	};
	this.lockInPlace = function ($itemsRow) {
		var lockInDuration = 300;
		var $itemCnt = $itemsRow.children().first();
		var a = {};
		
		//$itemsRow.find('td').each(function (i,e) {$(e).attr('title',i);});
		
		a.totalItems = $itemsRow.find('td').length;
		a.itemWidth = $itemCnt.width() / $itemCnt.find('td').length;
		a.totalWidth = a.itemWidth * a.totalItems;
		//$debug.log('[lockInPl] a: '+$(a).dump());
		
		var b = {};
		b.currPos = ( $itemsRow.position().left * -1 );
		b.currPosRatio = b.currPos / a.totalWidth;
		b.currItem = Math.round( a.totalItems * b.currPosRatio );
		b.newPos = ( b.currItem * a.itemWidth );
		//$itemsRow.css('left','-'+b.newPos+'px');
		$itemsRow.animate({left:'-'+b.newPos},{duration:lockInDuration});
		//$debug.log('[lockInPl] b: '+$(b).dump());
	};

	function BottomBarAction(initialEvent) {
		var $pbBasePage = $('#pbBasePage');
		var $doc = $(document);
		var init = {};
		var mid = {};
		var fin = {};
		var clickNotDragThreshold = 5; //pix
		
		var justClick = true;
		
		initialEvent.preventDefault();
		init.x = initialEvent.pageX - $pbBasePage.position().left;
		init.t = new Date().getTime();
		init.iconsRowPos = $iconsRow.position().left;
		init.promosRowPos = $promosRow.position().left;
		
		thisGizmo.setSlideTimer('suspend');
		
		this.initByDragAction = function () {
			$greyBar.addClass('pressed');
			$doc.bind('mouseup', function (evt) {thisGizmo.currBottomBarAction.endAction(evt);});
			$doc.bind('mousemove', function (evt) {thisGizmo.currBottomBarAction.middleAction(evt);});
			return this;
		};
		this.middleAction = function (evt) {
			evt.preventDefault();
			mid.x = evt.pageX - $pbBasePage.position().left;
			mid.off = mid.x-init.x;
			mid.dist = mid.off; if ( mid.dist < 0 ) mid.dist = mid.dist * -1;
			//$debug.log($(mid).dump());
			
			if ( mid.dist < clickNotDragThreshold ) justClick = true; else justClick = false;
			if ( justClick ) return;
			
			var promosRowPos = Math.round( init.promosRowPos + ( mid.off * iconsToPromosScrollRatio ) );
			//$debug.log(promosRowPos);
			$promosRow.css('left', promosRowPos+'px');
			$iconsRow.css('left', Math.round( init.iconsRowPos + ( mid.off ) )+'px');
			$currIconsRow.css('left', Math.round( init.iconsRowPos + ( mid.off ) )+'px');
		}
		this.endAction = function (evt) {
			fin.x = evt.pageX - $pbBasePage.position().left;
			fin.dir;
			fin.dist = fin.x-init.x; if ( fin.dist < 0 ) {fin.dist = fin.dist*-1; fin.dir='fwd';} else {fin.dir='rev';}
			fin.t = new Date().getTime();
			fin.duration = fin.t - init.t;
			fin.spd = Math.round(( fin.dist/fin.duration ) * 8);
			
			if ( justClick ) {
				this.justClick();
			} else {
				thisGizmo.resetPosition($promosRow);
				thisGizmo.resetPosition($iconsRow);
				thisGizmo.resetPosition($currIconsRow);
				thisGizmo.lockInPlace($promosRow);
				thisGizmo.lockInPlace($iconsRow);
				thisGizmo.lockInPlace($currIconsRow);
				this.cleanup();
			}
		}
		this.justClick = function () {
			var slideDurationPerSlot = 350;
			var clk = {};
			clk.iconWidth = $iconsCnt.find('td:first').width();
			clk.iconHeight = $iconsCnt.find('td:first').height();
			clk.barWidth = $greyBar.width();
			clk.iconLeft = parseInt($iconsCnt.find('img:first').css('left')); if ( Page.compat.ie9 ) clk.iconLeft += parseInt($('#pbBasePage').css('margin-left'));
			clk.iconShift = clk.iconLeft - clk.iconWidth;
			clk.posShifted = init.x - clk.iconShift;
			clk.iconSlots = ( clk.barWidth / clk.iconWidth );
			clk.slotsTotalWidth = ( clk.iconWidth * clk.iconSlots );
			clk.posOnBar = ( ( clk.posShifted ) / ( clk.slotsTotalWidth ));
			clk.posOnSlots = ( clk.iconSlots * clk.posOnBar );
			clk.clickOverSlot = Math.ceil(clk.posOnSlots);
			/*$debug.log(clk.clickOverSlot);*/
			var b = {};
			//if ( clk.clickOverSlot == 1 ) clk.clickOverSlot = $iconsCnt.find('td').length +1;
			b.iconsAnimDist = ( clk.iconWidth * ( clk.clickOverSlot - 2 ) );
			b.animDuration = slideDurationPerSlot * ( clk.clickOverSlot - 2 ); if ( b.animDuration < 0 ) b.animDuration *= -1;
			b.promosAnimDist = Math.round(b.iconsAnimDist * iconsToPromosScrollRatio);
			
			$promosRow.animate({left:'-='+b.promosAnimDist},{duration:b.animDuration, complete:function () {thisGizmo.resetPosition($promosRow); thisGizmo.lockInPlace($promosRow);}});
			$iconsRow.animate({left:'-='+b.iconsAnimDist},{duration:b.animDuration, complete:function () {thisGizmo.resetPosition($iconsRow); thisGizmo.lockInPlace($iconsRow);}});
			$currIconsRow.animate({left:'-='+b.iconsAnimDist},{duration:b.animDuration, complete:function () {thisGizmo.resetPosition($currIconsRow); thisGizmo.lockInPlace($currIconsRow);}});
			
			this.cleanup();
		}
		this.cleanup = function () {
			$doc.unbind('mouseup').unbind('mousemove');
			$greyBar.removeClass('pressed');
			thisGizmo.currBottomBarAction = null;
			thisGizmo.setSlideTimer('resumeDelayed');
		}
	};
	
	this.construct();
}
function Spotlight() {
	var Page = $(document).data('Page');
	var $debug = Page.$debug;
	var thisSpotlight = this;
	var $Base = $('#Spotlight');
	var $ul = $Base.children('ul');
	var $imgCache = $Base.children('div.imgCache');
	var $initialItems = $ul.children('li');
	var imgWidth = $initialItems.first().find('img').width();
	var $mask;
	var $clicker;
	
	this.construct = function () {
		var offset = imgWidth / 2;
		// arrange and process items
			$ul.children('li').each(function (i,e) {
				var $li = $(e);
				$li.attr('imgIndex',i);
				var $img = $li.find('img:first');
				$img.appendTo($imgCache);
				$img.bind('load',function () {
					$img.attr('isLoaded',true);
					$Base.find('li[imgIndex='+i+'] a').each(function () {$img.clone().appendTo($(this)).fadeIn('fast',function () {$(this).parent().css('background-image','none')})});
				});
				$img.attr('src',$img.attr('setSrc'));
			});
			$ul.children('li').each(function (i,e) {$(e).css('left',( ( offset * i ) + ( imgWidth / 2 ) )+'px').css('z-index',( $ul.children('li').length - i ));});
			var $lastClone = $ul.children('li').last().clone(true);
			$lastClone.appendTo($ul).css('left','0px').addClass('stackFirst').css('z-index','0');
			$lastClone.find('img').remove();
			$ul.children('li').last().find('img').clone(true).appendTo($lastClone);
		//
		// mask, clicker
			$mask = $('<div class="mask">').appendTo($Base).width($Base.width()).height($Base.height()).css('z-index',( $initialItems.length - 1 ));
			$clicker = $('<div class="clicker">').appendTo($Base).width($Base.width()).height($Base.height()).css('z-index',( $initialItems.length + 1 ));
			$clicker.bind('click',function (evt) {thisSpotlight.clickerClick(evt)});
			$clicker.bind('mousemove',function (evt) {thisSpotlight.clickerHover(evt)});
			$clicker.bind('mouseout',function (evt) {thisSpotlight.clickerHoverReset(evt)});
		//
		// event binds
			$Base.prevAll('h2').click(function () {thisSpotlight.cycle()});
			this.setCycleTimer('start');
			$Base.parent().hover(function () {
				thisSpotlight.setCycleTimer('suspend');
				$mask.addClass('hover');
				},function () {
				thisSpotlight.setCycleTimer('resumeFast');
				$mask.removeClass('hover');
			});
		//
		$Base.fadeIn(1000);
	}
	this.clickerHover = function (evt) {
		if ( $Base.queue().length ) return;
		var relPos = evt.pageX - $('#pbBasePage').position().left; if ( Page.compat.ie9 ) {relPos -= parseInt($('#pbBasePage').css('margin-left'));}
		var slots = Math.round($Base.width() / ( imgWidth / 2 ));
		var hoveredSlot = Math.ceil(relPos / ( $Base.width() / slots ));
		
		this.clickerHoverReset();
		var hoveredItemIndex = hoveredSlot - 2;
		
		//$debug.log('hoveredSlot:'+hoveredSlot+' hoveredItemIndex:'+hoveredItemIndex);
		if ( hoveredSlot <= 3 ) {
			if ( hoveredSlot > 1 ) {
				hoveredItemIndex = 1;
				$ul.children('li').filter(':nth-child('+hoveredItemIndex+')').addClass('hovered');
			}
		} else {
			$ul.children('li').filter(':nth-child('+hoveredItemIndex+')').attr('hoverSavedPos',function () {return $(this).css('left')}).css('left', '+='+( imgWidth / 10 ));
		}
	}
	this.clickerHoverReset = function () {
		$ul.children('li').removeClass('hovered');
		$ul.children('li').filter('[hoverSavedPos]').css('left',function () {return $(this).attr('hoverSavedPos')}).removeAttr('hoverSavedPos');
	}
	this.clickerClick = function (evt) {
		var relPos = evt.pageX - $('#pbBasePage').position().left; if ( Page.compat.ie9 ) {relPos -= parseInt($('#pbBasePage').css('margin-left'));}
		var slots = Math.round($Base.width() / ( imgWidth / 2 ));
		var clickedSlot = Math.ceil(relPos / ( $Base.width() / slots ));
		var clickedItem;
		if ( clickedSlot == 2 || clickedSlot == 3 ) {
			clickedItem = 0;
			var linkLoc = $ul.find('li:first>a').attr('href');
			if ( linkLoc ) window.location.href = linkLoc;
		} else if ( clickedSlot > 3 ) {
			clickedItem = clickedSlot - 3;
			this.clickerHoverReset();
			var complete; var i; for ( i = 0; i < clickedItem; i++ ) {
				complete = null; /*if ( i == clickedItem -1 ) complete = function () {window.location.href = $ul.children('li').filter(':first-child').find('a').attr('href');};*/
				thisSpotlight.cycle({quickly:true, complete:complete});
			}
		}
		//$debug.log('clickedItem:'+clickedItem);
	}
	this.cycle = function (params) {
		if ( ! params ) params = {queued:null,quickly:null,complete:null};
		if ( $(document).data('blurred') && $(document).data('blurred') < ( new Date().getTime() - 1000 ) ) {/*$debug.log('Spotlight cycle: doc is blurred');*/ return;}
		if ( ! params ) params = {'queued':false,'quickly':false};
		if ( ! params.queued ) {params.queued = true; $Base.queue(function () {thisSpotlight.cycle(params);}); return;}
		var duration = 300; if ( params.quickly ) duration *= 0.5;
		var $allButFirst = $ul.children().not(':first');
		var $first = $ul.children(':first');
		$allButFirst.each(function (i,e) {
			$(e).css('z-index','+=1').animate({left:'-='+( imgWidth / 2 )},{duration:duration});
		});
		$first.css('z-index','+=1').animate({left:'-='+( imgWidth * 1.5 )},{duration:duration,complete:function () {
			$(this).css('z-index','2');
			$(this).animate({left:'+='+( imgWidth )},{duration:( duration * 2 ),complete:function () {
				$ul.children('li.stackFirst').remove();
				$(this).clone().appendTo($ul).css('left', ( ( imgWidth / 2 ) * $initialItems.length )+'px').css('z-index','1');
				$(this).clone().appendTo($ul).addClass('stackFirst').css('z-index','0');
				$(this).remove();
				$Base.dequeue();
				if ( params.complete ) params.complete();
			}});
		}});
	};
	this.cycleTimerId; this.setCycleTimer = function (mode) {
		//return false;
		var interval = 4000;
		var delayFactor = 2.5;
		window.clearTimeout(this.cycleTimerId);
		switch (mode) {
			case 'suspend' :
			break;
			case 'resumeDelayed' :
				this.cycleTimerId = window.setTimeout("$('#Spotlight').data('Spotlight').setCycleTimer('continue');", ( interval * delayFactor ));
			break;
			case 'start' :
			case 'resume' :
				this.cycleTimerId = window.setTimeout("$('#Spotlight').data('Spotlight').setCycleTimer('continue');", ( interval ));
			break;
			case 'resumeFast' :
				this.cycleTimerId = window.setTimeout("$('#Spotlight').data('Spotlight').setCycleTimer('continue');", ( interval * 0.6 ));
			break;
			case 'continue' :
				thisSpotlight.cycle();
				this.cycleTimerId = window.setTimeout("$('#Spotlight').data('Spotlight').setCycleTimer('continue');", ( interval ));
			break;
		}
	};
	
	this.construct();
}
function WeSupPop(data) {
	var Page = $(document).data('Page');
	var $debug = Page.$debug;
	var thisWeSupPop = this;
	var $Base = $('#WeSupPop');
	
	var originPoints = [
		{'c':[103,156],'v':['l','t']},
		{'c':[258,234],'v':['r','t']},
		{'c':[135,324],'v':['l','b']},
		{'c':[233,147],'v':['r','t']},
		{'c':[235,307],'v':['r','b']},
		{'c':[80,244],'v':['l','t']}
	];
	var currOriginIndex = 0;
	var data = data;
	var currDataIndex = 0;
	var fadeOutLength = 1000;
	var maxItems = 5;
	var margin = {'top':0,'right':0,'bottom':0,'left':0};
	var shift = {'top':-105,'left':-75};
	
	this.construct = function () {
		this.setCycleTimer('start');
	};
	this.cycle = function () {
		if ( $(document).data('blurred') ) {thisWeSupPop.setCycleTimer('continue'); return;}
		var o = originPoints[currOriginIndex];
		
		var $item = $('<div class="item">'+data[currDataIndex]+'</div>').appendTo($Base);
		var $img = $item.find('img');
		var $a = $item.find('a').attr('title',$img.attr('alt'));
		
		$img.bind('load',function () {
			var b = {};
			b.maxR = $Base.width() - margin.right;
			b.maxB = $Base.height() - margin.bottom;
			b.setLeft = ( o.c[0] + shift.left - Math.round( $img.width() / 2 ) );
			b.setTop = ( o.c[1] + shift.top - Math.round( $img.height() / 2 ) );
			b.verRight = b.setLeft + $img.width();
			b.verBottom = b.setTop + $img.height();
			//$debug.log($(b).dump());
			// verify overflow
				$.each(o.v,function (i,v) {switch (v) {
					case 'l' : if ( b.setLeft < margin.left ) b.setLeft = margin.left; break;
					case 'r' : if ( b.verRight > b.maxR ) b.setLeft = b.setLeft - ( b.verRight - b.maxR ); break;
					case 't' : if ( b.setTop < margin.top ) b.setTop = margin.top; break;
					case 'b' : if ( b.verBottom > b.maxh ) b.setTop = b.setTop - ( b.verBottom - b.maxh ); break;
				}});
			//
			$item.css('left', b.setLeft+'px').css('top', b.setTop+'px').fadeIn('fast');
			
			if ( $Base.children().length == maxItems ) {
				$Base.children(':first').fadeOut(fadeOutLength,function () {$(this).remove();});
			} else if ( $Base.children().length > maxItems ) {
				$Base.children(':not(:last)').remove();
			}
			thisWeSupPop.setCycleTimer('continue');
		});
		$img.attr('src', $img.attr('setSrc'));
		
		// advance data pointers
			if ( currOriginIndex +1 == originPoints.length ) currOriginIndex = 0; else currOriginIndex += 1;
			if ( currDataIndex +1 == data.length ) currDataIndex = 0; else currDataIndex += 1;
		//
	};
	this.cycleTimerId; this.setCycleTimer = function (mode) {
		//return false;
		var interval = 2000;
		var delayFactor = 2.5;
		var fastFactor = 0.6;
		
		window.clearTimeout(this.cycleTimerId);
		switch (mode) {
			case 'suspend' :
			break;
			case 'resumeDelayed' :
				this.cycleTimerId = window.setTimeout("$('#WeSupPop').data('WeSupPop').cycle();", ( interval * delayFactor ));
			break;
			case 'start' :
			case 'resume' :
			case 'continue' :
				this.cycleTimerId = window.setTimeout("$('#WeSupPop').data('WeSupPop').cycle();", ( interval ));
			break;
			case 'resumeFast' :
				this.cycleTimerId = window.setTimeout("$('#WeSupPop').data('WeSupPop').cycle();", ( interval * fastFactor ));
			break;
		}
	};
	
	this.construct();
}


























