function homeShowAuctionsTable(what, el){
	setCookie('homepage_what',what,10);
	strParams = 'what=' + what;
	$j.ajax({
		type: 'POST',
		url : '/ajax/',
		data : 'call=showAucTable&'+strParams,
		success : doResponse_homeShowAuctionsTable(what, el)
	});
}

function doResponse_homeShowAuctionsTable(what, el){
	return function(response) {

		$j(el).siblings().removeClass('selected');
		$j(el).addClass('selected');

		$('auctions_table').innerHTML = response;

		if(typeof(ExpCountDown)=='function'){
			ExpCountDown();
		};

		clearInterval(livetimeoutid);

		if (what == 'live') {
			livetimeoutid = setTimeout('live()',2000);
			return;
		}

		if (what == 'speciale') {
			var headTag = document.getElementsByTagName("head")[0];
			var myScript = document.createElement('script');
			myScript.type = 'text/javascript';
			myScript.src = 'http://ads.okazii.ro/content/651/js3/';
			headTag.appendChild(myScript);
		}
	}
}

function okaziiAdsInjectCodeForZone651(code) {
	document.getElementById('okazii_ads_zone_651_3146').innerHTML= code;
}

function setCookie(c_name,value,expiredays)
{
	//console.log('cookie '+c_name+'  '+value+'  '+expiredays);
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function timeFormat(ttl){
	if (ttl < 0) return 'Expirat';
	var sec = ttl%60;
	var min = Math.floor(ttl/60);
	var hour = Math.floor(min/60);
	var min = min%60;

	return   min + ' m ' + sec + ' s';
}

var ExpCountDownRunning = 0;
function ExpCountDown(selfCall){
	//console.log(selfCall);

	//console.log(ExpCountDownRunning);
	if (ExpCountDownRunning == 1 && selfCall == 'undefined') return false;

	ExpCountDownRunning = 1;
	$j('.counter_data').each(function(i){
		//var ExpCountDownRunning;



				//if (i == 0) console.log(i+' '+this.value);
				var ttl = this.value;
				$j('#'+this.name).html(timeFormat(ttl));
				if (ttl == 0){
					$j('#'+this.name).parent('td').fadeTo('slow',0.33);
					this.value = -1;
				} else if(this.value < 0){

				} else {

					//console.log($j('#'+this.name).html());
					if (ttl >= 1)
	  					this.value = ttl - 1;
					else
						this.value = 0;
				}
 	});

 	setTimeout("ExpCountDown(1)", 1000);
}

function deleteUserRecentAuctions(){
	$j.ajax({
		type: 'GET',
		url : '/ajax/zone',
		data : 'call=delete_recent',
		success : function(response) {
			document.location = document.location;
		}
	});
	return false;
}

var latestId = 0, livetimeoutid = 0;

function live() {
	$j.ajax({
		 url : '/ajax/'
		,data : 'call=live-json'
		,dataType : 'json'
		,success : function(response) {
			if (response !== null) {
				if (response.id != latestId) {
					$j('#live_container').prepend(response.html);
					$j('#live_' + response.id).fadeIn(500);
					if ($j('#live_container').children().length > 6) {
						$j('#live_container').children().last().remove();
					}					
					latestId = response.id;
				}
			}
			clearTimeout(livetimeoutid);
			livetimeoutid = setTimeout('live()',2000);
		}
	});
}

$j(function() {

	$j('#terms').focus();
	
	if ($j('#special_auction').length) {
		$j.ajax({
			 url : '/ajax?call=speciale'
			 ,success : function(response) {
				 	if (response.length) {
						$j('#special_auction').html(response).show();
						$j("#homepageSpecialeSlider").carousel({
							pagination:false
							,loop:true
							,height: 200
							,dispItems: 6
						});
					}
				}
		});
		
	}
	
	if ($j('#licitatii_recomandateX').length) {
		$j.ajax({
			 url : '/ajax?call=recomandate'
			 ,success : function(response) {
				 	if (response.length) {
				 		$j('#licitatii_recomandateX').html(response).show();
						$j("#homepageRecomandariXSlider").carousel({
							pagination:false
							,loop:true
							,height: 200
							,dispItems: 6
						});
						//$j('#licitatii_recomandateY').html(response.responseText).show();
						$j("#homepageRecomandariYSlider").carousel({
							pagination:false
							,loop:true
							,height: 200
							,dispItems: 6
						});
					}
				}
		});
		
	}

	if ($j('#recent_vizualizate').length) {
		$j.ajax({
			 url : '/ajax?call=vizualizate'
			 ,success : function(response) {
				 	if (response.length) {
				 		$j('#recent_vizualizate').html(response).show();
						$j("#homepageRecentvizualizateSlider").carousel({
							pagination:false
							,loop:true
							,height: 200
							,dispItems: 1
						});
					}
				}
		});
		
	}
	
	if ($j('#recomandategenerale').length) {
		$j.ajax({
			 url : '/ajax?call=recomandategenerale'
			 ,success : function(response) {
				 	if (response.length) {
				 		$j('#recomandategenerale').html(response).show();
						$j("#homepageRecomandariSlider").carousel({
							pagination:false
							,loop:true
							,height: 200
							,dispItems: 1
						});
					}
				}
		});
		
	}
	
	if ($j('#watched_auction').length) {
		$j.ajax({
			 url : '/ajax?call=watched'
			 ,success : function(response) {
				 	if (response.length) {
				 		$j('#watched_auction').html(response).show();
						$j("#homepagestrWatchedauctionsSlider").carousel({
							pagination:false
							,loop:true
							,height: 230
							,dispItems: 6
						});
					}
				}
		});
	
	}




	$j('div.Main-Banner').carousel({
		 loop: true 
		,height: 271
		,pagination: true
		,autoSlide: true
		,autoSlideInterval:8000
		,callback: function(currentPos) {

			displayInnerBanner(currentPos);						
			
			_gaq.push(['_setAccount', 'UA-1670804-8']);  
			if (typeof(_gat) == 'object') {
				var pageTracker = _gat._getTracker("UA-1670804-8"); 
				pageTracker._trackPageview();	
			}
			campaign = document.getElementById('banner_campaign_ga').innerHTML;
			trackElement = $j('.el_' + currentPos).attr('id');
			_gaq.push(['_trackEvent', campaign, 'View', $j('.el_' + currentPos).attr('id')]);
		} 
	});

	function displayInnerBanner(currentPos) {
		if (!$j.trim($j('.el_' + currentPos).html()).length) {
			$j('.el_' + currentPos).html($j('#elJs' + currentPos).val());
			$j('#elJs' + currentPos).val('');
		} else if ($j('#elJs' + currentPos).val().length) {
			$j('#scripts').html('<script>' + $j('#elJs' + currentPos).val() + '</' + 'script>');
			$j('#elJs' + currentPos).val('');
		}
	}
	$j(function() {
		for (i=0;i<countBannerElements;i++) {
			displayInnerBanner(i);
		}
	});
	

	$j('#live').load('/ajax?call=live');
		
});
