
$(document).ready( function()
	{	
		setSaving();
		setBookmark();
		setVarSelect();
		setBasketBtn();
		setZoomPic();
		setMdcBtn();
		setLongDesc();
	}
);

function setSaving()
{
	if ( $( '#saving_more2').length)
	{
		$( '#saving_more2').click( function()
		{
			if ( $( '.payment_types').hasClass( 'open'))
			{
				$( '.payment_types').removeClass( 'open');
				$( this).removeClass( 'close');
			}
			else
			{
				$( '.payment_types').addClass( 'open');
				$( this).addClass( 'close');
			}
			return false;
		});
	}
	if ( $( '#saving_more').length)
	{
		$( '#saving_more').click( function()
		{
			if ( $( '.payment_types').hasClass( 'open'))
			{
				$( '.payment_types').removeClass( 'open');
			}
			else
			{
				$( '.payment_types').addClass( 'open');
			}
			return false;
		});
	}
}

function setBookmark()
{
	if ( $('#show_bookmark').length)
	{
		$('#show_bookmark').click( function ()
		{
			if( $( '#bookmark_layer').hasClass( 'open'))
			{
				$( '#bookmark_layer').removeClass( 'open');
				$( this).removeClass( 'active');
			}
			else
			{
				$( '#bookmark_layer').addClass( 'open');
				$( this).addClass( 'active');
			}
			return false;
		});
	}

	$( '#bookmark_layer').hover( function ()
	{
		$( this).addClass( 'open');
		$('#show_bookmark').addClass( 'active');
	},
	function ()
	{
				$( this).removeClass( 'open');
				$('#show_bookmark').removeClass( 'active');
	}
	);	
}
 
function setVarSelect()
{
	$( '#var_select').change( function()
	{
		$( '#var_select').parentsUntil( '.slct').submit();
	});
}

function setBasketBtn()
{
	if ( $( '#am_minus').length)
	{
		$( '#am_minus').click( function ()
		{
			if ( $( '#am').val() != '1')
			{
				$( '#am').val( parseInt( $( '#am').val()) - 1);
			}
			calc_price();
			check_price(); 
			return false;
		} );
	}

	if ( $( '#am_plus').length)
	{
		$( '#am_plus').click( function () 
		{ 
			$( '#am').val( parseInt( $( '#am').val()) + 1); 
			calc_price();
			check_price();
			return false;
		} 
		);
	}
}

function setZoomPic()
{
	div_cnt = $( '#more_pics');
	if ( $( div_cnt).length)
	{
		$( 'a', $( div_cnt)).click( function()
				{
					idx = $( this).parent().index();
					if ( idx >= 0 && idx < 4)
					{
						$( '#zoom_link').attr( 'href', zoomPics[ idx]);
					}
				}
		);
	}
}

function setZoomHs()
{
	hs.Expander.prototype.onBeforeExpand = function (sender) 
	{ 
		
		w = 0;
		h = 0;
		cnt = sender.content; 
		img = $( '#zoom_img');
		if ( $( img).length)
		{
			w = $( img).width();
			h = $( img).height();
		}
		
		console.debug( w);
		console.debug( h);
		
		ul_cnt = $( '#other_zooms');
		if ( $( ul_cnt).length)
		{
			w += $( ul_cnt).width();
			h += $( ul_cnt).height();
		}
		console.debug( $( cnt).width());
		console.debug( $( cnt).height());
		console.debug( $( cnt));
		
		
		$( cnt).width( w);
		$( cnt).height( h);
	}
	
	hs.Expander.prototype.onAfterExpand = function (sender)
	{
		sender.reflow();
	}
}

function PreisFormat( zahl)
{
    wert = zahl.toFixed(2);
    wert = wert.replace(/\./,",");
    
    while( wert.match(/^(\d+)(\d{3}\b)/)) 
    {
        wert = wert.replace(/^(\d+)(\d{3}\b)/, RegExp.$1 + '.' + RegExp.$2);
    }
    return wert;
}


function check_price()
{
	var widthElm = document.getElementById( 'breite');
	var length = document.getElementById( 'laenge');
	
	width_val = 1;
	length_val = 1;
	
	if ( widthElm)
	{
		width_val = widthElm.value;
		width_val = width_val.replace(",", ".");
		width_val = parseFloat( width_val);
		
		if ( width_val < 0.5)
		{
			widthElm.value = '0,5';
		}
	}

	if ( length)
	{
		length_val = length.value;
		length_val = length_val.replace(",", ".");
		length_val = parseFloat( length_val);
		
		if ( length_val < 0.5)
		{
			length.value = '0,5';
		}
	}
	
	calc_price();
}

