var floatDiv;
var floatImgParams = '&w=260&c=1';
var floatImgArray = Array();
var wantShow = null;



function getStorageAdress(adress) {
	var a_match = adress.match(/^(.*\?id=[0-9]+)/);
	return a_match[0];
}

function showHotImg(element) {
	if (!floatImgArray[element.imgIndex].complete) {
		return ;
	}
	wantShow = null; 
	$(floatDiv).hide();
	var offset = $(element).offset();
	var img = document.createElement('img');
	img.src = floatImgArray[element.imgIndex].src;
	img.alt = element.alt;
	floatDiv.innerHTML = '';
	floatDiv.href = element.parentNode.href;
	$(floatDiv).append(img);
	var leftCorection = (floatImgArray[element.imgIndex].width / 2) - (element.width / 2);
	var topCorection = (floatImgArray[element.imgIndex].height / 2) - (element.height / 2);
	$(floatDiv).css('height',floatImgArray[element.imgIndex].height + 'px');
	$(floatDiv).css('top',(offset.top - topCorection) +'px');
	$(floatDiv).css('left',(offset.left - leftCorection) +'px');
	floatDiv.canHide = false;
	$(floatDiv).show('fast',
		function() {this.canHide = true;}
	);
}

$(document).ready(function() {
	
	$.countdown.regional['cs'] = {
		labels: ['r', 'm', 'Týdnů', 'dní', 'h', 'm', 's'],
		labels1: ['r', 'm', 'Týden', 'den', 'h', 'm', 's'],
		labels2: ['r', 'm', 'Týdny', 'dny', 'h', 'm', 's'],
		labels3: ['r', 'm', 'Týdny', 'dny', 'h', 'm', 's'],
		labels4: ['r', 'm', 'Týdny', 'dny', 'h', 'm', 's'],
		compactLabels: ['r', 'm', 't', 'd'],
		timeSeparator: ':', isRTL: false};
	$.countdown.setDefaults($.countdown.regional['cs']);

	
	floatDiv = document.createElement('a');
	floatDiv.id = 'floatDiv';
	floatDiv.canHide = true
	$('#wrapper').append(floatDiv);

	$('.item.hot img.main-photo').each(function(index) {
		var img = new Image();
//		alert(this.src);
		img.src = getStorageAdress(this.src)  + floatImgParams;
		floatImgArray.push(img);
		this.imgIndex = index;
	});
	
	$(floatDiv).hover(
		function() {
			
		}
		,
		function() {
			if (floatDiv.canHide) {
				floatDiv.canHide = false;
				$(floatDiv).hide('fast',function() {
					floatDiv.canHide = true; 
					if (wantShow != null) showHotImg(wantShow);
				
				});
			}
		}
	);
	
	
	$('.item.hot img.main-photo').hover(
		function() {
			if (!floatDiv.canHide) {
				wantShow = this;
			} else {
				showHotImg(this);
			}
			
		},
		function() {
			
	});
	
	$('.auc-time').each(function() {
		try {
			var timeString = $(this).find('.left-time-input').val();
			var timeReg = timeString.match(/^([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)/);
			var year = parseInt(timeReg[1]);
			var month = timeReg[2];
			if (month[0] == '0') {
				month = parseInt(month[1]);
			} else {
				month = parseInt(timeReg[2])
			}
			var day = timeReg[3];
			if (day[0] == '0') {
				day = parseInt(day[1]);
			} else {
				day = parseInt(timeReg[3]);
			}
			var hour = timeReg[4];
			if (hour[0] == '0') {
				hour = parseInt(hour[1]);
			} else {
				hour = parseInt(timeReg[4]);
			}
			var min = timeReg[5];
			if (min[0] == '0') {
				min = parseInt(min[1]);
			} else {
				min = parseInt(timeReg[5]);
			}
			
			
			var dateNow = new Date();
			var auctionEnd = new Date(year,month-1, day); 
	//		alert(auctionEnd);
			auctionEnd.setHours(hour);
			auctionEnd.setMinutes(min);
			var oldContent = $(this).find('.left-time').text();
			$(this).find('.left-time').text('');
			if (dateNow > auctionEnd) {
				$(this).find('.left-time').text(_l('Aukce skončila'));
			} else {
			
				var countDownLayout = '%D%n %l%D %H%n%l%H %M%n%l%M %S%n%l%S';
				
				if (dateNow.getUTCDate() == auctionEnd.getUTCDate() && 
						dateNow.getUTCMonth() == auctionEnd.getUTCMonth() && 
						dateNow.getUTCFullYear() == auctionEnd.getUTCFullYear()) {
					countDownLayout = '%H%n%l%H %M%n%l%M %S%n%l%S';
				}
				
				$($(this).find('.left-time')).countdown({until: auctionEnd, layout: countDownLayout}); 
			}
		} catch(e) {
			$(this).find('.left-time').text(oldContent);
//			alert(e.toString());
		}
	});
	
});
