$(document).ready(function(){

	var oxidArray = {};

	function setPrices( targetForm, pricesLiteral )
	{
		$( targetForm ).find( "div.ohne-mwst span.price" ).text( pricesLiteral.FPrice );
		$( targetForm ).find( "div.mit-mwst span.price" ).text( pricesLiteral.FPriceWithTax );
		$( targetForm ).find( "div.uvp span.price" ).text( pricesLiteral.FPriceRecommendedRetailsale );
		$( targetForm ).find( "span.saved" ).text( pricesLiteral.FPricePercentageSaved + ' %');
	}

	$( "form select" ).change(
		function()
		{
			var oxid = $( this ).find( "option:selected" ).attr( 'value' );

			var parentForm = $( this ).closest( "form" );
			
			// lazyloaded price is defined
			if ( typeof oxidArray[oxid] != 'undefined' ) {
				setPrices( parentForm, oxidArray[oxid] );
			// load via ajax								
			} else {
				$.ajax(
					{
						type: 'POST',
						url: '/modules/variantenwahl/json.php',
						data: 
							{
								'oxid': oxid
							},
						dataType: 'json',
						success: function( json )
							{ 
								setPrices( parentForm, json[oxid] );
							}
					}
				);			
			}
		}
	);

	var oxid = new Array(); 

	$( "form select[name=aid] option" ).each(
		function( index, el )
		{
			oxid.push( $(el).attr( 'value' ) );
		}
	);

	$.ajax(
		{
			type: 'POST',
			url: '/modules/variantenwahl/json.php',
			data: { 'oxid': oxid },
			dataType: 'json',
			success: function( json )
				{ 
					oxidArray = json;		
				}
		}
	);	
	
	$( "a.printview" ).click(function() { 
		var variantId = $( 'option:selected ').attr( 'value' ); 
		if ( typeof variantId != 'undefined' ) {
 			commonFunctions.openPopup( 780, 480, $(this).attr( "rel" ) + '?anid=' +variantId );
		} else {
 			commonFunctions.openPopup( 780, 480, $(this).attr( "href" ) );
 		}
		return false;
	} );


	$( "a.basket-printview" ).click(function() { 
		commonFunctions.openPopup( 780, 480, $(this).attr( "href" ) );
 		return false;
	} );



	// init ticker
     // 	$( '.box.news-ticker ul li:eq(2)' ).addClass( 'last' );
     // 	$( '.box.news-ticker ul li:lt(3)' ).show();
    $( 'div.newsticker ul li:gt(0)' ).hide();

	function newsticker()
	{
   		last = $('div.newsticker ul li:first' ).hide( 'fast' ).remove();  
   		$( 'div.newsticker ul li:first' ).show( 'fast' ); 
	    $( 'div.newsticker ul' ).append( last );
	}

	if ( $( "div.newsticker" ).length ) {
		interval = setInterval( newsticker, 2000 );
	}
	
});






var commonFunctions = {

	openPopup :function( width, height, href )
	{
		var height = height - 20;
		var x = 0;
		var y = 0;

		if (width < screen.availWidth || height < screen.availHeight) {

			x = ( screen.availWidth - width - 12 ) / 2;
			y = ( screen.availHeight - height - 104 ) / 2;

			if ( window.opera ) {
				y = 0; // Opera positioniert unter den Symbolleisten
			} 

			if ( x < 0 || y < 0 ) {
				x = 0;
				y = 0;
			} else {
				parameter = "width=" + width + ",height=" + height + ",";
			}

		}
		
		// arguments-literal for open.window
		var args = {
			'left' : x,
			'top' : y,
			'menubar' : 'yes',
			'location' : 'yes',
			'toolbar' : 'no',
			'status' : 'no',
			'resizable' :'yes',
			'scrollbars' : 'yes'
		}

		var windowInstanceArgs = '';
		for ( var key in args ) {
			if( 0 !== windowInstanceArgs.length ) {
				windowInstanceArgs += ',';
			}
			windowInstanceArgs += key + '=' + args[key];
		}

		var windowInstance = window.open( href, "popup", windowInstanceArgs );

		if ( windowInstance ) {
			windowInstance.focus();
		}

		return !windowInstance;
	}

}


