// Author:              Matt Rossi

// Website:             ifohdesigns.com

// Article Source:      http://ifohdesigns.com/blog/tutorials/

// Last Modified:       August 27, 2008// IF YOU WISH TO USE THIS SCRIPT I WOULD APPRECIATE A BACKLINK

// Multiple DIV support modification: 		Graham Huber 

// ---------------------------


$(function(){
		 
		// TOGGLER LOAD 

		$(".arrow_up").css('display', 'inline');
		$(".arrow_down").css('display', 'none');
		
		var slide_speed = "fast";											// values: slow, normal, fast

		 $(".toggler_slide").hide(); 										// Collapse all togglers by default 
		 
		var ids = new Array();
		
		$('.toggler').each(function() {										// Load all toggler #ids into array 
		  ids.push($(this).attr('id'));
		})
		
		// ----------------------------
		
		
		// COOKIES FUNCTION 
		
		$.each(ids, function(n, togglerID) { 								// Cycle through all togglers and set state via cookie
		
			// READ COOKIES
		    // Header State
		    var showTop = $.cookie(togglerID + '_showTop'); 				// Define cookie ID		    
		    var togglerID_id = "#" + togglerID; 							// Add hash to ID
		    var id_to_slide = "#" + togglerID + "_slide";  					// Define toggler slider
		    
		    // Set the user's selection for the Header State
		    if (showTop == 'collapsed') {
   				  $(id_to_slide).show();				
				  $(togglerID_id).find(".arrow_up").css('display', 'none');
				  $(togglerID_id).find(".arrow_down").css('display', 'inline');
				  
		    };
		});		 

		// ----------------------------
		   

		// DEFAULTS
		
		var IDs_open_by_default = new Array( 'country_bar', 'country_gallery_bar', 'tour_bar', 'tour_gallery_bar', 'tour_itinerary_bar', 'day_item_1_bar', 'tour_itinerary_additional_info_bar', 'services_features_bar', 'services_features_included_bar', 'services_features_additional_info_bar', 'tour_booking_bar', 'tour_booking_additional_info_bar' );			// define IDs to open by default
			//alert(IDs_open_by_default[0]);
 		
		$.each(IDs_open_by_default, function(p, openID) { 					// Cycle through all togglers and set state via cookie
	
			// READ COOKIES
		    // Header State
		    
		    var open_by_default = $.cookie(openID + '_showTop'); 
		    
		    if ( open_by_default == null ) {	
		    
		    	// Check cookie ID
		    	$("#" + openID + "_slide").show();							// If saved cookie state does not exist, open by default

	    		// Set Toggler arrows 				
				$("#" + openID).find(".arrow_up").css('display', 'none');
				$("#" + openID).find(".arrow_down").css('display', 'inline');
						    
		    } else {
		    			
		    	if ( open_by_default == 'collapsed') {

		       	  $("#" + openID).find(".arrow_up").css('display', 'none');
				  $("#" + openID).find(".arrow_down").css('display', 'inline');	
				  
    			} else {

	    	 	  $("#" + openID).find(".arrow_up").css('display', 'inline');
				  $("#" + openID).find(".arrow_down").css('display', 'none');	

	 			}
		    }
		});	

		// ----------------------------		

		// TOGGLER FUNCTION 
		    		
		   $(".toggler").click(function(){	

			if (click_halt_id == "") {

		          var togglerID = $(this).attr('id');
		          var id_holder = "#" + togglerID;
		          var id_to_slide = id_holder + "_slide";
		          
		       	  var showTop = togglerID + "_showTop";
			
		      if ($(id_to_slide).is(":hidden")) {				
		      	
		      	// Slide toggler down
		        $(id_to_slide).slideDown(slide_speed);
		         
		       	$(id_holder).find(".arrow_up").css('display', 'none');
				$(id_holder).find(".arrow_down").css('display', 'inline');
				
				
				$(this).addClass("active");
			
		        $.cookie(showTop, 'collapsed');							// Set cookie state
				//return false;
			
		      } else {
		      
		      	// Slide toggler up
		        $(id_to_slide).slideUp(slide_speed);					
	
		       	$(id_holder).find(".arrow_up").css('display', 'inline');
				$(id_holder).find(".arrow_down").css('display', 'none');
								    		        
				$(this).removeClass("active");
		        $.cookie(showTop, 'expanded');							// Set cookie state
				//return false;		
		      }
		      
			} else {
				click_halt_id = "";
			}
			
		   });
		   
});

var click_halt_id = "";

// ------------------------



