Now = new Date();

ExpireTime = new Date(Now.getTime()-(Now.getTime()%86400000)+97200000);
ExpireTime = ExpireTime.toGMTString();

function Cookie( Name )
{
	var Cookies = document.cookie.split(';');
	
	for ( var i in Cookies )
	{
		var Cookie = Cookies[i].split('=');
		
		if ( Cookie[0].replace (/^\s+/, '').replace (/\s+$/, '') == Name ) return Cookie[1];
	}
}

function Debug( Text )
{
	if ( User.Ident == '100000' ) alert(Text);
}


UserObject = function( ID, Ident, Name )
{
	this.ID = ID;
	this.Ident = Ident;
	this.Name = Name;
	this.Prices = new Array();
}

PostLoader = {
	Data: new Array(),
	Add: function( Data )
	{
		var Article = parseFloat(Data.Article);
		
		if ( this.Data[Article] ) return false;
		
		Data.Requested = false;
		
		this.Data[Article] = Data;
	},
	Request: function()
	{
		if ( typeof User == 'undefined' ) return false;
		
		if ( User.ID )
		{
			var Articles = Array();
			var Amounts = Array();
			
			for ( var i in this.Data )
			{
				if ( this.Data[i].Requested ) continue;
				
				Articles.push(this.Data[i].Article);
				Amounts.push(this.Data[i].Amount);
			}
			
			if ( Articles.length && Amounts.length )
			{
				$.post(
					System.Dir.Root+'vacos_live_request.php',
					{'Customer': User.Ident, 'Articles': Articles, 'Amounts': Amounts},
					function( Response )
					{					
						$('VacosAntwort Preisermittlung Preisermittlung_Artikel', Response).each(function()
						{
							PostLoader.SetPrice(parseFloat($(this).children('Artikelnummer').text()), $(this).children('Nettopreis').text());
						});
						
						$('VacosAntwort Terminermittlung Terminermittlung_Artikel', Response).each(function()
						{
							PostLoader.SetAvailability(parseFloat($(this).children('Artikelnummer').text()), $(this).children('LieferbareMenge').text());
						});
					},
					'xml'
				);
			}
		}
		else
		{
			for ( var i in this.Data )
			{
				this.SetPrice(i, null);
				this.SetAvailability(i, null);
			}
		}
	},
	SetPrice: function( i, Price )
	{
		$('.Price.'+this.Data[i].ID).html(User.ID ? ( parseFloat(Price) ? this.Data[i].UserPriceString.replace(/<\!--<Price>-->.*?<\!--<\/Price>-->/i, this.Format(Price)) : this.Data[i].UserPriceString ) : this.Data[i].GuestPriceString);
			
		this.Data[i].Requested = true;
	},
	SetAvailability: function( i, Amount )
	{
		var Product = this.Data[i];
		
		if ( Product.Buyable )
		{
			$('.Availability.'+Product.ID).html(User.ID ? ( parseInt(Amount) ? Product.UserAvailability1String : Product.UserAvailability0String ) : Product.GuestAvailabilityString);
			if ( User.ID ) $('.StatusImage.'+Product.ID+' img').attr('src', ( parseInt(Amount) ? Product.StatusImage1Source : Product.StatusImage0Source ));
		}
		else
		{
			$('.StatusImage.'+Product.ID+' img').attr('src', Product.Amount ? Product.StatusImage1Source : Product.StatusImage0Source);
			$('.Availability.'+Product.ID).html(Product.Amount ? Product.UserAvailability1String : Product.UserAvailability0String);
		}
	},
	Format: function( Price )
	{
		Price = Price.split('.');
		
		return '€ '+Price[0].replace(/(\d{3}(\.|$))/, '.$1').replace(/^\./, '')+','+Price[1];
	}
}

ListingObject = function( Name, ListingElement, PagerElement, Translations, SorterElement, LimiterElement )
{
	this.Name = Name;
	this.Translations = Translations;
	this.ListingElement = ListingElement;
	this.PagerElement = PagerElement;
	this.SorterElement = SorterElement;
	this.LimiterElement = LimiterElement;
	this.Page;
	this.Pages;
	this.Display = 3;
	this.Limit;
	this.Items = new Array();
	this.PageItems = new Array();
}

