var CCLPccslogos = {};

CCLPccslogos.debug = {
	enabled: true,
	hasConsole: !!(window.console && console.log),
	messages: [],
	log: function() {
		if (this.enabled && this.hasConsole) {
			console.log.apply(this,arguments);
		} else {
			CCLPccslogos.debug.messages.push(arguments);
		}
	},
	enable: function() {
		if (this.hasConsole) {
			while (this.messages.length) {
				console.log.apply(this,this.messages.shift());
			}
		}
		this.enabled=true;
	}
};

CCLPccslogos.ops = {
	isIE: !!(document.all),
	
	addEvent: function(elem, event, fn) {
		if (window.attachEvent)
			elem.attachEvent('on'+event, fn);
		else if (window.addEventListener) {
			elem.addEventListener(event, fn, false);
		}
		else {
			CCLPccslogos.debug.log('Failed to addEvent '+event+' to '+(elem.id || elem));
		}
	},

	setVals: function(obj, vals, addPx) {
		for (var x in vals)
			if (vals.hasOwnProperty(x))
				if (vals[x] || vals[x]===0)
					obj[x] = (addPx && parseInt(vals[x])) ? parseInt(vals[x])+'px' : vals[x];
	},
	
	createElement: function(tag, opts) {
		var elem = document.createElement(tag.toUpperCase());
		if (opts) {
			if (typeof opts.styles == 'object') {
				this.setVals(elem.style, opts.styles, true);
				delete opts.styles;
			}
			this.setVals(elem, opts);
		}
		return elem;
	}, 
	
	extend: function(ns, obj) {
		if (!ns || !obj) return null;
		for (var x in obj)
			if (obj.hasOwnProperty(x)) ns.prototype[x] = obj[x];
	},
	
	addStyles: function(styleObj, id) {
		//if (id && document.getElementById(id)) return;
		var style = document.getElementById(id) || CCLPccslogos.ops.createElement('style', {
			'type': 'text/css',
			'id': id || null
		});
		document.getElementsByTagName('head')[0].appendChild(style);
		if (CCLPccslogos.ops.isIE) {
			style.styleSheet.cssText = this.parseRules(styleObj);
		} else {
			style.appendChild(document.createTextNode(this.parseRules(styleObj)));
		}
	},

	parseRules: function(rules) {
		var toWrite = '';
		for (var x in rules)
			if (rules.hasOwnProperty(x)) {
				toWrite += x+'{';
				var styles = rules[x];
				for (var y in styles)
					if (styles.hasOwnProperty(y))
						toWrite += y+':'+styles[y]+';'
				toWrite += '}\n';
			}
		return toWrite;
	}
};

CCLPccslogos.Logo = function(specs, options) {
	this.specs = specs;
	this.options = {};
	this.over = {
		logo: false,
		ad: false
	};
	this.timeout = false;
	if (!this.specs) {
		CCLPccslogos.debug.log('CCLPccslogos.Logo constructor: No specs sent for icon.');
		return;
	}
	this.setLogo();
	if (this.specs.logo.hasHoverHtml == 'true')
		this.setInteraction('logo');
	this.setOptions(options);
	return this.logoShell;
};

