$(document).ready(function() {

	if ($("#prompt form").length > 0) $("#prompt form").validate({errorLabelContainer: $("#formerror")});

});


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



$(document).ready(function() {

	if ($('#comments').length > 0) {
		$('#comments').wysiwyg({
			controls: {
				strikeThrough : { visible : true },
				underline     : { visible : true },

				separator00 : { visible : true },

				justifyLeft   : { visible : false },
				justifyCenter : { visible : false },
				justifyRight  : { visible : false },
				justifyFull   : { visible : false },

				separator01 : { visible : false },

				indent  : { visible : false },
				outdent : { visible : false },

				separator02 : { visible : false },

				subscript   : { visible : false },
				superscript : { visible : false },

				separator03 : { visible : false },

				undo : { visible : false },
				redo : { visible : false },

				separator04 : { visible : false },

				insertOrderedList    : { visible : true },
				insertUnorderedList  : { visible : true },
				insertHorizontalRule : { visible : false },

				separator07 : { visible : false },

				cut   : { visible : false },
				copy  : { visible : false },
				paste : { visible : false },

				separator07 : { visible : false },

				html : { visible : false }
			}
							});
	}

	$('.wysiwyg').width('310px');
	$('#commentsIFrame').width('310px');

	var _page_id = '';
	var _week = '';
	var _section = '';
	var _parent = '';
	var _file_id = '';
	var _file_name = '';
	var _action = '';

	var _file = '';
	var _type = '';
	var _title = '';
	var _redir = '';

	$(".addfile[rel]").click( function() {
		_page_id = $(this).attr('page_id');
		_week = $(this).attr('week');
		_section = $(this).attr('section');
		_parent = $(this).attr('parent');
		_action = $(this).attr('action');
		_redir = $(this).attr('redir');
	});

	$(".removefile[rel]").click( function() {
		_page_id = $(this).attr('page_id');
		_week = $(this).attr('week');
		_section = $(this).attr('section');
		_file_id = $(this).attr('file_id');
		_file_name = $(this).attr('file_name');
		_action = $(this).attr('action');
		_redir = $(this).attr('redir');
		//alert(_page_id+_file_id);
	});

	$(".listen[rel]").click( function() {

		_file = $(this).attr('file');
		_type = $(this).attr('type');
		_title = $(this).attr('title');
		_redir = $(this).attr('redir');
	});


	if ($("#formaddfile").length > 0) $("#formaddfile").validate();

	var triggers = $(".addfile[rel]").overlay({
		onLoad: function(event, tabIndex) {
			//var element = event.originalTarget || event.srcElement;
			$('#page_id').val(_page_id);
			$('#week').val(_week);
			$('#section').val(_section);
			$('#parent').val(_parent);
			$('#action').val(_action);
			$('#redir').val(_redir);
		},
		// some mask tweaks suitable for modal dialogs
		mask: {
			// you might also consider a "transparent" color for the mask
			color: '#000',
			// load mask a little faster
			loadSpeed: 200,
			// very transparent
			opacity: 0.5
		},
		closeOnClick: false
	});


	var triggers = $(".removefile[rel]").overlay({
		onLoad: function(event, tabIndex) {
			//var element = event.originalTarget || event.srcElement;
			$('#remove_page_id').val(_page_id);
			$('#remove_week').val(_week);
			$('#remove_section').val(_section);
			$('#remove_file_id').val(_file_id);
			$('#remove_action').val(_action);
			$('#remove_redir').val(_redir);
			$('#remove_file_name').val(_file_name);
			$('#remove_file_name_show').html('<b>'+_file_name+'</b>');
			$('#remove_file_id_show').html('<b>File #'+_file_id+'</b>');
			//alert(_file_id+'//'+_page_id);
		},
		// some mask tweaks suitable for modal dialogs
		mask: {
			// you might also consider a "transparent" color for the mask
			color: '#000',
			// load mask a little faster
			loadSpeed: 200,
			// very transparent
			opacity: 0.5
		},
		closeOnClick: false
	});




	var listens = $(".listen[rel]").overlay({

		onLoad: function(event, tabIndex) {

			//var element = event.originalTarget || event.srcElement;
			$('#filetitle').html(_title);
			//$('#action').val(_action);

			//$('#formaddfile').attr('action','#'+_week);

			var so = new SWFObject('/wp-content/plugins/thecourse/js/flvplayer/player.swf','mpl','470','320','9');
			so.addParam('allowfullscreen','true');
			so.addParam('allowscriptaccess','always');
			so.addParam('wmode','opaque');

			if (_type == 'mp3')
			so.addVariable("file","/wp-content/plugins/thecourse/data/audio/" + _file);
			else
			so.addVariable("file","/wp-content/plugins/thecourse/data/video/" + _file);

			so.addVariable('autostart','true');
			so.write('mediaspace');

		},

		// some mask tweaks suitable for modal dialogs
		mask: {
			// you might also consider a "transparent" color for the mask
			color: '#000',
			// load mask a little faster
			loadSpeed: 200,
			// very transparent
			opacity: 0.5
		},

		closeOnClick: true
	});

	$(".history").click(function(e) {
			//$('.divaddfile').hide();
			//$('.divreplacefile').hide();
			toshow = $(this).attr('rel');
			$(this).toggleClass('history_opened');
			//alert(toshow);
			$('#' + toshow).toggle(300);
			return e.preventDefault();
	});

	$('.tooltipanchor').tooltip({// use div.tooltip as our tooltip
		tip: '.tooltip',
		// use the fade effect instead of the default
		effect: 'fade'
	});

	}
);
