$(document).ready(function(){
	
	$(".section").click(function(){
		$(this).toggleClass("active");
		$(this).next().slideToggle();
	});
	
	$("#expandable-list li div.hide").css({'display':'none'});
	
	if($(".click").length>0) {
		$(".click").colorbox({width:"1000px", height:"597px", iframe:true});
	}
	
	var hrefe = "";
	var downloads = {'Language Diagnostic':'/_uploads/lewisbarned-language-diagnostic.pdf','Style Guide':'/_uploads/lewisbarned-style-guide.pdf'}; // JSON for storing paths to downloads
	$(".download_link").attr('href','javascript:void(0)'); // DEACTIVATE HREFS FOR UPLOAD LINKS
	$(".download_link").click(function() {
		var title = $(this).attr('title'); // GET THE TITLE OF THE DOWNLOAD
		$('#dfile').val(title);
		hrefe = downloads[title]; // SET HREF DEPENDENT ON VALUE OF TITLE
		$.colorbox({inline:true, href:".detailsBox"}); // SHOW FORM
		return false
	});
	
	// SUBMIT DETAILS OF USER AND THEN DISPLAY DOWNLOAD LINK
	$("#details_submit").submit(function() {
		$("#details span.error").remove();
		var name = $("#dname").val();
		var email = $("#demail").val();
		var file = $("#dfile").val();
		if(name.length<1) {
			$("<span class=\"error\">Please enter your name.</span>").insertBefore("#dname");
			$(".detailsBox").css({"height":220});
			$.colorbox.resize();
		}
		var email_tmp = email.split("@");
		if(email_tmp.length<2) {
			$("<span class=\"error\">Please enter your email address.</span>").insertBefore("#demail");
			$(".detailsBox").css({"height":220});
			$.colorbox.resize();
		}
		
		if(email_tmp.length>1 && name.length>0) {
			$.ajax({
				url: "/download/submit",
				type: "post",
				data: "name=" + name + "&email=" + email + "&file=" + file,
				success: function(data) {
					if(data=="true") {
						var i = false;
						$.colorbox.settings.onClosed = function() {
							if(i==false) {
								$.colorbox({inline:true, href:".loaderBox"});
								i = true;
							} else {
								$.colorbox.settings.onClosed = false;
							}
						}
						$.colorbox.close($.colorbox({inline:true, href:".loaderBox"}));
						$('div.loaderBox a').attr('href','/download/get'+hrefe);
						$('div.loaderBox a').click(function() {
							func = '$.colorbox.close()';
							setTimeout(func,3000);
						});
					} else {
						alert("An error occurred");
					}
				}
			});
		}
		return false;
	});
	
//	if ($('#quote-bubble').length > 0) reposition_bubble();
});

function reposition_bubble() {
	var h = $('#quote-bubble img').height();
	var y0 = 165;
	var y1 = 80;
	if (h < y0) {
		y = y1+y0-h;
	} else if (h == y0) {
		y = y1;
	} else {
		y = 60;
	}
	alert (y);
//	$('#quote-bubble').css('paddingTop',y + 'px');
}
