function toggle_div(eid) {
	//jQuery built in slideToggle function
	$(eid).slideToggle('normal');
	//Toggling images for button that initiates toggle - named the same except for suffix of _closed and _open
	if($(eid+'_button').attr('src').indexOf("_closed")!=-1) {
		$(eid+'_button').attr('src', $(eid+'_button').attr('src').replace(/_closed/,"_open"));
	} else {
		$(eid+'_button').attr('src', $(eid+'_button').attr('src').replace(/_open/,"_closed"));
	}
}


window.onbeforeunload = function(){
	var url = window.location.href;
	createCookie('lastPage',url);
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


$(document).ready(function(){
						   
		$(".product_detail h2").each(function(){
			$(this).bind("click",function(){
				$(this).siblings(".expandable").slideToggle();
				if($(this).css("background-image").indexOf("img_arrow_expand")!=-1){
					$(this).css("background-image", $(this).css("background-image").replace("img_arrow_expand.gif","img_arrow_collapse.gif"));
				} else {
					$(this).css("background-image", $(this).css("background-image").replace("img_arrow_collapse.gif","img_arrow_expand.gif"));
				}
			});
		});
		$(".product_detail .expandable .hide_details").each(function(){
			$(this).bind("click",function(){
				$(this).parent(".expandable").slideToggle();
				var h2 = $(this).parent(".expandable").siblings("h2");
				if(h2.css("background-image").indexOf("img_arrow_expand")!=-1){
					h2.css("background-image", h2.css("background-image").replace("img_arrow_expand.gif","img_arrow_collapse.gif"));
				} else {
					h2.css("background-image", h2.css("background-image").replace("img_arrow_collapse.gif","img_arrow_expand.gif"));
				}
			});
		});
	var faq_text = "";
	var faq_item_text = "";
		
	$("#faqs .faq_section h2").each(function(){
		$(this).bind("click",function(){
			$(".faq_a").each(function(){ //close all open faq_a sections and return their associated h2 to proper class
				if($(this).css("display")=="block") {
					$(this).slideUp('normal',function(){
						var q = $(this).siblings(".faq_q");
						if(q.hasClass("open")){
							q.removeClass("open");
						}
					});
				}
			});
			
			if($(this).text()!=faq_text){ //check to see if the header of the currently open section was clicked.
				//if new section clicked, close open section
				$(".expandable").each(function(){
					if($(this).css("display")=="block") {
						$(this).slideUp('normal',function(){
							var h2 = $(this).siblings("h2");
							if(h2.hasClass('open')){
								h2.removeClass('open');
							} 
						});
					}
				});
				//open new section
				$(this).siblings(".expandable").slideToggle('normal',function(){
					var h2 = $(this).siblings("h2");
					if(h2.hasClass('open')){
						h2.removeClass('open');
					} else {
						h2.addClass('open');
					}
				});
				faq_text = $(this).text(); //note text of currently open section
			} else {
				//if header of open section is clicked, just close the section
				$(this).siblings(".expandable").slideUp('normal',function(){
					var h2 = $(this).siblings("h2");
					if(h2.hasClass('open')){
						h2.removeClass('open');
					} else {
						h2.addClass('open');
					}
				});
				faq_text = ""; //all sections should be closed, so clear note of currently open section
			}
		});
	});

	$(".faq_item .faq_q").each(function(){
		$(this).bind("click",function(){
			if($(this).text()!=faq_item_text){
				$(".faq_a").each(function(){
					if($(this).css("display")=="block") {
						$(this).slideUp('normal',function(){
							var q = $(this).siblings(".faq_q");
							if(q.hasClass("open")){
								q.removeClass("open");
							} else {
								q.addClass("open");
							}
						});
					}
				});
				$(this).siblings(".faq_a").slideToggle('normal',function(){
					var faq_q = $(this).siblings(".faq_q");
					if(faq_q.hasClass("open")){
						faq_q.removeClass("open");
					} else {
						faq_q.addClass("open");
					}
				});
				faq_item_text=$(this).text();
			} else {
				$(this).siblings(".faq_a").slideUp('normal',function(){
					var faq_q = $(this).siblings(".faq_q");
					if(faq_q.hasClass("open")){
						faq_q.removeClass("open");
					} else {
						faq_q.addClass("open");
					}
				});
				faq_item_text="";
			}
		});
	});
	$(".faq_item .faq_q").each(function(){
		$(this).bind("mouseover",function(){
			$(this).addClass("hover");
		});
		$(this).bind("mouseout",function(){
			$(this).removeClass("hover");
		});
	});

	$(".faq_item .faq_a .hide_details").each(function(){
		$(this).bind("click",function(){
			$(this).parent(".faq_a").slideToggle('normal',function(){
				var faq_q = $(this).siblings(".faq_q");
				if(faq_q.hasClass("open")){
					faq_q.removeClass("open");
				} else {
					faq_q.addClass("open");
				}
			});
		});
	});
	
});