
$(document).ready( function()
	{	
		setLiveshopping();
	}
);

function setLiveshopping()
{
	end_time_elm = $( '#angb_end_time .zeit');
	end_time = $( end_time_elm).text();
	
	date_arr = end_time.split( ' ');
	
	date = date_arr[ 0].split( '.');
	time_arr = date_arr[ 1].split( ':');

	var enddatum = new Date();
	enddatum.setDate( date[ 0]);
	enddatum.setMonth( parseInt(date[ 1], 10) - 1);
	enddatum.setFullYear( date[ 2]);
	enddatum.setHours( parseInt(time_arr[ 0], 10));
	enddatum.setMinutes( parseInt( time_arr[ 1], 10) );
	enddatum.setSeconds( time_arr[ 2]);
	
	var_layout = '<span class="digit hour">{hnn}</span> ' + txt_h + '<br />' + 
                        '<span class="digit mins">{mnn}</span> ' + txt_m + '<br />' +
                        '<span class="digit sec">{snn}</span> ' + txt_s;
						
	$( '#ticker').countdown({until: enddatum, layout: var_layout, format: 'HMS', serverSync: serverTime, onTick: setStatus, onExpiry: resetLiveshopping });
}

function serverTime()
{
	var server_time = $( '#akt_server_time');
	server_time = server_time.val();
	return new Date( server_time);
}

function resetLiveshopping()
{
	$('#liveshopping').append( '<div class="disable"><span>ANGEBOT BEENDET!</span></div>');
}

oldMin = null;
function setStatus( mins)
{
	if ( oldMin != 0 && !oldMin)
	{
		oldMin = mins[5];
	}
	newMin = mins[5];
	if ( newMin < oldMin)
	{
		oldMin = newMin;
		
		oldAvailText = $( '#angb_avail').text();
		//$( '#angb_avail').load( shopUrl + 'cl=dd_liveshopping_status');
		$.get( shopUrl + 'cl=dd_liveshopping_status', '', function(responseText, textStatus, xhr)
				{
					if( textStatus != 'success' || xhr.status != '200')
					{
						//alert( textStatus);
						$( '#angb_avail').html( oldAvailText);
					}
					else
					{
						$( '#angb_avail').html( responseText);
					}
				}
		);
		
	}
	
	newSec = mins[6];
	
	if ( newSec % 10 == 1)
	{
		old_preis = $( '#dd_liveshopping_preis').text();
		dd_a = "";
		dd_x = "";
		dd_x1 = "";
		
		$.get( shopUrl + 'cl=dd_liveshopping_preis', '', function(responseText, textStatus, xhr)
				{
					dd_a = textStatus;
					dd_x = xhr.status;
					dd_x1 = xhr.statusText;
					if( textStatus != 'success' || xhr.status != '200')
					{
						//alert( textStatus);
						$( '#dd_liveshopping_preis').html( old_preis);
					}
					else
					{
						$( '#dd_liveshopping_preis').html( responseText);
					}
				}
		);
		
		/*$( '#dd_liveshopping_preis').load( shopUrl + 'cl=dd_liveshopping_preis', '', function(responseText, textStatus, xhr)
				{
					dd_a = textStatus;
					dd_x = xhr.status;
					dd_x1 = xhr.statusText;
					if( textStatus != 'success' || xhr.status != '200')
					{
						alert( textStatus);
						$( '#dd_liveshopping_preis').html( old_preis);
					}
				}
		);*/
	}
}

function setInfoLink()
{
	if( oLink = $( '#liveshopping_info'))
	{
		oLink.click( function()
				{
					if( oLiveLayer = $( '#liveshopping_info_layer'))
					{
						$( oLiveLayer).addClass( 'show');
						$( oLiveLayer).addClass( 'load');
						sUrl = oLink.attr( 'href');
						$.get( sUrl, function( data)
								{
									$( '.info_layer_content', oLiveLayer).html( data);
									$( oLiveLayer).removeClass( 'load');
								}
						);
						
					}
					return false;
				}
		);
	}
}

function closeInfoLayer()
{
	if( oLiveLayer = $( '#liveshopping_info_layer'))
	{
		$( oLiveLayer).removeClass( 'show');
	}
}