$(document).ready(function() {

// Tabs hover state
	$('.yui-nav li').mouseover(function(){
		$(this).addClass('hover');
	});
	
	$('.yui-nav li').mouseout(function(){
		$(this).removeClass('hover');
	});
	
	// Hide Save / Hide options
	
	$('.saveHide').hide();
	
	// Publication hover state
	
	$('#publications .frame div').mouseover(function(){
		$(this).addClass('hover');
		$(this).find('.saveHide').show();
	});
	$('#publications .frame div').mouseout(function(){
		$(this).removeClass('hover');
		$('.item .saveHide').hide();
	});
	
	$('#presses .frame div').mouseover(function(){
		$(this).addClass('hover');
		$(this).find('.saveHide').show();
	});
	$('#presses .frame div').mouseout(function(){
		$(this).removeClass('hover');
		$('.item .saveHide').hide();
	});
	
	$('#events .frame div').mouseover(function(){
		$(this).addClass('hover');
		$(this).find('.saveHide').show();
	});
	$('#events .frame div').mouseout(function(){
		$(this).removeClass('hover');
		$('.item .saveHide').hide();
	});
	
	// Recommended list item hover
	
	$('.sections .left li').mouseover(function(){
		$(this).addClass('hover');
		$(this).find('.saveHide').show();
	});
	$('.sections .left li').mouseout(function(){
		$(this).removeClass('hover');
		$('.sections .left li .saveHide').hide();
	});
	
	// Show/Hide sub sections in favorites area of myCatalyst
	

	$('.favorites ul li>a').click(function(){
		if ($(this).parent('li').hasClass('reveal')){
			$(this).parent('li').removeClass('reveal');
			$(this).siblings('ul').hide();
			return false;
		} else {
			$(this).parent('li').addClass('reveal');
			$(this).siblings('ul').show();
			return false;
		}
	});
	
// Show/Hide sub sections in publication tab
	$('#publications .frame div').each(function (i) {
		if ($(this).hasClass('hidden')) {
			$(this).hide();
		}
	});

// Show/Hide sub sections in press release tab
	$("#presses .frame div").each(function (i) {
		if ($(this).hasClass('hidden')) {
			$(this).hide();
		}
	});
	
// Show/Hide sub sections in event tab	
	$("#events .frame div").each(function (i) {
		if ($(this).hasClass('hidden')) {
			$(this).hide();
		}
	});
// Publications	
	$("#publications .frame div").each(function (i) {
			var pubs = $.cookie('expandPubs');
			if (pubs == 'open') {
				$(this).removeClass('hidden');
				$(this).addClass('item');
				$(this).show();
				$('#publications .frame .viewMore a').removeClass('viewMore');
				$('#publications .frame .viewMore a').addClass('viewLess');
				$('#publications .frame .viewMore a').text("View Fewer Briefings");
			}
		});
	
	$('#publications .frame .viewMore a').click(function(){
		if ($(this).hasClass('viewMore')) {
			$(this).removeClass('viewMore');
			$(this).addClass('viewLess');
			$(this).text("View Fewer Briefings");
			$.cookie('expandPubs', 'open');
		} else {
			$(this).removeClass('viewLess');
			$(this).addClass('viewMore');
			$(this).text("View More Briefings");
			$.cookie('expandPubs', null);
		}
		$("#publications .frame div").each(function (i) {
			if (i > 2) {
				if ($(this).hasClass('hidden')) {
					$(this).removeClass('hidden');
					$(this).addClass('item');
					$(this).show();
				} else {
					$(this).removeClass('item');
					$(this).addClass('hidden');
					$(this).hide();
				}
			}
		});
		return false;
	});
	
// Press Releases
	$("#presses .frame div").each(function (i) {
			var press = $.cookie('expandPress');
			if (press == 'open') {
				$(this).removeClass('hidden');
				$(this).addClass('item');
				$(this).show();
				$('#presses .frame .viewMore a').removeClass('viewMore');
				$('#presses .frame .viewMore a').addClass('viewLess');
				$('#presses .frame .viewMore a').text("View Fewer Briefings");
			}
		});
	
	$('#presses .frame .viewMore a').click(function(){
		if ($(this).hasClass('viewMore')) {
			$(this).removeClass('viewMore');
			$(this).addClass('viewLess');
			$(this).text("View Fewer Briefings");
			$.cookie('expandPress', 'open');
		} else {
			$(this).removeClass('viewLess');
			$(this).addClass('viewMore');
			$(this).text("View More Briefings");
			$.cookie('expandPress', null);
		}
		$("#presses .frame div").each(function (i) {
			if (i > 2) {
				if ($(this).hasClass('hidden')) {
					$(this).removeClass('hidden');
					$(this).addClass('item');
					$(this).show();
				} else {
					$(this).removeClass('item');
					$(this).addClass('hidden');
					$(this).hide();
				}
			}
		});
		return false;
	});
	
// Events
	$("#events .frame div").each(function (i) {
			var eventList = $.cookie('expandEvents');
			if (eventList == 'open') {
				$(this).removeClass('hidden');
				$(this).addClass('item');
				$(this).show();
				$('#events .frame .viewMore a').removeClass('viewMore');
				$('#events .frame .viewMore a').addClass('viewLess');
				$('#events .frame .viewMore a').text("View Fewer Briefings");
			}
		});
	
	$('#events .frame .viewMore a').click(function(){
		if ($(this).hasClass('viewMore')) {
			$(this).removeClass('viewMore');
			$(this).addClass('viewLess');
			$(this).text("View Fewer Briefings");
			$.cookie('expandEvents', 'open');
		} else {
			$(this).removeClass('viewLess');
			$(this).addClass('viewMore');
			$(this).text("View More Briefings");
			$.cookie('expandEvents', null);
		}
		$("#events .frame div").each(function (i) {
			if (i > 2) {
				if ($(this).hasClass('hidden')) {
					$(this).removeClass('hidden');
					$(this).addClass('item');
					$(this).show();
				} else {
					$(this).removeClass('item');
					$(this).addClass('hidden');
					$(this).hide();
				}
			}
		});
		return false;
	});
	
// Click link in favorites area	
	$('.favorites li ul>a').click(function(){
		window.location = $(this).attr("href");
		return false;
	});

// Cookies for folder links in favorites area
	$('.favorites li a').click(function (){
		var folderID =  $(this).parent('li').attr('id');
		if($(this).parent('li').hasClass('reveal')) {
			$.cookie(folderID, 'open');
		} else {
			$.cookie(folderID, null);
		}
	});
	
	$('.favorites li').each(function(i){
		var folderID = $(this).attr('id');
		if ($.cookie(folderID) == 'open') {
			$(this).addClass('reveal');
		} else {
			$(this).children('ul').hide();
		}
	
	});
	
	// Show popups when hovering over favorite links
	$('.popUp').hide();
	$('.favorites li li').mouseover(function(e){
		var box = $(this);
		var offset = box.offset();
		var boxtop=offset.top-(15)+"px";
		var boxleft=offset.left-(150)+"px";
		$('.popUp',this).css({left:boxleft,top:boxtop}).show();
	});
	
	$('.favorites li li').mouseout(function(){
		$('.popUp',this).hide();
	});
	
	// Folder Editing options
	$('#folderName').hide();
	$('.notesEntry').hide();
	$('.delete').hide();
	$('.moveFolder').hide();
	
	$('.editName').click(function(){
		$('#folderName').show();
		return false;
	});
	
	$('.editNotes').click(function(){
		$(this).parent('span').siblings('.notesEntry').show();
		$(this).parent('span').siblings('.delete').hide();
		$(this).parent('span').siblings('.moveFolder').hide();
		return false;
	});
	
	$('.deleteItem').click(function(){
		$(this).parent('span').parent().siblings('.notesEntry').hide();
		$(this).parent('span').parent().siblings('.delete').show();
		$(this).parent('span').parent().siblings('.moveFolder').hide();
		$(this).parent().parent().hide();
		return false;
	});
	
	$('.moveItem').click(function(){
		$(this).parent('span').siblings('.notesEntry').hide();
		$(this).parent('span').siblings('.delete').hide();
		$(this).parent('span').siblings('.moveFolder').show();
		$(this).parent('span').children('.delete_link').hide();
		return false;
	});
	

	
//	behavior for mycatalyst palette
	/*$("#avaible").find('option').remove().end();
	$("#selected").find('option').remove().end();
	var selecteds = [];
	$("#hiddenCompanies option:selected").each(function(i, option){
		var last = $('#selected').children(":last");
		$('#selected').append($("<option></option>").attr("value",last.attr('value')+1).text($(option).text()));
		selecteds[i] = $(option).text();
		alert('JQuery: selected' + i)
	});
	$("#hiddenCompanies option").each(function(i, option){
		if($.inArray($(option).text(), selecteds) < 0) {
			var last = $('#avaible').children(":last");
			$('#avaible').append($("<option></option>").attr("value",last.attr('value')+1).text($(option).text()));
		}
	});*/

	$(function(){
		$('input.button#paletteadd').bind('click', function(evt){
			var foo = []; 
			$('#avaible option:selected').each(function(i, selected){
				foo[i] = $(selected).text(); 
				var option = $('#selected').children(":last");
				$('#selected').append($("<option></option>").attr("value",option.attr('value')+1).text(foo[i]));
				$("#avaible option[value='" + $(selected).attr("value") + "']").remove();
				
			});
			$("#hiddenCompanies option").each(function(i, option){
				if($.inArray($(option).text(), foo) > -1) {
					$(option).attr('selected', 'selected');
				}
			});
		});
	 });
	
	$(function(){
		$('input.button#paletteremove').bind('click', function(evt){
			var foo = []; 
			$('#selected option:selected').each(function(i, selected){
				foo[i] = $(selected).text(); 
				var option = $('#avaible').children(":last");
				$('#avaible').append($("<option></option>").attr("value",option.attr('value')+1).text(foo[i]));
				$("#selected option[value='" + $(selected).attr("value") + "']").remove(); 
			});
			$("#hiddenCompanies option").each(function(i, option){
				if($.inArray($(option).text(), foo) > -1) {
					$(option).removeAttr("selected");
				}
			});
		});
	 });
	
	$(function(){
		$('input.button#formSubmit').bind('click', function(evt){
			$('#selected option:selected').each(function(i, selected){
				$("#selected option[value='" + $(selected).attr("value") + "']").removeAttr("selected"); 
			});
		});
	 });

	// Draggable folder order
/*	$( "#folder-order li" ).draggable({ connectToSortable: '#folder-order' });*/
});

function showSave(id) {
	document.getElementById('hiddenBriefing').value=id;
	Shadowbox.open({
		content:	'#actionsPopup',
		handleOversize: "none",
		player:		"inline",
		height:		300,
		width:		330
	});
	return false;
}
