$(document).ready(function(){
    if($.browser.msie){
        //Configuring ajax
        $.ajaxSetup({
            cache: false
        });
    }

    fetchTimestamp = function(){
        return parseInt($('#timestamp').text());
    };

    // Default frequency for auction updating and time sync
    var auctionUpdateTime = 10;

    if($('#auctionupdatefrequency').text()){
        auctionUpdateTime = parseInt($('#auctionupdatefrequency').text());
		
    }

    // Convert it to milisecond
    auctionUpdateTime = auctionUpdateTime * 1000;
    
	  
    // Variable to hold auction data
    var auctions = '';

    // Collecting auction data, the layer id and auction id
    $('.auction-item').each(function(){
        var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');

        if($('#' + auctionId + ' .countdown').length){
            if($('#' + auctionId + ' .countdown').attr('title') > fetchTimestamp()){
                auctions = auctions + auctionId + '=' + auctionTitle + '&';
            }
        }
    });
	
    // Main countdown for updating auction and flashing
    setInterval(function(){
        if(auctions){
            if(document.getElementById('bidHistoryTable')){
                getStatusUrl = 'getstatus.php?type=uk';
            }else{
                getStatusUrl = 'getstatus.php';
            }
            
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'POST',
                data: auctions,
                success: function(data){
                    $.each(data, function(i, item){
                        var auctionBlock        = $('#' + item.Auction.element);
                        var auctionEndTime      = $('#' + item.Auction.element + ' .countdown').attr('title');
                        var auctionLatestBidder = $('#' + item.Auction.element + ' .bid-bidder').text();
						//alert(auctionLatestBidder);
                        var auctionCountdown    = $('#' + item.Auction.element + ' .countdown');
						 
					    if(item.Auction.pagereferce=='t')
						{
							window.location.href=location.href;
						}
                        if(auctionLatestBidder != item.LastBid.username){
                            $('#' + item.Auction.element + ' .bid-bidder').html(item.LastBid.username);
                        }
						$('#bidavailablenow').text('');
						$('#bidavailablenow').html(""+item.Auction.bidavailablenow);
						
                       
                        if(auctionEndTime != item.Auction.end_time){
                            auctionCountdown.attr('title', item.Auction.end_time);
							$('#' + item.Auction.element + ' .bid-price').html(item.Auction.start_price);
                            $('#' + item.Auction.element + ' .bid-savings-percentage').html(item.Auction.savings.percentage);
                            $('#' + item.Auction.element + ' .bid-savings-price').html(item.Auction.savings.price);
							$('#' + item.Auction.element + ' .youpay').html(item.Auction.savings.youpay);
							$('#' + item.Auction.element + ' .savingfinal').html(item.Auction.savings.savingfinal);
							
							
                            if(document.getElementById('bidHistoryTable')){
                                if($('#bidHistoryTable p').text()){
                                    $('#bidHistoryTable p').remove();
                                }

                                $('#bidHistoryTable tbody tr').remove();

                                $.each(item.Histories, function(n, tRow){
                                    var row = '<tr><td style=padding-left:20px>' + tRow.Bid.created + '</td><td style=padding-left:15px>' + tRow.User.username + '</td><td>' + tRow.Bid.description + '</td></tr>';

                                    $('#bidHistoryTable tbody').append(row);
                                });
                            }

                           if(auctionLatestBidder != 'Latest Bidder'){
							    
							 $('#' + item.Auction.element + ' .bid-price').animate( { backgroundColor: '#68b121' }, 500).animate( { backgroundColor: '#EEFFCE' }, 1000);
                            }
                        }
				       
                        
					//
                     
						if(item.Auction.peak_only == 1 && item.Auction.isPeakNow == 0){
                            auctionCountdown.text(item.Auction.end_time_string);
                         
                            $('#' + item.Auction.element + ' .bid-button a').hide();
                            if($('#' + item.Auction.element + ' .bid-button p').text() == ''){
								$('#' + item.Auction.element + ' .bid-button p').text('');
								 $('#' + item.Auction.element + ' .bid-button').append('<p style="margin:0"><img src="image/paused.png" border="0" /></p>');
                            }
                        }else{
							 
                            if(item.Auction.end_time - item.Auction.serverTimestamp > 0){
								 if(item.Auction.end_time - item.Auction.serverTimestamp < 10)
								 {
								   if(item.LastBid.autobidder=='t')
					              {
						
                        $.ajax({ type: "POST",
		  					    url: "ukraj.php?id="+item.Auction.id+"&time="+item.Auction.serverTimestamp,
		  						dataType:"json",
									
								success:function(autoval){
								
								
									 $.each(autoval,function(nos,valauto){;
									  //  alert(valauto);
									 // alert(valauto.Autobider.start_price);
									  $('#' + item.Auction.element + ' .bid-price').html(valauto);
									})
									
									
									}});
					  }
								  auctionCountdown.text('');
                                  auctionCountdown.append('<font color="#68b121">'+item.Auction.end_time_string+'</font>');
								 }
								else
								  auctionCountdown.text(item.Auction.end_time_string);
                            }
							   
                              if($('#' + item.Auction.element + ' .bid-button p').append()){
                               $('#' + item.Auction.element + ' .bid-button a').show();
                               $('#' + item.Auction.element + ' .bid-button p').remove();
                           }
                        }
                        if(item.Auction.t2>item.Auction.t3)
						{
							
						   $('#' + item.Auction.element + ' .bid-button a').hide();
                           if($('#' + item.Auction.element + ' .bid-button p').text() == ''){
								$('#' + item.Auction.element + ' .bid-button p').text('');
								 $('#' + item.Auction.element + ' .bid-button').append('<p><img src="image/nobid.gif" border="0" /></p>');
                            }
                           
                        } 
						
						
                        if(item.Auction.end_time - item.Auction.serverTimestamp <=0 && item.Auction.peak_only==0 ){
						   
							auctionCountdown.text('');
							
                            auctionCountdown.append('<font color="#68b121">Ended</font>');
							$('#' + item.Auction.element + ' .bid-button a').hide();
                            $('#' + item.Auction.element + ' .bid-button').text('');
						$('#' + item.Auction.element + ' .bid-button').append('<img src="image/ended.gif"  border="0" />');
                          $('#' + item.Auction.element + ' .bid-bookbidbutler').hide();
							 window.location.href="update.php?id="+item.Auction.id+"&time="+item.Auction.serverTimestamp; 
                            }
                        						
                    });
                }
            });
        }
    }, auctionUpdateTime);

    // Function for bidding
    $('.bid-button-link').click(function(){
        var auctionId         = $(this).parent().parent().parent().attr('title');
        var bidMessage        = $(this).parent().parent().parent().children('.bid-message');
        var bidButton         = $(this).parent().parent().children('.bid-button');
        var bidLoading        = $(this).parent().parent().children('.bid-loading');

       // bidButton.hide(1);
        bidLoading.show(1);

        $.ajax({
            url: $(this).attr('href'),
            dataType: 'json',
            success: function(data){
                bidMessage.html(data.Auction.message).show(1).animate({opacity: 2.0}, 2000).hide(1);
                bidButton.show(1);
                bidLoading.hide(1);
            }
        });

        return false;
    });
	////function for going/////
	/*$('.bid-button-going')function(){
        var auctionId         = $(this).parent().parent().parent().attr('title');
        var bidMessage        = $(this).parent().parent().parent().children('.bid-message1');
        var bidButton         = $(this).parent().parent().children('.bid-button');
        var bidLoading        = $(this).parent().parent().children('.bid-loading');

       // bidButton.hide(1);
        bidLoading.show(1);

        $.ajax({
            url: $(this).attr('href'),
            dataType: 'json',
            success: function(data){
                bidMessage1.html(going....).show(1).animate({opacity: 2.0}, 2000).hide(1);
               
            }
        });

        return false;
    };*/

    if($('.productImageThumb').length){
        $('.productImageThumb').click(function(){
            $('.productImageMax').fadeOut('fast').attr('src', $(this).attr('href')).fadeIn('fast');
            return false;
        });
    }

    if($('#CategoryId').length){
        $('#CategoryId').change(function(){
            document.location = '/categories/view/' + $('#CategoryId option:selected').attr('value');
        });
    }

    if($('#myselectbox').length){
        $('#myselectbox').change(function(){
            document.location = '/categories/view/' + $('#myselectbox option:selected').attr('value');
        });
    }

});

