// Paths
var fullBaseUrl = 'http://dev.thefocalpoint.com';
var workingDir  = '/'; 

/* primary menu dropdown */
$(document).ready(function () {		
	$('#primary-nav > li').hover(
		function() { 
			//show its submenu
			$('ul', this).slideDown(100);
		}, 
		function() {
			//hide its submenu
			$('ul', this).slideUp(100);			
		}
	);
	
	if ($("span.icon-rollover").length != 0) {
		$('span.icon-rollover').hover(
			function() { 
				$(this).toggleClass('icon-rollover-hover');
			}, 
			function() {
				$(this).toggleClass('icon-rollover-hover');			
			}
		);
	}
});

$(document).ready(function() {    
	var maxHeight;
	
	// equalize page layout columns
	if ($(".column").length != 0) {
		maxHeight = 0;
		
		$('.column').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); }).height(maxHeight);
		maxHeight = (maxHeight <= 600) ? 600 : maxHeight;
		$('div.column').height(maxHeight);
	}
	
	// equalize press landing page items
	if ($(".press-item").length != 0) {
		maxHeight = 0;
		
		$('.press-item').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); }).height(maxHeight);
		$('div.press-item').height(maxHeight); 
	}
	
	// equalize category landing page items
	if ($(".category").length != 0) {
		maxHeight = 0;
		
		$('.category').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); }).height(maxHeight);
		$('div.category').height(maxHeight); 
	}
	
	// equalize wins featured cases
	if ($(".featured-case").length != 0) {
		maxHeight = 0;
		
		$('.featured-case').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); }).height(maxHeight);
		$('div.featured-case').height(maxHeight); 
	}
	
	// append learn more link to last p tag of featured recent content
	if ($('#recent-link').length != 0) {
		$('.recent p').last().append('<br/>');
		$('.recent p').last().append($('#recent-link'));
	}
	
	// append learn more link to last p tag of featured historic content
	if ($('#historic-link').length != 0) {
		$('.historic p').last().append('<br/>');
		$('.historic p').last().append($('#historic-link'));
	}
	
	// equalize sitemap page columns
	if ($(".sitemap").length != 0) {
		maxHeight = 0;
		
		$('.sitemap-links-container').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); }).height(maxHeight);
		$('div.sitemap-links-container').height(maxHeight); 
	}
	
	// hide expand links
	$(".collapse").hide();
	
	// set impressions parameters
	if ($("#impressions").length != 0) {
		$('#impressions').cycle({ 
			fx:     'fade', 
			timeout: 10000, 
			delay:  -2000 ,
			next: '#impression-next', 
			prev: '#impression-prev',
			pause: 1 
		});
	}
	
	// set trial tool tabs parameters
	if ($("#flowpanes-trialtools").length != 0) {
		$("#flowpanes-trialtools").scrollable({circular:true, mousewheel:true}).navigator({
			navi: "#flowtabs-trialtools",
			naviItem: 'a',
			activeClass: 'current',
			history: false
		});
	}
	
	// set cases tabs parameters
	if ($("#flowpanes-cases").length != 0) {
		$("#flowpanes-cases").scrollable({circular:true, mousewheel:false}).navigator({
			navi: "#flowtabs-cases",
			naviItem: 'a',
			activeClass: 'current',
			history: false
		});
	}
	
	// set case slideshow parameters
	if ($("#slideshow").length != 0) {
		$('#slideshow').cycle({ 
			fx:     'fade', 
			timeout: 5000, 
			delay:  -2000 ,
			next: '#slide-next', 
			prev: '#slide-prev',
			pause: 1,
			pager: '#slideshow-indices', 
			pagerEvent: 'mouseover', 
			pauseOnPagerHover: true,
			pagerAnchorBuilder: function(index, el) {
				return '<a href="#"></a>'; 
			}
		});
	}	
});
	
$('.expand').live('click', function(event){
	event.preventDefault();
	
	var tgt = $(this).next('.collapse');
	
	if ($(tgt).is(':hidden')) { 
		$(this).addClass('expandselected');
		$(tgt).slideToggle(200);
	} else { 
		$(this).removeClass('expandselected');
		$(tgt).hide();		
	}
});