function calc_price()
{
	var price_field = document.getElementById( "price_change");

	var widthElm = document.getElementById( 'breite');
	var length = document.getElementById( 'laenge');
	var amount = document.getElementById( 'am');
	
	width_val = 1;
	length_val = 1;
	amount_val = 1;
	
	if ( widthElm)
	{
		widthElm.value = widthElm.value.replace( ".", ",");
		width_val = widthElm.value;
		width_val = width_val.replace(",", ".");
		
		if ( width_val == '')
		{
			width_val = 0;
		}
		
		width_val = parseFloat( width_val);
	}

	if ( length)
	{
		length.value = length.value.replace( ".", ",");
		length_val = length.value;
		length_val = length_val.replace(",", ".");
		
		if ( length_val == '')
		{
			length_val = 0;
		}
		
		length_val = parseFloat( length_val);
	}
	
	if ( amount)
	{
		amount_val = amount.value;
		amount_val = amount_val.replace(",", ".");
		
		if ( amount_val == '')
		{
			amount_val = 0;
		}
		
		amount_val = parseFloat( amount_val);
	}

  	if ( price_field)
  	{
  		var price = ( parseFloat( width_val)) * ( parseFloat( length_val)) * baseprice * amount_val;
  		
  		vKomma = $( '.vkomma', $( price_field));
  		nKomma = $( '.cent', $( price_field));
  		
  		price = PreisFormat( price);
  		aPrice = price.split( ',');
  		$( vKomma).text( aPrice[ 0] + ',');
  		$( nKomma).text( aPrice[ 1]);
  	}
}

function setMdcBtn()
{
	mdc_fotos_elm = $( '#mdc_fotos');
	if( $( mdc_fotos_elm).length)
	{
		$( mdc_fotos_elm).click( function()
				{
					openMediaCenter();
					return false;
				}
		);
	}
	mdc_videos_elm = $( '#mdc_videos');
	if( $( mdc_videos_elm).length)
	{
		$( mdc_videos_elm).click( function()
				{
					openMediaCenter();
					return false;
				}
		);
	}
	mdc_dok_elm = $( '#mdc_dok');
	if( $( mdc_dok_elm).length)
	{
		$( mdc_dok_elm).click( function()
				{
					openMediaCenter();
					return false;
				}
		);
	}
}

function setSliderSelections( sliderId)
{
	$( '#' + sliderId + ' .title a').click( function()
			{
				//Laden-Symbol zeigen
				$( '#' + sliderId + ' .load').css( 'display', 'block');
				
				//anid holen
				artid = $( '#' + sliderId + " input[name='anid']").attr( 'value');
				
				elm_href = $( this).attr( 'href');
				elm_href = elm_href.split( '?');
				slctid = elm_href[1].split( '=');
				slctid = slctid[1];

				//Link holen
				link = $( this).attr( 'href'); 
				link += '&fnc=reloadslider&anid=' + artid + '&sliderId=' + sliderId;
				
				//Slider aktualisieren
				$.get( link, function(data)
						{
							$( '#' + sliderId ).replaceWith( data);
							setSliderSelections( sliderId);
							setSlider( 5, sliderId, 5);
							return false;
						}
				);
				
				return false;
			}
	);
}

function setLongDesc()
{
	longd_cont = $( '#longdesc_content');
	lheight = parseInt( longd_cont.css( 'height'));
	longd_cont.css( 'height', 'auto');
	lrheight = longd_cont.height();
	longd_cont.css( 'height', '');
	if ( lrheight > lheight)
	{
		longd_btn = $( '.longdesc_cont_on');
		longd_btn_off = $( '.longdesc_cont_off');
		longd_btn.addClass( 'on');
		longd_btn.click( function()
				{
					longd_cont.addClass( 'aufklapp');
					longd_btn.removeClass( 'on')
					longd_btn_off.addClass( 'on');
					
					longd_btn_off.click( function()
							{
								longd_cont.removeClass( 'aufklapp');		
								longd_btn_off.removeClass( 'on');
								longd_btn.addClass( 'on')
							}
					);
				}
		);
		
	}
}

function resetCompareFnc( sProdId)
{
	window.setTimeout("reloadPage('" + sProdId + "')", 3000);
}

function reloadPage( sProdId)
{
	window.location.href = shopUrl + "cl=details&anid=" + sProdId;
}