var toGo='';

function setPar(url) {
	toGo = url;
}

function goTo(e) {
	if (!e)
		e = window.event;

	if (console)
		console.log(window.event);

	if(e.target.tagName != 'a' && toGo)
		location.href = toGo;
}

function showSubMenu(rel) {
		rel=rel.replace(/cat/,'sub');

		$('#globsubnav > ul').addClass('subhide');

		$('#'+rel).removeClass('subhide');
	}

$(document).ready(function() {

	$('#globalnav > li').bind('mouseenter',
		function() {showSubMenu($(this).attr('id'))}
	);

	if( typeof(keywords_y) == "undefined" ){
		keywords_y = cate;
	}

	$.getJSON(
		"/ajax/get2.php",
		{ keyword: keywords_y, time: time, nonce: nonce, service:'y', count:ycount, id_tr_y: id_tr_y },
		function(data){

			if (data[0] == "NULL") {
				$('#y-fall-iframe').attr('src','/ajax/adsense.php');
				$('#y-fallback').show();
				$('#results-y').hide()
			} else {

				cache_y=''
				for (i=0;i<data.length;i++) {
					if (i!=(data.length-1))
						classs = ' bord';
					else
						classs = '';

					if (typeof(isnew)!='undefined') {
						//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(2,5)+'.gif" />';
						appx = '';
						sufx = '<div style="clear:both;"></div>';
					} else {
						appx = '';
						sufx = '';
					}

					if (typeof(ypreview)!='undefined') {
						if (i<ypreview) {
							imgy = '<a rel="nofollow" href="'+data[i].url+'" title="'+data[i].title+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';"><img src="/ajax/thumb.php?url=http://'+data[i].merch+'" alt="" align="left" style="margin-right:5px;width:100px;height:70px;border:1px solid #CCC;cursor:pointer;"></a>';
							//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(3,5)+'.gif" />';
							appx = '';
							sufx = '<div style="clear:both;"></div>';
							ccccclass = ' img';
						} else {
							imgy = '';
							//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(1,3)+'.gif" />';
							appx = '';
							sufx = '<div style="clear:both;"></div>';
							ccccclass = '';
						}
					} else {
						imgy = '';
						ccccclass = '';
					}

					cache_y += '<div class="result'+classs+' '+ccccclass+'">'+
								imgy+'<strong><a rel="nofollow" href="'+data[i].url+'" title="'+data[i].title+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';">'+data[i].title+''+appx+'</a></strong>'+
								'<a rel="nofollow" href="'+data[i].url+'" target="_blank"><p>'+data[i].desc+'</p></a>'+
								'<a rel="nofollow" href="'+data[i].url+'" target="_blank">'+data[i].merch+'</a>'+sufx+
								'<div style="clear:both"></div></div>'
				}
				$('#results-y').html(cache_y);
				$.post(
					'/ajax/cache.php',
					{ keyword: cate, service:'y', cache:cache_y, time: time, nonce: nonce }
				);
				$('#more-products').css('visibility','visible')
				if($('#more-y-res').length) {
					cache_y2 ='';
					for (i=0;i<1;i++) {
						if (i!=(data.length-1))
							classs = '';
						else
							classs = '';

						if (typeof(isnew)!='undefined') {
							//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(2,5)+'.gif" />';
							appx = '';
							sufx = '<div style="clear:both;"></div>';
						} else {
							appx = '';
							sufx = '';
						}

						if (typeof(ypreview)!='undefined') {
							if (i<ypreview) {
								imgy = '<a rel="nofollow" href="'+data[i].url+'" title="'+data[i].title+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';"><img src="/ajax/thumb.php?url=http://'+data[i].merch+'" alt="" align="left" style="margin-right:5px;width:100px;height:70px;border:1px solid #CCC;cursor:pointer;"></a>';
								//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(3,5)+'.gif" />';
								appx = '';
								sufx = '<div style="clear:both;"></div>';
								ccccclass = ' img';
							} else {
								imgy = '';
								//appx = '<img class="stelle" src="/img/stelle/voto_small_'+rand(1,3)+'.gif" />';
								appx = '';
								sufx = '<div style="clear:both;"></div>';
								ccccclass = '';
							}
						} else {
							imgy = '';
							ccccclass = '';
						}

						cache_y2 += '<div class="result'+classs+' '+ccccclass+'">'+
									imgy+'<strong><a rel="nofollow" href="'+data[i].url+'" title="'+data[i].title+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';">'+data[i].title+''+appx+'</a></strong>'+
									'<a rel="nofollow" href="'+data[i].url+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';"><p>'+data[i].desc+'</p></a>'+
									'<a rel="nofollow" href="'+data[i].url+'" target="_blank" onclick="document.getElementById(\'conversion-tracking\').src=\''+data[i].url+'&go=1\';">'+data[i].merch+'</a>'+sufx+
									'<div style="clear:both"></div></div>'
					}
					$('#more-y-res').append(cache_y2);
				}
			}
		}
	);

	if( typeof(keywords_e) == "undefined" ){
		keywords_e = cate;
	}


	if (!donothing)
	if (show_e)
	$.getJSON(
		"/ajax/get.php",
		{ keyword: keywords_e, time: time, nonce: nonce, service:'e', count:ecount , id_tr_e: id_tr_e},
		function(data){
			cache_e='';
			if (data.length == 0) {
				$.ajax({
				   type: "GET",
				   url: '/ajax/get.php',
				   data: "service=c",
				   success: function(msg){
						$('#results-e').html('<h3 style="font-size:14px;color:#000;">Nessuna offerta trovata per la tua ricerca - Controlla queste offerte consigliate!</h3><br />'+msg)
				   }
				 });
			}
			for (i=0;i<data.length;i++) {


				if (i == (data.length-1) && data[i]==0) {
					$('#ebayz-title').html('Le offerte in scadenza sugli oggetti pi&ugrave; cliccati');
					continue;
				}

				classN = '';

				if (i<(ecount-2))
					classN += ' bott-border'

				if(data[i].img!=null)
					data[i].img='<span class="gallery-image" style="background-image:url('+data[i].img+')">'+data[i].title+'</span>'
				else
					data[i].img='';

				cache_e += '<div class="result '+classN+'" >'+
								'<a rel="nofollow" href="'+data[i].url+'" title="'+data[i].title+'" class="title" target="_blank">'+data[i].title+'</a>'+
									'<p>'+
										'<a href="'+data[i].url+'" rel="nofollow" title="'+data[i].title+'" target="_blank">'+
											data[i].img+
										'</a>'+
										'Prezzo : <strong>'+data[i].price+'&euro;</strong> <br /><br />'+
										'Scade tra '+data[i].ending+
									'</p>'+
							'</div>'

				if (i%2 == 0)
					cache_e += '<div class="left-border">&nbsp;</div>';


			}
			$('#results-e').html(cache_e);
			$.post(
				'/ajax/cache.php',
				{ keyword: cate, service:'e', cache:cache_e, time: time, nonce: nonce }
			)
		}
	);

	/*$.getJSON(
		"/ajax/get.php",
		{ keyword: keywords_e, time: time, nonce: nonce, service:'g' },
		function(data){
			cache_g='';
			for (i=0;i<data.length;i++) {
				if(data[i].img!=null)
					cache_g += '<a href="'+data[i].url+'" title="'+data[i].title+'"><span class="gallery-image" style="background-image:url('+data[i].img+')">'+data[i].title+'</span></a>'
			}
			$('#results-g').html(cache_g)
			/$.post(
				'/ajax/cache.php',
				{ keyword: cate, service:'g', cache:cache_e, time: time, nonce: nonce }
			)/
		}
	);*/

	/*if (typeof(carousel)!="undefined") {
		carousel_interval = setInterval('showNextImage()',5000);
		/*$('#carousel_small > li.item').bind(
			'click',
			function() {
				carouselShow(this.id.replace(/cs_/,''))
			}
		);
		$('#carousel_small > li.item').bind('mouseenter',
			function() {
				carouselShow(this.id.replace(/cs_/,''))
				if (typeof(carousel_interval)!="undefined")
					clearInterval(carousel_interval)
			}
		);
		$('#carousel_small > li.item').bind('mouseleave',
			function() {
				carousel_interval = setInterval('showNextImage()',5000);
			}
		);
	}*/
});
/*
function showNextImage() {
	current = $('#carousel_small > li.currentz').attr('id').replace(/cs_/,'');
	if (current==carousel)
		current=0;
	current++;
	carouselShow(current)
}*/
/*
function carouselShow(el) {
	$('#carousel_small > li ').removeClass('currentz');
	$('#cs_'+el+'').addClass('currentz');
	$('#carousel_big > li').stop(true,true);
	$('#carousel_big > li.currentz').fadeOut('slow',function() {
		$('#carousel_big > li').removeClass('currentz');
		$('#cb_'+el).fadeIn('slow');
		$('#cb_'+el).addClass('currentz');
	});
}*/