CCLPccslogos.ops.extend(CCLPccslogos.Logo, {
	setLogo: function() {
		if (!this.specs || !this.specs.logo || !this.specs.logo.src) {
			CCLPccslogos.debug.log('CCLPccslogos.Logo.setLogo: Specs problem. arguments: %o', arguments);
			return;
		}
		

		
		this.logoShell = CCLPccslogos.ops.createElement('div', {
			'className': 'CCLPlogo',
			styles: {
				'position': 'relative'
			}
		});
	
		this.logo = CCLPccslogos.ops.createElement('img', {
			'src': this.specs.logo.src,
			'alt': this.specs.logo.alt || null,
			'width': this.specs.logo.width || null,
			'height': this.specs.logo.height || null,
			styles: {
				'width': this.specs.logo.width,
				'height': this.specs.logo.height
			}
			
		});
		
		this.logoShell.appendChild(this.logo);
		return this;
	},
	
	getLogoCoords: function(obj){
		this.logoLeft = this.logoTop = 0;
				
		if (obj.offsetParent) {
			do {
				this.logoLeft += obj.offsetLeft;
				this.logoTop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
	},
	
	positionHover: function() {
		this.getLogoCoords(this.logo);
		
		CCLPccslogos.ops.setVals(this.ad.style, {
			'width': this.specs.ad.width,
			'height': this.specs.ad.height
		}, true);
		
		if(this.specs.ad.position == undefined) this.specs.ad.position = 'right';
		
		if(CCLPccslogos.ops.isIE){
			if(document.compatMode == 'CSS1Compat'){//doctype is valid
				var scrollTop = document.documentElement.scrollTop;
				var scrollLeft = document.documentElement.scrollLeft;
				
				var viewportHeight = document.documentElement.clientHeight - 8;
				var viewportWidth = document.documentElement.clientWidth - 8;
			}else{//doctype is NOT valid
				var scrollTop = document.body.scrollTop;
				var scrollLeft = document.body.scrollLeft;
				
				var viewportHeight = document.body.clientHeight - 8;
				var viewportWidth = document.body.clientWidth - 8;
			}
		}else{
			var scrollTop = window.pageYOffset;
			var scrollLeft = window.pageXOffset;
			
			var viewportHeight = window.innerHeight - 8;
			var viewportWidth = window.innerWidth - 8;
				
		}
		
		var popUpHeight = parseInt(this.ad.height)+4;
		var popUpWidth = parseInt(this.ad.width)+4;
		var protocol = window.parent.document.location.protocol;
		if (protocol == 'file:') { protocol = 'http:'; }
		var arrowUrl = protocol + '//logo.cnetcontentsolutions.com/images/';
		var border = 11;

		if(this.specs.ad.position == 'top' || this.specs.ad.position == 'bottom'){
			
			// Center of pop up should match center of logo horizontally...									
			var popUpLeft = (this.logoLeft - (popUpWidth/2)) + (this.logo.width/2);
			var popUpRight = popUpLeft + popUpWidth;

			// Adjust the pop up to be horizontally positioned within the viewport...
			if (popUpRight > scrollLeft + viewportWidth) {
				popUpLeft = scrollLeft + viewportWidth - popUpWidth;
			}
			// See above, but also, if viewport is smaller than pop up, then have the right side be off the screen...
			if (popUpLeft < scrollLeft) {
				popUpLeft = scrollLeft;
			}
			// Calc the position of the background arrow...
			var width_of_arrow = 12;
			var bkgPosition = (this.logoLeft - popUpLeft - width_of_arrow) + (this.logo.width/2);

			// Vertically position the pop up...
			var position;
			var popUpTop;
			// Figure out the top and bottom location if position = 'top'...
			var popUpBottomIfTop = this.logoTop;
			var popUpTopIfTop = popUpBottomIfTop - popUpHeight - border;
			// Figure out the top and bottom location if position = 'bottom'...
			var popUpTopIfBottom = this.logoTop + this.logo.height;
			var popUpBottomIfBottom = popUpTopIfBottom + popUpHeight + border;

			// Put the pop up on 'top' if it only fits on top...
			if (popUpTopIfTop >= scrollTop && popUpBottomIfBottom > scrollTop + viewportHeight) {
				position = 'top';
				var popUpTop = popUpTopIfTop;
			// Put the pop up on 'bottom' if it only fits on bottom...
			} else if (popUpTopIfTop < scrollTop && popUpBottomIfBottom <= scrollTop + viewportHeight) {
				position = 'bottom';
				var popUpTop = popUpTopIfBottom;
			// Otherwise, honor the customer preference...
			} else if (this.specs.ad.position == 'top') {
				position = 'top';
				var popUpTop = popUpTopIfTop;
			} else if (this.specs.ad.position == 'bottom') {
				position = 'bottom';
				var popUpTop = popUpTopIfBottom;
			}

			if (position == 'top') {
				//Position top
				CCLPccslogos.ops.setVals(this.hover.style, {
					'top': popUpTop,
					'left': popUpLeft,
					'background': 'url(' + arrowUrl + this.options.arrowOptions.topArrowPic + ') '+bkgPosition+'px bottom no-repeat',
					'paddingTop': 0,
					'paddingBottom': border 
				}, true);
			}else{
				//position bottom
				CCLPccslogos.ops.setVals(this.hover.style, {
					'top': popUpTop,
					'left': popUpLeft,
					'background': 'url(' + arrowUrl +  this.options.arrowOptions.bottomArrowPic + ') '+bkgPosition+'px top no-repeat',
					'paddingTop': border,
					'paddingBottom': 0 
				}, true);
			}
			
		}else if(this.specs.ad.position == 'left' || this.specs.ad.position == 'right'){
			this.hover.style.paddingTop = '0px';
			
			// Center of pop up should match center of logo vertically...									
			var popUpTop = (this.logoTop - (popUpHeight/2)) + (this.logo.height/2);
			var popUpBottom = popUpTop + popUpHeight;

			// Adjust the pop up to be Vertically positioned within the viewport...
			if (popUpBottom > scrollTop + viewportHeight) {
				popUpTop = scrollTop + viewportHeight - popUpHeight;
			}
			// See above, but also, if viewport is smaller than pop up, then have the bottom be off the screen...
			if (popUpTop < scrollTop) {
				popUpTop = scrollTop;
			}
			// Calc the position of the background arrow...
			var height_of_arrow = 12;
			var bkgPosition = (this.logoTop - popUpTop - height_of_arrow) + (this.logo.height/2);

			// Vertically position the pop up...
			var position;
			var popUpLeft;
			// Figure out the left and right location if position = 'left'...
			var popUpRightIfLeft = this.logoLeft;
			var popUpLeftIfLeft = popUpRightIfLeft - popUpWidth - border;
			// Figure out the left and right location if position = 'right'...
			var popUpLeftIfRight = this.logoLeft + this.logo.width;
			var popUpRightIfRight = popUpLeftIfRight + popUpWidth + border;

			// Put the pop up on 'left' if it only fits on left...
			if (popUpLeftIfLeft >= scrollLeft && popUpRightIfRight > scrollLeft + viewportWidth) {
				position = 'left';
				var popUpLeft = popUpLeftIfLeft;
			// Put the pop up on 'right' if it only fits on right...
			} else if (popUpLeftIfLeft < scrollLeft && popUpRightIfRight <= scrollLeft + viewportWidth) {
				position = 'right';
				var popUpLeft = popUpLeftIfRight;
			// Otherwise, honor the customer preference...
			} else if (this.specs.ad.position == 'left') {
				position = 'left';
				var popUpLeft = popUpLeftIfLeft;
			} else if (this.specs.ad.position == 'right') {
				position = 'right';
				var popUpLeft = popUpLeftIfRight;
			}

			if (position == 'left') {
				//Position left
				CCLPccslogos.ops.setVals(this.hover.style, {
					'top': popUpTop,
					'left': popUpLeft,
					'background': 'url(' + arrowUrl +  this.options.arrowOptions.leftArrowPic + ') right '+bkgPosition+'px no-repeat',
					'paddingRight': border, 
					'paddingLeft': 0
				}, true);
			}else{
				//position right
				CCLPccslogos.ops.setVals(this.hover.style, {
					'top': popUpTop,
					'left': popUpLeft,
					'background': 'url(' + arrowUrl +  this.options.arrowOptions.rightArrowPic + ') left '+bkgPosition+'px no-repeat',
					'paddingLeft': border,
					'paddingRight': 0 
				}, true);
			}
		}
	},

	setHover: function() {

		this.hover = CCLPccslogos.ops.createElement('div', {
			'className': this.options.classOptions.hoverClass
		});

		this.hover.startTime = 0;

		var temp = CCLPccslogos.ops.createElement('div', {
			'styles': {
				'width': this.specs.ad.width,
				'height': this.specs.ad.height
			}
		});
		this.hover.appendChild(temp);
		temp.appendChild(this.ad = CCLPccslogos.ops.createElement('iframe', {
			'width': this.specs.ad.width,
			'height': this.specs.ad.height,
			'scrolling': 'auto',
			'frameBorder': '0',
			'src': this.specs.ad.src,
			styles: {
				'background':'#FFF'
			}
		}));
		document.body.appendChild(this.hover);
		this.setInteraction('hover');
		this.setInteraction('ad');
	},
	
	setInteraction: function(elem) {
		var instance = this;
		CCLPccslogos.ops.addEvent(this[elem], 'mouseover', function() {
			instance.over[elem] = true;
			if (instance.timeOut) clearTimeout(instance.timeOut);
			instance.timeOut = setTimeout(function(){instance.setDisplay.apply(instance, [elem, instance.over])}, 20);
		});
		CCLPccslogos.ops.addEvent(this[elem], 'mouseout', function() {
			instance.over[elem] = false;
			if (instance.timeOut) clearTimeout(instance.timeOut);
			instance.timeOut = setTimeout(function(){instance.setDisplay.apply(instance, [elem, instance.over])}, 20);
		});
	},
	
	setDisplay: function(elem, over) {
		var show = false;
		for (var x in this.over) {			
			if (this.over.hasOwnProperty(x)) {
				if (this.over[x]){ 
					show = true;
					break;
				}
			}
		}
		if (!show) this.hideHover();
		else (this.showHover());
	},
	
	showHover: function() {
		if (!this.hover) {
			 this.setHover();
		}
		if (this.hover.startTime == 0)
			this.hover.startTime = new Date().getTime();
		this.positionHover();
		this.hover.style.display = 'block';
	},

	hideHover: function() {
		try {
			this.hover.style.display = 'none';
			this.logo.offsetParent.style.zIndex = 0;
			var duration = 0;
			if (this.hover.startTime != 0)
				duration =  new Date().getTime() - this.hover.startTime;
			this.hover.startTime = 0;
			var hoverTimeUrl = this.specs.ad.src.replace("hoverhtml", "event") + "&type=hover&duration=" + duration;
			new Image().src = hoverTimeUrl;
		} catch(e) {
			CCLPcontainer1.debug.log('this.hover not yet created');
		}
	},
	
	

	setOptions: function(options) {
		this.options = options;
	}
});

CCLPccslogos.LogoShell = function(partData, logoData) {
	if (!partData || !logoData) {
		CCLPccslogos.debug.log('data incomplete');
		return null;
	}
	if (!partData.locationId || !document.getElementById(partData.locationId)) {
		CCLPccslogos.debug.log('target location not found');
		return null;
	}
	var shell = CCLPccslogos.ops.createElement('table', {
		'border': '0',
		'cellpadding': '0',
		'cellspacing': '0'
	});

	CCLPccslogos.ops.addStyles({
		'.CCLPpopup': {
			'position': 'absolute',
			'top': '0px',
			'left': '0px',
			'width':'auto',
			'height':'auto',
			'z-index': 10000,
			'padding': '0px',
			'margin': '0px'
		},
		'.CCLPpopup div': {
			'background':'#d7d8d7',
			'position':'relative',
			'padding': '2px'
		},
		'.CCLPpopup div iframe': {
			'border':'0',
			'margin': '0'
		},
		'.CCLPpopupWindows': {
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				'width':'auto',
				'height':'auto',
				'z-index': 10000,
				'padding': '0px',
				'margin': '0px'
			},
			'.CCLPpopupWindows div': {
				'background':'black',
				'position':'relative',
				'padding': '2px'
			},
			'.CCLPpopupWindows div iframe': {
				'border':'0',
				'margin': '0'
	}
	},'CCLPStyles');
	
	var tbody = CCLPccslogos.ops.createElement('tbody');
	shell.appendChild(tbody);
	
	var showLogos = [];
	for (var i=0; i<logoData.length; i++) {
		if (logoData[i].logo.success == 'true') showLogos.push(logoData[i]);
	}
	
	document.getElementById(partData.locationId).appendChild(shell);
	var cols = (partData.layout) ? partData.layout.split('x')[0]-0 : showLogos.length;
	var rows = (partData.layout) ? partData.layout.split('x')[1]-0 : 1;
	
	for (var y=0, t=0; y<rows; y++) {
		var tr = CCLPccslogos.ops.createElement('tr');
		tbody.appendChild(tr);
		for (var x=0; x<cols; x++) {
			var td = CCLPccslogos.ops.createElement('td');
			tr.appendChild(td);
			if (t < showLogos.length)
				if (showLogos[t].logo.type == 'windows7' || showLogos[t].logo.type == 'office2010'){
					td.appendChild(new CCLPccslogos.Logo(showLogos[t++], {
							arrowOptions : {
								rightArrowPic : 'cclpArrow_blck_right.gif',
								leftArrowPic : 'cclpArrow_blck_left.gif',
								topArrowPic : 'cclpArrow_blck_top.gif',
								bottomArrowPic : 'cclpArrow_blck_bottom.gif'
							}, 
							classOptions : {
								hoverClass : 'CCLPpopupWindows'
							}
					}));
					
				} else {
					td.appendChild(new CCLPccslogos.Logo(showLogos[t++], {
							arrowOptions : {
								rightArrowPic : 'cclpArrow_right.gif',
								leftArrowPic : 'cclpArrow_left.gif',
								topArrowPic : 'cclpArrow_top.gif',
								bottomArrowPic : 'cclpArrow_bottom.gif'
							},
							classOptions : {
								hoverClass : 'CCLPpopup'
							}
							
					}));
				}
			
		}
	}
};


CCLPccslogos.details = {partnerInfo : {"layout":"1x3","locationId":"ccslogos"},logos : [{"logo":{"hasHoverHtml":"false","height":"87","alt":"","width":"60","src":"http://logo.cnetcontentsolutions.com/s/VS00000000000000000004en.gif?customerId=1843&contractId=2587&requestLogoId=1000&responseLogoId=1007&productId=9111791&h=ef0917ea&mf=Olympus&pn=N3596592&locale=en&style=1&layout=1x3&locationId=ccslogos&sellprice_1=%c2%a3145.78&inventory_1=877&sellprice_2=%c2%a3190.95&inventory_2=1265&sellprice_3=%c2%a3219.97&inventory_3=3","type":"vista","success":"true"},"ad":{"position":"right","height":"185","width":"300","src":"http://logo.cnetcontentsolutions.com/hoverhtml/vista?&productId=9111791&customerId=1843&contractId=2587&logoType=vista&responseLogoId=1007&h=ef0917ea&mf=Olympus&pn=N3596592&locale=en&style=1&layout=1x3&locationId=ccslogos&sellprice_1=%c2%a3145.78&inventory_1=877&sellprice_2=%c2%a3190.95&inventory_2=1265&sellprice_3=%c2%a3219.97&inventory_3=3"}}]
};

CCLPccslogos.ops.addEvent(window,'load', function() {
	CCLPccslogos.LogoShell(CCLPccslogos.details.partnerInfo, CCLPccslogos.details.logos);
});