ListingObject.prototype.Sort = function( Term )
{
	if ( typeof Term != 'undefined' )
	{
		document.cookie = 'ListingSortTerm='+Term+'; expires='+ExpireTime;

		
		this.Page = 1;
	}
	else
	{
		var Page = parseInt(Cookie('ListingLastPage'));
		this.Page = Page && Page <= this.Pages ? Page : 1;
		
		Term = Cookie('ListingSortTerm');
		
		if ( typeof Term != 'undefined' )
		{
			$(this.SorterElement+' option').removeAttr('selected');
			$(this.SorterElement+' option[value='+Term+']').attr('selected', 'selected');
		}
	}
	
	switch (Term)
	{
		case 'ArticlenumberDesc':
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; return b.Articlenumber - a.Articlenumber });
			break;
		case 'ArticlenumberAsc':
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; return a.Articlenumber - b.Articlenumber });
			break;
		case 'PriceDesc':
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; return b.Price - a.Price });
			break;
		case 'PriceAsc':
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; return a.Price - b.Price });
			break;
		case 'NameDesc':
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; if ( (a.Name+' '+a.SubName) == (b.Name+' '+b.SubName) ) return 0; return (a.Name+' '+a.SubName) > (b.Name+' '+b.SubName) ? -1 : 1; });
			break;
		default:
			this.Items.sort(function(a, b) { if ( !a.Articlenumber ) return -1; if ( (a.Name+' '+a.SubName) == (b.Name+' '+b.SubName) ) return 0; return (a.Name+' '+a.SubName) < (b.Name+' '+b.SubName) ? -1 : 1; });
	}
	
	if ( this.Pages ) this.Build();
	
}

ListingObject.prototype.SetLimit = function( Value )
{
	if ( typeof Value != 'undefined' )
	{
		document.cookie = 'ListingLimit='+Value+'; expires='+ExpireTime;
		
		this.Limit = Value;
		this.Page = 1;
	}
	else
	{
		var Limit = Cookie('ListingLimit');
		this.Limit = Limit ? parseInt(Limit) : 10;
		
		var Page = parseInt(Cookie('ListingLastPage'));
		this.Page = Page && Page <= this.Pages ? Page : 1;
		
		$(this.LimiterElement+' option').removeAttr('selected');
		$(this.LimiterElement+' option[value='+this.Limit+']').attr('selected', 'selected');
	}
	
	this.Pages = Math.ceil(this.Items.length/this.Limit);
	
	this.Build();
}

ListingObject.prototype.SetPage = function( Value )
{
	this.Page = Value;
	
	this.Build();
}

ListingObject.prototype.PreviousPage = function()
{
	if ( this.Page > 1 )
	{
		--this.Page;
	
		this.Build();
	}
}

ListingObject.prototype.NextPage = function()
{
	if ( this.Page < this.Pages )
	{
		++this.Page;
		
		this.Build();
	}
}

ListingObject.prototype.Build = function()
{
	document.cookie = 'ListingLastPage='+this.Page+'; expires='+ExpireTime;
	
	$(this.ListingElement).empty();
	
	End = this.Page*this.Limit;
	Start = End-this.Limit;
	
	this.PageItems.length = 0;
	
	for ( var ID in this.Items )
	{
		if ( Start > ID ) continue;
			
		if ( End <= ID ) break;
		
		$(this.ListingElement).append(this.Items[ID].HTML);
		
		this.PageItems.push(ID);
	}
	
	$(this.PagerElement).css('display', 'none');
	
	if ( this.Pages > 1 )
	{
		$(this.PagerElement).empty();
		
		$(this.PagerElement).append(this.Page > 1 ? '<a class="Previous" href="javascript:'+this.Name+'.PreviousPage()">&laquo;&nbsp;'+this.Translations.Previous+'</a>' : '<span class="Previous">&laquo;&nbsp;'+this.Translations.Previous+'</span>');
		
		$(this.PagerElement).append(this.Page < this.Pages ? '<a class="Next" href="javascript:'+this.Name+'.NextPage()">'+this.Translations.Next+'&nbsp;&raquo;</a>' : '<span class="Next">'+this.Translations.Next+'&nbsp;&raquo;</span>');
		
		if ( this.Page > this.Display+1 ) $(this.PagerElement).append(this.Page > (this.Display+2) ? '<a class="Page First" href="javascript:'+this.Name+'.SetPage(1)">1</a><span class="Page">...</span>' : '<a class="Page" href="javascript:'+this.Name+'.SetPage(1)">1</a>');
		
		for ( var Number = (this.Page-this.Display); Number <= (this.Page+this.Display); ++Number )
		{
			if ( Number >= 1 && Number <= this.Pages )
			{
				$(this.PagerElement).append(Number == this.Page ? '<span class="Page Current">'+Number+'</span>' : '<a class="Page" href="javascript:'+this.Name+'.SetPage('+Number+')">'+Number+'</a>');
			}
		}
		
		if ( this.Page < this.Pages-this.Display ) $(this.PagerElement).append(this.Page < (this.Pages-this.Display-1) ? '<span class="Page">...</span><a class="Page Last" href="javascript:'+this.Name+'.SetPage('+this.Pages+')">'+this.Pages+'</a>' : '<a class="Page" href="javascript:'+this.Name+'.SetPage('+this.Pages+')">'+this.Pages+'</a>');
		
		$(this.PagerElement).css('display', 'block');
	}
}








