$(document).ready(function(){
    
$('#holder').click(function(){
    
    var aid = $('.active').attr('id');    
    
    var patt=/[0-9]/;
    var link_number=patt.exec(aid);
    
    link_location = $('#box_'+link_number+'_message a').attr('href');

    window.location = link_location;

});
    

var xspeed = 175;

$("#box_1_over").mouseenter(function(){
	xcheck();
	box_1_hover(xspeed);
});

$("#box_2_over").mouseenter(function(){
	xcheck();
	box_2_hover(xspeed);
});

$("#box_3_over").mouseenter(function(){
	xcheck();
	box_3_hover(xspeed);
});

$("#box_4_over").mouseenter(function(){
	xcheck();
	box_4_hover(xspeed);
});


function box_1_hover(xspeed)
{
	$("#box_1_over").flip({
		direction:'tb',
		speed: xspeed,
		onAnimation: function(){
			$("#box_1_over").css({display: 'none'});			
		},
		onEnd: function(){
			$("#box_1_row_2").css({display: 'inline'});				
			$("#box_1_row_2_under").css({display: 'inline'});				
			//flip second block
			$("#box_1_row_2").flip({
				direction:'lr',
				speed: xspeed,
				onEnd: function(){
					$("#box_2_row_2").css({display: 'inline'});
					$("#box_2_row_2_under").css({display: 'inline'});
					//trigger 3rd flip
					$("#box_2_row_2").flip({
						direction:'lr',
						speed: xspeed,
						onEnd: function(){
							$("#box_3_row_2").css({display: 'inline'});
							$("#box_3_row_2_under").css({display: 'inline'});
							//trigger 4th flip
							$("#box_3_row_2").flip({
								direction:'lr',
								speed: xspeed,
								onEnd: function(){
									$("#box_4_row_2").css({display: 'inline'});
									$("#box_3_row_2_under").css({display: 'none'});
									
									//show final content
									$("#box_1_message").fadeIn("slow");
									
									$("#holder").css({ width: "550px", height: "260px", zIndex: 9 });
									$("#box_1_under").addClass('active');
									
									//rebind
									$('#box_1_over').bind('mouseenter',function(){xcheck();box_1_hover(xspeed)});
									$('#box_2_over').bind('mouseenter',function(){xcheck();box_2_hover(xspeed)});
									$('#box_3_over').bind('mouseenter',function(){xcheck();box_3_hover(xspeed)});
									$('#box_4_over').bind('mouseenter',function(){xcheck();box_4_hover(xspeed)});
								}
							})//end $("#box_2_row_2").flip({
						$("#box_2_row_2_under").css({display: 'none'});							
						}
					})//end $("#box_1_row_2").flip({
				$("#box_1_row_2_under").css({display: 'none'});
				}
			})//end $("#box_1_row_2").flip({			
		}
	});//end $("#box_1_over").flip({	
}

function box_2_hover(xspeed)
{
	$("#box_2_over").flip({
		direction:'tb',
		speed: xspeed,
		onAnimation: function(){
			$("#box_2_over").css({display: 'none'});			
		},
		onEnd: function(){
			$("#box_2_row_2").css({display: 'inline'});				
			$("#box_2_row_2_under").css({display: 'inline'});					
						
			//flip block right
			$("#box_2_row_2").flip({
				direction:'lr',
				speed: xspeed,
				onEnd: function(){
					$("#box_3_row_2").css({display: 'inline'});

					
					$("#box_2_row_2_under_under").css({display: 'inline'});
											
					//flip block left				
					$("#box_2_row_2_under_under").flip({
						direction:'rl',
						speed: xspeed,
						onEnd: function(){
							$("#box_1_row_2").css({display: 'inline'});
							$("#box_3_row_2_under").css({display: 'inline'});

							//trigger final flip
							$("#box_3_row_2").flip({
								direction:'lr',
								speed: xspeed,
								onEnd: function(){
									$("#box_4_row_2").css({display: 'inline'});															
									$("#box_2_row_2_under").css({display: 'none'});		
									$("#box_3_row_2_under").css({display: 'none'});	
									$("#box_2_row_2_under_under").css({display: 'none'});														
									//show final content
									$("#box_2_message").fadeIn("slow");	
									
									$("#holder").css({ width: "550px", height: "260px", zIndex: 9 });
									$("#box_2_under").addClass('active');
									
									//rebind
									$('#box_1_over').bind('mouseenter',function(){xcheck();box_1_hover(xspeed)});
									$('#box_2_over').bind('mouseenter',function(){xcheck();box_2_hover(xspeed)});
									$('#box_3_over').bind('mouseenter',function(){xcheck();box_3_hover(xspeed)});
									$('#box_4_over').bind('mouseenter',function(){xcheck();box_4_hover(xspeed)});					
								}
							})//end ("#box_3_row_2").flip({								
						}
					})//end $("#box_2_row_2").flip({
					$("#box_3_row_2_under").css({display: 'none'});
				}
			})//end $("#box_2_row_2").flip({	
		}
	});//end $("#box_2_over").flip({	
}

function box_3_hover(xspeed)
{
	$("#box_3_over").flip({
		direction:'tb',
		speed: xspeed,
		onAnimation: function(){
			$("#box_3_over").css({display: 'none'});			
		},
		onEnd: function(){
			$("#box_3_row_2").css({display: 'inline'});				
			$("#box_3_row_2_under").css({display: 'inline'});					
						
			//flip block left
			$("#box_3_row_2").flip({
				direction:'rl',
				speed: xspeed,
				onEnd: function(){
					$("#box_2_row_2").css({display: 'inline'});
					$("#box_3_row_2_under_under").css({display: 'inline'});							
											
					//flip block right				
					$("#box_3_row_2_under_under").flip({
						direction:'lr',
						speed: xspeed,
						onEnd: function(){
							$("#box_4_row_2").css({display: 'inline'});
							$("#box_2_row_2_under").css({display: 'inline'});

							//trigger final flip
							$("#box_2_row_2_under").flip({
								direction:'rl',
								speed: xspeed,
								onEnd: function(){
									$("#box_1_row_2").css({display: 'inline'});															
									$("#box_2_row_2_under").css({display: 'none'});	
									$("#box_3_row_2_under_under").css({display: 'none'});															
									//show final content
									$("#box_3_message").fadeIn("slow");	
									
									$("#holder").css({ width: "550px", height: "260px", zIndex: 9 });
									$("#box_3_under").addClass('active');		
									
									//rebind
									$('#box_1_over').bind('mouseenter',function(){xcheck();box_1_hover(xspeed)});
									$('#box_2_over').bind('mouseenter',function(){xcheck();box_2_hover(xspeed)});
									$('#box_3_over').bind('mouseenter',function(){xcheck();box_3_hover(xspeed)});
									$('#box_4_over').bind('mouseenter',function(){xcheck();box_4_hover(xspeed)});			
								}
							})//end ("#box_3_row_2").flip({								
						}
					})//end $("#box_2_row_2").flip({
					$("#box_3_row_2_under").css({display: 'none'});
				}
			})//end $("#box_2_row_2").flip({	
		}
	})//end $("#box_2_over").flip({	
}

function box_4_hover(xspeed)
{
	$("#box_4_over").flip({
		direction:'tb',
		speed: xspeed,
		onAnimation: function(){
			$("#box_4_over").css({display: 'none'});			
		},
		onEnd: function(){
			$("#box_4_row_2").css({display: 'inline'});				
			$("#box_4_row_2_under").css({display: 'inline'});				
			//flip second block
			$("#box_4_row_2").flip({
				direction:'rl',
				speed: xspeed,
				onEnd: function(){
					$("#box_3_row_2").css({display: 'inline'});
					$("#box_3_row_2_under").css({display: 'inline'});
					//trigger 3rd flip
					$("#box_3_row_2").flip({
						direction:'rl',
						speed: xspeed,
						onEnd: function(){
							$("#box_2_row_2").css({display: 'inline'});
							$("#box_2_row_2_under").css({display: 'inline'});
							//trigger 4th flip
							$("#box_2_row_2").flip({
								direction:'rl',
								speed: xspeed,
								onEnd: function(){
									$("#box_1_row_2").css({display: 'inline'});
									$("#box_2_row_2_under").css({display: 'none'});
									
									//show final content
									$("#box_4_message").fadeIn("slow");
									
									$("#holder").css({ width: "550px", height: "260px", zIndex: 9 });
									$("#box_4_under").addClass('active');
									
									//rebind
									$('#box_1_over').bind('mouseenter',function(){xcheck();box_1_hover(xspeed)});
									$('#box_2_over').bind('mouseenter',function(){xcheck();box_2_hover(xspeed)});
									$('#box_3_over').bind('mouseenter',function(){xcheck();box_3_hover(xspeed)});
									$('#box_4_over').bind('mouseenter',function(){xcheck();box_4_hover(xspeed)});
								}
							})//end $("#box_2_row_2").flip({
						$("#box_3_row_2_under").css({display: 'none'});							
						}
					})//end $("#box_1_row_2").flip({
				$("#box_4_row_2_under").css({display: 'none'});
				}
			})//end $("#box_1_row_2").flip({			
		}
	});//end $("#box_1_over").flip({
}

$('#holder').mouseout(function(){
	xcheck();
});


function xcheck()
{
	if ( $("#box_1_under").hasClass('active') )
	{
		fade_out(1);
	}
	if ( $("#box_2_under").hasClass('active') )
	{
		fade_out(2);
	}
	if ( $("#box_3_under").hasClass('active') )
	{
		fade_out(3);
	}
	if ( $("#box_4_under").hasClass('active') )
	{
		fade_out(4);
	}
}

function fade_out(number)
{
	var fade_speed = 300;
	
	$("#holder").css({ width: 0, height: 0, zIndex: 1 });
	$("#box_"+number+"_message").fadeOut(fade_speed);
	$("#box_1_row_2").fadeOut(fade_speed);
	$("#box_2_row_2").fadeOut(fade_speed);
	$("#box_3_row_2").fadeOut(fade_speed);
	$("#box_4_row_2").fadeOut(fade_speed);
	$("#box_"+number+"_over").fadeIn(fade_speed);
	$("#box_"+number+"_under").removeClass('active')
}

});
