/* `Special Situations (no jquery ui)--------*/
//set width of div for images with captions so caption will wrap
function setImgCaptionWidth(){
	var img = $("div[class^='caption_'] img");
  	img.parent().css("width",img.width() + "px");
}
//utility function for getting index of dynamic elements
function getNum(element, attrPrefix) {
    //set prefix, get number
    var prefix = attrPrefix;
    var num = element.attr("id").substring((prefix.length));
    return num;
}
//For email guide form (udfs/EmailGuides.cfm) called in guides section below
function limitChars(textid, limit, infodiv){
	var text = $('#'+textid).val(), textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}
//For conference agenda display; called in conf_agenda section below
function scrolltoday(day){
		//hides open days and scrolls to selected day
		$('div[id^="day"]').each(function(){
			if ($(this).attr('id') !== day){
				$(this).prev('h3').css('background-image','url(/images/bg/arrow_down.gif)');
				$(this).hide();
			}
		});
		window.scrollTo(0,$('a#'+day+'Top').offset().top);	
	}
//For homepage section_special height equalization
if(bodyID === "home" && !mobileWidth){
	$.fn.equalizeHeights = function(){
		return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
	}
	$('div.section_special').equalizeHeights();
	// for text in guide heading to wrap properly at smaller window sizes
	function lineheight () {
		if($(window).width() >= 768 && $(window).width() < 960){
			$("div.home_guides h2").css("line-height", "1");
		}else{
			$("div.home_guides h2").css("line-height", "40px");
		}
	}
	lineheight();
	$(window).resize(function() {
  		lineheight();
	});	
}
//Hide (collapse) conference agenda on mobile (expanded by click)
if(bodyID === "conf_agenda" && mobileWidth) {
		  $("h3.agenda_date").next('div').hide();
		  $('h3').css('background-image','url(/images/bg/arrow_down.gif)');
}
// start ON LOAD ********************************************/
$(function() { 	
	/* `Home --------*/
	if(bodyID === "home") {
		
		$("input#mce-EMAIL").focus(function(){
			$("input#mce-EMAIL").val("");
		});		
	
	} // end Home //		
	/* `Conference --------*/
	if(bodyID === "conf_agenda") {
		
		// day jump links
		$("a[class^='day']").click(function(){
			var day = $(this).attr("class").slice(0,4);	
			if ($("div#" + day).is(':not(:visible)')){
				$("div#" + day).toggle();
			}
			window.scrollTo(0,$('a#'+day+'Top').offset().top);
		});
			 
		$("h3.agenda_date").click(function(){
			var dayOpen = $(this).next('div').attr('id');
			
			$(this).next('div').toggle();
			
			if ($(this).next('div').is(':visible')){
				$(this).css('background-image','url(/images/bg/arrow_up.gif)');
				//close any open days so only one a time is open
				$('div[id^="day"]').each(function(){
					if ($(this).attr('id') !== dayOpen){
						$(this).prev('h3').css('background-image','url(/images/bg/arrow_down.gif)');
						$(this).hide();
					}
				});
				scrolltoday(dayOpen);	
			} 
			else{
				$(this).css('background-image','url(/images/bg/arrow_down.gif)');
			}		
		});
		
		$("button.showbtn").click(function(){
			var prezNum = getNum($(this), "showbutton");
			if(!mobileWidth){$("#abstract" + prezNum).slideToggle();}else{$("#abstract" + prezNum).toggle();}
			$("#showText" + prezNum).toggle();
			$("#hideText" + prezNum).toggle();
		});
	}// end Conference //
	// `About --------*/
	if(bodyID === "about") {
		
		$("div#situational div a.lang").click(function (e) {
		  	e.preventDefault();
		  	$("div.situational_content").toggle();
		});
		
		$("div#triangle div a.lang").click(function (e) {
		  	e.preventDefault();
		  	$("div.triangle_content").toggle();
		});
		
		//set width of div for images with captions so caption will wrap
		var img = $("div[class^='caption_'] img");
  	
		$("<img/>")
			.attr("src", $(img).attr("src"))
			.load(function() {
				$(img).parent().css("width",$(img).width() + "px");
		});
		
	}// end About //
	// `Library and Guides--------*/
	if((bodyID === "library" || bodyID === "guide") && !mobileWidth) {
			$("#boundCopy").dialog({
				autoOpen: false,
				modal: true,
				resizable: false,
				width: 450,
				buttons: {	
					"Close": function(){$(this).dialog("close");}
				}
			});
			
			$("a.orderBound").click(function(e){
				e.preventDefault();
				$( "#boundCopy" ).dialog("open");
			});
	}
	// `Library --------*/
	//these pages are in the library folder
		if(bodyID === "goldstein" || bodyID === "tilley") {
			if(!mobileWidth){
				$( "#videoDialog" ).dialog({
					autoOpen: false,
					modal: true,
					resizable: false,
					width: 550,
					buttons: {	
						"Close": function(){$(this).dialog("close");}
						}
				});
			}		
			$("a.videoLink").click(function(e){			
				//we're mobile so send them to youtube link, otherwise pop-up player on page.
				if (mobileWidth){
					return true;
				}else{
					e.preventDefault();
					$(".ui-dialog-title").html($(this).attr("title"));
					$("#ytIframe").attr("src", "http://www.youtube.com/embed/" + $(this).attr("id") + "?rel=0&showinfo=0");
					$( "#videoDialog" ).dialog("open");
				}
			});
			
		}
		if(bodyID === "scpdatabase") {
			if(!mobileWidth){
				$("#caseDetails").dialog({
						autoOpen: false,
						modal: true,
						resizable: false,
						width: 600,
						buttons: {	
							"Close": function(){$(this).dialog("close");}
							}
					});	
				
				$("a.details").click(function(e){
					e.preventDefault();
					$( "#caseDetails" ).dialog("open");	
				});	
			}// end if(!mobileWidth)
			
			$('#selectionOfStudies').hide();
			$('#selectionLink').click(function(e){
				e.preventDefault();
				$('#selectionOfStudies').slideToggle("slow");
			});
			
			$('#selectAll').removeAttr('checked');
		    $('#selectAll').click(function(){
				if( $(this).is(':checked') ) {
					$('.categoryCB').attr('checked','checked');
				}else {
					$('.categoryCB').removeAttr('checked');	
				}
		     });

			$('form.required').submit(function() {
				$('span.errorMessage').remove();
		
				var hasError = false;
				var numSelectedType = 0;
				var numSelectedSort = 0;
				var errorMessageText = "";
				
				$.each($('form.required ul.forms li.required'),function() {		
					if($(this).hasClass('type')) {
						$.each($(this).find('input'),function() {
							if($(this).is(':checked') ) {
								numSelectedType++;
							}
						});				
					}
					
					if($(this).hasClass('sort')) {
						$.each($(this).find('input'),function() {
							if($(this).is(':checked') ) {
								numSelectedSort++;
							}
						});	
					}
				});
		
				if(numSelectedType === 0 || numSelectedSort === 0){
					errorMessageText = "<p><strong>Sorry - there was a problem with your search</strong></p>";
					hasError = true;
						
					if(numSelectedType === 0) {
						errorMessageText = errorMessageText + "<p>You must select a problem type.</p>";
					}
					
					errorMessageText = numSelectedSort === 0 ? errorMessageText + "<p>You must select a sorting method.</p>" : errorMessageText = errorMessageText;
					
				}		
				if(hasError) {
					$("div.errors").append(errorMessageText);			
					$("div.errors").show();
					return false;
				} else {
					//prevents flash of red border when page is submitted
					$("div.errors").css("border","none");
					return true;
				}
			});
		}// end Library //
	// `Casestudies --------*/
	if(bodyID === "casestudies") {
		if(mobileWidth){
			$("iframe.youtube_iframe").remove();
		}		
	}
	// `Guides --------*/
	if(bodyID === "guide" && !mobileWidth) {
			//$( "div.side_nav" ).insertBefore($("div#page_content"));//move guide page nav to top
			$('#emailGuide').dialog({
					autoOpen: false,
					resizable: false,
					modal: true,
					width: 500,
					buttons: {
						"Close": function(){$(this).dialog("close");},
						"Send E-mail": function(){
							if(($("#to_address").val().length) && ($("#from_address").val().length) && ($("#your_name").val().length)) {
								$.post("/udfs/EmailGuidesSend.cfm", $("#emailForm").serialize(),																																											  								function(data) {																																																															
										if(data.VALID){	
										$("#emailSuccess").show();
										$("#to_address,#from_address,#your_name,#your_note").val("");
										}else{
											$("#emailError").show();
											$("#emailGuide").dialog("open");	
										}		
									} 																																																																													 							, "json");									
							}
							else {
								$("#emailError").show();
								$("#emailGuide").dialog("open");	
							}
						}
					}
				});
				
				$("a.emailGuide").click(function(e){
					e.preventDefault();
					$("#to_address,#from_address,#your_name,#your_note").val("");
					$("#emailGuide").dialog("open");
					$("#emailError,#emailSuccess").hide();
				});
				
			   /*For email guide form (udfs/EmailGuides.cfm)*/
				$('#your_note').keyup(function(){
					limitChars('your_note', 200, 'charlimitinfo');
				});

		} // end Gudies //
		
 }); //end ONLOAD **************************************/