//animation OBJECT
animation = {

	animations: new Array(),
	speed: 20,
	frame: 1,
	run: function( a, t, o, p ) {

		animations = 0;
		
		for( var id in this.animations ) ++animations;
		
		animationExists = this.animations[a+o] ? true : false;
		
		this.animations[a+o] = [a, t, o, p];
		
		if( !animations ) this.animate();
	
	},
	animate: function() {
	
		var animations = 0;
		
		for( var id in this.animations ) {
		
			if( !((this.frame*this.speed)%(this.animations[id][1]*this.speed)) ) this[this.animations[id][0]](id, this.animations[id][2], this.animations[id][3]);
			
			++animations;
			
		}
		
		if( animations ) {
			
			++this.frame;
			
			setTimeout('animation.animate()', this.speed);
			
		}
	
	},
	quit: function( id ) {
	
		delete this.animations[id];
		
	}

}

//animation: MOVE MENU
animation['ScrollMenu'] = function( id, o, p ) {
	
	var menu = document.getElementById(o);
	var destination = document.getElementById(p[0]);
	
	destination.style.display = '';
	
	var mPos = menu.scrollLeft;
	var dPos = destination.offsetLeft;
	var ratio = 0.3;
	var multiplikator = 0;
	
	if( typeof this.step == 'undefined' ) this.step = new Object();
	
	if( !this.step[o] ) this.step[o] = 0;
	
	this.step[o] = (this.step[o]*multiplikator) + (dPos - mPos) * ratio;
	
	menu.scrollLeft += mPos > dPos ? Math.floor(this.step[o]) : Math.ceil(this.step[o]);
	
	if( this.step[o] > -1 && this.step[o] < 1 && menu.scrollLeft == dPos ) {
		
		delete this.step[o];
		
		animation.quit(id);

		for( i in p[1] ) {
				
			document.getElementById('menu'+p[1][i]).style.display = 'none';
			
			document.getElementById('item'+p[1][i]).scrollIntoView(false);
			
		}
		
	}

}

//animation: FADE
animation['Fade'] = function( id, o, p ) {

	var fadeTo = p[0];
	var explorer = navigator.appName == 'Microsoft Internet Explorer' ? true : false;

	object = document.getElementById(o);

	if( typeof this.fader == 'undefined' ) this.fader = new Object();

	if( !this.fader[o] ) {

		if( object.style.display == 'none' ) {

			explorer ? object.style.filter = 'alpha(opacity=0)' : object.style.opacity = 0;

			this.fader[o] = 0;

			object.style.display = '';

		}

		else {

			explorer ? object.style.filter = 'alpha(opacity=100)' : object.style.opacity = 1;

			this.fader[o] = 100;

		}

	}

	var fadingRatio = 0.5;

	this.fader[o] = this.fader[o] + (fadeTo - this.fader[o]) * fadingRatio;

	explorer ? object.style.filter = 'alpha(opacity='+this.fader[o]+')' : object.style.opacity = this.fader[o]/100;

	if( Math.round(this.fader[o]) == fadeTo ) {

		explorer ? object.style.filter = 'alpha(opacity='+fadeTo+')' : object.style.opacity = fadeTo/100;

		if( !fadeTo ) object.style.display = 'none';
		
		if( p[1] ) p[1]();

		delete this.fader[o];

		animation.quit(id);

	}

}


