$(function(){
	// aux bar pull downs
	$('#panel_centers_of_excellence').click(function(e){
		e.stopPropagation();
	});
	$("#panel_centers_of_excellence .trigger").click( function(e){
		e.preventDefault();
		e.stopPropagation();
		if($("#panel_centers_of_excellence").hasClass('on')){
			closeCentersTab();
		}else{
			openCentersTab();
		}
	});

	$('#panel_my_clipboard').click(function(e){
		e.stopPropagation();
	});
	$("#panel_my_clipboard .trigger").click( function(e){
		e.preventDefault();
		e.stopPropagation();
		if($("#panel_my_clipboard").hasClass('on')){
			closeClipboard();
		}else{
			openClipboard();
		}
	});

	// form errors
	$(".formWrapper form").submit(function(evt){
		
		var submit_flag = true;
		$(".error_txt").remove();
		$("input[type!=radio][type!=checkbox].dsRequiredText, select.dsRequiredSelect, textarea.dsRequiredTextArea, textarea.textareadsRequiredTextArea", $(this)).each(function(){
			var parent_item = $(this).parent("p");	
			$(this).removeClass("needed");
			parent_item.removeClass('error_wrapper');
			if($(this).val() == ''){
				$(this).addClass("needed").after("<em class='error_txt'>Required</em>");
				parent_item.addClass('error_wrapper');
				submit_flag = false;
			}
		});	
		
		$("input[type=radio].dsRequiredRadio, input[type=checkbox].dsRequiredRadio", $(this)).each(function(){
			var parent_item = $(this).closest("p");
			var parent_form = parent_item.closest("form");			
			if($(".error_txt", parent_item).length == 0){
				parent_item.removeClass('error_wrapper');
				var item_name = $(this).attr("name");
				var filled = false;
				
				var items = $("input:checked", parent_form);
				items.each(function(){
					if ($(this).attr('name') == item_name) {
						filled = true;
					}
				});

				if(filled == false){
					parent_item.addClass('error_wrapper');
					parent_item.append("<em class='error_txt'>Required</em>");
					submit_flag = false;
				}
			}
		});

		if (submit_flag == false){
			evt.preventDefault();	
			//location.hash = "form_top";
			if ($('h3.error').length == 0) {
				$("div.formWrapper").prepend("<h3 class=\"error\"><span>Please check all required fields</span></h3>");
			}
		}
	
	});

	Shadowbox.init({onFinish:function(){ $('#sb-body-inner img').one('click',Shadowbox.close); }});
	
	// save to clipboard feedback
	$('a.save, a.save_img').click(function(e){
		e.preventDefault();
		var typeId = parseInt($(this).attr('data-type'));
		var itemId = parseInt($(this).attr('data-id'));
		if(typeId > 0 && itemId > 0){
			$.getJSON('/action/clip/add.php', {type_id: typeId, item_id: itemId}, clipBack);
		}
	});

	$('#clipboard_links').delegate('a.remove', 'click', function(e){
		e.preventDefault();
		var dl = $(this).closest('dl');
		var dd = $(this).closest('dd');

		dd.remove();
		if (!dl.children('dd').length) {
			dl.remove();
		}

		var typeId = parseInt($(this).attr('data-type'));
		var itemId = parseInt($(this).attr('data-id'));
		if(typeId > 0 && itemId > 0){
			$.get('/action/clip/remove.php', {type_id: typeId, item_id: itemId});
		}
	});
	
	$("#printPage").click(function(evt){
		evt.preventDefault();
		window.print();
	});

	/* labelify */
	$('.label').labelify({labelledClass:'labeled'});

	$('.sidebar_gallery .slides').each(function(){
		var el = $(this);
		var slides = el.children('.slide');
		var len = slides.length;
		if (len > 1) {
			var maxheight = 0;
			$(slides).each(function(){
				slideheight = $(this).outerHeight();
				maxheight = ( slideheight > maxheight ) ? slideheight : maxheight ;
			});
			
			$('.sidebar_gallery .slides').css({'height':maxheight});
			
			var infoRow = $('<div class="info_row"><h2><span class="slide_count">1</span> of '+len+'</h2><div class="slide_nav"><a class="prev btn_small" href=""><span class="btn_horz_liner"><img alt="&lt;" class="bgPng" src="/media/images/master/small-arrow-left.png" /></span></a> <a class="next btn_small" href=""><span class="btn_horz_liner"><img alt="&lt;" class="bgPng" src="/media/images/master/small-arrow-right.png" /></span></a></div></div>').prependTo(el.parent());

			el.cycle({
					before: function(curSlide, nextSlide){
						var s = $(nextSlide);
						infoRow.children('h2').children('span.slide_count').html(s.index() + 1);
					}
					,next: infoRow.children('div.slide_nav').children('a.next')
					,prev: infoRow.children('div.slide_nav').children('a.prev')
			});
		}

	});
});

function openCentersTab() {
	var el = $("#panel_centers_of_excellence");
	if(!el.hasClass('on')){
		el.addClass('on');
		$("#header-search-form select").css({'visibility':'hidden'}); // hide select box next to search, ie6 fix
		$(".panel", el).slideDown();

		$('body').click(closeCentersTab);
	}
}

function closeCentersTab() {
	var el = $("#panel_centers_of_excellence");
	if(el.hasClass('on')){
		$('body').unbind('click', closeCentersTab);

		$("#header-search-form select").css({'visibility':'visible'}); // hide select box next to search, ie6 fix
		$(".panel", el).slideUp(function(){
			el.removeClass('on');
		});
	}
}

function openClipboard() {
	var el = $("#panel_my_clipboard");
	if(!el.hasClass('on')){
		el.addClass('on');
		$("#header-search-form select").css({'visibility':'hidden'}); // hide select box next to search, ie6 fix
		$(".panel", el).slideDown();

		$('body').click(closeClipboard);
	}
}

function closeClipboard(){
	var el = $("#panel_my_clipboard");
	if(el.hasClass('on')){
		$('body').unbind('click', closeClipboard);

		$("#header-search-form select").css({'visibility':'visible'}); // hide select box next to search, ie6 fix
		$(".panel", el).slideUp(function(){
			el.removeClass('on');
		});
	}
}

function clipBack(data){
	var user_logged = data.logged_in;
	if (user_logged) {
		$('#clipboard_links').html(data.menu);
		Shadowbox.open({
			content: '/media/images/master/saved-to-clipboard.png',
			player: 'img',
			width: 460,
			height: 271
		});
	} else {
		$(document).scrollTop(0);
		openClipboard();
		$('#panel_my_clipboard p.error_txt').remove();
		$('<p class="error_txt">Please log in or sign up to save an item to your clipboard.</p>').insertAfter($('#panel_my_clipboard p').eq(1));
	}
}

