var Auction = Class.create();
Auction.prototype = {
    initialize: function(changeProductUrl){
        
        this.changeProductUrl = changeProductUrl;
       
    },
	
	changeProduct : function(radio_product_id)
	{
		var product_id = $(radio_product_id).value;
		
		var url = this.changeProductUrl;
		
		url += 'product_id/' + product_id;

		new Ajax.Updater('product_name',url,{method: 'get', onComplete: function(){updateProductName();} ,onFailure: ""}); 	
		
	}
}

var AuctionUpdater = Class.create();
	AuctionUpdater.prototype = {

	initialize: function(url)
	{
		this.url = url;
	},
	
	updateInfo : function(elementId)
	{
		new Ajax.Updater(elementId,this.url,{method: 'get', onFailure: ""}); 		 		
	}
}

// This is needed for the asynchronous countdown bugfix:
var auctionTimeCounterStartDateObject = new Date();


var AuctionTimeCounter = Class.create();
AuctionTimeCounter.prototype = {
	//params now_time, end_time : seconds
	initialize: function(start_time,end_time,displayId){
		this.startServer = start_time;
                this.startClient = new Date().getTime() / 1000;
                this.endServer = end_time;
                this.endClient = this.startClient + (this.endServer - this.startServer);
                this.displayElement = document.getElementById(displayId);
                this.intervalId = null;
                this.lastTime = null;
                this.maxDerivation = 10;
	},
        run: function() {
                this.intervalId = setInterval(jQuery.proxy(this.update, this), 1000);
        },
        update: function() {
                var currentTime = new Date().getTime() / 1000;
                if (this.lastTime == null) this.lastTime = currentTime;
                var delta = this.endClient - currentTime;
                var htmlString = '';
                        var daysLeft = Math.floor(delta / 86400);
                        var hoursLeft = Math.floor((delta % 86400) / 3600);
                        var minutesLeft = Math.floor(((delta % 86400)%3600) / 60);
                        var secondsLeft = Math.floor(((delta % 86400)%3600) % 60);
                        
                        if (daysLeft > 0) {
                            htmlString += '<span class="timeleft-text">' + daysLeft + '</span>Tage ';
                        }
                        if (hoursLeft > 0 || daysLeft > 0) {
                            htmlString += '<span class="timeleft-text">' + hoursLeft + '</span>Std ';
                        }
                        
                        if (minutesLeft > 0 || hoursLeft > 0 || daysLeft > 0) {
                            if (daysLeft <= 0 && hoursLeft <= 0)
                                htmlString += '<span class="timeleft-text-end">' + minutesLeft + 'Min</span>';
                            else
                                htmlString += '<span class="timeleft-text">' + minutesLeft + '</span>Min';
                        } 
                        
                        if (daysLeft <= 0 && hoursLeft <= 0) {
                            htmlString += ' <span class="timeleft-text-end">' + secondsLeft + 'Sek</span>';
                        }
                if (currentTime >= this.endClient || (Math.abs(currentTime - this.lastTime) > this.maxDerivation) || (daysLeft <= 0 && hoursLeft <= 0 && minutesLeft <= 0 && secondsLeft <= 0))
                {
                        htmlString = '<span class="timeleft-text-end">0Sek</span>';
                        clearInterval(this.intervalId);
                        this.displayElement.innerHTML = htmlString;
                        window.location.reload(true);
                }
                this.displayElement.innerHTML = htmlString;
                this.lastTime = currentTime;
        }
}

function updateProductName()
{
	$('product_name').value = $('newproduct_name').value;
}