function search(val) {
	location.href="/search/"+val.toLowerCase()+"/";
	return false;
}


function storeAddress() {
	$('#response').html('Adding email address...');
	$.ajax({
		type: "GET",
		url: "/ajaxServer.php",
		data: "address="+escape($('#address').val()),
		success: function(msg){
			$("#response").html(msg);
		}
	});
	return false;
}

function nlNextstep() {

	$('#nl-nome').css('border','1px solid '+maincolor);
	$('#nl-cognome').css('border','1px solid '+maincolor);
	$('#nl-email').css('border','1px solid '+maincolor);

	err=0;
	if (!$('#nl-nome').val()) {
		$('#nl-nome').css('border','1px solid red');
		err++;
	}

	if (!$('#nl-cognome').val()) {
		$('#nl-cognome').css('border','1px solid red');
		err++;
	}

	if (!$('#nl-email').val()) {
		$('#nl-email').css('border','1px solid red');
		err++;
	}

	if (err==0){
		$('#nl-step1').hide();
		$('#nl-step2').show();
	}
}

function nlShow() {
	$('html, body').animate({scrollTop: '0px'}, 300);
	if ($('#address').val().indexOf('@')!=-1)
		$('#nl-email').val($('#address').val());
	$('#nl-step1').show();
	$('#nl-step2').hide();
	$('#nl-results').hide();
	$('#opaque').show();
	$('#newsletter-signup').show();
}

