$(document).ready(function(){



/**************** rotation  controls**************/

$("ul.ambigrams").eq(0).show();

showambi(0);

function binderr(){
var rot=$("#showcase img").rotate({maxAngle:180,minAngle:0});
	$("#rotate").mouseover(function(){
		$(this).find("img").attr("src","http://www.johnlangdon.net/images/rotate2.gif");
		rot.rotateAnimation(180);
	});
	$("#rotate").mouseout(function(){
		rot.rotateAnimation(0);
		$(this).find("img").attr("src","http://www.johnlangdon.net/images/rotate1.gif");
	});		
}

binderr();


tabb=0;
	

$("#ambigramsgallery .tabs a").click(function(){
	u=$("#ambigramsgallery .tabs a").index(this);
	tabb=u;
	$("ul.ambigrams").hide();
	$(".tabs a").removeClass("active");
	$(this).addClass("active");
	$("ul.ambigrams").eq(u).show();
	return false;
});

currentambigram=0;

   $(document).keypress(function(e) {
		curr=currentambigram;
	  switch(e.keyCode) { 
         case 38:         // up
		if(currentambigram-(21*tabb)>=7){	showambi(currentambigram-7);}
         break;
         case 40:         // down
		if(currentambigram-(21*tabb)<14){	 showambi(currentambigram+7);		 }
         break;
         case 37:         // left
		 if(currentambigram-(21*tabb)>0){showambi(currentambigram-1);}
         break;
         case 39:         // right
		if(currentambigram-(21*tabb)<20){showambi(currentambigram+1);}
         break;
		 }
   });

   
$("ul.ambigrams li>a").click(function(){ 
	d=$("ul.ambigrams li>a").index(this);
	showambi(d);
	return false;
});

/**************** rotation  controls**************/


/**************** ambigram gallery controls**************/
$("a#viewtext").click(function(){ //hide thumbnails, show text
	$(this).parent().find("a").removeClass("active");
	$(this).addClass("active");
	$("ul.ambigrams img").hide();
	$("ul.ambigrams h3").show();
	return false;
});
$("a#viewthumbnails").click(function(){ //hide text, show thumbs
	$(this).parent().find("a").removeClass("active");
	$(this).addClass("active");
	$("ul.ambigrams h3").hide();
	$("ul.ambigrams img").show();
	return false;	
});


$("#goup").mousedown(function(){ $(this).addClass("clicked")}); 
$("#goup").mouseup(function(){ $(this).removeClass("clicked")});
$("#godown").mousedown(function(){ $(this).addClass("clicked")});
$("#godown").mouseup(function(){ $(this).removeClass("clicked")});


/**************** ambigram gallery controls**************/


function showambi(d){
	currentambigram=d;
	ambis=$("ul.ambigrams li>a");
	ambis.find("img").css("border","1px solid #fff");
	ambis.eq(d).find("img").css("border","1px solid #743");
	ambis.eq(d).find("img").addClass("active");
	$("#rotate").unbind('mouseover').unbind('mouseout');
	
	theid=ambis.eq(d).parent().attr("id").replace("ambigramid","");
	var dataString="id="+theid;
	$.ajax({
		type: "POST",
		url: "../ambigramcounter.php",
		data: dataString,
		success: function(){}
	});
	
	detailedimage=ambis.eq(d).attr("href"); //find ambigram's picture
	extrainfo=ambis.eq(d).closest("li").find("div").html(); //find ambigram's info
	ambigramflash=ambis.eq(d).closest("li").find("div.ambigram_flash").html(); //find ambigram flash
	fulltitle=ambis.eq(d).closest("li").find("h3").html(); //find ambigram's title
	theclass=ambis.eq(d).closest("li").attr("class");
	if(theclass=="typeMirro"){
		var so = new SWFObject("/gallery/mirror.swf", "sotester", "350", "400", "8", "");
		so.addParam("wmode", "transparent");
		so.addVariable("filename", detailedimage);
		so.write("showcaseflash");
	$('#showcase').html('<img src="'+detailedimage+'">');
	$('#showcase').hide();
	$('#ambigraminfo').remove();
	$('#showcase').after('<div id="ambigraminfo"><h3>'+fulltitle+'</h3>'+extrainfo+'<a href="#" id="rotate"><img src="http://www.johnlangdon.net/images/rotate1.gif" alt="Rotate"></a></div>');
	
	}
	else{
	$('#showcaseflash').html('');
	$('#showcase').show();
	$('#showcase').html('<img src="'+detailedimage+'">');
	$('#ambigraminfo').remove();
	
	$('#showcase').after('<div id="ambigraminfo"><h3>'+fulltitle+'</h3>'+extrainfo+'<a href="#" id="rotate"><img src="http://www.johnlangdon.net/images/rotate1.gif" alt="Rotate"></a></div>');
	
	} 	
	
	if(ambis.eq(d).closest("li").hasClass("typeFigur") || ambis.eq(d).eq(0).closest("li").hasClass("typeMirro") || ambis.eq(d).eq(0).closest("li").hasClass("typeOscil") || ambis.eq(d).eq(0).closest("li").hasClass("typeTotem") )
	{ 
		$("#rotate").hide();
		if(ambis.eq(d).eq(0).closest("li").hasClass("typeMirro")){
			$("#showcase img").addClass("typeMirro");
		}
		else{
			$("#showcase img").addClass("nonrotational");
		}
		
	} 
	else{
		$("#rotate").show();
		binderr();
	}
	return false;
}



});