$(function() {
	$('img[img-hover]').hover(function() { 
		$(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('img-hover')).attr('img-hover', $(this).attr('tmp')).removeAttr('tmp');
	}).each(function() {
		$('<img />').attr('src', $(this).attr('img-hover'));
	});
});

function openURL(url) {
	opener.document.location = url;
	self.close();
} 

function printableImagesExhibit(exhibitId) {
	window.open(fullBaseUrl + workingDir + 'insights/key-exhibits/' + exhibitId + '?print=' + 1, '_blank', 'menubar=no,scrollbars=yes,resizable=no,status=no,width=928,height=560,top=0,left=0');
}

function printableAnimationsExhibit(exhibitId) {
	window.open(fullBaseUrl + workingDir + 'insights/key-exhibits/' + exhibitId + '?print=' + 1, '_blank', 'menubar=no,scrollbars=yes,resizable=no,status=no,width=982,height=608,top=0,left=0');
}

function validateUserInfo(form) {  
	if (form.username.value == "") {
		alert("Please enter a username");
		form.username.focus();
		return false;
	} else if (form.password.value == "") {
		alert("Please enter a password");
		form.password.focus();
		return false;
	} else if (form.confirm_password.value == "") {
		alert("Please re-enter your password");
		form.confirm_password.focus();
		return false;
	} else if (form.password.value != form.confirm_password.value) {
		alert("The confirmed password does not match");
		form.confirm_password.focus();	
		return false;	
	}
	
	if (form.item_id.value == "") {
		var html = $.ajax({
		type: "POST",
		url: fullBaseUrl + workingDir + "ajax/check_username.php",
		data: "username=" + form.username.value, 
		async: false
		}).responseText;
		
		if (html == "success") {
			return true;
		} else {
			alert("Username already exists");  
			return false;
		}
	}
}

function validateTeamMemberInput(form) {  
	if (form.fname.value == "") {
		alert("Please enter a first name for this person");
		form.fname.focus();
		return false;
	} else if (form.lname.value == "") {
		alert("Please enter a last name for this person");
		form.lname.focus();
		return false;
	} else if (form.nice_url.value == "") {
		alert("Please enter a nice url value for this person");
		form.nice_url.focus();
		return false;
	} 
	
	var regexp = /[^A-Za-z0-9-]/;
	if (regexp.test(form.nice_url.value)) {
		alert("Only alphanumeric characters and '-' are allowed for nice urls");
		form.nice_url.focus();
		return false;	
	}
}

function validateCaseCategoryInput(form) {  
	if (form.name.value == "") {
		alert("Please enter a name for this cateogry");
		form.name.focus();
		return false;
	} else if (form.nice_url.value == "") {
		alert("Please enter a nice url value for this cateogry");
		form.nice_url.focus();
		return false;
	} 
	
	var regexp = /[^A-Za-z0-9-]/;
	if (regexp.test(form.nice_url.value)) {
		alert("Only alphanumeric characters and '-' are allowed for nice urls");
		form.nice_url.focus();
		return false;	
	}
}

function validateEditExhibitForm(form) {  
	if ((form.item_id == '') && (form.thumbnail.value == "")) {
		alert("You must upload a thumbnail for this exhibit");
		form.thumbnail.focus();
		return false;
	}
}

function validateEditCaseForm(form) { 
	if ((form.type.options[form.type.selectedIndex].value == '') && (form.featured.checked)) {
		alert("You must select a case type to check this case as featured");
		form.type.focus();
		return false;
	}
}

function slide(slideId) {
	document.getElementById('slide').src = slides[slideId].src;
	document.getElementById('slide-btn' + slideId).style.backgroundColor = "#d4d3d2";
	currSlideId = slideId;
	
	for (i in slides) {
		if ((!isNaN(i))&&(i!=slideId)) {				
			document.getElementById('slide-btn' + i).style.backgroundColor = "#fff";
		}
	}
}