function nlClose() {
	$('#opaque').hide();
	$('#newsletter-signup').hide();
}

function nlBack() {
	$('#nl-step1').show();
	$('#nl-step2').hide();
	$('#nl-results').hide();
}

function nlSubmit() {

	$('#nl-errors').hide();
	fields = new Array();
	if ($('.nl-check > input.checkbox:checked').length > 4) {

		$('#nl-errors').show();

		return false;

	}


	for (a=0;a<nlNumchecks;a++)
		if($('#nl-'+a+':checked').val())
			fields.push($('#nl-'+a+':checked').attr('name'));


	$.post("/ajax/nl.php?signup=1",
		{
			nome: $('#nl-nome').val(),
			cognome: $('#nl-cognome').val(),
			email: $('#nl-email').val(),
			eta: $('#nl-giorno').val()+"/"+$('#nl-mese').val()+"/"+$('#nl-anno').val(),
			interessi : fields.join(',')
		},
		function(data){
			$('#nl-step2').hide();
			$('#nl-step1').hide();
			$('#nl-results').show();
			//$('#nl-results').html(data);
		});
}

$(document).ready(function(){

	$("#pageflip").hover(function() {
		$("#pageflip img , .msg_block").stop()
			.animate({
				width: '307px',
				height: '319px'
			}, 500);
		} , function() {
		$("#pageflip img").stop()
			.animate({
				width: '50px',
				height: '52px'
			}, 220);
		$(".msg_block").stop()
			.animate({
				width: '50px',
				height: '50px'
			}, 200);
	});


});

function rand (min, max) {
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;

}
