TQ = {
	version: "1.0",

	init: function() {
	},

	log: function(variable) {
		if ( window.console && window.console.log ) {
			console.log( variable );
		}
	},

	htmlEscape: function(value){
		var ret = "";

		if( value && value != undefined ) {
			ret = value;
			ret = ret.replace(/&/g,	'&amp;').
			ret = ret.replace(/</g,	'&lt;');
			ret = ret.replace(/>/g,	'&gt;');
			ret = ret.replace(/'/g,	'&#039;');
			ret = ret.replace(/"/g,	'&quot;');
		}
	   return ret;
	},

	stopPropagation: function(event) {
		if(!event) {
			return false;
		}

		try {
			event.stopPropagation();
			event.preventDefault();
		} catch(ex) {}

		try {
			event.cancelBubble = true;
			event.returnValue = false;
		} catch(ex) {}
	}

};

/******************************************************************************
** Page handling methods
******************************************************************************/

TQ.page = {
	init: function() {
	},

	go: function(url, event) {
		if(!url || url == "") {
			TQ.stopPropagation(event);
			return;
		}

		window.location = url;
		TQ.stopPropagation(event);
	},

	reload: function() {
		var url = window.location.href;

		if( url.search(/\?/) != -1 ) {
			url = url + "&nc="+Math.floor(new Date().getTime()/1000);
		} else {
			url = url + "?nc="+Math.floor(new Date().getTime()/1000);
		}

		window.location.href=url;
	}
};

/******************************************************************************
** News fader
******************************************************************************/

t3NewsFader = {
	parent:	{},
	newsList: {},
	newsCount: 0,
	current: 0,

	fadeTime: 5000,
	fadeTimeout: false,

	init: function(element, time) {
		this.parent		= element;
		this.newsList	= element.find(".news-latest-item");
		this.newsCount	= this.newsList.length;

		if( time != undefined && time != false && time >= 0 ) {
			this.fadeTime = time;
		}

		if(this.newsCount == 1) {
			this.next();
		} else if( this.newsCount >= 1) {
			this.start();
		}
	},

	start: function() {
		jQuery(this.newsList[0]).fadeIn();
		this.current = 0;

		var scope = this;

		this.parent.mouseover(function() {
			scope.timerStop();
		});

		this.parent.mouseout(function() {
			scope.timerStart();
		});

		this.timerStart();
	},

	timerStop: function() {
		try {
			if( this.fadeTimeout ) {
				window.clearTimeout(this.fadeTimeout);
			}
		} catch(e) {}
	},

	timerStart: function() {
		this.timerStop();

		var scope = this;

		var callback = function() {
			scope.timerStart();
			scope.next();
		};

		this.fadeTimeout = window.setTimeout(callback, this.fadeTime);
	},

	next: function() {
		var old = this.current;

		this.current++;

		if(this.current >= this.newsCount) {
			this.current = 0;
		}

		jQuery(this.newsList[ old ]).fadeOut();
		jQuery(this.newsList[ this.current ]).fadeIn();
	}
}

/******************************************************************************
** Chart
******************************************************************************/

TQ.chart = {};
TQ.chart.teqtest = {
	init: function(chartElId, userElId, infrastructureElId, langKey) {

		jQuery(document).ready(function($) {
			var userElement				= $("#"+userElId);
			var infrastructureElement	= $("#"+infrastructureElId);

			var callback = function() {
				var licenseUsers			= parseInt( userElement.val() );
				var infrastructurePrice		= parseInt( infrastructureElement.val() );

				if( !(infrastructurePrice >= 0) && infrastructurePrice >= 1000000 ) {
					infrastructurePrice = 0;
				}

				if( licenseUsers >= 0 ) {
					TQ.chart.teqtest.build(chartElId, licenseUsers, infrastructurePrice, langKey);
				}
			};

			var licenseUsers			= userElement.val();
			var infrastructurePrice		= infrastructureElement.val();

			userElement.change(callback);
			infrastructureElement.change(callback);

			callback();
		});
	},

	build: function(element, licenseUsers, infrastructurePrice, langKey) {
		jQuery(document).ready(function($) {
			$.jqplot.config.enablePlugins	= true;

			var langTokenList = {};

			switch(langKey) {
				case "de":
					langTokenList.labelLicenseInfrastrcuture	= "Lizenz + Infrastruktur";
					langTokenList.labelLicense					= "Lizenz";
					langTokenList.labelSaas						= "SaaS";
					langTokenList.labelMonths					= "Monate";
					langTokenList.labelTco						= "Total cost of ownership";
					break;

				case "en":
					langTokenList.labelLicenseInfrastrcuture	= "License + Infrastructure";
					langTokenList.labelLicense					= "License";
					langTokenList.labelSaas						= "SaaS";
					langTokenList.labelMonths					= "Months";
					langTokenList.labelTco						= "Total cost of ownership";
					break;
			}


			var licenseMonths				= 36;

			var saasPriceSetup				= 250;
			var saasPricePerUser			= 40;

			var tickListX					= [];
			var tickIntervalY				= false;
			var lineBreakEven				= [];
			var lineSaas					= [];
			var lineLicense					= [];
			var lineLicenseInfrastrucuture	= [];

			var licensePricingList = {
				5: {
					setup: 				4500,
					support:			900
				},
				10: {
					setup: 				7500,
					support:			1500
				},
				20: {
					setup: 				9000,
					support:			1800
				},
				30: {
					setup: 				10500,
					support:			2100
				},
				40: {
					setup: 				12000,
					support:			2400
				},
				50: {
					setup: 				13500,
					support:			2700
				},
				60: {
					setup: 				15000,
					support:			3000
				},
				70: {
					setup: 				16500,
					support:			3300
				},
				80: {
					setup: 				18000,
					support:			3600
				},
				90: {
					setup: 				19500,
					support:			3900
				},
				100: {
					setup: 				21000,
					support:			4200
				}
			};

			var saasPricing		= {
				setup:		saasPriceSetup,
				monthly:	saasPricePerUser * licenseUsers
			};

			var licensePricing	= licensePricingList[licenseUsers];
			var breakEvenMonth = -1;
			var maxPrice		= 0;

			for(var i=0;i<=licenseMonths; i++) {
				var supportYear				= Math.floor(i/12);

				var saasPriceCur			= saasPricing.setup + (saasPricing.monthly * i);
				var licensePriceCur			= licensePricing.setup +  ( licensePricing.support * supportYear);
				var licenseInfraPriceCur	= licensePricing.setup +  ( licensePricing.support * supportYear) + ( infrastructurePrice * i);

				// calc saasPricing
				lineSaas.push([i, saasPriceCur] );
				lineLicense.push( [i, licensePriceCur ] );
				lineLicenseInfrastrucuture.push( [i,licenseInfraPriceCur] );

				if( breakEvenMonth == -1 && licenseInfraPriceCur <= saasPriceCur ) {
					breakEvenMonth = i;

					// recalc max months
					licenseMonths = Math.ceil(breakEvenMonth / 12) * 12;
				}

				tickListX.push(i);

				if( maxPrice <= saasPriceCur ) {
					maxPrice = saasPriceCur;
				}

				if( maxPrice <= licensePriceCur ) {
					maxPrice = licensePriceCur;
				}

				if( maxPrice <= licenseInfraPriceCur ) {
					maxPrice = licenseInfraPriceCur;
				}
			}

			switch(true) {
				case(maxPrice <= 60000):
					tickIntervalY = 10000;
					break;

				case(maxPrice <= 100000):
					tickIntervalY = 20000;
					break;

				case(maxPrice <= 1000000):
					tickIntervalY = 100000;
					break;

				case(maxPrice <= 10000000):
					tickIntervalY = 1000000;
					break;

				case(maxPrice <= 100000000):
					tickIntervalY = 10000000;
					break;

				default:
					tickIntervalY = 10000;
					break;
			}

			var maxPrice	= Math.ceil( maxPrice / tickIntervalY ) * tickIntervalY;

			if( breakEvenMonth >= 0 ) {
				lineBreakEven = [ [breakEvenMonth,0], [breakEvenMonth,maxPrice]  ];
			}

			// clear chart
			$("#"+element).html('');

			var plot = $.jqplot(element, [lineLicenseInfrastrucuture,lineLicense,lineSaas,lineBreakEven], {
				legend: {
					show: true,
					location: 'nw'
				},
				title: '',
				seriesDefaults: {
					rendererOptions: {
						highlightMouseOver	: false,
						highlightMouseDown	: false,
						highlightColor		: null
					}
				},
				series: [
					{
						label		: langTokenList.labelLicenseInfrastrcuture,
						fill		: true,
						//fillAlpha	: 0.8,
						color		: '#FFBD3F'
					},{
						label		: langTokenList.labelLicense,
						fill		: true,
						//fillAlpha	: 0.8,
						color		: '#794435'
					},{
						label		: langTokenList.labelSaas,
						fill		: true,
						fillAlpha	: 0.8,
						color		: '#6FB6F9'
					},{
						label		: false,
						lineWidth	: 3,
						color		: '#6D1B00',
						showMarker	: false,
						showLabel	: false
					}],
				axes: {
					xaxis: {
						label		: langTokenList.labelMonths,
						ticks		: tickListX,
						tickOptions	: {
							formatString	: '%s',
							showGridline	: true
						}
					},
					yaxis: {
						label			: langTokenList.labelTco,
						labelRenderer	: $.jqplot.CanvasAxisLabelRenderer,
						min				: 0,
						max				: maxPrice,
						tickInterval	: tickIntervalY,
						tickOptions		: {
							formatString	: '%s',
							showGridline	: true
						}
					}
				}
			});
		});
	}
};



/******************************************************************************
** Init
******************************************************************************/

jQuery(document).ready(function($) {
	$("input.tqmailform-date").datepicker();

	if ( !($.browser.msie && $.browser.version == "6.0")  ) {
		$("#pageOverall").find("a.linkToolip").tooltip({
			effect        : 'slide',
			relative      : false,
			position      : 'top center',
			tipClass      : 'tooltip tooltipTop',
			onBeforeShow  : function(e,pos) {
				var tipEl		= this.getTip();
				var tipContent = tipEl.text();
				tipContent = '<div class="tooltipContentWrapper"><div class="tooltipContent"><div class="tooltipContentTop"><div class="tooltipContentTopLeft"></div><div class="tooltipContentTopMiddle"></div><div class="tooltipContentTopRight"></div></div><div class="tooltipContentBody">'+TQ.htmlEscape( tipContent )+'</div><div class="tooltipContentBottom"><div class="tooltipContentBottomLeft"></div><div class="tooltipContentBottomMiddle"></div><div class="tooltipContentBottomRight"></div></div></div></div>';
				tipEl.html( tipContent );
			}
		});

		$("#pageHeaderNavigation").find("li").tooltip({
			effect        : 'slide',
			relative      : false,
			position      : 'bottom center',
			tipClass      : 'tooltip tooltipBottom',
			offset        : [10, 0],
			onBeforeShow  : function(e,pos) {
				var tipEl		= this.getTip();
				var tipContent = tipEl.text();
				tipContent = '<div class="tooltipContentWrapper"><div class="tooltipContent"><div class="tooltipContentTop"><div class="tooltipContentTopLeft"></div><div class="tooltipContentTopMiddle"></div><div class="tooltipContentTopRight"></div></div><div class="tooltipContentBody">'+TQ.htmlEscape( tipContent )+'</div><div class="tooltipContentBottom"><div class="tooltipContentBottomLeft"></div><div class="tooltipContentBottomMiddle"></div><div class="tooltipContentBottomRight"></div></div></div></div>';
				tipEl.html( tipContent );
			}
		});

		$("#pageBody").find("abbr[title]").tooltip({
			effect        : 'slide',
			relative      : false,
			position      : 'top center',
			tipClass      : 'tooltip tooltipTop',
			onBeforeShow  : function(e,pos) {
				var tipEl		= this.getTip();
				var tipContent = tipEl.text();
				tipContent = '<div class="tooltipContentWrapper"><div class="tooltipContent"><div class="tooltipContentTop"><div class="tooltipContentTopLeft"></div><div class="tooltipContentTopMiddle"></div><div class="tooltipContentTopRight"></div></div><div class="tooltipContentBody">'+TQ.htmlEscape( tipContent )+'</div><div class="tooltipContentBottom"><div class="tooltipContentBottomLeft"></div><div class="tooltipContentBottomMiddle"></div><div class="tooltipContentBottomRight"></div></div></div></div>';
				tipEl.html( tipContent );
			}
		});

		$("#pageBody").find("acronym[title]").tooltip({
			effect        : 'slide',
			relative      : false,
			position      : 'top center',
			tipClass      : 'tooltip tooltipTop',
			onBeforeShow  : function(e,pos) {
				var tipEl		= this.getTip();
				var tipContent = tipEl.text();
				tipContent = '<div class="tooltipContentWrapper"><div class="tooltipContent"><div class="tooltipContentTop"><div class="tooltipContentTopLeft"></div><div class="tooltipContentTopMiddle"></div><div class="tooltipContentTopRight"></div></div><div class="tooltipContentBody">'+TQ.htmlEscape( tipContent )+'</div><div class="tooltipContentBottom"><div class="tooltipContentBottomLeft"></div><div class="tooltipContentBottomMiddle"></div><div class="tooltipContentBottomRight"></div></div></div></div>';
				tipEl.html( tipContent );
			}
		});

		$("input.defaultText").focus(function(srcc) {
			if ($(this).val() == $(this)[0].title) {
				$(this).removeClass("defaultTextActive");
				$(this).val("");
			}
		});

		$("input.defaultText").blur(function() {
			if ($(this).val() == "") {
				$(this).addClass("defaultTextActive");
				$(this).val($(this)[0].title);
			}
		});

		$("input.defaultText").blur();

	}
});



