/*Stylesheet switching and recipe card generation script for The Splendid Table's recipe archive:    	http://splendidtable.publicradio.org/recipes/ 
written by Nicholas Wallen August, 2008
Dependency: jquery 1.2.3.*/

 $(document).ready(function(){	
		//insert print menu			
		 $("#interior_content").prepend('<img src="/images/colorprintlogo.gif" id="printWordmark" alt="The Splendid Table" /><ul id="recipePrintList"> <li><img src="/images/icon_print.gif" alt="print icon" /> Print Recipe:</li><li><a id="printPageLarge" title="Full Page Print Preview" href="#">Full Page</a> | <a id="printPageSmall" title="Compact Print Preview" href="#">Compact</a></li></ul><ul id="printPreviewText"><li><em>This is a Print Preview.</em></li><li><a href="#" id="printLink" accesskey="3">click here to print</a></li><li><a href="#return" title="return to The Splendid Table" id="returnToSite">Return To Site</a></li></ul>'); 
		 //display intial print menu--users without javascript will not see the menu 
		 $("#recipePrintList").css('display','block');
		 
//variable to track if recipe has been broken into cards		 
var run=false;
//variable to track recipe card checkbox state
var checked=true;
  
		 $("#printPageLarge").click(function() {
				//hide cards if run							 
				if(run){							 
		       	$("#interior_content").find("img.firstdotted, .printcardbreak, .largecardbreak, .cardbreak, #cards_select ").hide();}        
				//switch stylesheets for preview
				$("link[rel=stylesheet]").not('link[media=print]').attr('href' , '/standard/css/002/preview_printLarge.css');
				//switch stylesheets for print
				$("link[media=print ]").attr('href', '/standard/css/002/printLarge.css');
				});
		 
		 
		 
		 
		 $("#printPageSmall").click(function() {
				//switch stylesheets for preview
				$("link[rel=stylesheet]").not('link[media=print]').attr('href' , '/standard/css/002/preview_printSmall.css');
				//switch stylesheets for print
				$("link[media=print]").attr('href', '/standard/css/002/printSmall.css');
                //show cards if run or break recipe into cards	 
				if(run){
					$("#interior_content").find("#cards_select").show();
					//check last state of checkbox 
					if(checked){
						$("#interior_content").find("img.firstdotted, .printcardbreak, .largecardbreak, .cardbreak").show();
					}
				}else{  //checkbox to toggle cards
						//add the checkbox
					    $("#interior_content #printPreviewText").prepend('<li id="cards_select">Break into 4x6 Cards:<input type="checkbox" /></li>');
						//set the checkbox to appear as checked or unchecked 
						if (checked){
							$("#cards_select input").attr("checked", "checked");
						}else{
							$("#cards_select input").attr("checked", " ");
						}
						//attach click event to hide and show cards and set checkbox state
						$('#cards_select input').click(
							function(){ 
								if(checked){
									$("#interior_content").find("img.firstdotted, .printcardbreak, .largecardbreak, .cardbreak").hide();         
									checked=false;
								}else{
									$("#interior_content").find("img.firstdotted, .printcardbreak, .largecardbreak, .cardbreak").show();
						 			checked=true;
								}
							});
					
					    //Breaking the recipe into cards
						//get the number of block elements in recipe
						var pCount=$("#interior_content").children().not("#printPreviewText, #recipePrintList,#contentFooter, img, span").size();
						
						//initialize limit for pixel height of each card
						var cardHeight=140;
						//initialize variable to store pixel height 
						var pHeight=0;
						//initialize variable track the number of cards
						var cardCount=0;
						//variable to track the number of elements counted
						var elemNum=0;
						//variable to track if card is first card
						var first=true;
						//loop through elements and insert card image. skip the last element. 
						for (var i=0;i<=pCount-2;++i){
							//store current and next elements and their heights in variables
							var current=$("#interior_content").children().not("#printPreviewText, #recipePrintList, #contentFooter, img, span").eq(i);
							var currentHeight=current.height();
							var currentNext=$("#interior_content").children().not("#printPreviewText, #recipePrintList,#contentFooter, img, span").eq(i+1);
							var currentNextHeight=currentNext.height();
							
					//set the element's background to white so it remains legible if over the borders
							current.css("background-color","#ffffff");
                            //fudge ul and ol list height since this is inconsistent between browsers
							if(current.is("ul")||current.is("ol")){
								 var numLI=current.children().size()+1;
								 currentHeight=numLI*12;
							}
							if(currentNext.is("ul")||currentNext.is("ol")){
								 var numLI=currentNext.children().size()+1;
								 currentNextHeight=numLI*12;
							}
							
							//for the first card allow a larger cardHeight to avoid a blank first card
							if(currentNextHeight>=150&&first&&!currentNext.is("ul")&&!currentNext.is("ol")){
								cardHeight=180;
								first=false;
							}
							//store the element height and increase element count by one 
							pHeight+=currentHeight;
							elemNum+=1;
							//if the next element is huge break card now
						    if(currentNextHeight>=160){
								cardHeight=pHeight;
							}
							
							if(pHeight>=cardHeight){
								//insert print break every two cards
								if(cardCount==1){
								current.after('<span class="printcardbreak"><img class="breakdotted" src="/recipes/images/dotted.jpg"/></span>');
								cardCount=0;
								}else{
	//if the next element is huge add a larger break to stop cards from running into eachother 
									if(currentNextHeight >175&&elemNum<=3){
								        current.after('<span class="largecardbreak"><img class="dotted" src="/recipes/images/dotted.jpg"/></span>');
									}else{
										current.after('<span class="cardbreak"><img class="dotted" src="/recipes/images/dotted.jpg"/></span>');
									}
								++cardCount;
								}
								//reset variables
								cardHeight=175;
								first=false;
								pHeight=0;	
							    elemNum=0
							}	
						}
						//add first card image
						$("#interior_content").prepend('<img class="firstdotted" src="/recipes/images/dotted.jpg"/>');
						
				//set run to true		
				run=true;		
				}
				
				});
		//add print and return to site(reload) click events
		$('#printLink').click(function(){window.print();});
		$("#returnToSite").click(function(){window.location.reload();});
							
});