function validNumeric(inputtext)
{
	var text = inputtext.value;
	var Char;
	var newtext = '';
	var newtext1 = '';
	var newtext2 = '';
	var j =0;
	var i;var is_decimal = false;
	
	for (i = 0; i < text.length ; i++) 
	{ 
		Char = text.charAt(i); 
		if( (Char != '0') || (newtext.length >0) )
		{
			if((! isNaN(Char) && Char != ' ') || (Char == ','))
			{
				newtext +=  Char;
			}
		}
    }
	
	for (i = (newtext.length -1) ; i >=0; i--) 
	{ 
		Char = newtext.charAt(i); 

		if( Char != ',' || ((Char == ',') && (is_decimal == false) && (newtext1.length > 0)) )
		{
			newtext1 =  Char + newtext1;
			if(Char == ',')
				is_decimal = true;
		}
    }	
	
	var begin = newtext1.length -1 ;
	var varend = '';
	var k = newtext1.indexOf(',');
	
	if(k != -1)
	{
		for(i = k;i < newtext.length;i++)
			varend +=  newtext1.charAt(i);
		
		begin = k-1;
	}
	
	j = 0;
	for(i = begin ;i>=0;i--)
	{
		Char = newtext1.charAt(i);
		newtext2 = Char + newtext2;
		j++;
//		if((j==3) && (i>0))
//		{
//			newtext2 = ',' + newtext2;
//			j = 0;
//		}
	}
	
	if(newtext2.length == 0)
		newtext2 = '0';
		
	newtext2 += varend;
	
	inputtext.value = newtext2;
}

function updateAuction(elementId,url)
{
	var auctionUpdater = new AuctionUpdater(url);
	
	auctionUpdater.updateInfo(elementId);
	
	delete auctionUpdater;
	
	window.setTimeout("updateAuction('"+ elementId +"','"+ url +"')",8000);
}

	function checkBidPrice()
	{
		var bid_price = $('bid_price').value;
		var newbid_price ='';
		var Char;
		var i;

    if ( bid_price == '' )
      return false;
	
	for(i = 0 ;i < bid_price.length ;i ++)
	{
		Char = bid_price.charAt(i); 

		if(Char != '.')
		{
      if ( Char == ',' )
        newbid_price += '.';
      else
        newbid_price += Char;
		}
	}	
		
		var price = parseFloat(newbid_price);
                var min_next_price = $('min_next_price').value;
                var max_next_price = $('max_next_price').value;

                max_next_price = parseFloat(max_next_price);
                min_next_price = parseFloat(min_next_price);

                if(price < min_next_price)
                {
                        alert($('min_next_price_nonce').value);
                        return false;
                }
                if(max_next_price != 0)
                if(price > max_next_price)
                {
                        alert($('max_next_price_nonce').value);				
                        return false;
                }
                return true;	
	}

	function setBackground(elementId,productId)
	{
		var codeColorId = 'codecolor'+ productId;
		var codeColor = $(codeColorId).value;
		var full_elementId = elementId + productId;
		
		codeColor = parseInt(codeColor);
		if(codeColor <255)
			codeColor +=4;
		
		$(codeColorId).value = codeColor;
		$(full_elementId).style.background = 'rgb(255,'+ codeColor +','+ codeColor +')';
		
		window.setTimeout("setBackground('"+ elementId +"','"+ productId +"')",10);
	}

//Adminhtml	
function setAuctionCheckboxId(url)
{
	var radios = document.getElementsByClassName('radio');
			
	for( var mi=0;mi<radios.length ;mi++)
	{
		radios[mi].name = 'candidate_product_id';
		radios[mi].id = 'candidate_product_id_' + radios[mi].value;
		
		if($('is_seted') == null)
		{
			Event.observe(radios[mi], 'click', function(event){ 		
				setproduct(url);
			});
		}
	}			
	
	if($('filter_entity_id') != null)
	{
		$('filter_entity_id').innerHTML = '<input type="hidden" id="is_seted" value="1" />';
	}
	
	window.setTimeout("setAuctionCheckboxId('"+ url +"')",1000); 
}

function setproduct(url)
{
	var radios = document.getElementsByClassName('radio');
	
	var auction = new Auction(url);
	
	for( var mi=0;mi<radios.length ;mi++)
	{
		if(radios[mi].checked == true)
		{
			auction.changeProduct(radios[mi].id);
		}
	}
}	
//End Adminhtml	


