
var livelloFlippato = "";
$(document).ready(function(){
	/* The following code is executed once the DOM is loaded */


	$('.sponsorFlip').bind("click",function(){
		
		var elem = $(this);

		if(elem.data('flipped'))
		{
			elem.flip({
				direction:'rl',
				speed: 350,
				color: '#000000',
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden from view with display:none)
					// into the clicked .sponsorFlip div before the flipping animation starts:
					
					elem.html(elem.siblings('.sponsorFlip2').html());
				},
				onEnd: function(){
//						alert("FINE FLIP");
//						$("#sponsor").animate({height:"500px",width:"500px"}, 2000);
//						$("#sponsor").animate({height:"250px",width:"250px"}, 500)
						elem.animate({height:"100px",width:"150px",marginLeft: "0px",marginTop: "0px"}, 500);
						$('#contenitoreflash').animate({opacity: 1},10);						
//						sponsorFlip
//						$("#sponsorFlip").animate({height:"500px"}, 2000)

				}				
			});
			
			// Setting the flag:
			livelloFlippato = "";
			elem.data('flipped',false);			
		}
		else
		{
			//SE E' IL CASO DEFLIPPO PRIMA QUELLO PRECENDTE 
			if (!(livelloFlippato=="")) {
				var elemDaDeflippare = $("#"+livelloFlippato);
					elemDaDeflippare.flip({
						direction:'rl',
						speed: 350,
						color: '#000000',
						onBefore: function(){
							elemDaDeflippare.html(elemDaDeflippare.siblings('.sponsorFlip2').html());
						},
						onEnd: function(){
								elemDaDeflippare.animate({height:"100px",width:"150px",marginLeft: "0px",marginTop: "0px"}, 500);
								$('#contenitoreflash').animate({opacity: 1},10);
						}				
					});
					elemDaDeflippare.data('flipped',false);	
			}


			// Using the flip method defined by the plugin:
			var coorx;
			coorx = (-150*(6-elem.css('z-index'))) - 2;
			
			elem.flip({
				direction:'lr',
				speed: 350,
				color: '#000000',
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden from view with display:none)
					// into the clicked .sponsorFlip div before the flipping animation starts:
					
					elem.html(elem.siblings('.sponsorData').html());
				},
				onEnd: function(){
/*						elem.animate({height:"440px",width:"912px",marginLeft: coorx,marginTop: "-350px"}, 500,'linear',
									 function() {
										$('#contenitoreflash').animate({opacity: 0},10);
										 });
*/						
						elem.animate({height:"440px",width:"912px",marginLeft: coorx,marginTop: "-440px"}, 500,'linear');
						$('#contenitoreflash').animate({opacity: 0},10);


				}				
			});
			livelloFlippato = elem.attr("id"); 
			// Setting the flag:
			elem.data('flipped',true);
//			alert("FLIPPATO");
		}
	});
	
});