
function closeShareResources(vo){
	$.fancybox.close();
}

function save(encoded){
	var vo = $.evalJSON(encoded);
	var cookievalue = $.cookie('alulte');
	var date = new Date();
	date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
	var savedvalues;
	if (cookievalue != null){
		savedvalues = $.evalJSON(cookievalue);
	}
	if (savedvalues == undefined){
		savedvalues = new Array();
	}
	
	var isDup = false;
	var i = 0;
	var length = savedvalues.length;
	for (var a = 0; a <length; a += 1) {
		var savedvo = savedvalues[a];
		if(savedvo.id == vo.id){
			isDup = true;
		}
	}
	
	if(!isDup){
		savedvalues.push(vo);
		$.cookie('alulte', $.toJSON(savedvalues),{ path: '/', expires:date});
		updateSaved();
	}
}

function updateSaved(){
	$('#msr_list').empty();
	$('#msr_count').empty();
	var cookievalue = $.cookie('alulte');
	if ( cookievalue  != null){
		var savedvalues = $.evalJSON(cookievalue);
		var textToInsert = [];
		var i = 0;
		var length = savedvalues.length;
		$('#msr_count').append(['(',length,')'].join(''));
		for (var a = 0; a <length; a += 1) {
			var vo = savedvalues[a];
			var pathNames = vo.path.split('/');
			textToInsert[i++]  = '<li><a href="';
			if ((pathNames[0] == 'video') || (pathNames[0] == 'podcast')) {
				textToInsert[i++] = '#/';
			}
			textToInsert[i++] = vo.path;
			textToInsert[i++] = '">';
			textToInsert[i++] = vo.title;
			textToInsert[i++] = '</a> <input id=';
			textToInsert[i++] = vo.id;
			textToInsert[i++] = ' type="checkbox" class="msr_checkbox" /></li>';
		}
		$('#msr_list').append(textToInsert.join(''));
	}else{
		$('#msr_count').append('(0)');
	}
}

$(document).ready(function() {
	
	var fbobg = $('#fancybox-outer').css('background');
	
	$('#msr_box').hide();
	
	$('.msr_link').click(function() {
		$(this).parent().find('#msr_box').show();
		return false;
	});

	$('#msr_box').mouseleave(function() {
		$(this).fadeOut('fast');
	});

	$('#wrap').bind("mouseleave",function() {
		$('#msr_box').fadeOut('fast');
	});

	$('#msr_remove_link').click(function() {
		var cookievalue = $.cookie('alulte');
		if ( cookievalue  != null){
			var savedvalues = $.evalJSON(cookievalue);
			var date = new Date();
			date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
			for (var a = savedvalues.length - 1; a > -1; a--) {
				var savedvalue = savedvalues[a];
				var checkbox = $("input[id="+savedvalue.id+"]").get(0);
				if (checkbox && checkbox.checked){
					savedvalues.splice(a,1);
				}
			}
			$.cookie('alulte', $.toJSON(savedvalues),{ path: '/', expires:date});
			updateSaved();
		}
	});

	$('#msr_share_link').click(function() {
		var cookievalue = $.cookie('alulte');
		if ( cookievalue  != null){
			$('#shareresources').trigger('click');
		}
	});
	
	$('a.saveBtn').click(function(){
		var vo = {id:content_id, title:document.title, path:document.location.href, type:'content'};
		var encoded = $.toJSON(vo);
		save(encoded);
	});
	
	$('#shareresources').fancybox({
		padding: 0,
		margin: 0,
		overlayOpacity: 0.85,
		overlayColor: '#000',
		width: 800,
		height: 396,
		onComplete: function () {
			$('#fancybox-outer').css('background', 'none');
			$('#fancybox-wrap').css('width', '800px');
			$('#fancybox-wrap').css('height', '396px');
			$('#fancybox-inner').css('width', '800px');
			$('#fancybox-inner').css('height', '396px');
			$.doTimeout( 100, function(){
				if ((typeof($('#lteshareresources').get(0)) != 'undefined') && (typeof($('#lteshareresources').get(0)['setSavedResources']) != 'undefined')){
					var cookievalue = $.cookie('alulte');
					if ( cookievalue  != null){
						var savedvalues = $.evalJSON(cookievalue);
						for (var a = savedvalues.length - 1; a > -1; a--) {
							var savedvalue = savedvalues[a];
							var checkbox = $("input[id="+savedvalue.id+"]").get(0);
							if (!checkbox.checked){
								savedvalues.splice(a,1);
							}
						}
						$('#lteshareresources').get(0).setSavedResources($.toJSON(savedvalues));
					}
					return false;
				}else{
					return true;
				}
			});
		},
		onClosed: function() {
			$('#fancybox-outer').css('background', fbobg);
		},
		swf: {
			hasVersion: '9.0.115',
			menu: false,
			scale: 'noScale',
			allowScriptAccess: 'always',
			wmode: 'transparent',
			id: 'lteshareresources',
			base: '/',
			flashvars: {
				locale: 'en_us'
			}
		}
	});
	
	updateSaved();
});