function switchMenu( toMove, toOpen, toClose ) {

	animation.run('ScrollMenu', 1, toMove, [toOpen, toClose]);

}

function jumpTo( o ) {
	
	document.getElementById(o).scrollIntoView();
	
}

ImageViewer = {
	
	open: function( ID, Data ) {
		
		--ID;
		
		document.getElementById('ImageViewer').style.display = '';
		
		animation.run('Fade', 1, 'Canvas', [80, function() { ImageViewer.show( ID, Data ) }]);
		
	},
	show: function( ID, Data ) {
		
		document.body.style.cursor = 'progress';
		
		document.getElementById('Frame').style.display = '';
		document.getElementById('Frame').style.left = Math.round((document.body.offsetWidth-document.getElementById('Frame').offsetWidth)/2)+'px';
		document.getElementById('Frame').style.display = 'none';
		
		document.getElementById('LeftButton').onclick = function() { ImageViewer.change(ID < 1 ? Data.length-1 : ID-1, Data); };
		document.getElementById('RightButton').onclick = function() { ImageViewer.change(Data.length > ID+1 ? ID+1 : 0, Data); };
		
		document.getElementById('ImageDescription').innerHTML = Data[ID][3]+' ('+(ID+1)+'/'+Data.length+')';
		
		document.getElementById('Image').src = Data[ID][0];
		document.getElementById('Image').width = Data[ID][1];
		document.getElementById('Image').height = Data[ID][2];
		document.getElementById('Image').onload = function() {
		
			animation.run('Fade', 1, 'Frame', [100]);
			
			document.body.style.cursor = 'default';
			
		}
		
	},
	change: function( ID, Data ) {
		
		animation.run('Fade', 1, 'Frame', [0,function() { ImageViewer.show( ID, Data ) }]);
		
	},
	close: function() {
		
		animation.run('Fade', 1, 'Frame', [0,function() {
			
			animation.run('Fade', 1, 'Canvas', [0,function() {
				
				document.getElementById('ImageViewer').style.display = 'none';
				
			
			}])
			
		}]);
		
	}
	
}

//ENABLE PNG SUPPORT FOR IE6
function pngAlpha( Spacer ) {

	if( navigator.appVersion.indexOf('MSIE 6.0') > 0 ) {

		for( i = 0; i < document.images.length; ++i ) {
			
			var Source = document.images[i].src;
			var Width = document.images[i].width;
			var Height = document.images[i].height;
			var Extension = /^.+\.png$/i;
			
			if( Extension.test(Source) ) {

				document.images[i].src = Spacer;
				document.images[i].width = Width;
				document.images[i].height = Height;
				document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + Source + "',sizingMethod='image')";

			}

		}

	}

}

function switchTab(ContentName, UniqueName) {
	
	if ( typeof TempPrices[UniqueName] != 'undefined' )
	{
		for ( var i in TempPrices[UniqueName] ) PostLoader.Add(TempPrices[UniqueName][i]);
		
		if ( typeof User != 'undefined' ) PostLoader.Request();
	}
	
	Tabs = document.getElementById(UniqueName).parentNode.getElementsByTagName('a');
	
	for( var i in Tabs ) Tabs[i].className = Tabs[i].id == UniqueName ? 'Selected' : '';
	
	Contents = document.getElementById(UniqueName).parentNode.parentNode.getElementsByTagName('div');
	
	for( var i in Contents ) {
		
		if( Contents[i].className && Contents[i].className.indexOf(ContentName) >= 0 ) Contents[i].style.display = Contents[i].className.indexOf(UniqueName) >= 0 ? 'block' : 'none';
		
	}
	
}

function b64dec(encStr) {
	
	var bits;
	var address;
	var decOut = '';
	var i = 0;
	var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

	for(; i<encStr.length; i += 4) {
		
		bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
		
		decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
		
	}

	if( encStr.charCodeAt(i-2) == 61 ) return decOut.substring(0, decOut.length -2);
	else if( encStr.charCodeAt(i-1) == 61 ) return decOut.substring(0, decOut.length -1);
	else return decOut;
	
}
